alist/internal/model/user.go

91 lines
2.0 KiB
Go
Raw Normal View History

2022-06-16 08:06:10 +00:00
package model
2022-06-25 14:05:02 +00:00
import (
"github.com/alist-org/alist/v3/internal/errs"
"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
)
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
Password string `json:"password"` // password
BasePath string `json:"base_path"` // base path
Role int `json:"role"` // user's role
// Determine permissions by bit
// 0: can see hidden files
// 1: can access without password
// 2: can add aria2 tasks
2022-06-30 07:53:57 +00:00
// 3: can mkdir and upload
// 4: can rename
// 5: can move
// 6: can copy
// 7: can remove
// 8: webdav read
// 9: webdav write
2022-06-29 10:03:12 +00:00
Permission int32 `json:"permission"`
2022-06-16 08:06:10 +00:00
}
func (u User) IsGuest() bool {
return u.Role == GUEST
}
func (u User) IsAdmin() bool {
return u.Role == ADMIN
}
2022-06-25 13:34:44 +00:00
func (u User) ValidatePassword(password string) error {
if password == "" {
2022-06-25 14:05:02 +00:00
return errors.WithStack(errs.EmptyPassword)
2022-06-25 13:34:44 +00:00
}
if u.Password != password {
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
2022-06-29 10:03:12 +00:00
func (u User) CanSeeHides() bool {
return u.IsAdmin() || u.Permission&1 == 1
}
func (u User) CanAccessWithoutPassword() bool {
return u.IsAdmin() || (u.Permission>>1)&1 == 1
}
func (u User) CanAddAria2Tasks() bool {
return u.IsAdmin() || (u.Permission>>2)&1 == 1
}
2022-06-30 07:53:57 +00:00
func (u User) CanWrite() bool {
2022-06-29 10:03:12 +00:00
return u.IsAdmin() || (u.Permission>>3)&1 == 1
}
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
}
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
}
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
}
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
}
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
}
func (u User) CanWebdavWrite() 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
}