From 8f40ee80931165c9ecee1448078c9db96c6bdbc3 Mon Sep 17 00:00:00 2001 From: mprahl Date: Sep 05 2017 08:29:57 +0000 Subject: Retain access when transfering ownership of the project --- diff --git a/pagure/api/project.py b/pagure/api/project.py index 5b69215..664e457 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -865,6 +865,12 @@ def api_modify_project(repo, namespace=None): | ``main_admin`` | string | Mandatory | | The new main admin of | | | | | the project. | +------------------+---------+--------------+---------------------------+ + | ``retain_access``| string | Optional | | The old main admin | + | | | | retains access on the | + | | | | project when giving the | + | | | | project. Defaults to | + | | | | ``False``. | + +------------------+---------+--------------+---------------------------+ Sample response ^^^^^^^^^^^^^^^ @@ -922,14 +928,16 @@ def api_modify_project(repo, namespace=None): raise pagure.exceptions.APIError( 401, error_code=APIERROR.EMODIFYPROJECTNOTALLOWED) - valid_keys = ['main_admin'] + valid_keys = ['main_admin', 'retain_access'] # Check if it's JSON or form data if flask.request.headers.get('Content-Type') == 'application/json': # Set force to True to ignore the mimetype. Set silent so that None is # returned if it's invalid JSON. args = flask.request.get_json(force=True, silent=True) or {} + retain_access = args.get('retain_access', False) else: args = flask.request.form + retain_access = args.get('retain_access', '').lower() in ['true', '1'] if not args: raise pagure.exceptions.APIError(400, error_code=APIERROR.EINVALIDREQ) @@ -953,7 +961,12 @@ def api_modify_project(repo, namespace=None): except pagure.exceptions.PagureException: raise pagure.exceptions.APIError(400, error_code=APIERROR.ENOUSER) + old_main_admin = project.user.user pagure.lib.set_project_owner(SESSION, project, new_main_admin) + if retain_access and flask.g.fas_user.username == old_main_admin: + pagure.lib.add_user_to_project( + SESSION, project, new_user=flask.g.fas_user.username, + user=flask.g.fas_user.username) try: SESSION.commit() diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 2a2dcc2..0e24552 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -2674,7 +2674,14 @@ def give_project(repo, username=None, namespace=None): 404, 'No such user %s found' % new_username) try: + old_main_admin = repo.user.user pagure.lib.set_project_owner(SESSION, repo, new_owner) + # If the person doing the action is the former main admin, keep + # them as admins + if flask.g.fas_user.username == old_main_admin: + pagure.lib.add_user_to_project( + SESSION, repo, new_user=flask.g.fas_user.username, + user=flask.g.fas_user.username) SESSION.commit() flask.flash( 'The project has been transferred to %s' % new_username) diff --git a/tests/test_pagure_flask_api_project.py b/tests/test_pagure_flask_api_project.py index 302772e..9bb5081 100644 --- a/tests/test_pagure_flask_api_project.py +++ b/tests/test_pagure_flask_api_project.py @@ -1177,7 +1177,71 @@ class PagureFlaskApiProjecttests(tests.Modeltests): } } self.assertEqual(data, expected_output) - + + def test_api_modify_project_main_admin_retain_access(self): + """ Test the api_modify_project method of the flask api when the + request is to change the main_admin of the project and retain_access + is true. """ + tests.create_projects(self.session) + tests.create_tokens(self.session, project_id=None) + tests.create_tokens_acl(self.session, 'aaabbbcccddd', 'modify_project') + headers = {'Authorization': 'token aaabbbcccddd'} + + user = pagure.lib.get_user(self.session, 'pingou') + user.cla_done = True + with tests.user_set(pagure.APP, user): + output = self.app.patch( + '/api/0/test', headers=headers, + data={'main_admin': 'foo', 'retain_access': True}) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['date_created'] = '1496338274' + data['date_modified'] = '1496338274' + expected_output = { + "access_groups": { + "admin": [], + "commit": [], + "ticket": [] + }, + "access_users": { + "admin": [ + "pingou" + ], + "commit": [], + "owner": [ + "foo" + ], + "ticket": [] + }, + "close_status": [ + "Invalid", + "Insufficient data", + "Fixed", + "Duplicate" + ], + "custom_keys": [], + "date_created": "1496338274", + "date_modified": "1496338274", + "description": "test project #1", + "fullname": "test", + "id": 1, + "milestones": {}, + "name": "test", + "namespace": None, + "parent": None, + "priorities": {}, + "tags": [], + "user": { + "default_email": "foo@bar.com", + "emails": [ + "foo@bar.com" + ], + "fullname": "foo bar", + "name": "foo" + } + } + self.assertEqual(data, expected_output) + def test_api_modify_project_main_admin_json(self): """ Test the api_modify_project method of the flask api when the request is to change the main_admin of the project using JSON. """ diff --git a/tests/test_pagure_flask_ui_app_give_project.py b/tests/test_pagure_flask_ui_app_give_project.py index ea5cb08..068570c 100644 --- a/tests/test_pagure_flask_ui_app_give_project.py +++ b/tests/test_pagure_flask_ui_app_give_project.py @@ -254,6 +254,11 @@ class PagureFlaskGiveRepotests(tests.SimplePagureTest): output.data) self._check_user('foo') + # Make sure that the user giving the project is still an admin + project = pagure.get_authorized_project( + self.session, project_name='test') + self.assertEqual(len(project.users), 1) + self.assertEqual(project.users[0].user, 'pingou') if __name__ == '__main__':