2019-12-08 03:59:58 -05:00
|
|
|
package model
|
|
|
|
|
2024-12-21 11:05:41 -05:00
|
|
|
import (
|
2024-12-21 12:08:07 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2024-12-21 11:05:41 -05:00
|
|
|
"github.com/nezhahq/nezha/pkg/utils"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
RoleAdmin uint8 = iota
|
|
|
|
RoleMember
|
|
|
|
)
|
|
|
|
|
2019-12-08 03:59:58 -05:00
|
|
|
type User struct {
|
2020-12-19 10:11:16 -05:00
|
|
|
Common
|
2024-12-21 11:05:41 -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)"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserInfo struct {
|
|
|
|
Role uint8
|
|
|
|
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
|
2019-12-08 03:59:58 -05:00
|
|
|
}
|
2024-11-23 03:22:22 -05:00
|
|
|
|
|
|
|
type Profile struct {
|
|
|
|
User
|
|
|
|
LoginIP string `json:"login_ip,omitempty"`
|
|
|
|
}
|
2024-12-21 12:08:07 -05:00
|
|
|
|
|
|
|
type OnlineUser struct {
|
|
|
|
UserID uint64 `json:"user_id,omitempty"`
|
|
|
|
ConnectedAt time.Time `json:"connected_at,omitempty"`
|
|
|
|
IP string `json:"ip,omitempty"`
|
|
|
|
|
|
|
|
Conn *websocket.Conn `json:"-"`
|
|
|
|
}
|