示例#1
0
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
0
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
}