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"`
|
||||
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
|
||||
|
|
|
@ -26,15 +26,22 @@ func TestUserStores(t *testing.T) {
|
|||
t.Fatalf("incorrect root fole")
|
||||
}
|
||||
|
||||
id, name1, name2 := uint64(1), "test_user1", "test_user2"
|
||||
id, name1 := uint64(1), "test_user1"
|
||||
pwd1, pwd2 := "666", "888"
|
||||
role1, role2 := UserRole, AdminRole
|
||||
spaceLimit1, upLimit1, downLimit1 := int64(3), 5, 7
|
||||
spaceLimit2, upLimit2, downLimit2 := int64(11), 13, 17
|
||||
|
||||
err = store.AddUser(&User{
|
||||
ID: id,
|
||||
Name: name1,
|
||||
Pwd: pwd1,
|
||||
Role: role1,
|
||||
Quota: &Quota{
|
||||
SpaceLimit: spaceLimit1,
|
||||
UploadSpeedLimit: upLimit1,
|
||||
DownloadSpeedLimit: downLimit1,
|
||||
},
|
||||
})
|
||||
|
||||
user, err := store.GetUser(id)
|
||||
|
@ -50,6 +57,15 @@ func TestUserStores(t *testing.T) {
|
|||
if user.Role != role1 {
|
||||
t.Fatalf("roles not matched %s %s", role1, user.Role)
|
||||
}
|
||||
if user.Quota.SpaceLimit != spaceLimit1 {
|
||||
t.Fatalf("space limit not matched %d %d", spaceLimit1, user.Quota.SpaceLimit)
|
||||
}
|
||||
if user.Quota.UploadSpeedLimit != upLimit1 {
|
||||
t.Fatalf("up limit not matched %d %d", upLimit1, user.Quota.UploadSpeedLimit)
|
||||
}
|
||||
if user.Quota.DownloadSpeedLimit != downLimit1 {
|
||||
t.Fatalf("down limit not matched %d %d", downLimit1, user.Quota.DownloadSpeedLimit)
|
||||
}
|
||||
|
||||
users, err := store.ListUsers()
|
||||
if err != nil {
|
||||
|
@ -65,34 +81,41 @@ func TestUserStores(t *testing.T) {
|
|||
t.Fatalf("incorrect user info %v", users[1])
|
||||
}
|
||||
|
||||
err = store.SetName(id, name2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = store.SetPwd(id, pwd2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = store.SetRole(id, role2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store.SetInfo(id, &User{
|
||||
ID: id,
|
||||
Role: role2,
|
||||
Quota: &Quota{
|
||||
SpaceLimit: spaceLimit2,
|
||||
UploadSpeedLimit: upLimit2,
|
||||
DownloadSpeedLimit: downLimit2,
|
||||
},
|
||||
})
|
||||
|
||||
user, err = store.GetUser(id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if user.Name != name2 {
|
||||
t.Fatalf("names not matched %s %s", name2, user.Name)
|
||||
}
|
||||
if user.Pwd != pwd2 {
|
||||
t.Fatalf("passwords not match %s", err)
|
||||
}
|
||||
if user.Role != role2 {
|
||||
t.Fatalf("roles not matched %s %s", role2, user.Role)
|
||||
}
|
||||
if user.Quota.SpaceLimit != spaceLimit2 {
|
||||
t.Fatalf("space limit not matched %d %d", spaceLimit2, user.Quota.SpaceLimit)
|
||||
}
|
||||
if user.Quota.UploadSpeedLimit != upLimit2 {
|
||||
t.Fatalf("up limit not matched %d %d", upLimit2, user.Quota.UploadSpeedLimit)
|
||||
}
|
||||
if user.Quota.DownloadSpeedLimit != downLimit2 {
|
||||
t.Fatalf("down limit not matched %d %d", downLimit2, user.Quota.DownloadSpeedLimit)
|
||||
}
|
||||
|
||||
user, err = store.GetUserByName(name2)
|
||||
user, err = store.GetUserByName(name1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -105,6 +128,15 @@ func TestUserStores(t *testing.T) {
|
|||
if user.Role != role2 {
|
||||
t.Fatalf("roles not matched %s %s", role2, user.Role)
|
||||
}
|
||||
if user.Quota.SpaceLimit != spaceLimit2 {
|
||||
t.Fatalf("space limit not matched %d %d", spaceLimit2, user.Quota.SpaceLimit)
|
||||
}
|
||||
if user.Quota.UploadSpeedLimit != upLimit2 {
|
||||
t.Fatalf("up limit not matched %d %d", upLimit2, user.Quota.UploadSpeedLimit)
|
||||
}
|
||||
if user.Quota.DownloadSpeedLimit != downLimit2 {
|
||||
t.Fatalf("down limit not matched %d %d", downLimit2, user.Quota.DownloadSpeedLimit)
|
||||
}
|
||||
|
||||
err = store.DelUser(id)
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue