Atomise permissions field in database

This commit is contained in:
Eccentric 2024-02-09 21:51:26 +00:00
parent 0e6788e775
commit 86748fa32b
3 changed files with 27 additions and 36 deletions

View File

@ -1,16 +1,18 @@
package person
type Permission string
type Permission int64
// DO NOT MOVE THE ORDER OF THESE PERMISSIONS AS THEY ARE USED IN THE DATABASE
const (
PermissionLookup Permission = "lookup"
PermissionBan Permission = "ban"
PermissionInformation Permission = "information"
PermissionDonator Permission = "donator"
PermissionGiveItem Permission = "give_item"
PermissionTakeItem Permission = "take_item"
PermissionReset Permission = "reset"
PermissionFullLocker Permission = "full_locker"
PermissionLookup Permission = 1 << iota
PermissionBan
PermissionInformation
PermissionItemControl
PermissionLockerControl
PermissionPermissionControl
// user roles, not really permissions but implemented as such
PermissionOwner
PermissionDonator
PermissionAll Permission = "all"
PermissionAll Permission = 1<<iota - 1
)

View File

@ -12,7 +12,7 @@ type Person struct {
ID string
DisplayName string
RefundTickets int
Permissions []string
Permissions Permission
AthenaProfile *Profile
CommonCoreProfile *Profile
CommonPublicProfile *Profile
@ -28,7 +28,7 @@ func NewPerson() *Person {
return &Person{
ID: uuid.New().String(),
DisplayName: uuid.New().String(),
Permissions: []string{},
Permissions: 0,
RefundTickets: 3,
AthenaProfile: NewProfile("athena"),
CommonCoreProfile: NewProfile("common_core"),
@ -43,7 +43,7 @@ func NewPersonWithCustomID(id string) *Person {
return &Person{
ID: id,
DisplayName: uuid.New().String(),
Permissions: []string{},
Permissions: 0,
RefundTickets: 3,
AthenaProfile: NewProfile("athena"),
CommonCoreProfile: NewProfile("common_core"),
@ -187,7 +187,7 @@ func findHelper(databasePerson *storage.DB_Person, shallow bool, save bool) *Per
person := &Person{
ID: databasePerson.ID,
DisplayName: databasePerson.DisplayName,
Permissions: databasePerson.Permissions,
Permissions: Permission(databasePerson.Permissions),
BanHistory: databasePerson.BanHistory,
AthenaProfile: athenaProfile,
CommonCoreProfile: commonCoreProfile,
@ -282,40 +282,29 @@ func (p *Person) Unban() {
p.SaveShallow()
}
func (p *Person) AddPermission(permission string) {
p.Permissions = append(p.Permissions, permission)
func (p *Person) AddPermission(permission Permission) {
p.Permissions |= permission
p.SaveShallow()
}
func (p *Person) RemovePermission(permission string) {
for i, perm := range p.Permissions {
if perm == permission {
p.Permissions = append(p.Permissions[:i], p.Permissions[i+1:]...)
break
}
}
func (p *Person) RemovePermission(permission Permission) {
p.Permissions &= ^permission
p.SaveShallow()
}
func (p *Person) HasPermission(permission Permission) bool {
for _, perm := range p.Permissions {
if perm == string(PermissionAll) {
return true
if permission == PermissionAll {
return p.Permissions == PermissionAll
}
if perm == string(permission) {
return true
}
}
return false
return p.Permissions & permission != 0
}
func (p *Person) ToDatabase() *storage.DB_Person {
dbPerson := storage.DB_Person{
ID: p.ID,
DisplayName: p.DisplayName,
Permissions: p.Permissions,
Permissions: int64(p.Permissions),
BanHistory: p.BanHistory,
RefundTickets: p.RefundTickets,
Profiles: []storage.DB_Profile{},
@ -384,7 +373,7 @@ func (p *Person) ToDatabaseShallow() *storage.DB_Person {
dbPerson := storage.DB_Person{
ID: p.ID,
DisplayName: p.DisplayName,
Permissions: p.Permissions,
Permissions: int64(p.Permissions),
BanHistory: p.BanHistory,
RefundTickets: p.RefundTickets,
Profiles: []storage.DB_Profile{},
@ -403,7 +392,7 @@ func (p *Person) Snapshot() *PersonSnapshot {
return &PersonSnapshot{
ID: p.ID,
DisplayName: p.DisplayName,
Permissions: p.Permissions,
Permissions: int64(p.Permissions),
AthenaProfile: *p.AthenaProfile.Snapshot(),
CommonCoreProfile: *p.CommonCoreProfile.Snapshot(),
CommonPublicProfile: *p.CommonPublicProfile.Snapshot(),

View File

@ -10,7 +10,7 @@ type DB_Person struct {
ID string
DisplayName string
RefundTickets int
Permissions pq.StringArray `gorm:"type:text[]"`
Permissions int64
Profiles []DB_Profile `gorm:"foreignkey:PersonID"`
Stats []DB_SeasonStat `gorm:"foreignkey:PersonID"`
Discord DB_DiscordPerson `gorm:"foreignkey:PersonID"`