當前位置: 首頁>>代碼示例>>Golang>>正文


Golang User.RoleSlice方法代碼示例

本文整理匯總了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
}
開發者ID:cloudawan,項目名稱:cloudone,代碼行數:40,代碼來源:storage_etcd.go

示例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
}
開發者ID:cloudawan,項目名稱:cloudone,代碼行數:39,代碼來源:storage_etcd.go


注:本文中的github.com/cloudawan/cloudone_utility/rbac.User.RoleSlice方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。