token.go 2.1 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 policy
  15. import (
  16. "context"
  17. "yunion.io/x/pkg/gotypes"
  18. "yunion.io/x/pkg/util/rbacscope"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. "yunion.io/x/onecloud/pkg/mcclient/auth"
  21. )
  22. type SPolicyTokenCredential struct {
  23. // usage embedded interface
  24. mcclient.TokenCredential
  25. }
  26. func (self *SPolicyTokenCredential) HasSystemAdminPrivilege() bool {
  27. return PolicyManager.IsScopeCapable(self.TokenCredential, rbacscope.ScopeSystem)
  28. }
  29. /*func (self *SPolicyTokenCredential) IsAllow(targetScope rbacscope.TRbacScope, service string, resource string, action string, extra ...string) rbacutils.SPolicyResult {
  30. allowScope, result := PolicyManager.AllowScope(self.TokenCredential, service, resource, action, extra...)
  31. if result.Result == rbacutils.Allow && !targetScope.HigherThan(allowScope) {
  32. return result
  33. }
  34. return rbacutils.PolicyDeny
  35. }*/
  36. func init() {
  37. gotypes.RegisterSerializableTransformer(mcclient.TokenCredentialType, func(input gotypes.ISerializable) gotypes.ISerializable {
  38. // log.Debugf("do TokenCredential transform for %#v", input)
  39. switch val := input.(type) {
  40. case *mcclient.SSimpleToken:
  41. return &SPolicyTokenCredential{val}
  42. default:
  43. return val
  44. }
  45. })
  46. }
  47. func FilterPolicyCredential(token mcclient.TokenCredential) mcclient.TokenCredential {
  48. switch token.(type) {
  49. case *SPolicyTokenCredential:
  50. return token
  51. default:
  52. return &SPolicyTokenCredential{TokenCredential: token}
  53. }
  54. }
  55. func FetchUserCredential(ctx context.Context) mcclient.TokenCredential {
  56. token := auth.FetchUserCredential(ctx, FilterPolicyCredential)
  57. return token
  58. }