pfug/models.go

92 lines
1.7 KiB
Go
Raw Permalink Normal View History

2023-11-04 01:44:04 +01:00
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
}
2024-06-10 21:33:16 +02:00
func (u User) ResetPassword(newPassword string) (User, error) {
u.PasswordHash = generatePasswordHash(newPassword, u.Salt)
return u, nil
}
2023-11-04 01:44:04 +01:00
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
}