feat(users): enable preference
This commit is contained in:
parent
087eca407d
commit
7ca5f5753f
3 changed files with 89 additions and 6 deletions
|
@ -14,6 +14,11 @@ export interface Quota {
|
||||||
downloadSpeedLimit: number;
|
downloadSpeedLimit: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Settings {
|
||||||
|
bgURL: string;
|
||||||
|
cssURL: string;
|
||||||
|
lanPackURL: string;
|
||||||
|
}
|
||||||
export interface User {
|
export interface User {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
|
@ -21,6 +26,7 @@ export interface User {
|
||||||
role: string;
|
role: string;
|
||||||
quota: Quota;
|
quota: Quota;
|
||||||
usedSpace: string;
|
usedSpace: string;
|
||||||
|
settings: Settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ListUsersResp {
|
export interface ListUsersResp {
|
||||||
|
|
|
@ -43,6 +43,12 @@ type Quota struct {
|
||||||
DownloadSpeedLimit int `json:"downloadSpeedLimit"`
|
DownloadSpeedLimit int `json:"downloadSpeedLimit"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Preferences struct {
|
||||||
|
BgURL string `json:"bgURL"`
|
||||||
|
CSSURL string `json:"cssURL"`
|
||||||
|
LanPackURL string `json:"lanPackURL"`
|
||||||
|
}
|
||||||
|
|
||||||
type UserCfg struct {
|
type UserCfg struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Role string `json:"role"`
|
Role string `json:"role"`
|
||||||
|
@ -50,12 +56,13 @@ type UserCfg struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID uint64 `json:"id,string"`
|
ID uint64 `json:"id,string"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Pwd string `json:"pwd"`
|
Pwd string `json:"pwd"`
|
||||||
Role string `json:"role"`
|
Role string `json:"role"`
|
||||||
UsedSpace int64 `json:"usedSpace,string"`
|
UsedSpace int64 `json:"usedSpace,string"`
|
||||||
Quota *Quota `json:"quota"`
|
Quota *Quota `json:"quota"`
|
||||||
|
Preferences *Preferences `json:"preferences"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type IUserStore interface {
|
type IUserStore interface {
|
||||||
|
@ -69,6 +76,7 @@ type IUserStore interface {
|
||||||
CanIncrUsed(id uint64, capacity int64) (bool, error)
|
CanIncrUsed(id uint64, capacity int64) (bool, error)
|
||||||
SetUsed(id uint64, incr bool, capacity int64) error
|
SetUsed(id uint64, incr bool, capacity int64) error
|
||||||
SetPwd(id uint64, pwd string) error
|
SetPwd(id uint64, pwd string) error
|
||||||
|
SetPreferences(id uint64, settings *Preferences) error
|
||||||
ListUsers() ([]*User, error)
|
ListUsers() ([]*User, error)
|
||||||
AddRole(role string) error
|
AddRole(role string) error
|
||||||
DelRole(role string) error
|
DelRole(role string) error
|
||||||
|
@ -276,6 +284,31 @@ func (us *KVUserStore) SetPwd(id uint64, pwd string) error {
|
||||||
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (us *KVUserStore) SetPreferences(id uint64, prefers *Preferences) error {
|
||||||
|
us.mtx.Lock()
|
||||||
|
defer us.mtx.Unlock()
|
||||||
|
|
||||||
|
userID := fmt.Sprint(id)
|
||||||
|
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("user (%d) does not exist", id)
|
||||||
|
}
|
||||||
|
gotUser := &User{}
|
||||||
|
err := json.Unmarshal([]byte(infoStr), gotUser)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else if gotUser.ID != id {
|
||||||
|
return fmt.Errorf("user id key(%d) info(%d) does match", id, gotUser.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
gotUser.Preferences = prefers
|
||||||
|
infoBytes, err := json.Marshal(gotUser)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
||||||
|
}
|
||||||
|
|
||||||
func (us *KVUserStore) CanIncrUsed(id uint64, capacity int64) (bool, error) {
|
func (us *KVUserStore) CanIncrUsed(id uint64, capacity int64) (bool, error) {
|
||||||
us.mtx.Lock()
|
us.mtx.Lock()
|
||||||
defer us.mtx.Unlock()
|
defer us.mtx.Unlock()
|
||||||
|
|
|
@ -296,6 +296,7 @@ func (h *MultiUsersSvc) SetPwd(c *gin.Context) {
|
||||||
c.JSON(q.ErrResp(c, 500, err))
|
c.JSON(q.ErrResp(c, 500, err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := h.deps.Users().GetUser(uid)
|
user, err := h.deps.Users().GetUser(uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(q.ErrResp(c, 402, err))
|
c.JSON(q.ErrResp(c, 402, err))
|
||||||
|
@ -673,3 +674,46 @@ func (h *MultiUsersSvc) SetUser(c *gin.Context) {
|
||||||
|
|
||||||
c.JSON(q.Resp(200))
|
c.JSON(q.Resp(200))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SetPreferencesReq struct {
|
||||||
|
Preferences *userstore.Preferences `json:"preferences"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *MultiUsersSvc) SetPreferences(c *gin.Context) {
|
||||||
|
req := &SetPreferencesReq{}
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(q.ErrResp(c, 400, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
claims, err := h.getUserInfo(c)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(q.ErrResp(c, 401, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if claims[q.RoleParam] == userstore.VisitorRole {
|
||||||
|
c.JSON(q.ErrResp(c, 403, errors.New("operation denied")))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// userstore.setPreferences
|
||||||
|
|
||||||
|
uidStr, ok := claims[q.UserIDParam]
|
||||||
|
if !ok {
|
||||||
|
c.JSON(q.ErrResp(c, 500, errors.New("user id not found")))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
uid, err := strconv.ParseUint(uidStr, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(q.ErrResp(c, 500, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: validate
|
||||||
|
err = h.deps.Users().SetPreferences(uid, req.Preferences)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(q.ErrResp(c, 500, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(q.Resp(200))
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue