2020-12-04 23:41:26 +01:00
|
|
|
package db
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2023-07-07 22:15:16 +02:00
|
|
|
type ProjectUserRole string
|
|
|
|
|
|
|
|
const (
|
2023-07-07 23:15:30 +02:00
|
|
|
ProjectOwner ProjectUserRole = "owner"
|
2023-07-08 23:35:39 +02:00
|
|
|
ProjectManager ProjectUserRole = "manager"
|
2023-07-07 23:15:30 +02:00
|
|
|
ProjectTaskRunner ProjectUserRole = "task_runner"
|
|
|
|
ProjectGuest ProjectUserRole = "guest"
|
2023-07-07 22:15:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProjectUserPermission int
|
|
|
|
|
|
|
|
const (
|
2023-07-08 23:35:39 +02:00
|
|
|
CanRunProjectTasks ProjectUserPermission = 1 << iota
|
|
|
|
CanUpdateProject
|
|
|
|
CanManageProjectResources
|
|
|
|
CanManageProjectUsers
|
2023-07-07 22:15:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var rolePermissions = map[ProjectUserRole]ProjectUserPermission{
|
2023-08-26 18:48:16 +02:00
|
|
|
ProjectOwner: CanRunProjectTasks | CanManageProjectResources | CanUpdateProject,
|
2023-07-08 23:35:39 +02:00
|
|
|
ProjectManager: CanRunProjectTasks | CanManageProjectResources,
|
|
|
|
ProjectTaskRunner: CanRunProjectTasks,
|
2023-07-07 23:15:30 +02:00
|
|
|
ProjectGuest: 0,
|
2023-07-07 22:15:16 +02:00
|
|
|
}
|
|
|
|
|
2023-07-09 10:23:13 +02:00
|
|
|
func (r ProjectUserRole) IsValid() bool {
|
|
|
|
_, ok := rolePermissions[r]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2020-12-01 20:06:49 +01:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *ProjectUser) Can(permissions ProjectUserPermission) bool {
|
|
|
|
userPermissions := rolePermissions[u.Role]
|
2023-08-26 18:48:16 +02:00
|
|
|
return (userPermissions & permissions) == permissions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r ProjectUserRole) Can(permissions ProjectUserPermission) bool {
|
|
|
|
return (rolePermissions[r] & permissions) == permissions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r ProjectUserRole) GetPermissions() ProjectUserPermission {
|
|
|
|
return rolePermissions[r]
|
2020-12-01 20:06:49 +01:00
|
|
|
}
|