feat(user_store): add quota to user info
This commit is contained in:
parent
1fcb2223a0
commit
75e10e6ad9
2 changed files with 171 additions and 120 deletions
|
@ -1,14 +1,12 @@
|
|||
package userstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
// "golang.org/x/crypto/bcrypt"
|
||||
|
||||
"github.com/ihexxa/quickshare/src/kvstore"
|
||||
)
|
||||
|
||||
|
@ -18,18 +16,25 @@ const (
|
|||
VisitorRole = "visitor"
|
||||
InitNs = "usersInit"
|
||||
IDsNs = "ids"
|
||||
NamesNs = "users"
|
||||
UsersNs = "users"
|
||||
PwdsNs = "pwds"
|
||||
RolesNs = "roles"
|
||||
RoleListNs = "roleList"
|
||||
InitTimeKey = "initTime"
|
||||
)
|
||||
|
||||
type Quota struct {
|
||||
SpaceLimit int64 `json:"spaceLimit,string"`
|
||||
UploadSpeedLimit int `json:"uploadSpeedLimit"`
|
||||
DownloadSpeedLimit int `json:"downloadSpeedLimit"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID uint64 `json:"id,string"`
|
||||
Name string `json:"name"`
|
||||
Pwd string `json:"pwd"`
|
||||
Role string `json:"role"`
|
||||
ID uint64 `json:"id,string"`
|
||||
Name string `json:"name"`
|
||||
Pwd string `json:"pwd"`
|
||||
Role string `json:"role"`
|
||||
Quota *Quota `json:"quota"`
|
||||
}
|
||||
|
||||
type IUserStore interface {
|
||||
|
@ -39,10 +44,11 @@ type IUserStore interface {
|
|||
DelUser(id uint64) error
|
||||
GetUser(id uint64) (*User, error)
|
||||
GetUserByName(name string) (*User, error)
|
||||
SetName(id uint64, name string) error
|
||||
// SetName(id uint64, name string) error
|
||||
// SetRole(id uint64, role string) error
|
||||
SetInfo(id uint64, user *User) error
|
||||
SetPwd(id uint64, pwd string) error
|
||||
ListUsers() ([]*User, error)
|
||||
SetRole(id uint64, role string) error
|
||||
AddRole(role string) error
|
||||
DelRole(role string) error
|
||||
ListRoles() (map[string]bool, error)
|
||||
|
@ -59,7 +65,7 @@ func NewKVUserStore(store kvstore.IKVStore) (*KVUserStore, error) {
|
|||
var err error
|
||||
for _, nsName := range []string{
|
||||
IDsNs,
|
||||
NamesNs,
|
||||
UsersNs,
|
||||
PwdsNs,
|
||||
RolesNs,
|
||||
InitNs,
|
||||
|
@ -109,7 +115,7 @@ func (us *KVUserStore) AddUser(user *User) error {
|
|||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(user.ID)
|
||||
_, ok := us.store.GetStringIn(NamesNs, userID)
|
||||
_, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if ok {
|
||||
return fmt.Errorf("userID (%d) exists", user.ID)
|
||||
}
|
||||
|
@ -121,20 +127,15 @@ func (us *KVUserStore) AddUser(user *User) error {
|
|||
return fmt.Errorf("user name (%s) exists", user.Name)
|
||||
}
|
||||
|
||||
var err error
|
||||
err = us.store.SetStringIn(IDsNs, user.Name, userID)
|
||||
err := us.store.SetStringIn(IDsNs, user.Name, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = us.store.SetStringIn(NamesNs, userID, user.Name)
|
||||
userBytes, err := json.Marshal(user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = us.store.SetStringIn(PwdsNs, userID, user.Pwd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return us.store.SetStringIn(RolesNs, userID, user.Role)
|
||||
return us.store.SetStringIn(UsersNs, userID, string(userBytes))
|
||||
}
|
||||
|
||||
func (us *KVUserStore) DelUser(id uint64) error {
|
||||
|
@ -142,18 +143,16 @@ func (us *KVUserStore) DelUser(id uint64) error {
|
|||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
name, ok := us.store.GetStringIn(NamesNs, userID)
|
||||
name, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return fmt.Errorf("userID (%s) exists", userID)
|
||||
return fmt.Errorf("userID (%s) does not exist", userID)
|
||||
}
|
||||
|
||||
// TODO: add complement operations if part of the actions fails
|
||||
err1 := us.store.DelStringIn(NamesNs, userID)
|
||||
err2 := us.store.DelStringIn(IDsNs, name)
|
||||
err3 := us.store.DelStringIn(PwdsNs, userID)
|
||||
err4 := us.store.DelStringIn(RolesNs, userID)
|
||||
if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
|
||||
return fmt.Errorf("get name(%s) id(%s) pwd(%s) role(%s)", err1, err2, err3, err4)
|
||||
err1 := us.store.DelStringIn(IDsNs, name)
|
||||
err2 := us.store.DelStringIn(UsersNs, userID)
|
||||
if err1 != nil || err2 != nil {
|
||||
return fmt.Errorf("get id(%s) user(%s)", err1, err2)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -164,32 +163,25 @@ func (us *KVUserStore) GetUser(id uint64) (*User, error) {
|
|||
|
||||
userID := fmt.Sprint(id)
|
||||
|
||||
name, ok := us.store.GetStringIn(NamesNs, userID)
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("name (%s) not found", userID)
|
||||
return nil, fmt.Errorf("user (%s) not found", userID)
|
||||
}
|
||||
gotID, ok := us.store.GetStringIn(IDsNs, name)
|
||||
user := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gotID, ok := us.store.GetStringIn(IDsNs, user.Name)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("user id (%s) not found", name)
|
||||
return nil, fmt.Errorf("user id (%s) not found", user.Name)
|
||||
} else if gotID != userID {
|
||||
return nil, fmt.Errorf("user id (%s) not match: got(%s) expected(%s)", name, gotID, userID)
|
||||
}
|
||||
pwd, ok := us.store.GetStringIn(PwdsNs, userID)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("pwd (%s) not found", userID)
|
||||
}
|
||||
role, ok := us.store.GetStringIn(RolesNs, userID)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("role (%s) not found", userID)
|
||||
return nil, fmt.Errorf("user id (%s) not match: got(%s) expected(%s)", user.Name, gotID, userID)
|
||||
}
|
||||
|
||||
// TODO: use sync.Pool instead
|
||||
return &User{
|
||||
ID: id,
|
||||
Name: name,
|
||||
Pwd: pwd,
|
||||
Role: role,
|
||||
}, nil
|
||||
return user, nil
|
||||
|
||||
}
|
||||
|
||||
|
@ -199,114 +191,141 @@ func (us *KVUserStore) GetUserByName(name string) (*User, error) {
|
|||
|
||||
userID, ok := us.store.GetStringIn(IDsNs, name)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("user (%s) not found", name)
|
||||
return nil, fmt.Errorf("user id (%s) not found", name)
|
||||
}
|
||||
gotName, ok := us.store.GetStringIn(NamesNs, userID)
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("user name (%s) not found", userID)
|
||||
} else if gotName != name {
|
||||
return nil, fmt.Errorf("user id (%s) not match: got(%s) expected(%s)", name, gotName, name)
|
||||
}
|
||||
pwd, ok := us.store.GetStringIn(PwdsNs, userID)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("pwd (%s) not found", userID)
|
||||
}
|
||||
role, ok := us.store.GetStringIn(RolesNs, userID)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("role (%s) not found", userID)
|
||||
}
|
||||
|
||||
uid, err := strconv.ParseUint(userID, 10, 64)
|
||||
user := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if user.Name != name {
|
||||
return nil, fmt.Errorf("user id (%s) not match: got(%s) expected(%s)", userID, user.Name, name)
|
||||
}
|
||||
|
||||
// TODO: use sync.Pool instead
|
||||
return &User{
|
||||
ID: uid,
|
||||
Name: name,
|
||||
Pwd: pwd,
|
||||
Role: role,
|
||||
}, nil
|
||||
return user, nil
|
||||
|
||||
}
|
||||
|
||||
func (us *KVUserStore) SetName(id uint64, name string) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
// func (us *KVUserStore) SetName(id uint64, name string) error {
|
||||
// us.mtx.Lock()
|
||||
// defer us.mtx.Unlock()
|
||||
|
||||
_, ok := us.store.GetStringIn(IDsNs, name)
|
||||
if ok {
|
||||
return fmt.Errorf("user name (%s) exists", name)
|
||||
}
|
||||
// _, ok := us.store.GetStringIn(IDsNs, name)
|
||||
// if ok {
|
||||
// return fmt.Errorf("user name (%s) exists", name)
|
||||
// }
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
_, ok = us.store.GetStringIn(NamesNs, userID)
|
||||
if !ok {
|
||||
return fmt.Errorf("Name (%d) does not exist", id)
|
||||
}
|
||||
if name == "" {
|
||||
return fmt.Errorf("Name can not be empty")
|
||||
}
|
||||
// userID := fmt.Sprint(id)
|
||||
// _, ok = us.store.GetStringIn(UsersNs, userID)
|
||||
// if !ok {
|
||||
// return fmt.Errorf("Name (%d) does not exist", id)
|
||||
// }
|
||||
// if name == "" {
|
||||
// return fmt.Errorf("Name can not be empty")
|
||||
// }
|
||||
|
||||
err := us.store.SetStringIn(IDsNs, name, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return us.store.SetStringIn(NamesNs, userID, name)
|
||||
}
|
||||
// err := us.store.SetStringIn(IDsNs, name, userID)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return us.store.SetStringIn(UsersNs, userID, name)
|
||||
// }
|
||||
|
||||
func (us *KVUserStore) SetPwd(id uint64, pwd string) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
_, ok := us.store.GetStringIn(PwdsNs, userID)
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return fmt.Errorf("Pwd (%d) does not exist", id)
|
||||
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)
|
||||
}
|
||||
|
||||
return us.store.SetStringIn(PwdsNs, userID, pwd)
|
||||
gotUser.Pwd = pwd
|
||||
infoBytes, err := json.Marshal(gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
||||
}
|
||||
|
||||
func (us *KVUserStore) SetRole(id uint64, role string) error {
|
||||
func (us *KVUserStore) SetInfo(id uint64, user *User) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
_, ok := us.store.GetStringIn(RolesNs, userID)
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return fmt.Errorf("Role (%d) does not exist", id)
|
||||
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)
|
||||
}
|
||||
|
||||
return us.store.SetStringIn(RolesNs, userID, role)
|
||||
// name and password can not be updated here
|
||||
if user.Role != "" {
|
||||
gotUser.Role = user.Role
|
||||
}
|
||||
if user.Quota != nil {
|
||||
gotUser.Quota = user.Quota
|
||||
}
|
||||
|
||||
infoBytes, err := json.Marshal(gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
||||
}
|
||||
|
||||
// func (us *KVUserStore) SetRole(id uint64, role string) error {
|
||||
// us.mtx.Lock()
|
||||
// defer us.mtx.Unlock()
|
||||
|
||||
// userID := fmt.Sprint(id)
|
||||
// _, ok := us.store.GetStringIn(RolesNs, userID)
|
||||
// if !ok {
|
||||
// return fmt.Errorf("Role (%d) does not exist", id)
|
||||
// }
|
||||
|
||||
// return us.store.SetStringIn(RolesNs, userID, role)
|
||||
// }
|
||||
|
||||
func (us *KVUserStore) ListUsers() ([]*User, error) {
|
||||
us.mtx.RLock()
|
||||
defer us.mtx.RUnlock()
|
||||
|
||||
idToName, err := us.store.ListStringsIn(NamesNs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
roles, err := us.store.ListStringsIn(RolesNs)
|
||||
idToInfo, err := us.store.ListStringsIn(UsersNs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
users := []*User{}
|
||||
for id, name := range idToName {
|
||||
intID, err := strconv.ParseUint(id, 10, 64)
|
||||
for _, infoStr := range idToInfo {
|
||||
user := &User{}
|
||||
err = json.Unmarshal([]byte(infoStr), user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user.Pwd = ""
|
||||
|
||||
users = append(users, &User{
|
||||
ID: intID,
|
||||
Name: name,
|
||||
Role: roles[id],
|
||||
})
|
||||
users = append(users, user)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue