user.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 cachesync
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/rbacscope"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/mcclient/auth"
  24. modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  25. "yunion.io/x/onecloud/pkg/util/ctx"
  26. )
  27. type userCacheSyncWorker struct {
  28. ids []string
  29. }
  30. func (w *userCacheSyncWorker) Run() {
  31. log.Debugf("[userCacheSyncWorker] Run project cache sync worker ...")
  32. err := syncUsers(ctx.CtxWithTime(), w.ids)
  33. if err != nil {
  34. log.Errorf("fail to syncUsers %s", err)
  35. }
  36. }
  37. func (w *userCacheSyncWorker) Dump() string {
  38. return "userCacheSyncWorker"
  39. }
  40. func syncUsers(ctx context.Context, ids []string) error {
  41. s := auth.GetAdminSession(ctx, consts.GetRegion())
  42. query := jsonutils.NewDict()
  43. query.Add(jsonutils.NewInt(1024), "limit")
  44. query.Add(jsonutils.NewString(string(rbacscope.ScopeSystem)), "scope")
  45. query.Add(jsonutils.JSONTrue, "details")
  46. query.Add(jsonutils.NewString("all"), "pending_delete")
  47. query.Add(jsonutils.NewString("all"), "delete")
  48. if len(ids) > 0 {
  49. query.Add(jsonutils.NewStringArray(ids), "id")
  50. }
  51. total := -1
  52. offset := 0
  53. for total < 0 || offset < total {
  54. query.Set("offset", jsonutils.NewInt(int64(offset)))
  55. results, err := modules.UsersV3.List(s, query)
  56. if err != nil {
  57. return errors.Wrap(err, "UsersV3.List")
  58. }
  59. total = results.Total
  60. for i := range results.Data {
  61. // update user cache
  62. item := db.SCachedUser{}
  63. deleted := jsonutils.QueryBoolean(results.Data[i], "deleted", false)
  64. err := results.Data[i].Unmarshal(&item)
  65. if err == nil && !deleted {
  66. db.UserCacheManager.Save(ctx, item.Id, item.Name, item.DomainId, item.ProjectDomain, item.Lang)
  67. } else if deleted {
  68. usrObj, _ := db.UserCacheManager.FetchById(item.Id)
  69. if usrObj != nil {
  70. usrObj.Delete(ctx, nil)
  71. }
  72. }
  73. offset++
  74. }
  75. }
  76. return nil
  77. }