mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #13582 from ioito/hotfix/qx-project-role-metadata
fix(keystone, apigateway): project role with metadata
This commit is contained in:
@@ -26,6 +26,11 @@ type SDomainObject struct {
|
||||
Domain SIdentityObject `json:"domain"`
|
||||
}
|
||||
|
||||
type SDomainObjectWithMetadata struct {
|
||||
SDomainObject
|
||||
Metadata map[string]string `json:"metadata"`
|
||||
}
|
||||
|
||||
type SFetchDomainObject struct {
|
||||
SIdentityObject
|
||||
Domain string `json:"domain"`
|
||||
@@ -34,8 +39,8 @@ type SFetchDomainObject struct {
|
||||
|
||||
type SRoleAssignment struct {
|
||||
Scope struct {
|
||||
Domain SIdentityObject `json:"domain"`
|
||||
Project SDomainObject `json:"project"`
|
||||
Domain SIdentityObject `json:"domain"`
|
||||
Project SDomainObjectWithMetadata `json:"project"`
|
||||
} `json:"scope"`
|
||||
User SDomainObject `json:"user"`
|
||||
Group SDomainObject `json:"group"`
|
||||
|
||||
@@ -611,7 +611,7 @@ type sAssignmentInternal struct {
|
||||
RoleId string `json:"role_id"`
|
||||
}
|
||||
|
||||
func (assign *sAssignmentInternal) getRoleAssignment(domains, projects, groups, users, roles map[string]api.SFetchDomainObject, fetchPolicies bool) api.SRoleAssignment {
|
||||
func (assign *sAssignmentInternal) getRoleAssignment(domains, projects, groups, users, roles map[string]api.SFetchDomainObject, fetchPolicies bool, projectMetadata map[string]map[string]string) api.SRoleAssignment {
|
||||
ra := api.SRoleAssignment{}
|
||||
ra.Role.Id = assign.RoleId
|
||||
ra.Role.Name = roles[assign.RoleId].Name
|
||||
@@ -632,6 +632,7 @@ func (assign *sAssignmentInternal) getRoleAssignment(domains, projects, groups,
|
||||
if len(assign.ProjectId) > 0 {
|
||||
ra.Scope.Project.Id = assign.ProjectId
|
||||
ra.Scope.Project.Name = projects[assign.ProjectId].Name
|
||||
ra.Scope.Project.Metadata, _ = projectMetadata[assign.ProjectId]
|
||||
ra.Scope.Project.Domain.Id = projects[assign.ProjectId].DomainId
|
||||
ra.Scope.Project.Domain.Name = projects[assign.ProjectId].Domain
|
||||
if fetchPolicies {
|
||||
@@ -739,6 +740,7 @@ func (manager *SAssignmentManager) FetchAll(
|
||||
if err != nil {
|
||||
return nil, -1, errors.Wrap(err, "fetchObjects ProjectManager")
|
||||
}
|
||||
projectMetadatas := fetchProjectMetadatas(projectIds)
|
||||
groups, err := fetchObjects(GroupManager, groupIds)
|
||||
if err != nil {
|
||||
return nil, -1, errors.Wrap(err, "fetchObjects GroupManager")
|
||||
@@ -754,11 +756,32 @@ func (manager *SAssignmentManager) FetchAll(
|
||||
|
||||
results := make([]api.SRoleAssignment, len(assigns))
|
||||
for i := range assigns {
|
||||
results[i] = assigns[i].getRoleAssignment(domains, projects, groups, users, roles, includePolicies)
|
||||
results[i] = assigns[i].getRoleAssignment(domains, projects, groups, users, roles, includePolicies, projectMetadatas)
|
||||
}
|
||||
return results, int64(total), nil
|
||||
}
|
||||
|
||||
func fetchProjectMetadatas(idList []string) map[string]map[string]string {
|
||||
ret := map[string]map[string]string{}
|
||||
if len(idList) == 0 {
|
||||
return ret
|
||||
}
|
||||
q := db.Metadata.Query().Equals("obj_type", "project").In("obj_id", idList)
|
||||
result := []db.SMetadata{}
|
||||
err := q.All(&result)
|
||||
if err != nil {
|
||||
return ret
|
||||
}
|
||||
for i := range result {
|
||||
_, ok := ret[result[i].ObjId]
|
||||
if !ok {
|
||||
ret[result[i].ObjId] = map[string]string{}
|
||||
}
|
||||
ret[result[i].ObjId][result[i].Key] = result[i].Value
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func fetchObjects(manager db.IModelManager, idList []string) (map[string]api.SFetchDomainObject, error) {
|
||||
results := make(map[string]api.SFetchDomainObject)
|
||||
if len(idList) == 0 {
|
||||
|
||||
@@ -116,9 +116,10 @@ type sGroupRole struct {
|
||||
}
|
||||
|
||||
type sProjectGroupRole struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Domain struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Metadata map[string]string `json:"metadata"`
|
||||
Domain struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
} `json:"domain"`
|
||||
@@ -345,9 +346,11 @@ func (this *RoleAssignmentManagerV3) GetProjectRole(s *mcclient.ClientSession, i
|
||||
|
||||
var groupById, groupByName, groupByDomainId, groupByDomainName string
|
||||
|
||||
metadatas := map[string]string{}
|
||||
if groupBy == "project" {
|
||||
groupById, _ = roleAssign.GetString("scope", "project", "id")
|
||||
groupByName, _ = roleAssign.GetString("scope", "project", "name")
|
||||
roleAssign.Unmarshal(&metadatas, "scope", "project", "metadata")
|
||||
groupByDomainId, _ = roleAssign.GetString("scope", "project", "domain", "id")
|
||||
groupByDomainName, _ = roleAssign.GetString("scope", "project", "domain", "name")
|
||||
} else if groupBy == "user" {
|
||||
@@ -376,8 +379,9 @@ func (this *RoleAssignmentManagerV3) GetProjectRole(s *mcclient.ClientSession, i
|
||||
if lineIdx < 0 {
|
||||
lineIdx = len(lines)
|
||||
pgr := sProjectGroupRole{
|
||||
Id: groupById,
|
||||
Name: groupByName,
|
||||
Id: groupById,
|
||||
Name: groupByName,
|
||||
Metadata: metadatas,
|
||||
}
|
||||
pgr.Domain.Id = groupByDomainId
|
||||
pgr.Domain.Name = groupByDomainName
|
||||
|
||||
Reference in New Issue
Block a user