mod_rolepolicies.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package identity
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/errors"
  18. api "yunion.io/x/onecloud/pkg/apis/identity"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  21. "yunion.io/x/onecloud/pkg/mcclient/modules"
  22. )
  23. type SRolePolicyManager struct {
  24. modulebase.ResourceManager
  25. }
  26. var RolePolicies SRolePolicyManager
  27. func (manager *SRolePolicyManager) FetchMatchedPolicies(s *mcclient.ClientSession, roleIds []string, projectId string, loginIp string) (map[string][]string, error) {
  28. input := api.RolePolicyListInput{}
  29. input.RoleIds = roleIds
  30. input.ProjectId = projectId
  31. limit := 2048
  32. input.Limit = &limit
  33. details := true
  34. input.Details = &details
  35. results, err := manager.List(s, jsonutils.Marshal(input))
  36. if err != nil {
  37. return nil, errors.Wrap(err, "List RolePolicyManager")
  38. }
  39. ret := make(map[string][]string)
  40. for i := range results.Data {
  41. policy, _ := results.Data[i].GetString("policy")
  42. scope, _ := results.Data[i].GetString("scope")
  43. if policies, ok := ret[scope]; !ok {
  44. ret[scope] = []string{policy}
  45. } else {
  46. ret[scope] = append(policies, policy)
  47. }
  48. }
  49. return ret, nil
  50. }
  51. func init() {
  52. RolePolicies = SRolePolicyManager{modules.NewIdentityV3Manager(
  53. "rolepolicy",
  54. "rolepolicies",
  55. []string{"id", "name", "role", "role_id", "project", "project_id", "policy", "policy_id", "ips", "scope"},
  56. []string{},
  57. )}
  58. modules.Register(&RolePolicies)
  59. }