chore: user permissions

This commit is contained in:
Noah Hsu
2022-06-29 18:03:12 +08:00
parent 3c7a2f78cf
commit d24e51bc86
11 changed files with 110 additions and 48 deletions

View File

@ -20,14 +20,11 @@ func initDevData() {
log.Fatalf("failed to create account: %+v", err)
}
err = db.CreateUser(&model.User{
Username: "Noah",
Password: "hsu",
BasePath: "/data",
ReadOnly: false,
Webdav: false,
Role: 0,
IgnoreHide: false,
IgnorePassword: false,
Username: "Noah",
Password: "hsu",
BasePath: "/data",
Role: 0,
Permission: 512,
})
if err != nil {
log.Fatalf("failed to create user: %+v", err)

View File

@ -23,7 +23,6 @@ func initUser() {
Password: adminPassword,
Role: model.ADMIN,
BasePath: "/",
Webdav: true,
}
if err := db.CreateUser(admin); err != nil {
panic(err)
@ -36,12 +35,11 @@ func initUser() {
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
guest = &model.User{
Username: "guest",
Password: "guest",
ReadOnly: true,
Webdav: true,
Role: model.GUEST,
BasePath: "/",
Username: "guest",
Password: "guest",
Role: model.GUEST,
BasePath: "/",
Permission: 512,
}
if err := db.CreateUser(guest); err != nil {
panic(err)

7
internal/errs/operate.go Normal file
View File

@ -0,0 +1,7 @@
package errs
import "errors"
var (
PermissionDenied = errors.New("permission denied")
)

View File

@ -66,7 +66,7 @@ func whetherHide(user *model.User, meta *model.Meta, path string) bool {
if user.IsGuest() {
return true
}
return !user.IgnoreHide
return !user.CanSeeHides()
}
func hide(objs []model.Obj, meta *model.Meta) {

View File

@ -12,16 +12,24 @@ const (
)
type User struct {
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
Aira2 bool `json:"aira_2"` // can add aria2 tasks
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
// 3: can mkdir
// 4: can upload
// 5: can rename
// 6: can move
// 7: can copy
// 8: can remove
// 9: webdav read
// 10: webdav write
Permission int32 `json:"permission"`
}
func (u User) IsGuest() bool {
@ -42,6 +50,46 @@ func (u User) ValidatePassword(password string) error {
return nil
}
func (u User) CanWrite() bool {
return u.IsAdmin() || !u.ReadOnly
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
}
func (u User) CanMkdir() bool {
return u.IsAdmin() || (u.Permission>>3)&1 == 1
}
func (u User) CanUpload() bool {
return u.IsAdmin() || (u.Permission>>4)&1 == 1
}
func (u User) CanRename() bool {
return u.IsAdmin() || (u.Permission>>5)&1 == 1
}
func (u User) CanMove() bool {
return u.IsAdmin() || (u.Permission>>6)&1 == 1
}
func (u User) CanCopy() bool {
return u.IsAdmin() || (u.Permission>>7)&1 == 1
}
func (u User) CanRemove() bool {
return u.IsAdmin() || (u.Permission>>8)&1 == 1
}
func (u User) CanWebdavRead() bool {
return u.IsAdmin() || (u.Permission>>9)&1 == 1
}
func (u User) CanWebdavWrite() bool {
return u.IsAdmin() || (u.Permission>>10)&1 == 1
}