Added seed code for access-management.
[ta/access-management.git] / src / access_management / rest-plugin / users_owndetails.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 UsersOwnDetails(AMApiBase):
21
22     """
23     User own details operations
24
25     .. :quickref: User own details;User own details operations
26
27     .. http:get:: /am/v1/users/owndetails
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
40     **Example response**:
41
42     .. sourcecode:: http
43
44         HTTP/1.1 200 OK
45         {
46             "code": 0,
47             "description": "User own details."
48             "data":
49             {
50                 "616de2097d1647e88bdb83bfd9fdbedf":
51                 {
52                     "default_project_id": "5dfb6baff51a4e10ab98e262e6f3f59d",
53                     "domain_id": "default",
54                     "email": "None",
55                     "enabled": true,
56                     "id": "616de2097d1647e88bdb83bfd9fdbedf",
57                     "links":
58                     {
59                         "self": "http://192.168.1.7:5000/v3/users/616de2097d1647e88bdb83bfd9fdbedf"
60                     },
61                     "name": "um_admin",
62                     "options": {},
63                     "password_expires_at": null,
64                     "roles": [ "infrastructure_admin", "basic_member" ]
65                 }
66             }
67         }
68
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.
82     """
83
84     endpoints = ['users/owndetails']
85
86     def get(self):
87         self.logger.info("Received a show own details request!")
88
89         id = self.get_uuid_from_token()
90         state, user_details = self.collect_user_details(id)
91         if state:
92             self.logger.info("User show own details response done!")
93             return AMApiBase.embed_data({id: user_details}, 0, "User own details.")
94         else:
95             self.logger.error(user_details)
96             return AMApiBase.embed_data({}, 1, user_details)
97
98     def collect_user_details(self, id):
99         try:
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)
107
108         state, roles = self.ask_user_roles(id)
109         if state:
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
115             if roles == None:
116                 s_user["roles"] = "You don't exist in the AM database!"
117             else:
118                 s_user["roles"] = roles
119             return True, s_user
120         else:
121             return False, roles
122
123     def ask_user_roles(self, id):
124         state_open, message_open = self._open_db()
125         if state_open:
126             try:
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!")
130                 s_user_db = None
131             except Exception as ex:
132                 self.logger.error("Internal error: {0}".format(ex))
133                 return False, ex
134             finally:
135                 state_close, message_close = self._close_db()
136                 if not state_close:
137                     self._close_db()
138             return True, s_user_db
139         else:
140             return False, message_open