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 UsersOwnDetails(AMApiBase):
23 User own details operations
25 .. :quickref: User own details;User own details operations
27 .. http:get:: /am/v1/users/owndetails
29 **Start User details**
35 GET am/v1/users/details HTTP/1.1
36 Host: haproxyvip:61200
37 Accept: application/json
47 "description": "User own details."
50 "616de2097d1647e88bdb83bfd9fdbedf":
52 "default_project_id": "5dfb6baff51a4e10ab98e262e6f3f59d",
53 "domain_id": "default",
56 "id": "616de2097d1647e88bdb83bfd9fdbedf",
59 "self": "http://192.168.1.7:5000/v3/users/616de2097d1647e88bdb83bfd9fdbedf"
63 "password_expires_at": null,
64 "roles": [ "infrastructure_admin", "basic_member" ]
69 :> json int code: the status code
70 :> json string description: the error description, present if code is non zero
71 :> json object data: the user details
72 :> json string default_project_id: The user's default project id.
73 :> json string domain_id: The user's domain id.
74 :> json string email: The user's e-mail.
75 :> json string enabled: The user's locking state.
76 :> json string id: The user's id.
77 :> json string links: The user's url address.
78 :> json string name: The user's name.
79 :> json string options: The user's options.
80 :> json string password_expires_at: The user's password expiration date.
81 :> json string roles: The user's roles.
84 endpoints = ['users/owndetails']
87 self.logger.info("Received a show own details request!")
89 id = self.get_uuid_from_token()
90 state, user_details = self.collect_user_details(id)
92 self.logger.info("User show own details response done!")
93 return AMApiBase.embed_data({id: user_details}, 0, "User own details.")
95 self.logger.error(user_details)
96 return AMApiBase.embed_data({}, 1, user_details)
98 def collect_user_details(self, id):
100 s_user = self.keystone.users.get(id)
101 except exceptions.http.NotFound as ex:
102 self.logger.error("{0}".format(ex))
103 return False, "You don't exist in the keystone!"
104 except Exception as ex:
105 self.logger.error("{0}".format(ex))
106 return False, "{0}".format(ex)
108 state, roles = self.ask_user_roles(id)
110 s_user = s_user._info
111 if 'email' not in s_user:
112 s_user["email"] = None
113 if 'description' not in s_user:
114 s_user["description"] = None
116 s_user["roles"] = "You don't exist in the AM database!"
118 s_user["roles"] = roles
123 def ask_user_roles(self, id):
124 state_open, message_open = self._open_db()
127 s_user_db = self.db.get_user_roles(id)
128 except amdb.NotExist:
129 self.logger.info ("You don't exist in the AM database!")
131 except Exception as ex:
132 self.logger.error("Internal error: {0}".format(ex))
135 state_close, message_close = self._close_db()
138 return True, s_user_db
140 return False, message_open