本文整理匯總了Golang中github.com/cloudawan/cloudone_utility/rbac.User.RoleSlice方法的典型用法代碼示例。如果您正苦於以下問題:Golang User.RoleSlice方法的具體用法?Golang User.RoleSlice怎麽用?Golang User.RoleSlice使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/cloudawan/cloudone_utility/rbac.User
的用法示例。
在下文中一共展示了User.RoleSlice方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: LoadAllUser
func (storageEtcd *StorageEtcd) LoadAllUser() ([]rbac.User, error) {
keysAPI, err := etcd.EtcdClient.GetKeysAPI()
if err != nil {
log.Error("Get keysAPI error %s", err)
return nil, err
}
response, err := keysAPI.Get(context.Background(), etcd.EtcdClient.EtcdBasePath+"/user", nil)
if err != nil {
log.Error("Load all user error: %s", err)
log.Error(response)
return nil, err
}
userSlice := make([]rbac.User, 0)
for _, node := range response.Node.Nodes {
user := rbac.User{}
err := json.Unmarshal([]byte(node.Value), &user)
if err != nil {
log.Error("Unmarshal user %v error %s", node.Value, err)
return nil, err
}
// User doesn't have role data but only role name so load role
roleSlice := make([]*rbac.Role, 0)
for _, savedRole := range user.RoleSlice {
role, err := storageEtcd.LoadRole(savedRole.Name)
if err != nil {
log.Error("Fail to load role with name %s error %s", savedRole.Name, err)
} else {
roleSlice = append(roleSlice, role)
}
}
user.RoleSlice = roleSlice
userSlice = append(userSlice, user)
}
return userSlice, nil
}
示例2: LoadUser
func (storageEtcd *StorageEtcd) LoadUser(name string) (*rbac.User, error) {
keysAPI, err := etcd.EtcdClient.GetKeysAPI()
if err != nil {
log.Error("Get keysAPI error %s", err)
return nil, err
}
response, err := keysAPI.Get(context.Background(), etcd.EtcdClient.EtcdBasePath+"/user/"+name, nil)
etcdError, _ := err.(client.Error)
if etcdError.Code == client.ErrorCodeKeyNotFound {
return nil, etcdError
}
if err != nil {
log.Error("Load name with name %s error: %s", name, err)
log.Error(response)
return nil, err
}
user := new(rbac.User)
err = json.Unmarshal([]byte(response.Node.Value), &user)
if err != nil {
log.Error("Unmarshal user %v error %s", response.Node.Value, err)
return nil, err
}
// User doesn't have role data but only role name so load role
roleSlice := make([]*rbac.Role, 0)
for _, savedRole := range user.RoleSlice {
role, err := storageEtcd.LoadRole(savedRole.Name)
if err != nil {
log.Error("Fail to load role with name %s error %s", savedRole.Name, err)
} else {
roleSlice = append(roleSlice, role)
}
}
user.RoleSlice = roleSlice
return user, nil
}