Added seed code for access-management.
[ta/access-management.git] / src / access_management / rest-plugin / users_details.py
1 # Copyright 2019 Nokia
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
15 import access_management.db.amdb as amdb
16 from am_api_base import *
17 from keystoneauth1 import exceptions
18
19
20 class UsersDetails(AMApiBase):
21
22     """
23     User details operations
24
25     .. :quickref: User details;User details operations
26
27     .. http:get:: /am/v1/users/details
28
29     **Start User details**
30
31     **Example request**:
32
33     .. sourcecode:: http
34
35         GET am/v1/users/details HTTP/1.1
36         Host: haproxyvip:61200
37         Accept: application/json
38         {
39             "user": <uuid> or <username>
40         }
41
42     :> json string user: The showed user's id or name.
43
44     **Example response**:
45
46     .. sourcecode:: http
47
48         HTTP/1.1 200 OK
49         {
50             "code": 0,
51             "description": "User details."
52             "data":
53             {
54                 "616de2097d1647e88bdb83bfd9fdbedf":
55                 {
56                     "default_project_id": "5dfb6baff51a4e10ab98e262e6f3f59d",
57                     "domain_id": "default",
58                     "email": "None",
59                     "enabled": true,
60                     "id": "616de2097d1647e88bdb83bfd9fdbedf",
61                     "links":
62                     {
63                         "self": "http://192.168.1.7:5000/v3/users/616de2097d1647e88bdb83bfd9fdbedf"
64                     },
65                     "name": "um_admin",
66                     "options": {},
67                     "password_expires_at": null,
68                     "roles": [ "infrastructure_admin", "basic_member" ]
69                 }
70             }
71         }
72
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.
86     """
87
88     endpoints = ['users/details']
89     parser_arguments = ['user']
90
91     def get(self):
92         self.logger.info("Received a user show request!")
93         args = self.parse_args()
94
95         state, user_info = self.get_uuid_and_name(args["user"])
96         if state:
97             state, user_details = self.collect_user_details(user_info)
98             if state:
99                 self.logger.info("User show response done!")
100                 return AMApiBase.embed_data({user_info["id"]: user_details}, 0, "User details.")
101             else:
102                 self.logger.error(user_details)
103                 return AMApiBase.embed_data({}, 1, user_details)
104         else:
105             self.logger.error(user_info)
106             return AMApiBase.embed_data({}, 1, user_info)
107
108     def collect_user_details(self, user_info):
109         try:
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)
117
118         state, roles = self.ask_user_roles(user_info)
119         if state:
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
125             if roles == None:
126                 s_user["roles"] = "The {0} user does not exist in the AM database!".format(user_info["name"])
127             else:
128                 s_user["roles"] = roles
129             return True, s_user
130         else:
131             return False, roles
132
133     def ask_user_roles(self, user_info):
134         state_open, message_open = self._open_db()
135         if state_open:
136             try:
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"]))
140                 s_user_db = None
141             except Exception as ex:
142                 self.logger.error("Internal error: {0}".format(ex))
143                 return False, ex
144             finally:
145                 state_close, message_close = self._close_db()
146                 if not state_close:
147                     self._close_db()
148             return True, s_user_db
149         else:
150             return False, message_open