Semaphore/db/ProjectUser.go

48 lines
1.3 KiB
Go
Raw Permalink Normal View History

package db
2023-07-07 22:15:16 +02:00
type ProjectUserRole string
const (
ProjectOwner ProjectUserRole = "owner"
ProjectManager ProjectUserRole = "manager"
ProjectTaskRunner ProjectUserRole = "task_runner"
ProjectGuest ProjectUserRole = "guest"
2023-09-17 16:15:44 +02:00
ProjectNone ProjectUserRole = ""
2023-07-07 22:15:16 +02:00
)
type ProjectUserPermission int
const (
CanRunProjectTasks ProjectUserPermission = 1 << iota
CanUpdateProject
CanManageProjectResources
CanManageProjectUsers
2023-07-07 22:15:16 +02:00
)
var rolePermissions = map[ProjectUserRole]ProjectUserPermission{
2023-08-26 21:13:48 +02:00
ProjectOwner: CanRunProjectTasks | CanManageProjectResources | CanUpdateProject | CanManageProjectUsers,
2023-09-17 15:17:15 +02:00
ProjectManager: CanRunProjectTasks | CanManageProjectResources,
ProjectTaskRunner: CanRunProjectTasks,
ProjectGuest: 0,
2023-07-07 22:15:16 +02:00
}
func (r ProjectUserRole) IsValid() bool {
_, ok := rolePermissions[r]
return ok
}
type ProjectUser struct {
2023-07-07 22:15:16 +02:00
ID int `db:"id" json:"-"`
ProjectID int `db:"project_id" json:"project_id"`
UserID int `db:"user_id" json:"user_id"`
Role ProjectUserRole `db:"role" json:"role"`
}
2023-08-26 18:48:16 +02:00
func (r ProjectUserRole) Can(permissions ProjectUserPermission) bool {
return (rolePermissions[r] & permissions) == permissions
}
func (r ProjectUserRole) GetPermissions() ProjectUserPermission {
return rolePermissions[r]
}