# HG changeset patch # User Marcin Kuzminski # Date 2017-05-16 12:48:07 # Node ID 9e04797126c88172facdf3c55ab15496a7e6b52c # Parent aa1d7f9969b3338f1110f4ea84dba58bbc6875b9 audit-logger: unify calls to repo.delete and also store source of call, api/web. diff --git a/rhodecode/api/tests/test_delete_repo.py b/rhodecode/api/tests/test_delete_repo.py --- a/rhodecode/api/tests/test_delete_repo.py +++ b/rhodecode/api/tests/test_delete_repo.py @@ -30,43 +30,45 @@ from rhodecode.api.tests.utils import ( class TestApiDeleteRepo(object): def test_api_delete_repo(self, backend): repo = backend.create_repo() - + repo_name = repo.repo_name id_, params = build_data( self.apikey, 'delete_repo', repoid=repo.repo_name, ) response = api_call(self.app, params) expected = { - 'msg': 'Deleted repository `%s`' % (repo.repo_name,), + 'msg': 'Deleted repository `%s`' % (repo_name,), 'success': True } assert_ok(id_, expected, given=response.body) def test_api_delete_repo_by_non_admin(self, backend, user_regular): repo = backend.create_repo(cur_user=user_regular.username) + repo_name = repo.repo_name id_, params = build_data( user_regular.api_key, 'delete_repo', repoid=repo.repo_name, ) response = api_call(self.app, params) expected = { - 'msg': 'Deleted repository `%s`' % (repo.repo_name,), + 'msg': 'Deleted repository `%s`' % (repo_name,), 'success': True } assert_ok(id_, expected, given=response.body) def test_api_delete_repo_by_non_admin_no_permission(self, backend): repo = backend.create_repo() + repo_name = repo.repo_name id_, params = build_data( self.apikey_regular, 'delete_repo', repoid=repo.repo_name, ) response = api_call(self.app, params) - expected = 'repository `%s` does not exist' % (repo.repo_name) + expected = 'repository `%s` does not exist' % (repo_name) assert_error(id_, expected, given=response.body) def test_api_delete_repo_exception_occurred(self, backend): repo = backend.create_repo() + repo_name = repo.repo_name id_, params = build_data( self.apikey, 'delete_repo', repoid=repo.repo_name, ) with mock.patch.object(RepoModel, 'delete', crash): response = api_call(self.app, params) - expected = 'failed to delete repository `%s`' % ( - repo.repo_name,) + expected = 'failed to delete repository `%s`' % (repo_name,) assert_error(id_, expected, given=response.body) diff --git a/rhodecode/api/views/repo_api.py b/rhodecode/api/views/repo_api.py --- a/rhodecode/api/views/repo_api.py +++ b/rhodecode/api/views/repo_api.py @@ -29,6 +29,7 @@ from rhodecode.api.utils import ( get_user_group_or_error, get_user_or_error, validate_repo_permissions, get_perm_or_error, parse_args, get_origin, build_commit_data, validate_set_owner_permissions) +from rhodecode.lib import audit_logger from rhodecode.lib import repo_maintenance from rhodecode.lib.auth import HasPermissionAnyApi, HasUserGroupPermissionAnyApi from rhodecode.lib.utils2 import str2bool, time_to_datetime @@ -1154,6 +1155,7 @@ def delete_repo(request, apiuser, repoid """ repo = get_repo_or_error(repoid) + repo_name = repo.repo_name if not has_superadmin_permission(apiuser): _perms = ('repository.admin',) validate_repo_permissions(apiuser, repoid, repo, _perms) @@ -1171,18 +1173,27 @@ def delete_repo(request, apiuser, repoid 'Cannot delete `%s` it still contains attached forks' % (repo.repo_name,) ) + repo_data = repo.get_api_data() + RepoModel().delete(repo, forks=forks) - RepoModel().delete(repo, forks=forks) + repo = audit_logger.RepoWrap(repo_id=None, + repo_name=repo.repo_name) + + audit_logger.store( + action='repo.delete', + action_data={'repo_data': repo_data, 'source': 'api_call'}, + user=apiuser, repo=repo, commit=False) + + ScmModel().mark_for_invalidation(repo_name, delete=True) Session().commit() return { - 'msg': 'Deleted repository `%s`%s' % ( - repo.repo_name, _forks_msg), + 'msg': 'Deleted repository `%s`%s' % (repo_name, _forks_msg), 'success': True } except Exception: log.exception("Exception occurred while trying to delete repo") raise JSONRPCError( - 'failed to delete repository `%s`' % (repo.repo_name,) + 'failed to delete repository `%s`' % (repo_name,) ) diff --git a/rhodecode/apps/repository/views/repo_settings_advanced.py b/rhodecode/apps/repository/views/repo_settings_advanced.py --- a/rhodecode/apps/repository/views/repo_settings_advanced.py +++ b/rhodecode/apps/repository/views/repo_settings_advanced.py @@ -97,13 +97,14 @@ class RepoSettingsView(RepoAppView): repo_data = self.db_repo.get_api_data() RepoModel().delete(self.db_repo, forks=handle_forks) - repo = audit_logger.RepoWrap(repo_id=self.db_repo.repo_id, + repo = audit_logger.RepoWrap(repo_id=None, repo_name=self.db_repo.repo_name) audit_logger.store( - action='repo.delete', action_data={'repo_data': repo_data}, + action='repo.delete', + action_data={'repo_data': repo_data, 'source': 'web_action'}, user=self._rhodecode_user, repo=repo, commit=False) - ScmModel().mark_for_invalidation(self.db_repo_name) + ScmModel().mark_for_invalidation(self.db_repo_name, delete=True) h.flash( _('Deleted repository `%s`') % self.db_repo_name, category='success') diff --git a/rhodecode/public/js/rhodecode/routes.js b/rhodecode/public/js/rhodecode/routes.js --- a/rhodecode/public/js/rhodecode/routes.js +++ b/rhodecode/public/js/rhodecode/routes.js @@ -12,7 +12,6 @@ ******************************************************************************/ function registerRCRoutes() { // routes registration - pyroutes.register('home', '/', []); pyroutes.register('new_repo', '/_admin/create_repository', []); pyroutes.register('edit_user', '/_admin/users/%(user_id)s/edit', ['user_id']); pyroutes.register('edit_user_group_members', '/_admin/user_groups/%(user_group_id)s/edit/members', ['user_group_id']); @@ -92,6 +91,7 @@ function registerRCRoutes() { pyroutes.register('register', '/_admin/register', []); pyroutes.register('reset_password', '/_admin/password_reset', []); pyroutes.register('reset_password_confirmation', '/_admin/password_reset_confirmation', []); + pyroutes.register('home', '/', []); pyroutes.register('user_autocomplete_data', '/_users', []); pyroutes.register('user_group_autocomplete_data', '/_user_groups', []); pyroutes.register('repo_list_data', '/_repos', []);