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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 import access_management.db.amdb as amdb
16 from am_api_base import *
17 from keystoneauth1 import exceptions
20 class UsersDetails(AMApiBase):
23 User details operations
25 .. :quickref: User details;User details operations
27 .. http:get:: /am/v1/users/details
29 **Start User details**
35 GET am/v1/users/details HTTP/1.1
36 Host: haproxyvip:61200
37 Accept: application/json
39 "user": <uuid> or <username>
42 :> json string user: The showed user's id or name.
51 "description": "User details."
54 "616de2097d1647e88bdb83bfd9fdbedf":
56 "default_project_id": "5dfb6baff51a4e10ab98e262e6f3f59d",
57 "domain_id": "default",
60 "id": "616de2097d1647e88bdb83bfd9fdbedf",
63 "self": "http://192.168.1.7:5000/v3/users/616de2097d1647e88bdb83bfd9fdbedf"
67 "password_expires_at": null,
68 "roles": [ "infrastructure_admin", "basic_member" ]
73 :> json int code: the status code
74 :> json string description: the error description, present if code is non zero
75 :> json object data: the user details
76 :> json string default_project_id: The user's default project id.
77 :> json string domain_id: The user's domain id.
78 :> json string email: The user's e-mail.
79 :> json string enabled: The user's locking state.
80 :> json string id: The user's id.
81 :> json string links: The user's url address.
82 :> json string name: The user's name.
83 :> json string options: The user's options.
84 :> json string password_expires_at: The user's password expiration date.
85 :> json string roles: The user's roles.
88 endpoints = ['users/details']
89 parser_arguments = ['user']
92 self.logger.info("Received a user show request!")
93 args = self.parse_args()
95 state, user_info = self.get_uuid_and_name(args["user"])
97 state, user_details = self.collect_user_details(user_info)
99 self.logger.info("User show response done!")
100 return AMApiBase.embed_data({user_info["id"]: user_details}, 0, "User details.")
102 self.logger.error(user_details)
103 return AMApiBase.embed_data({}, 1, user_details)
105 self.logger.error(user_info)
106 return AMApiBase.embed_data({}, 1, user_info)
108 def collect_user_details(self, user_info):
110 s_user = self.keystone.users.get(user_info["id"])
111 except exceptions.http.NotFound as ex:
112 self.logger.error("{0}".format(ex))
113 return False, "This user does not exist in the keystone!"
114 except Exception as ex:
115 self.logger.error("{0}".format(ex))
116 return False, "{0}".format(ex)
118 state, roles = self.ask_user_roles(user_info)
120 s_user = s_user._info
121 if 'email' not in s_user:
122 s_user["email"] = None
123 if 'description' not in s_user:
124 s_user["description"] = None
126 s_user["roles"] = "The {0} user does not exist in the AM database!".format(user_info["name"])
128 s_user["roles"] = roles
133 def ask_user_roles(self, user_info):
134 state_open, message_open = self._open_db()
137 s_user_db = self.db.get_user_roles(user_info["id"])
138 except amdb.NotExist:
139 self.logger.info ("The {0} user does not exist in the AM database!".format(user_info["id"]))
141 except Exception as ex:
142 self.logger.error("Internal error: {0}".format(ex))
145 state_close, message_close = self._close_db()
148 return True, s_user_db
150 return False, message_open