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;
|
||||
}
|
||||
|
||||
export interface Settings {
|
||||
bgURL: string;
|
||||
cssURL: string;
|
||||
lanPackURL: string;
|
||||
}
|
||||
export interface User {
|
||||
id: string;
|
||||
name: string;
|
||||
|
@ -21,6 +26,7 @@ export interface User {
|
|||
role: string;
|
||||
quota: Quota;
|
||||
usedSpace: string;
|
||||
settings: Settings;
|
||||
}
|
||||
|
||||
export interface ListUsersResp {
|
||||
|
|
|
@ -43,6 +43,12 @@ type Quota struct {
|
|||
DownloadSpeedLimit int `json:"downloadSpeedLimit"`
|
||||
}
|
||||
|
||||
type Preferences struct {
|
||||
BgURL string `json:"bgURL"`
|
||||
CSSURL string `json:"cssURL"`
|
||||
LanPackURL string `json:"lanPackURL"`
|
||||
}
|
||||
|
||||
type UserCfg struct {
|
||||
Name string `json:"name"`
|
||||
Role string `json:"role"`
|
||||
|
@ -56,6 +62,7 @@ type User struct {
|
|||
Role string `json:"role"`
|
||||
UsedSpace int64 `json:"usedSpace,string"`
|
||||
Quota *Quota `json:"quota"`
|
||||
Preferences *Preferences `json:"preferences"`
|
||||
}
|
||||
|
||||
type IUserStore interface {
|
||||
|
@ -69,6 +76,7 @@ type IUserStore interface {
|
|||
CanIncrUsed(id uint64, capacity int64) (bool, error)
|
||||
SetUsed(id uint64, incr bool, capacity int64) error
|
||||
SetPwd(id uint64, pwd string) error
|
||||
SetPreferences(id uint64, settings *Preferences) error
|
||||
ListUsers() ([]*User, error)
|
||||
AddRole(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))
|
||||
}
|
||||
|
||||
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) {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
|
|
@ -296,6 +296,7 @@ func (h *MultiUsersSvc) SetPwd(c *gin.Context) {
|
|||
c.JSON(q.ErrResp(c, 500, err))
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.deps.Users().GetUser(uid)
|
||||
if err != nil {
|
||||
c.JSON(q.ErrResp(c, 402, err))
|
||||
|
@ -673,3 +674,46 @@ func (h *MultiUsersSvc) SetUser(c *gin.Context) {
|
|||
|
||||
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