86 lines
1.5 KiB
Go
86 lines
1.5 KiB
Go
|
package pfug
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
|
||
|
"git.0x0001f346.de/andreas/utils"
|
||
|
)
|
||
|
|
||
|
type User struct {
|
||
|
UUID string
|
||
|
Username string
|
||
|
Salt string
|
||
|
PasswordHash string
|
||
|
Mail string
|
||
|
FirstName string
|
||
|
LastName string
|
||
|
}
|
||
|
|
||
|
type Group struct {
|
||
|
UUID string
|
||
|
Name string
|
||
|
Members map[string]bool
|
||
|
Permissions map[string]bool
|
||
|
}
|
||
|
|
||
|
type Permission struct {
|
||
|
UUID string
|
||
|
Name string
|
||
|
}
|
||
|
|
||
|
func (u User) ChangePassword(oldPassword string, newPassword string) (User, error) {
|
||
|
if !u.IsUsingThisPassword(oldPassword) {
|
||
|
return u, errors.New("wrong password")
|
||
|
}
|
||
|
|
||
|
u.PasswordHash = generatePasswordHash(newPassword, u.Salt)
|
||
|
|
||
|
return u, nil
|
||
|
}
|
||
|
|
||
|
func (u User) IsMemberOfGroup(g Group) bool {
|
||
|
return g.Members[u.UUID]
|
||
|
}
|
||
|
|
||
|
func (u User) IsUsingThisPassword(password string) bool {
|
||
|
return generatePasswordHash(password, u.Salt) == u.PasswordHash
|
||
|
}
|
||
|
|
||
|
func (g Group) AddMember(u User) (Group, error) {
|
||
|
if !utils.IsValidUUID(u.UUID) {
|
||
|
return g, errors.New("unvalid UUID")
|
||
|
}
|
||
|
|
||
|
g.Members[u.UUID] = true
|
||
|
|
||
|
return g, nil
|
||
|
}
|
||
|
|
||
|
func (g Group) AddPermission(p Permission) (Group, error) {
|
||
|
if !utils.IsValidUUID(p.UUID) {
|
||
|
return g, errors.New("unvalid UUID")
|
||
|
}
|
||
|
|
||
|
g.Permissions[p.UUID] = true
|
||
|
|
||
|
return g, nil
|
||
|
}
|
||
|
|
||
|
func (g Group) HasUserAsMember(u User) bool {
|
||
|
return g.Members[u.UUID]
|
||
|
}
|
||
|
|
||
|
func (g Group) HasPermission(p Permission) bool {
|
||
|
return g.Permissions[p.UUID]
|
||
|
}
|
||
|
|
||
|
func (g Group) RemoveMember(u User) Group {
|
||
|
delete(g.Members, u.UUID)
|
||
|
return g
|
||
|
}
|
||
|
|
||
|
func (g Group) RemovePermission(p Permission) Group {
|
||
|
delete(g.Permissions, p.UUID)
|
||
|
return g
|
||
|
}
|