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-27 11:10:02 +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
|
|
|
|
ReadOnly bool `json:"read_only"` // read only
|
|
|
|
Webdav bool `json:"webdav"` // allow webdav
|
|
|
|
Role int `json:"role"` // user's role
|
|
|
|
IgnoreHide bool `json:"can_hide"` // can see hide files
|
|
|
|
IgnorePassword bool `json:"ignore_password"` // can access without password
|
2022-06-29 09:08:31 +00:00
|
|
|
Aira2 bool `json:"aira_2"` // can add aria2 tasks
|
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
|
|
|
|
|
|
|
func (u User) CanWrite() bool {
|
|
|
|
return u.IsAdmin() || !u.ReadOnly
|
|
|
|
}
|