expandidps.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 models
  15. import (
  16. "database/sql"
  17. "yunion.io/x/log"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/sqlchemy"
  20. api "yunion.io/x/onecloud/pkg/apis/identity"
  21. "yunion.io/x/onecloud/pkg/util/stringutils2"
  22. )
  23. func expandIdpAttributes(entType string, idList []string, fields stringutils2.SSortedStrings) []api.IdpResourceInfo {
  24. rows := make([]api.IdpResourceInfo, len(idList))
  25. if len(fields) == 0 || fields.Contains("idp_id") || fields.Contains("idp") || fields.Contains("idp_entity_id") || fields.Contains("idp_driver") {
  26. idps, err := fetchIdmappings(idList, entType)
  27. if err == nil && idps != nil {
  28. for i := range idList {
  29. if idp, ok := idps[idList[i]]; ok {
  30. if len(fields) == 0 || fields.Contains("idp_id") {
  31. rows[i].IdpId = idp[0].IdpId
  32. }
  33. if len(fields) == 0 || fields.Contains("idp") {
  34. rows[i].Idp = idp[0].Idp
  35. }
  36. if len(fields) == 0 || fields.Contains("idp_entity_id") {
  37. rows[i].IdpEntityId = idp[0].IdpEntityId
  38. }
  39. if len(fields) == 0 || fields.Contains("idp_driver") {
  40. rows[i].IdpDriver = idp[0].IdpDriver
  41. }
  42. if len(fields) == 0 || fields.Contains("template") {
  43. rows[i].Template = idp[0].Template
  44. }
  45. }
  46. }
  47. } else if err != nil {
  48. log.Warningf("fetchIdmappings error %s", err)
  49. }
  50. }
  51. return rows
  52. }
  53. type sIdpInfo struct {
  54. api.IdpResourceInfo
  55. PublicId string
  56. }
  57. func fetchIdmappings(idList []string, resType string) (map[string][]sIdpInfo, error) {
  58. idmappings := IdmappingManager.Query().SubQuery()
  59. idps := IdentityProviderManager.Query().SubQuery()
  60. q := idmappings.Query(idmappings.Field("domain_id", "idp_id"),
  61. idmappings.Field("local_id", "idp_entity_id"),
  62. idps.Field("name", "idp"),
  63. idps.Field("driver", "idp_driver"),
  64. idps.Field("template", "template"),
  65. idps.Field("is_sso", "is_sso"),
  66. idmappings.Field("public_id"),
  67. )
  68. q = q.Join(idps, sqlchemy.Equals(idps.Field("id"), idmappings.Field("domain_id")))
  69. q = q.Filter(sqlchemy.In(idmappings.Field("public_id"), idList))
  70. q = q.Filter(sqlchemy.Equals(idmappings.Field("entity_type"), resType))
  71. idpInfos := make([]sIdpInfo, 0)
  72. err := q.All(&idpInfos)
  73. if err != nil && err != sql.ErrNoRows {
  74. return nil, errors.Wrap(err, "query")
  75. }
  76. ret := make(map[string][]sIdpInfo)
  77. for i := range idpInfos {
  78. if idpList, ok := ret[idpInfos[i].PublicId]; ok {
  79. ret[idpInfos[i].PublicId] = append(idpList, idpInfos[i])
  80. } else {
  81. ret[idpInfos[i].PublicId] = []sIdpInfo{idpInfos[i]}
  82. }
  83. }
  84. return ret, nil
  85. }