Skip to content

Commit

Permalink
test: check role permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Aug 26, 2023
1 parent 5f9e5e9 commit b522169
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
34 changes: 13 additions & 21 deletions api/projects/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func ProjectMiddleware(next http.Handler) http.Handler {
}

// check if user in project's team
_, err = helpers.Store(r).GetProjectUser(projectID, user.ID)
projectUser, err := helpers.Store(r).GetProjectUser(projectID, user.ID)

if err != nil {
helpers.WriteError(w, err)
Expand All @@ -38,6 +38,7 @@ func ProjectMiddleware(next http.Handler) http.Handler {
return
}

context.Set(r, "projectUserRole", projectUser.Role)
context.Set(r, "project", project)
next.ServeHTTP(w, r)
})
Expand All @@ -47,27 +48,12 @@ func ProjectMiddleware(next http.Handler) http.Handler {
func GetMustCanMiddlewareFor(permissions db.ProjectUserPermission) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
user := context.Get(r, "user").(*db.User)
projectUserRole := context.Get(r, "projectUserRole").(db.ProjectUserRole)

if !user.Admin {
// check if user in project's team
projectUser, err := helpers.Store(r).GetProjectUser(project.ID, user.ID)

if err == db.ErrNotFound {
w.WriteHeader(http.StatusForbidden)
return
}

if err != nil {
helpers.WriteError(w, err)
return
}

if r.Method != "GET" && r.Method != "HEAD" && !projectUser.Can(permissions) {
w.WriteHeader(http.StatusForbidden)
return
}
if !user.Admin && r.Method != "GET" && r.Method != "HEAD" && !projectUserRole.Can(permissions) {
w.WriteHeader(http.StatusForbidden)
return
}

next.ServeHTTP(w, r)
Expand All @@ -77,7 +63,13 @@ func GetMustCanMiddlewareFor(permissions db.ProjectUserPermission) mux.Middlewar

// GetProject returns a project details
func GetProject(w http.ResponseWriter, r *http.Request) {
helpers.WriteJSON(w, http.StatusOK, context.Get(r, "project"))
var project struct {
db.Project
UserPermissions db.ProjectUserPermission `json:"userPermissions"`
}
project.Project = context.Get(r, "project").(db.Project)
project.UserPermissions = context.Get(r, "projectUserRole").(db.ProjectUserRole).GetPermissions()
helpers.WriteJSON(w, http.StatusOK, project)
}

// UpdateProject saves updated project details to the database
Expand Down
12 changes: 10 additions & 2 deletions db/ProjectUser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
)

var rolePermissions = map[ProjectUserRole]ProjectUserPermission{
ProjectOwner: CanRunProjectTasks | CanUpdateProject | CanManageProjectResources,
ProjectOwner: CanRunProjectTasks | CanManageProjectResources | CanUpdateProject,
ProjectManager: CanRunProjectTasks | CanManageProjectResources,
ProjectTaskRunner: CanRunProjectTasks,
ProjectGuest: 0,
Expand All @@ -39,5 +39,13 @@ type ProjectUser struct {

func (u *ProjectUser) Can(permissions ProjectUserPermission) bool {
userPermissions := rolePermissions[u.Role]
return (userPermissions & userPermissions) == permissions
return (userPermissions & permissions) == permissions
}

func (r ProjectUserRole) Can(permissions ProjectUserPermission) bool {
return (rolePermissions[r] & permissions) == permissions
}

func (r ProjectUserRole) GetPermissions() ProjectUserPermission {
return rolePermissions[r]
}
15 changes: 15 additions & 0 deletions db/ProjectUser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package db

import (
"testing"
)

func TestProjectUsers_RoleCan(t *testing.T) {
if !ProjectManager.Can(CanManageProjectResources) {
t.Fatal()
}

if ProjectManager.Can(CanUpdateProject) {
t.Fatal()
}
}

0 comments on commit b522169

Please sign in to comment.