nezha/model/user.go

39 lines
689 B
Go
Raw Normal View History

2019-12-08 03:59:58 -05:00
package model
2024-12-18 04:26:24 -05:00
import (
"github.com/nezhahq/nezha/pkg/utils"
"gorm.io/gorm"
)
2024-12-16 06:40:37 -05:00
const (
RoleAdmin uint8 = iota
RoleMember
)
2019-12-08 03:59:58 -05:00
type User struct {
Common
2024-12-18 04:26:24 -05:00
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)"`
}
func (u *User) BeforeSave(tx *gorm.DB) error {
2024-12-21 05:34:46 -05:00
if u.AgentSecret != "" {
return nil
}
2024-12-18 04:26:24 -05:00
key, err := utils.GenerateRandomString(32)
if err != nil {
return err
}
u.AgentSecret = key
return nil
2019-12-08 03:59:58 -05:00
}
type Profile struct {
User
LoginIP string `json:"login_ip,omitempty"`
}