feat: user manage api

This commit is contained in:
Noah Hsu
2022-06-26 19:36:27 +08:00
parent 6b9bca893b
commit cab498e376
5 changed files with 101 additions and 10 deletions

View File

@ -86,6 +86,9 @@ func DeleteUserById(id uint) error {
if err != nil {
return err
}
if old.IsAdmin() || old.IsGuest() {
return errors.WithStack(errs.DeleteAdminOrGuest)
}
userCache.Del(old.Username)
return errors.WithStack(db.Delete(&model.User{}, id).Error)
}

View File

@ -3,7 +3,8 @@ package errs
import "errors"
var (
EmptyUsername = errors.New("username is empty")
EmptyPassword = errors.New("password is empty")
WrongPassword = errors.New("password is incorrect")
EmptyUsername = errors.New("username is empty")
EmptyPassword = errors.New("password is empty")
WrongPassword = errors.New("password is incorrect")
DeleteAdminOrGuest = errors.New("cannot delete admin or guest")
)

View File

@ -12,13 +12,13 @@ const (
)
type User struct {
ID uint `json:"id" gorm:"primaryKey"` // unique key
Username string `json:"username" gorm:"unique"` // 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
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
}
func (u User) IsGuest() bool {