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

View File

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

View File

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