Initial commit: Эфир мессенджер
This commit is contained in:
45
internal/models/user.go
Normal file
45
internal/models/user.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type UserRole string
|
||||
|
||||
const (
|
||||
RoleUser UserRole = "user"
|
||||
RoleGlobalAdmin UserRole = "global_admin"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID int64 `json:"id"`
|
||||
Login string `json:"login"`
|
||||
PasswordHash string `json:"-"` // never send to client
|
||||
Role UserRole `json:"role"`
|
||||
LastSeen *time.Time `json:"last_seen,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// SafeUser возвращает пользователя без чувствительных данных
|
||||
type SafeUser struct {
|
||||
ID int64 `json:"id"`
|
||||
Login string `json:"login"`
|
||||
Role UserRole `json:"role"`
|
||||
LastSeen *time.Time `json:"last_seen,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (u *User) ToSafe() *SafeUser {
|
||||
return &SafeUser{
|
||||
ID: u.ID,
|
||||
Login: u.Login,
|
||||
Role: u.Role,
|
||||
LastSeen: u.LastSeen,
|
||||
CreatedAt: u.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// IsGlobalAdmin проверяет, является ли пользователь глобальным админом
|
||||
func (u *User) IsGlobalAdmin() bool {
|
||||
return u.Role == RoleGlobalAdmin
|
||||
}
|
||||
Reference in New Issue
Block a user