feat(users): add roles APIs (#63)
* feat(kvstore): add namespace operations for bool * feat(userstore): add methods for roles * chore(multiusers): remove useless todo * feat(multiusers): add apis for roles * test(roles): add e2e tests for role APIs * test(e2e/files): enable files tests
This commit is contained in:
parent
4b6f6d9e1f
commit
9748d0cab4
10 changed files with 316 additions and 11 deletions
|
@ -21,6 +21,7 @@ const (
|
|||
NamesNs = "users"
|
||||
PwdsNs = "pwds"
|
||||
RolesNs = "roles"
|
||||
RoleListNs = "roleList"
|
||||
InitTimeKey = "initTime"
|
||||
)
|
||||
|
||||
|
@ -40,6 +41,9 @@ type IUserStore interface {
|
|||
SetName(id uint64, name string) error
|
||||
SetPwd(id uint64, pwd string) error
|
||||
SetRole(id uint64, role string) error
|
||||
AddRole(role string) error
|
||||
DelRole(role string) error
|
||||
ListRoles() (map[string]bool, error)
|
||||
}
|
||||
|
||||
type KVUserStore struct {
|
||||
|
@ -57,6 +61,7 @@ func NewKVUserStore(store kvstore.IKVStore) (*KVUserStore, error) {
|
|||
PwdsNs,
|
||||
RolesNs,
|
||||
InitNs,
|
||||
RoleListNs,
|
||||
} {
|
||||
if err = store.AddNamespace(nsName); err != nil {
|
||||
return nil, err
|
||||
|
@ -71,7 +76,8 @@ func NewKVUserStore(store kvstore.IKVStore) (*KVUserStore, error) {
|
|||
}
|
||||
|
||||
func (us *KVUserStore) Init(rootName, rootPwd string) error {
|
||||
err := us.AddUser(&User{
|
||||
var err error
|
||||
err = us.AddUser(&User{
|
||||
ID: 0,
|
||||
Name: rootName,
|
||||
Pwd: rootPwd,
|
||||
|
@ -81,6 +87,13 @@ func (us *KVUserStore) Init(rootName, rootPwd string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
for _, role := range []string{AdminRole, UserRole, VisitorRole} {
|
||||
err = us.AddRole(role)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return us.store.SetStringIn(InitNs, InitTimeKey, fmt.Sprintf("%d", time.Now().Unix()))
|
||||
}
|
||||
|
||||
|
@ -244,3 +257,33 @@ func (us *KVUserStore) SetRole(id uint64, role string) error {
|
|||
|
||||
return us.store.SetStringIn(RolesNs, userID, role)
|
||||
}
|
||||
|
||||
func (us *KVUserStore) AddRole(role string) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
_, ok := us.store.GetBoolIn(RoleListNs, role)
|
||||
if ok {
|
||||
return fmt.Errorf("role (%s) exists", role)
|
||||
}
|
||||
|
||||
return us.store.SetBoolIn(RoleListNs, role, true)
|
||||
}
|
||||
|
||||
func (us *KVUserStore) DelRole(role string) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
if role == AdminRole || role == UserRole || role == VisitorRole {
|
||||
return errors.New("predefined roles can not be deleted")
|
||||
}
|
||||
|
||||
return us.store.DelBoolIn(RoleListNs, role)
|
||||
}
|
||||
|
||||
func (us *KVUserStore) ListRoles() (map[string]bool, error) {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
return us.store.ListBoolsIn(RoleListNs)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue