From 7ca5f5753fa962a2eb39237dca13c02dbdde46d7 Mon Sep 17 00:00:00 2001 From: hexxa Date: Wed, 13 Oct 2021 18:49:32 +0800 Subject: [PATCH] feat(users): enable preference --- src/client/web/src/client/index.ts | 6 ++++ src/db/userstore/user_store.go | 45 +++++++++++++++++++++++++---- src/handlers/multiusers/handlers.go | 44 ++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 6 deletions(-) diff --git a/src/client/web/src/client/index.ts b/src/client/web/src/client/index.ts index fdd6b1b..50da248 100644 --- a/src/client/web/src/client/index.ts +++ b/src/client/web/src/client/index.ts @@ -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 { diff --git a/src/db/userstore/user_store.go b/src/db/userstore/user_store.go index 1fe8e25..91fc710 100644 --- a/src/db/userstore/user_store.go +++ b/src/db/userstore/user_store.go @@ -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"` @@ -50,12 +56,13 @@ type UserCfg struct { } type User struct { - ID uint64 `json:"id,string"` - Name string `json:"name"` - Pwd string `json:"pwd"` - Role string `json:"role"` - UsedSpace int64 `json:"usedSpace,string"` - Quota *Quota `json:"quota"` + ID uint64 `json:"id,string"` + Name string `json:"name"` + Pwd string `json:"pwd"` + 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() diff --git a/src/handlers/multiusers/handlers.go b/src/handlers/multiusers/handlers.go index 5b80ed2..e29d32b 100644 --- a/src/handlers/multiusers/handlers.go +++ b/src/handlers/multiusers/handlers.go @@ -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)) +}