def edit_user(request): try: user_id = request.json_body['data']['user_id'] # user_id: number changes_obj = request.json_body['data']['changes_obj'] # changes_obj: Object session = request.dbsession imanage_access = IManageAccess(ssn=session) user = get_user_by_id(session, user_id) # Change password if 'password' in changes_obj: user.set_password(changes_obj['password']) # Add roles if 'roles' in changes_obj: for role_id in changes_obj['roles']: role = imanage_access.get_role(id=role_id) imanage_access.add_role_to_user(user, role) user = get_user_by_id(session, user_id) list_role = [] for role in user.roles: if role.id not in list_role: list_role.append(role.id) data = {'user_id': user.id, 'email': user.email, 'active': True, 'roles': list_role, 'avatar': ''} except Exception: raise http_exc.HTTPClientError() else: return Response(json_body=json.dumps(data), content_type='application/json')
def add_user_to_group(self, user_id, group_id): """ TEMPRORARY DOESN'T WORK Add user to group :param user_id: :type user_id: :param group_id: :type group_id: :return: :rtype: """ # Validate inputs user_id = _get_id_or_err(user_id, 'user_id') group_id = _get_id_or_err(group_id, 'group_id') # Get objects user = a_m.get_user_by_id(self.ssn, user_id) if user is None: raise ex.NotExistsError('User', 'id', user_id) group = a_m.get_user_group_by_id(self.ssn, group_id) if group is None: raise ex.NotExistsError('UserGroup', 'id', group_id) if user in group.users: raise ex.AlreadyExistsError('UserGroup', 'users', user.id) return group.users.append(user)
def reset_password(request): try: user_id = request.json_body['data']['user_id'] # id: number session = request.dbsession user = get_user_by_id(session, user_id) user.set_password('') except Exception: raise http_exc.HTTPClientError() else: return Response(json_body=json.dumps('Success'), content_type='application/json')
def get_user(self, **kwargs): """ Args: **kwargs: id|email """ if 'id' in kwargs: user_id = _get_id_or_err(kwargs['id'], 'id') return a_m.get_user_by_id(self.ssn, user_id) elif 'email' in kwargs: email = _get_str_or_err(kwargs['email'], 'email') return a_m.get_user_by_email(self.ssn, email)
def get_user_roles(self, user_id): """ Return Users Role's :param user_id: :type user_id: :return: :rtype: """ user_id = _get_id_or_err(user_id, 'user_id') user = a_m.get_user_by_id(self.ssn, user_id) if user is None: raise ex.NotExistsError('User', 'id', user_id) return user.roles
def get_entity_permission(self, user_id, project_id, entity_path): """ :param user_id: :type user_id: :param project_id: :type project_id: :param entity_path: :type entity_path: :return: :rtype: """ user = a_m.get_user_by_id(user_id) perms = user.perms for perm in perms: pass
def add_permission_value(self, tool_id, perm_node_id, value, user_id): tool_id = _get_id_or_err(tool_id, 'tool_id') perm_node_id = _get_id_or_err(perm_node_id, 'perm_node_id') user_id = _get_id_or_err(user_id, 'user_id') tool = a_m.get_tool_by_id(self.ssn, tool_id) if tool is None: raise ex.NotExistsError('Tool', 'id', tool_id) user = a_m.get_user_by_id(self.ssn, user_id) if user is None: raise ex.NotExistsError('User', 'id', user_id) perm_node = a_m.get_perm_node_in_tool(self.ssn, perm_node_id, tool) if perm_node is None: raise ex.NotExistsError('PermNode', 'id', perm_node_id) return a_m.add_perm_value(self.ssn, tool, perm_node, value, user)
def get_permissions(self, tool_id, user_id): tool_id = _get_id_or_err(tool_id, 'tool_id') user_id = _get_id_or_err(user_id, 'user_id') tool = wha.get_tool_by_id(self.ssn, tool_id) user = wha.get_user_by_id(self.ssn, user_id) if tool is None: raise ex.NotExistsError('Tool', 'id', tool_id) if user is None: raise ex.NotExistsError('User', 'id', user_id) # default_perms = wha.get_perms_to_tool(ssn, tool) # wha.get_user_perms_to_tool(sess, tool, user), return { 'permissions': wha.get_perms_to_tool(self.ssn, tool, user), 'features': wha.get_user_features_to_tool(self.ssn, tool, user) }
def init_user_wb(self, user_id, tool_id): # Validate inputs tool_id = _get_id_or_err(tool_id, 'tool_id') user_id = _get_id_or_err(user_id, 'user_id') # Get Objects tool = a_m.get_tool_by_id(self.ssn, tool_id) if tool is None: raise ex.NotExistsError('Tool', 'id', tool_id) user = a_m.get_user_by_id(self.ssn, user_id) if user is None: raise ex.NotExistsError('User', 'id', user_id) # Version 2 # Get raw data from table with masks raw_perm_values = a_m.get_raw_perm_values_for_user( self.ssn, tool, None) # Copy/Insert this raw data for specified user # TODO - check if there is no data for this user!!! for per_v in raw_perm_values: perm_node = per_v.perm_node a_m.add_perm_value(self.ssn, tool, perm_node, per_v.value, user) # TODO Fix bug with initialize db # # Get recently created permissions for user iaccess = _IAccess(ssn=self.ssn) u_perms = iaccess.get_permissions(tool_id, user_id) # self.istorage.backup.save(user_id, tool_id, u_perms, 'models_managers') # # Make backup file # dir_path = os.path.dirname(os.path.realpath(__file__)) # file_name = "backup_tool_" + str(tool_id) + "_user_" \ # + str(user_id) + ".json" # file_path = os.path.join(dir_path, file_name) # with open(file_path, "w+") as backup_file: # json.dump(u_perms, backup_file) pass # TODO Confirm This Realization
def check_permission_for_tool_and_project(self, user_id, tool_id, project_id): """ Function will check permission to project and tool :return: :rtype: """ access = {'tool': False, 'project': False} user = a_m.get_user_by_id(self.ssn, user_id) for role in user.roles: if tool_id == role.tool_id: access['tool':True] for perm in user.perms: for data_perm in perm: if data_perm.project == project_id: access['project':True] if access == {'tool': True, 'project': True}: return True else: return False
def update_user_data_permissions(self, tool_id, user_id, permissions): """ TEMPRORARY DOESN'T WORK :param tool_id: :type tool_id: :param user_id: :type user_id: :param permissions: :type permissions: :return: :rtype: """ #TODO # Validate inputs tool_id = _get_id_or_err(tool_id, 'user_id') user_id = _get_id_or_err(user_id, 'user_id') # Get objects tool = a_m.get_tool_by_id(self.ssn, tool_id) if tool is None: raise ex.NotExistsError('Tool', 'id', tool_id) user = a_m.get_user_by_id(self.ssn, user_id) if user is None: raise ex.NotExistsError('User', 'id', user_id) # TODO Validating paths for nodes # Delete previous permissions for user a_m.del_perm_values_for_user(self.ssn, tool, user) # Get objects storage = {} permissions = sorted(permissions, key=lambda p: len(p['path'])) for perm in permissions: self._add_permission(tool, storage, perm, user)