Added seed code for access-management.
[ta/access-management.git] / src / access_management / rest-plugin / users_owndetails.py
diff --git a/src/access_management/rest-plugin/users_owndetails.py b/src/access_management/rest-plugin/users_owndetails.py
new file mode 100644 (file)
index 0000000..30467da
--- /dev/null
@@ -0,0 +1,140 @@
+# Copyright 2019 Nokia
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import access_management.db.amdb as amdb
+from am_api_base import *
+from keystoneauth1 import exceptions
+
+
+class UsersOwnDetails(AMApiBase):
+
+    """
+    User own details operations
+
+    .. :quickref: User own details;User own details operations
+
+    .. http:get:: /am/v1/users/owndetails
+
+    **Start User details**
+
+    **Example request**:
+
+    .. sourcecode:: http
+
+        GET am/v1/users/details HTTP/1.1
+        Host: haproxyvip:61200
+        Accept: application/json
+        {}
+
+    **Example response**:
+
+    .. sourcecode:: http
+
+        HTTP/1.1 200 OK
+        {
+            "code": 0,
+            "description": "User own details."
+            "data":
+            {
+                "616de2097d1647e88bdb83bfd9fdbedf":
+                {
+                    "default_project_id": "5dfb6baff51a4e10ab98e262e6f3f59d",
+                    "domain_id": "default",
+                    "email": "None",
+                    "enabled": true,
+                    "id": "616de2097d1647e88bdb83bfd9fdbedf",
+                    "links":
+                    {
+                        "self": "http://192.168.1.7:5000/v3/users/616de2097d1647e88bdb83bfd9fdbedf"
+                    },
+                    "name": "um_admin",
+                    "options": {},
+                    "password_expires_at": null,
+                    "roles": [ "infrastructure_admin", "basic_member" ]
+                }
+            }
+        }
+
+    :> json int code: the status code
+    :> json string description: the error description, present if code is non zero
+    :> json object data: the user details
+    :> json string default_project_id: The user's default project id.
+    :> json string domain_id: The user's domain id.
+    :> json string email: The user's e-mail.
+    :> json string enabled: The user's locking state.
+    :> json string id: The user's id.
+    :> json string links: The user's url address.
+    :> json string name: The user's name.
+    :> json string options: The user's options.
+    :> json string password_expires_at: The user's password expiration date.
+    :> json string roles: The user's roles.
+    """
+
+    endpoints = ['users/owndetails']
+
+    def get(self):
+        self.logger.info("Received a show own details request!")
+
+        id = self.get_uuid_from_token()
+        state, user_details = self.collect_user_details(id)
+        if state:
+            self.logger.info("User show own details response done!")
+            return AMApiBase.embed_data({id: user_details}, 0, "User own details.")
+        else:
+            self.logger.error(user_details)
+            return AMApiBase.embed_data({}, 1, user_details)
+
+    def collect_user_details(self, id):
+        try:
+            s_user = self.keystone.users.get(id)
+        except exceptions.http.NotFound as ex:
+            self.logger.error("{0}".format(ex))
+            return False, "You don't exist in the keystone!"
+        except Exception as ex:
+            self.logger.error("{0}".format(ex))
+            return False, "{0}".format(ex)
+
+        state, roles = self.ask_user_roles(id)
+        if state:
+            s_user = s_user._info
+            if 'email' not in s_user:
+                s_user["email"] = None
+            if 'description' not in s_user:
+                s_user["description"] = None
+            if roles == None:
+                s_user["roles"] = "You don't exist in the AM database!"
+            else:
+                s_user["roles"] = roles
+            return True, s_user
+        else:
+            return False, roles
+
+    def ask_user_roles(self, id):
+        state_open, message_open = self._open_db()
+        if state_open:
+            try:
+                s_user_db = self.db.get_user_roles(id)
+            except amdb.NotExist:
+                self.logger.info ("You don't exist in the AM database!")
+                s_user_db = None
+            except Exception as ex:
+                self.logger.error("Internal error: {0}".format(ex))
+                return False, ex
+            finally:
+                state_close, message_close = self._close_db()
+                if not state_close:
+                    self._close_db()
+            return True, s_user_db
+        else:
+            return False, message_open