mirror of
https://github.com/semaphoreui/semaphore.git
synced 2024-11-23 12:30:41 +01:00
feat(be): add role based checks
This commit is contained in:
parent
29b38c446c
commit
23841a240f
@ -1,8 +1,36 @@
|
||||
package db
|
||||
|
||||
type ProjectUser struct {
|
||||
ID int `db:"id" json:"-"`
|
||||
ProjectID int `db:"project_id" json:"project_id"`
|
||||
UserID int `db:"user_id" json:"user_id"`
|
||||
Admin bool `db:"admin" json:"admin"`
|
||||
type ProjectUserRole string
|
||||
|
||||
const (
|
||||
ProjectUserOwner ProjectUserRole = "owner"
|
||||
ProjectUserRunner ProjectUserRole = "runner"
|
||||
ProjectUserGuest ProjectUserRole = "guest"
|
||||
)
|
||||
|
||||
type ProjectUserPermission int
|
||||
|
||||
const (
|
||||
ProjectUserCanRunTask ProjectUserPermission = 1 << iota
|
||||
ProjectCanEditProjectSettings
|
||||
ProjectCanRunTasks
|
||||
)
|
||||
|
||||
var rolePermissions = map[ProjectUserRole]ProjectUserPermission{
|
||||
ProjectUserOwner: ProjectUserCanRunTask | ProjectCanEditProjectSettings | ProjectCanRunTasks,
|
||||
ProjectUserRunner: ProjectCanRunTasks,
|
||||
ProjectUserGuest: 0,
|
||||
}
|
||||
|
||||
type ProjectUser struct {
|
||||
ID int `db:"id" json:"-"`
|
||||
ProjectID int `db:"project_id" json:"project_id"`
|
||||
UserID int `db:"user_id" json:"user_id"`
|
||||
Admin bool `db:"admin" json:"admin"`
|
||||
Role ProjectUserRole `db:"role" json:"role"`
|
||||
}
|
||||
|
||||
func (u *ProjectUser) Can(permissions ProjectUserPermission) bool {
|
||||
userPermissions := rolePermissions[u.Role]
|
||||
return (userPermissions & userPermissions) == permissions
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user