def add_user_right_to_workspace(request): """ Add rights to user for the workspace. Args: request Returns """ workspace_id = request.POST.get('workspace_id', None) users_ids = request.POST.getlist('users_id[]', []) is_read_checked = request.POST.get('read', None) == 'true' is_write_checked = request.POST.get('write', None) == 'true' if len(users_ids) == 0: return HttpResponseBadRequest("You need to select at least one user.") if not is_read_checked and not is_write_checked: return HttpResponseBadRequest( "You need to select at least one permission (read and/or write).") try: workspace = workspace_api.get_by_id(str(workspace_id)) for user in user_api.get_all_users_by_list_id(users_ids): if is_read_checked: workspace_api.add_user_read_access_to_workspace( workspace, user, request.user) if is_write_checked: workspace_api.add_user_write_access_to_workspace( workspace, user, request.user) except AccessControlError, ace: return HttpResponseBadRequest(ace.message)
def _switch_user_right(user_id, action, value, workspace, request_user): """ Change the user rights to the workspace. Args: user_id: action: value: workspace: request_user: Returns: """ user = user_api.get_user_by_id(user_id) if action == ACTION_READ: if value: workspace_api.add_user_read_access_to_workspace( workspace, user, request_user) else: workspace_api.remove_user_read_access_to_workspace( workspace, user, request_user) elif action == ACTION_WRITE: if value: workspace_api.add_user_write_access_to_workspace( workspace, user, request_user) else: workspace_api.remove_user_write_access_to_workspace( workspace, user, request_user)
def add_user_right_to_workspace(request): """ Add rights to user for the workspace. Args: request Returns """ workspace_id = request.POST.get('workspace_id', None) users_ids = request.POST.getlist('users_id[]', []) is_read_checked = request.POST.get('read', None) == 'true' is_write_checked = request.POST.get('write', None) == 'true' if len(users_ids) == 0: return HttpResponseBadRequest("You need to select at least one user.") if not is_read_checked and not is_write_checked: return HttpResponseBadRequest("You need to select at least one permission (read and/or write).") try: workspace = workspace_api.get_by_id(str(workspace_id)) for user in user_api.get_all_users_by_list_id(users_ids): if is_read_checked: workspace_api.add_user_read_access_to_workspace(workspace, user, request.user) if is_write_checked: workspace_api.add_user_write_access_to_workspace(workspace, user, request.user) except AccessControlError as ace: return HttpResponseBadRequest(str(ace)) except DoesNotExist as dne: return HttpResponseBadRequest(str(dne)) except Exception as exc: return HttpResponseBadRequest('Something wrong happened.') return HttpResponse(json.dumps({}), content_type='application/javascript')
def test_get_if_user_has_read_permission_in_workspace( self, data_get_by_id): # Arrange user_request = UserFixtures().create_user("read_user") data = self.fixture.data_collection[self.fixture.USER_2_WORKSPACE_2] data_get_by_id.return_value = data workspace_api.add_user_read_access_to_workspace( self.fixture.workspace_2, user_request, self.admin_user) # Act response = RequestMock.do_request_get( data_rest_views.DataPermissions.as_view(), user_request, data={"ids": f'["{data.id}"]'}, ) # Assert excepted_result = {} excepted_result[str(data.id)] = False self.assertEqual(response.data, excepted_result)