mod_users.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/printutils"
  21. "yunion.io/x/onecloud/pkg/httperrors"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  24. "yunion.io/x/onecloud/pkg/mcclient/modules"
  25. )
  26. type UserManager struct {
  27. modulebase.ResourceManager
  28. }
  29. func (this *UserManager) GetTenantRoles(session *mcclient.ClientSession, uid string, tenantId string) (*printutils.ListResult, error) {
  30. url := fmt.Sprintf("/users/%s/roles", uid)
  31. if len(tenantId) > 0 {
  32. url = fmt.Sprintf("/tenants/%s/%s", tenantId, url)
  33. }
  34. return modulebase.List(this.ResourceManager, session, url, "roles")
  35. }
  36. func (this *UserManager) GetTenantRoleList(session *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  37. uid, e := params.GetString("id")
  38. if e != nil {
  39. return nil, e
  40. }
  41. tenantId, _ := params.GetString("tenantId")
  42. ret, e := this.GetTenantRoles(session, uid, tenantId)
  43. if e != nil {
  44. return nil, e
  45. }
  46. return modulebase.ListResult2JSON(ret), nil
  47. }
  48. type UserManagerV3 struct {
  49. modulebase.ResourceManager
  50. }
  51. func (this *UserManagerV3) GetProjects(session *mcclient.ClientSession, uid string) (*printutils.ListResult, error) {
  52. url := fmt.Sprintf("/users/%s/projects?admin=true", uid)
  53. return modulebase.List(this.ResourceManager, session, url, "projects")
  54. }
  55. func (this *UserManagerV3) GetGroups(session *mcclient.ClientSession, uid string) (*printutils.ListResult, error) {
  56. url := fmt.Sprintf("/users/%s/groups?admin=true", uid)
  57. return modulebase.List(this.ResourceManager, session, url, "groups")
  58. }
  59. func (this *UserManagerV3) GetProjectsRPC(s *mcclient.ClientSession, uid string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  60. ret, e := this.GetProjects(s, uid)
  61. if e != nil {
  62. return nil, e
  63. }
  64. return modulebase.ListResult2JSON(ret), nil
  65. }
  66. /*
  67. func (this *UserManagerV3) GetIsLdapUser(s *mcclient.ClientSession, uid string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  68. ret := jsonutils.NewDict()
  69. ret.Add(jsonutils.JSONFalse, "isldap")
  70. log.Infof("GetIsLdapUser ret: %s", ret)
  71. user, err := this.Get(s, uid, nil)
  72. if err != nil {
  73. return ret, err
  74. }
  75. domain_id, err := user.GetString("domain_id")
  76. if err != nil {
  77. return ret, nil
  78. }
  79. domain, err := Domains.GetConfig(s, domain_id)
  80. if err != nil {
  81. log.Errorf("domain config error: %v", err)
  82. return ret, nil
  83. }
  84. driver, err := domain.GetString("identity", "driver")
  85. if err != nil {
  86. return ret, nil
  87. }
  88. if strings.ToLower(driver) == "ldap" {
  89. // ret["isldap"] = jsonutils.JSONTrue
  90. ret.Set("isldap", jsonutils.JSONTrue)
  91. }
  92. return ret, nil
  93. }*/
  94. func (this *UserManagerV3) _groupAction(s *mcclient.ClientSession, gid, uid, action string) error {
  95. if action == "join" {
  96. _, err := this.PutInContext(s, uid, nil, &Groups, gid)
  97. if err != nil {
  98. return err
  99. }
  100. } else if action == "leave" {
  101. _, err := this.DeleteInContext(s, uid, nil, &Groups, gid)
  102. if err != nil {
  103. return err
  104. }
  105. }
  106. return nil
  107. }
  108. func (this *UserManagerV3) DoJoinGroups(s *mcclient.ClientSession, uid string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  109. // params format:
  110. // {
  111. // "uid": "CCCGwOsrpp6h",
  112. // "action": "leave" / "join", select one of them
  113. // "gids": ["L6ssbAJUG3rC", "pu8lkunxP4z8"]
  114. // }
  115. gids, err := params.GetArray("gids")
  116. if err != nil {
  117. return nil, httperrors.NewMissingParameterError("gids")
  118. }
  119. action, err := params.GetString("action")
  120. if err != nil {
  121. return nil, httperrors.NewMissingParameterError("action")
  122. }
  123. if action != "join" && action != "leave" {
  124. return nil, httperrors.NewInputParameterError("unsupported action %s", action)
  125. }
  126. if enabled, _ := params.Bool("enabled"); enabled && action == "join" {
  127. _, err := this.PerformAction(s, uid, "enable", nil)
  128. if err != nil {
  129. return nil, err
  130. }
  131. }
  132. errs := make([]error, 0)
  133. for _, gid := range gids {
  134. _gid, _ := gid.GetString()
  135. if len(_gid) > 0 {
  136. err := this._groupAction(s, _gid, uid, action)
  137. if err != nil {
  138. errs = append(errs, err)
  139. }
  140. }
  141. }
  142. if len(errs) > 0 {
  143. if len(errs) == len(gids) {
  144. return nil, httperrors.NewGeneralError(errors.NewAggregate(errs))
  145. }
  146. log.Errorf("join group error %s", errors.NewAggregate(errs))
  147. }
  148. return jsonutils.NewDict(), nil
  149. }
  150. // create user && assgin user with project_domain、project、role
  151. func (this *UserManagerV3) DoCreateUser(s *mcclient.ClientSession, p jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  152. // params format:
  153. // {
  154. // "project": ["projectA", "projectB"],
  155. // "role": ["RoleA", "RoleB"],
  156. // }
  157. params := p.(*jsonutils.JSONDict)
  158. _project, _ := params.Get("project")
  159. _role, _ := params.Get("role")
  160. params.Remove("project")
  161. params.Remove("role")
  162. response, err := UsersV3.Create(s, params)
  163. if err != nil {
  164. return nil, err
  165. }
  166. uid, err := response.GetString("id")
  167. if err != nil {
  168. return nil, err
  169. }
  170. // assgin project & role
  171. projects := []string{}
  172. roles := []string{}
  173. if _project != nil {
  174. if _p, ok := _project.(*jsonutils.JSONArray); ok {
  175. projects = _p.GetStringArray()
  176. }
  177. }
  178. if _role != nil {
  179. if _r, ok := _role.(*jsonutils.JSONArray); ok {
  180. roles = _r.GetStringArray()
  181. }
  182. }
  183. errs := make([]error, 0)
  184. if len(projects) > 0 && len(roles) > 0 {
  185. for i := range projects {
  186. pid := projects[i]
  187. for j := range roles {
  188. rid := roles[j]
  189. err := Projects.JoinProject(s, rid, pid, uid)
  190. if err != nil {
  191. errs = append(errs, err)
  192. }
  193. }
  194. }
  195. }
  196. if len(errs) > 0 {
  197. log.Errorf("join project errors: %s", errors.NewAggregate(errs))
  198. }
  199. return response, nil
  200. }
  201. func (this *UserManagerV3) FetchId(s *mcclient.ClientSession, user string, domain string) (string, error) {
  202. userQuery := jsonutils.NewDict()
  203. if len(domain) > 0 {
  204. domainId, err := Domains.GetId(s, domain, nil)
  205. if err != nil {
  206. return "", err
  207. }
  208. userQuery.Add(jsonutils.NewString(domainId), "domain_id")
  209. }
  210. return this.GetId(s, user, userQuery)
  211. }
  212. var (
  213. Users UserManager
  214. UsersV3 UserManagerV3
  215. )
  216. func init() {
  217. Users = UserManager{modules.NewIdentityManager("user", "users",
  218. []string{},
  219. []string{"ID", "Name", "TenantId", "Tenant_name",
  220. "Enabled", "Email", "Mobile"})}
  221. modules.Register(&Users)
  222. UsersV3 = UserManagerV3{modules.NewIdentityV3Manager("user", "users",
  223. []string{},
  224. []string{"ID", "Name", "Domain_Id", "Project_Domain", "readonly", "idp_id",
  225. "Enabled", "Email", "Mobile", "Displayname",
  226. "is_system_account", "allow_web_console", "enable_mfa"})}
  227. modules.Register(&UsersV3)
  228. }