roleresource.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 models
  15. import (
  16. "context"
  17. "database/sql"
  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/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/httperrors"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. )
  25. type SRoleResourceBaseManager struct{}
  26. func (manager *SRoleResourceBaseManager) ListItemFilter(
  27. ctx context.Context,
  28. q *sqlchemy.SQuery,
  29. userCred mcclient.TokenCredential,
  30. query api.RoleFilterListInput,
  31. ) (*sqlchemy.SQuery, error) {
  32. if len(query.RoleId) > 0 {
  33. var ownerId mcclient.IIdentityProvider
  34. if len(query.RoleDomainId) > 0 {
  35. domain, err := DomainManager.FetchDomainByIdOrName(query.RoleDomainId)
  36. if err != nil {
  37. if errors.Cause(err) == sql.ErrNoRows {
  38. return nil, httperrors.NewResourceNotFoundError2(DomainManager.Keyword(), query.RoleDomainId)
  39. } else {
  40. return nil, errors.Wrap(err, "DomainManager.FetchDomainByIdOrName")
  41. }
  42. }
  43. ownerId = &db.SOwnerId{
  44. Domain: domain.Name,
  45. DomainId: domain.Id,
  46. }
  47. } else {
  48. ownerId = userCred
  49. }
  50. roleObj, err := RoleManager.FetchByIdOrName(ctx, ownerId, query.RoleId)
  51. if err != nil {
  52. if errors.Cause(err) == sql.ErrNoRows {
  53. return nil, httperrors.NewResourceNotFoundError2(RoleManager.Keyword(), query.RoleId)
  54. } else {
  55. return nil, errors.Wrap(err, "RoleManager.FetchByIdOrName")
  56. }
  57. }
  58. q = q.Equals("role_id", roleObj.GetId())
  59. }
  60. return q, nil
  61. }