♻️ Use power level permission management

This commit is contained in:
LittleSheep 2024-02-06 11:05:40 +08:00
parent 922fa45fa7
commit 37ac26378f
3 changed files with 17 additions and 20 deletions

View File

@ -5,8 +5,6 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
"path/filepath" "path/filepath"
"time" "time"
"gorm.io/datatypes"
) )
type AccountState = int8 type AccountState = int8
@ -19,21 +17,21 @@ const (
type Account struct { type Account struct {
BaseModel BaseModel
Name string `json:"name" gorm:"uniqueIndex"` Name string `json:"name" gorm:"uniqueIndex"`
Nick string `json:"nick"` Nick string `json:"nick"`
Avatar string `json:"avatar"` Avatar string `json:"avatar"`
State AccountState `json:"state"` State AccountState `json:"state"`
Profile AccountProfile `json:"profile"` Profile AccountProfile `json:"profile"`
Sessions []AuthSession `json:"sessions"` Sessions []AuthSession `json:"sessions"`
Challenges []AuthChallenge `json:"challenges"` Challenges []AuthChallenge `json:"challenges"`
Factors []AuthFactor `json:"factors"` Factors []AuthFactor `json:"factors"`
Contacts []AccountContact `json:"contacts"` Contacts []AccountContact `json:"contacts"`
Events []ActionEvent `json:"events"` Events []ActionEvent `json:"events"`
MagicTokens []MagicToken `json:"-" gorm:"foreignKey:AssignTo"` MagicTokens []MagicToken `json:"-" gorm:"foreignKey:AssignTo"`
ThirdClients []ThirdClient `json:"clients"` ThirdClients []ThirdClient `json:"clients"`
Notifications []Notification `json:"notifications" gorm:"foreignKey:RecipientID"` Notifications []Notification `json:"notifications" gorm:"foreignKey:RecipientID"`
ConfirmedAt *time.Time `json:"confirmed_at"` ConfirmedAt *time.Time `json:"confirmed_at"`
Permissions datatypes.JSONType[[]string] `json:"permissions"` PowerLevel int `json:"power_level"`
} }
func (v Account) GetPrimaryEmail() AccountContact { func (v Account) GetPrimaryEmail() AccountContact {

View File

@ -29,7 +29,6 @@ var auth = keyauth.New(keyauth.Config{
} }
c.Locals("principal", user) c.Locals("principal", user)
c.Locals("permissions", user.Permissions.Data())
return true, nil return true, nil
}, },

View File

@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/samber/lo" "github.com/samber/lo"
"gorm.io/datatypes"
"gorm.io/gorm" "gorm.io/gorm"
"time" "time"
) )
@ -68,7 +67,7 @@ func CreateAccount(name, nick, email, password string) (models.Account, error) {
VerifiedAt: nil, VerifiedAt: nil,
}, },
}, },
Permissions: datatypes.NewJSONType(make([]string, 0)), PowerLevel: 0,
ConfirmedAt: nil, ConfirmedAt: nil,
} }
@ -100,6 +99,7 @@ func ConfirmAccount(code string) error {
return database.C.Transaction(func(tx *gorm.DB) error { return database.C.Transaction(func(tx *gorm.DB) error {
user.ConfirmedAt = lo.ToPtr(time.Now()) user.ConfirmedAt = lo.ToPtr(time.Now())
user.PowerLevel += 5
if err := database.C.Delete(&token).Error; err != nil { if err := database.C.Delete(&token).Error; err != nil {
return err return err