mirror of
https://github.com/nezhahq/nezha.git
synced 2025-03-13 17:18:13 -04:00

* [WIP] feat: user roles * update * update * admin handler * update * feat: user-specific connection secret * simplify some logics * cleanup * update waf * update user api error handling * update waf api * fix codeql * update waf table * fix several problems * add pagination for waf api * update permission checks * switch to runtime check * 1 * cover? * some changes
45 lines
775 B
Go
45 lines
775 B
Go
package model
|
|
|
|
import (
|
|
"github.com/nezhahq/nezha/pkg/utils"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const (
|
|
RoleAdmin uint8 = iota
|
|
RoleMember
|
|
)
|
|
|
|
type User struct {
|
|
Common
|
|
Username string `json:"username,omitempty" gorm:"uniqueIndex"`
|
|
Password string `json:"password,omitempty" gorm:"type:char(72)"`
|
|
Role uint8 `json:"role,omitempty"`
|
|
AgentSecret string `json:"agent_secret,omitempty" gorm:"type:char(32)"`
|
|
}
|
|
|
|
type UserInfo struct {
|
|
Role uint8
|
|
_ [3]byte
|
|
AgentSecret string
|
|
}
|
|
|
|
func (u *User) BeforeSave(tx *gorm.DB) error {
|
|
if u.AgentSecret != "" {
|
|
return nil
|
|
}
|
|
|
|
key, err := utils.GenerateRandomString(32)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
u.AgentSecret = key
|
|
return nil
|
|
}
|
|
|
|
type Profile struct {
|
|
User
|
|
LoginIP string `json:"login_ip,omitempty"`
|
|
}
|