feat(user_store): add quota to user info

This commit is contained in:
hexxa 2021-08-07 16:41:40 +08:00 committed by Hexxa
parent 1fcb2223a0
commit 75e10e6ad9
2 changed files with 171 additions and 120 deletions

View file

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

View file

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