feat(users): enable quota in handlers

This commit is contained in:
hexxa 2021-08-07 17:18:10 +08:00 committed by Hexxa
parent 75e10e6ad9
commit e73947de0d
4 changed files with 60 additions and 34 deletions

View file

@ -354,11 +354,19 @@ func (h *MultiUsersSvc) AddUser(c *gin.Context) {
return return
} }
spaceLimit := h.cfg.IntOr("Users.SpaceLimit", 1024)
uploadSpeedLimit := h.cfg.IntOr("Users.UploadSpeedLimit", 100*1024)
downloadSpeedLimit := h.cfg.IntOr("Users.DownloadSpeedLimit", 100*1024)
err = h.deps.Users().AddUser(&userstore.User{ err = h.deps.Users().AddUser(&userstore.User{
ID: uid, ID: uid,
Name: req.Name, Name: req.Name,
Pwd: string(pwdHash), Pwd: string(pwdHash),
Role: req.Role, Role: req.Role,
Quota: &userstore.Quota{
SpaceLimit: spaceLimit,
UploadSpeedLimit: uploadSpeedLimit,
DownloadSpeedLimit: downloadSpeedLimit,
},
}) })
if err != nil { if err != nil {
c.JSON(q.ErrResp(c, 500, err)) c.JSON(q.ErrResp(c, 500, err))

View file

@ -9,17 +9,20 @@ type FSConfig struct {
} }
type UsersCfg struct { type UsersCfg struct {
EnableAuth bool `json:"enableAuth" yaml:"enableAuth"` EnableAuth bool `json:"enableAuth" yaml:"enableAuth"`
DefaultAdmin string `json:"defaultAdmin" yaml:"defaultAdmin" cfg:"env"` DefaultAdmin string `json:"defaultAdmin" yaml:"defaultAdmin" cfg:"env"`
DefaultAdminPwd string `json:"defaultAdminPwd" yaml:"defaultAdminPwd" cfg:"env"` DefaultAdminPwd string `json:"defaultAdminPwd" yaml:"defaultAdminPwd" cfg:"env"`
CookieTTL int `json:"cookieTTL" yaml:"cookieTTL"` CookieTTL int `json:"cookieTTL" yaml:"cookieTTL"`
CookieSecure bool `json:"cookieSecure" yaml:"cookieSecure"` CookieSecure bool `json:"cookieSecure" yaml:"cookieSecure"`
CookieHttpOnly bool `json:"cookieHttpOnly" yaml:"cookieHttpOnly"` CookieHttpOnly bool `json:"cookieHttpOnly" yaml:"cookieHttpOnly"`
MinUserNameLen int `json:"minUserNameLen" yaml:"minUserNameLen"` MinUserNameLen int `json:"minUserNameLen" yaml:"minUserNameLen"`
MinPwdLen int `json:"minPwdLen" yaml:"minPwdLen"` MinPwdLen int `json:"minPwdLen" yaml:"minPwdLen"`
CaptchaWidth int `json:"captchaWidth" yaml:"captchaWidth"` CaptchaWidth int `json:"captchaWidth" yaml:"captchaWidth"`
CaptchaHeight int `json:"captchaHeight" yaml:"captchaHeight"` CaptchaHeight int `json:"captchaHeight" yaml:"captchaHeight"`
CaptchaEnabled bool `json:"captchaEnabled" yaml:"captchaEnabled"` CaptchaEnabled bool `json:"captchaEnabled" yaml:"captchaEnabled"`
UploadSpeedLimit int `json:"uploadSpeedLimit" yaml:"uploadSpeedLimit"`
DownloadSpeedLimit int `json:"downloadSpeedLimit" yaml:"downloadSpeedLimit"`
SpaceLimit int `json:"spaceLimit" yaml:"spaceLimit"`
} }
type Secrets struct { type Secrets struct {
@ -55,17 +58,20 @@ func DefaultConfig() (string, error) {
OpenTTL: 60, // 1 min OpenTTL: 60, // 1 min
}, },
Users: &UsersCfg{ Users: &UsersCfg{
EnableAuth: true, EnableAuth: true,
DefaultAdmin: "", DefaultAdmin: "",
DefaultAdminPwd: "", DefaultAdminPwd: "",
CookieTTL: 3600 * 24 * 7, // 1 week CookieTTL: 3600 * 24 * 7, // 1 week
CookieSecure: false, CookieSecure: false,
CookieHttpOnly: true, CookieHttpOnly: true,
MinUserNameLen: 4, MinUserNameLen: 4,
MinPwdLen: 6, MinPwdLen: 6,
CaptchaWidth: 256, CaptchaWidth: 256,
CaptchaHeight: 60, CaptchaHeight: 60,
CaptchaEnabled: true, CaptchaEnabled: true,
UploadSpeedLimit: 100 * 1024, // B
DownloadSpeedLimit: 100 * 1024, // B
SpaceLimit: 1024, // GB
}, },
Secrets: &Secrets{ Secrets: &Secrets{
TokenSecret: "", TokenSecret: "",

View file

@ -17,16 +17,18 @@ const (
InitNs = "usersInit" InitNs = "usersInit"
IDsNs = "ids" IDsNs = "ids"
UsersNs = "users" UsersNs = "users"
PwdsNs = "pwds"
RolesNs = "roles"
RoleListNs = "roleList" RoleListNs = "roleList"
InitTimeKey = "initTime" InitTimeKey = "initTime"
defaultSpaceLimit = 1024 // 1GB
defaultUploadSpeedLimit = 100 * 1024 // 100KB
defaultDownloadSpeedLimit = 100 * 1024 // 100KB
) )
type Quota struct { type Quota struct {
SpaceLimit int64 `json:"spaceLimit,string"` SpaceLimit int `json:"spaceLimit"`
UploadSpeedLimit int `json:"uploadSpeedLimit"` UploadSpeedLimit int `json:"uploadSpeedLimit"`
DownloadSpeedLimit int `json:"downloadSpeedLimit"` DownloadSpeedLimit int `json:"downloadSpeedLimit"`
} }
type User struct { type User struct {
@ -44,8 +46,6 @@ type IUserStore interface {
DelUser(id uint64) error DelUser(id uint64) error
GetUser(id uint64) (*User, error) GetUser(id uint64) (*User, error)
GetUserByName(name string) (*User, error) GetUserByName(name string) (*User, error)
// SetName(id uint64, name string) error
// SetRole(id uint64, role string) error
SetInfo(id uint64, user *User) error SetInfo(id uint64, user *User) error
SetPwd(id uint64, pwd string) error SetPwd(id uint64, pwd string) error
ListUsers() ([]*User, error) ListUsers() ([]*User, error)
@ -66,8 +66,6 @@ func NewKVUserStore(store kvstore.IKVStore) (*KVUserStore, error) {
for _, nsName := range []string{ for _, nsName := range []string{
IDsNs, IDsNs,
UsersNs, UsersNs,
PwdsNs,
RolesNs,
InitNs, InitNs,
RoleListNs, RoleListNs,
} { } {
@ -90,6 +88,11 @@ func (us *KVUserStore) Init(rootName, rootPwd string) error {
Name: rootName, Name: rootName,
Pwd: rootPwd, Pwd: rootPwd,
Role: AdminRole, Role: AdminRole,
Quota: &Quota{
SpaceLimit: defaultSpaceLimit,
UploadSpeedLimit: defaultUploadSpeedLimit,
DownloadSpeedLimit: defaultDownloadSpeedLimit,
},
}) })
if err != nil { if err != nil {
return err return err

View file

@ -23,14 +23,23 @@ func TestUserStores(t *testing.T) {
t.Fatalf("passwords not match %s", err) t.Fatalf("passwords not match %s", err)
} }
if root.Role != AdminRole { if root.Role != AdminRole {
t.Fatalf("incorrect root fole") t.Fatalf("incorrect root role")
}
if root.Quota.SpaceLimit != defaultSpaceLimit {
t.Fatalf("incorrect root SpaceLimit")
}
if root.Quota.UploadSpeedLimit != defaultUploadSpeedLimit {
t.Fatalf("incorrect root UploadSpeedLimit")
}
if root.Quota.DownloadSpeedLimit != defaultDownloadSpeedLimit {
t.Fatalf("incorrect root DownloadSpeedLimit")
} }
id, name1 := uint64(1), "test_user1" id, name1 := uint64(1), "test_user1"
pwd1, pwd2 := "666", "888" pwd1, pwd2 := "666", "888"
role1, role2 := UserRole, AdminRole role1, role2 := UserRole, AdminRole
spaceLimit1, upLimit1, downLimit1 := int64(3), 5, 7 spaceLimit1, upLimit1, downLimit1 := 3, 5, 7
spaceLimit2, upLimit2, downLimit2 := int64(11), 13, 17 spaceLimit2, upLimit2, downLimit2 := 11, 13, 17
err = store.AddUser(&User{ err = store.AddUser(&User{
ID: id, ID: id,