2022-06-16 08:06:10 +00:00
|
|
|
package model
|
|
|
|
|
2022-06-25 14:05:02 +00:00
|
|
|
import (
|
2023-08-06 14:09:17 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-06-25 14:05:02 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
2022-11-30 13:38:00 +00:00
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
2023-08-07 07:46:19 +00:00
|
|
|
"github.com/alist-org/alist/v3/pkg/utils/random"
|
2022-06-25 14:05:02 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
2022-06-25 13:34:44 +00:00
|
|
|
|
2022-06-16 08:06:10 +00:00
|
|
|
const (
|
|
|
|
GENERAL = iota
|
|
|
|
GUEST // only one exists
|
|
|
|
ADMIN
|
|
|
|
)
|
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
const StaticHashSalt = "https://github.com/alist-org/alist"
|
2023-08-06 14:09:17 +00:00
|
|
|
|
2022-06-16 08:06:10 +00:00
|
|
|
type User struct {
|
2022-06-29 10:03:12 +00:00
|
|
|
ID uint `json:"id" gorm:"primaryKey"` // unique key
|
|
|
|
Username string `json:"username" gorm:"unique" binding:"required"` // username
|
2023-08-06 14:09:17 +00:00
|
|
|
PwdHash string `json:"-"` // password hash
|
2023-08-07 07:46:19 +00:00
|
|
|
Salt string // unique salt
|
2023-08-07 10:51:54 +00:00
|
|
|
Password string `json:"password"` // password
|
2023-08-07 07:46:19 +00:00
|
|
|
BasePath string `json:"base_path"` // base path
|
|
|
|
Role int `json:"role"` // user's role
|
2023-02-04 03:44:17 +00:00
|
|
|
Disabled bool `json:"disabled"`
|
2022-06-29 10:03:12 +00:00
|
|
|
// Determine permissions by bit
|
2023-02-14 07:20:45 +00:00
|
|
|
// 0: can see hidden files
|
|
|
|
// 1: can access without password
|
|
|
|
// 2: can add aria2 tasks
|
|
|
|
// 3: can mkdir and upload
|
|
|
|
// 4: can rename
|
|
|
|
// 5: can move
|
|
|
|
// 6: can copy
|
|
|
|
// 7: can remove
|
|
|
|
// 8: webdav read
|
|
|
|
// 9: webdav write
|
|
|
|
// 10: can add qbittorrent tasks
|
2022-08-08 08:29:56 +00:00
|
|
|
Permission int32 `json:"permission"`
|
|
|
|
OtpSecret string `json:"-"`
|
2023-07-20 08:30:30 +00:00
|
|
|
SsoID string `json:"sso_id"` // unique by sso platform
|
2022-06-16 08:06:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) IsGuest() bool {
|
2022-06-16 08:06:10 +00:00
|
|
|
return u.Role == GUEST
|
|
|
|
}
|
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) IsAdmin() bool {
|
2022-06-16 08:06:10 +00:00
|
|
|
return u.Role == ADMIN
|
|
|
|
}
|
2022-06-25 13:34:44 +00:00
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) ValidateRawPassword(password string) error {
|
|
|
|
return u.ValidatePwdStaticHash(StaticHash(password))
|
2023-08-06 14:09:17 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) ValidatePwdStaticHash(pwdStaticHash string) error {
|
|
|
|
if pwdStaticHash == "" {
|
2022-06-25 14:05:02 +00:00
|
|
|
return errors.WithStack(errs.EmptyPassword)
|
2022-06-25 13:34:44 +00:00
|
|
|
}
|
2023-08-07 07:46:19 +00:00
|
|
|
if u.PwdHash != HashPwd(pwdStaticHash, u.Salt) {
|
2022-06-25 14:05:02 +00:00
|
|
|
return errors.WithStack(errs.WrongPassword)
|
2022-06-25 13:34:44 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-29 09:08:31 +00:00
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) SetPassword(pwd string) *User {
|
|
|
|
u.Salt = random.String(16)
|
|
|
|
u.PwdHash = TwoHashPwd(pwd, u.Salt)
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) CanSeeHides() bool {
|
2022-06-29 10:03:12 +00:00
|
|
|
return u.IsAdmin() || u.Permission&1 == 1
|
|
|
|
}
|
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) CanAccessWithoutPassword() bool {
|
2022-06-29 10:03:12 +00:00
|
|
|
return u.IsAdmin() || (u.Permission>>1)&1 == 1
|
|
|
|
}
|
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) CanAddAria2Tasks() bool {
|
2022-06-29 10:03:12 +00:00
|
|
|
return u.IsAdmin() || (u.Permission>>2)&1 == 1
|
|
|
|
}
|
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) CanWrite() bool {
|
2022-06-29 10:03:12 +00:00
|
|
|
return u.IsAdmin() || (u.Permission>>3)&1 == 1
|
|
|
|
}
|
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) CanRename() bool {
|
2022-06-30 07:53:57 +00:00
|
|
|
return u.IsAdmin() || (u.Permission>>4)&1 == 1
|
2022-06-29 10:03:12 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) CanMove() bool {
|
2022-06-30 07:53:57 +00:00
|
|
|
return u.IsAdmin() || (u.Permission>>5)&1 == 1
|
2022-06-29 10:03:12 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) CanCopy() bool {
|
2022-06-30 07:53:57 +00:00
|
|
|
return u.IsAdmin() || (u.Permission>>6)&1 == 1
|
2022-06-29 10:03:12 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) CanRemove() bool {
|
2022-06-30 07:53:57 +00:00
|
|
|
return u.IsAdmin() || (u.Permission>>7)&1 == 1
|
2022-06-29 10:03:12 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) CanWebdavRead() bool {
|
2022-06-30 07:53:57 +00:00
|
|
|
return u.IsAdmin() || (u.Permission>>8)&1 == 1
|
2022-06-29 10:03:12 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) CanWebdavManage() bool {
|
2022-06-30 07:53:57 +00:00
|
|
|
return u.IsAdmin() || (u.Permission>>9)&1 == 1
|
2022-06-29 09:08:31 +00:00
|
|
|
}
|
2022-11-30 13:38:00 +00:00
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) CanAddQbittorrentTasks() bool {
|
2023-02-14 07:20:45 +00:00
|
|
|
return u.IsAdmin() || (u.Permission>>10)&1 == 1
|
|
|
|
}
|
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func (u *User) JoinPath(reqPath string) (string, error) {
|
2022-11-30 13:38:00 +00:00
|
|
|
return utils.JoinBasePath(u.BasePath, reqPath)
|
|
|
|
}
|
2023-08-06 14:09:17 +00:00
|
|
|
|
2023-08-07 07:46:19 +00:00
|
|
|
func StaticHash(password string) string {
|
|
|
|
return utils.GetSHA256Encode([]byte(fmt.Sprintf("%s-%s", password, StaticHashSalt)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func HashPwd(static string, salt string) string {
|
|
|
|
return utils.GetSHA256Encode([]byte(fmt.Sprintf("%s-%s", static, salt)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TwoHashPwd(password string, salt string) string {
|
|
|
|
return HashPwd(StaticHash(password), salt)
|
2023-08-06 14:09:17 +00:00
|
|
|
}
|