Mercurial > kallithea
changeset 8746:259213d96dca
lib: move action_logger to new userlog model
author | Mads Kiilerich <mads@kiilerich.com> |
---|---|
date | Mon, 02 Nov 2020 13:57:26 +0100 |
parents | aa067dbcdc82 |
children | 09826e98b064 |
files | kallithea/controllers/admin/repos.py kallithea/controllers/admin/user_groups.py kallithea/controllers/admin/users.py kallithea/controllers/api/api.py kallithea/controllers/changeset.py kallithea/controllers/files.py kallithea/lib/celerylib/tasks.py kallithea/lib/hooks.py kallithea/lib/utils.py kallithea/model/scm.py kallithea/model/userlog.py |
diffstat | 11 files changed, 119 insertions(+), 89 deletions(-) [+] |
line wrap: on
line diff
--- a/kallithea/controllers/admin/repos.py Wed Nov 04 21:00:44 2020 +0100 +++ b/kallithea/controllers/admin/repos.py Mon Nov 02 13:57:26 2020 +0100 @@ -41,11 +41,10 @@ from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired, NotAnonymous from kallithea.lib.base import BaseRepoController, jsonify, render from kallithea.lib.exceptions import AttachedForksError -from kallithea.lib.utils import action_logger from kallithea.lib.utils2 import safe_int from kallithea.lib.vcs import RepositoryError from kallithea.lib.webutils import url -from kallithea.model import db, meta +from kallithea.model import db, meta, userlog from kallithea.model.forms import RepoFieldForm, RepoForm, RepoPermsForm from kallithea.model.repo import RepoModel from kallithea.model.scm import AvailableRepoGroupChoices, RepoList, ScmModel @@ -222,7 +221,7 @@ webutils.flash(_('Repository %s updated successfully') % repo_name, category='success') changed_name = repo.repo_name - action_logger(request.authuser, 'admin_updated_repo', + userlog.action_logger(request.authuser, 'admin_updated_repo', changed_name, request.ip_addr) meta.Session().commit() except formencode.Invalid as errors: @@ -261,7 +260,7 @@ handle_forks = 'delete' webutils.flash(_('Deleted %s forks') % _forks, category='success') repo_model.delete(repo, forks=handle_forks) - action_logger(request.authuser, 'admin_deleted_repo', + userlog.action_logger(request.authuser, 'admin_deleted_repo', repo_name, request.ip_addr) ScmModel().mark_for_invalidation(repo_name) webutils.flash(_('Deleted repository %s') % repo_name, category='success')
--- a/kallithea/controllers/admin/user_groups.py Wed Nov 04 21:00:44 2020 +0100 +++ b/kallithea/controllers/admin/user_groups.py Mon Nov 02 13:57:26 2020 +0100 @@ -42,10 +42,9 @@ from kallithea.lib.auth import HasPermissionAnyDecorator, HasUserGroupPermissionLevelDecorator, LoginRequired from kallithea.lib.base import BaseController, render from kallithea.lib.exceptions import RepoGroupAssignmentError, UserGroupsAssignedException -from kallithea.lib.utils import action_logger from kallithea.lib.utils2 import safe_int, safe_str from kallithea.lib.webutils import url -from kallithea.model import db, meta +from kallithea.model import db, meta, userlog from kallithea.model.forms import CustomDefaultPermissionsForm, UserGroupForm, UserGroupPermsForm from kallithea.model.scm import UserGroupList from kallithea.model.user_group import UserGroupModel @@ -127,7 +126,7 @@ active=form_result['users_group_active']) gr = form_result['users_group_name'] - action_logger(request.authuser, + userlog.action_logger(request.authuser, 'admin_created_users_group:%s' % gr, None, request.ip_addr) webutils.flash(webutils.HTML(_('Created user group %s')) % webutils.link_to(gr, url('edit_users_group', id=ug.users_group_id)), @@ -168,7 +167,7 @@ form_result = users_group_form.to_python(request.POST) UserGroupModel().update(c.user_group, form_result) gr = form_result['users_group_name'] - action_logger(request.authuser, + userlog.action_logger(request.authuser, 'admin_updated_users_group:%s' % gr, None, request.ip_addr) webutils.flash(_('Updated user group %s') % gr, category='success')
--- a/kallithea/controllers/admin/users.py Wed Nov 04 21:00:44 2020 +0100 +++ b/kallithea/controllers/admin/users.py Mon Nov 02 13:57:26 2020 +0100 @@ -42,10 +42,9 @@ from kallithea.lib.auth import AuthUser, HasPermissionAnyDecorator, LoginRequired from kallithea.lib.base import BaseController, IfSshEnabled, render from kallithea.lib.exceptions import DefaultUserException, UserCreationError, UserOwnsReposException -from kallithea.lib.utils import action_logger from kallithea.lib.utils2 import datetime_to_time, fmt_date, generate_api_key, safe_int from kallithea.lib.webutils import url -from kallithea.model import db, meta +from kallithea.model import db, meta, userlog from kallithea.model.api_key import ApiKeyModel from kallithea.model.forms import CustomDefaultPermissionsForm, UserForm from kallithea.model.ssh_key import SshKeyModel, SshKeyModelException @@ -114,7 +113,7 @@ try: form_result = user_form.to_python(dict(request.POST)) user = user_model.create(form_result) - action_logger(request.authuser, 'admin_created_user:%s' % user.username, + userlog.action_logger(request.authuser, 'admin_created_user:%s' % user.username, None, request.ip_addr) webutils.flash(_('Created user %s') % user.username, category='success') @@ -153,7 +152,7 @@ user_model.update(id, form_result, skip_attrs=skip_attrs) usr = form_result['username'] - action_logger(request.authuser, 'admin_updated_user:%s' % usr, + userlog.action_logger(request.authuser, 'admin_updated_user:%s' % usr, None, request.ip_addr) webutils.flash(_('User updated successfully'), category='success') meta.Session().commit()
--- a/kallithea/controllers/api/api.py Wed Nov 04 21:00:44 2020 +0100 +++ b/kallithea/controllers/api/api.py Mon Nov 02 13:57:26 2020 +0100 @@ -35,10 +35,10 @@ from kallithea.lib.auth import (AuthUser, HasPermissionAny, HasPermissionAnyDecorator, HasRepoGroupPermissionLevel, HasRepoPermissionLevel, HasUserGroupPermissionLevel) from kallithea.lib.exceptions import DefaultUserException, UserGroupsAssignedException -from kallithea.lib.utils import action_logger, repo2db_mapper +from kallithea.lib.utils import repo2db_mapper from kallithea.lib.vcs.backends.base import EmptyChangeset from kallithea.lib.vcs.exceptions import EmptyRepositoryError -from kallithea.model import db, meta +from kallithea.model import db, meta, userlog from kallithea.model.changeset_status import ChangesetStatusModel from kallithea.model.comment import ChangesetCommentsModel from kallithea.model.gist import GistModel @@ -2377,7 +2377,7 @@ status_change=db.ChangesetStatus.get_status_lbl(status), closing_pr=close_pr ) - action_logger(apiuser, + userlog.action_logger(apiuser, 'user_commented_pull_request:%s' % pull_request_id, pull_request.org_repo, request.ip_addr) if status: @@ -2390,7 +2390,7 @@ ) if close_pr: PullRequestModel().close_pull_request(pull_request_id) - action_logger(apiuser, + userlog.action_logger(apiuser, 'user_closed_pull_request:%s' % pull_request_id, pull_request.org_repo, request.ip_addr) meta.Session().commit()
--- a/kallithea/controllers/changeset.py Wed Nov 04 21:00:44 2020 +0100 +++ b/kallithea/controllers/changeset.py Mon Nov 02 13:57:26 2020 +0100 @@ -40,11 +40,10 @@ from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired from kallithea.lib.base import BaseRepoController, jsonify, render from kallithea.lib.graphmod import graph_data -from kallithea.lib.utils import action_logger from kallithea.lib.utils2 import ascii_str, safe_str from kallithea.lib.vcs.backends.base import EmptyChangeset from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError, EmptyRepositoryError, RepositoryError -from kallithea.model import db, meta +from kallithea.model import db, meta, userlog from kallithea.model.changeset_status import ChangesetStatusModel from kallithea.model.comment import ChangesetCommentsModel from kallithea.model.pull_request import PullRequestModel @@ -132,11 +131,11 @@ action = 'user_commented_pull_request:%s' % pull_request_id else: action = 'user_commented_revision:%s' % revision - action_logger(request.authuser, action, c.db_repo, request.ip_addr) + userlog.action_logger(request.authuser, action, c.db_repo, request.ip_addr) if pull_request and close_pr: PullRequestModel().close_pull_request(pull_request_id) - action_logger(request.authuser, + userlog.action_logger(request.authuser, 'user_closed_pull_request:%s' % pull_request_id, c.db_repo, request.ip_addr)
--- a/kallithea/controllers/files.py Wed Nov 04 21:00:44 2020 +0100 +++ b/kallithea/controllers/files.py Mon Nov 02 13:57:26 2020 +0100 @@ -44,7 +44,6 @@ from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired from kallithea.lib.base import BaseRepoController, jsonify, render from kallithea.lib.exceptions import NonRelativePathError -from kallithea.lib.utils import action_logger from kallithea.lib.utils2 import asbool, convert_line_endings, detect_mode, safe_str from kallithea.lib.vcs.backends.base import EmptyChangeset from kallithea.lib.vcs.conf import settings @@ -53,6 +52,7 @@ from kallithea.lib.vcs.nodes import FileNode from kallithea.lib.vcs.utils import author_email from kallithea.lib.webutils import url +from kallithea.model import userlog from kallithea.model.repo import RepoModel from kallithea.model.scm import ScmModel @@ -546,7 +546,7 @@ log.debug('Destroying temp archive %s', archive_path) os.remove(archive_path) - action_logger(user=request.authuser, + userlog.action_logger(user=request.authuser, action='user_downloaded_archive:%s' % (archive_name), repo=repo_name, ipaddr=request.ip_addr, commit=True)
--- a/kallithea/lib/celerylib/tasks.py Wed Nov 04 21:00:44 2020 +0100 +++ b/kallithea/lib/celerylib/tasks.py Mon Nov 02 13:57:26 2020 +0100 @@ -44,10 +44,9 @@ import kallithea.lib.helpers as h from kallithea.lib import celerylib, conf, ext_json, hooks from kallithea.lib.indexers.daemon import WhooshIndexingDaemon -from kallithea.lib.utils import action_logger from kallithea.lib.utils2 import asbool, ascii_bytes from kallithea.lib.vcs.utils import author_email -from kallithea.model import db +from kallithea.model import db, userlog from kallithea.model.repo import RepoModel @@ -394,7 +393,7 @@ state=state ) - action_logger(cur_user, 'user_created_repo', + userlog.action_logger(cur_user, 'user_created_repo', form_data['repo_name_full'], '') DBS.commit() @@ -468,7 +467,7 @@ fork_of=fork_of, copy_fork_permissions=copy_fork_permissions ) - action_logger(cur_user, 'user_forked_repo:%s' % repo_name_full, + userlog.action_logger(cur_user, 'user_forked_repo:%s' % repo_name_full, fork_of.repo_name, '') DBS.commit()
--- a/kallithea/lib/hooks.py Wed Nov 04 21:00:44 2020 +0100 +++ b/kallithea/lib/hooks.py Mon Nov 02 13:57:26 2020 +0100 @@ -30,8 +30,8 @@ import kallithea from kallithea.lib.exceptions import UserCreationError -from kallithea.lib.utils import action_logger from kallithea.lib.utils2 import get_hook_environment +from kallithea.model import userlog def log_pull_action(): @@ -42,7 +42,7 @@ ex = get_hook_environment() action = 'pull' - action_logger(ex.username, action, ex.repository, ex.ip, commit=True) + userlog.action_logger(ex.username, action, ex.repository, ex.ip, commit=True) # extension hook call callback = getattr(kallithea.EXTENSIONS, 'PULL_HOOK', None) if callable(callback): @@ -62,7 +62,7 @@ ex = get_hook_environment() action = '%s:%s' % (ex.action, ','.join(revs)) - action_logger(ex.username, action, ex.repository, ex.ip, commit=True) + userlog.action_logger(ex.username, action, ex.repository, ex.ip, commit=True) from kallithea.model.scm import ScmModel ScmModel().mark_for_invalidation(ex.repository)
--- a/kallithea/lib/utils.py Wed Nov 04 21:00:44 2020 +0100 +++ b/kallithea/lib/utils.py Mon Nov 02 13:57:26 2020 +0100 @@ -25,7 +25,6 @@ :license: GPLv3, see LICENSE.md for more details. """ -import datetime import logging import os import re @@ -38,7 +37,7 @@ import kallithea.lib.conf from kallithea.lib.exceptions import InvalidCloneUriException -from kallithea.lib.utils2 import ascii_bytes, aslist, extract_mentioned_usernames, get_current_authuser, safe_bytes, safe_str +from kallithea.lib.utils2 import ascii_bytes, aslist, extract_mentioned_usernames, safe_bytes, safe_str from kallithea.lib.vcs.backends.git.repository import GitRepository from kallithea.lib.vcs.backends.hg.repository import MercurialRepository from kallithea.lib.vcs.conf import settings @@ -108,60 +107,6 @@ return path -def action_logger(user, action, repo, ipaddr='', commit=False): - """ - Action logger for various actions made by users - - :param user: user that made this action, can be a unique username string or - object containing user_id attribute - :param action: action to log, should be on of predefined unique actions for - easy translations - :param repo: string name of repository or object containing repo_id, - that action was made on - :param ipaddr: optional IP address from what the action was made - - """ - - # if we don't get explicit IP address try to get one from registered user - # in tmpl context var - if not ipaddr: - ipaddr = getattr(get_current_authuser(), 'ip_addr', '') - - if getattr(user, 'user_id', None): - user_obj = db.User.get(user.user_id) - elif isinstance(user, str): - user_obj = db.User.get_by_username(user) - else: - raise Exception('You have to provide a user object or a username') - - if getattr(repo, 'repo_id', None): - repo_obj = db.Repository.get(repo.repo_id) - repo_name = repo_obj.repo_name - elif isinstance(repo, str): - repo_name = repo.lstrip('/') - repo_obj = db.Repository.get_by_repo_name(repo_name) - else: - repo_obj = None - repo_name = '' - - user_log = db.UserLog() - user_log.user_id = user_obj.user_id - user_log.username = user_obj.username - user_log.action = action - - user_log.repository = repo_obj - user_log.repository_name = repo_name - - user_log.action_date = datetime.datetime.now() - user_log.user_ip = ipaddr - meta.Session().add(user_log) - - log.info('Logging action:%s on %s by user:%s ip:%s', - action, repo, user_obj, ipaddr) - if commit: - meta.Session().commit() - - def get_filesystem_repos(path): """ Scans given path for repos and return (name,(type,path)) tuple
--- a/kallithea/model/scm.py Wed Nov 04 21:00:44 2020 +0100 +++ b/kallithea/model/scm.py Mon Nov 02 13:57:26 2020 +0100 @@ -40,14 +40,14 @@ from kallithea.lib import hooks from kallithea.lib.auth import HasPermissionAny, HasRepoGroupPermissionLevel, HasRepoPermissionLevel, HasUserGroupPermissionLevel from kallithea.lib.exceptions import IMCCommitError, NonRelativePathError -from kallithea.lib.utils import action_logger, get_filesystem_repos, make_ui +from kallithea.lib.utils import get_filesystem_repos, make_ui from kallithea.lib.utils2 import safe_bytes, safe_str, set_hook_environment, umask from kallithea.lib.vcs import get_repo from kallithea.lib.vcs.backends.base import EmptyChangeset from kallithea.lib.vcs.exceptions import RepositoryError, VCSError from kallithea.lib.vcs.nodes import FileNode from kallithea.lib.vcs.utils.lazy import LazyProperty -from kallithea.model import db, meta +from kallithea.model import db, meta, userlog log = logging.getLogger(__name__) @@ -225,7 +225,7 @@ if f is not None: try: meta.Session().delete(f) - action_logger(UserTemp(user_id), + userlog.action_logger(UserTemp(user_id), 'stopped_following_repo', RepoTemp(follow_repo_id)) return @@ -239,7 +239,7 @@ f.follows_repository_id = follow_repo_id meta.Session().add(f) - action_logger(UserTemp(user_id), + userlog.action_logger(UserTemp(user_id), 'started_following_repo', RepoTemp(follow_repo_id)) except Exception:
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/kallithea/model/userlog.py Mon Nov 02 13:57:26 2020 +0100 @@ -0,0 +1,90 @@ +# -*- coding: utf-8 -*- +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +""" +kallithea.model.db +~~~~~~~~~~~~~~~~~~ + +Database Models for Kallithea + +This file was forked by the Kallithea project in July 2014. +Original author and date, and relevant copyright and licensing information is below: +:created_on: Apr 08, 2010 +:author: marcink +:copyright: (c) 2013 RhodeCode GmbH, and others. +:license: GPLv3, see LICENSE.md for more details. +""" + + +import datetime +import logging + +from kallithea.lib.utils2 import get_current_authuser +from kallithea.model import db, meta + + +log = logging.getLogger(__name__) + + +def action_logger(user, action, repo, ipaddr='', commit=False): + """ + Action logger for various actions made by users + + :param user: user that made this action, can be a unique username string or + object containing user_id attribute + :param action: action to log, should be on of predefined unique actions for + easy translations + :param repo: string name of repository or object containing repo_id, + that action was made on + :param ipaddr: optional IP address from what the action was made + + """ + + # if we don't get explicit IP address try to get one from registered user + # in tmpl context var + if not ipaddr: + ipaddr = getattr(get_current_authuser(), 'ip_addr', '') + + if getattr(user, 'user_id', None): + user_obj = db.User.get(user.user_id) + elif isinstance(user, str): + user_obj = db.User.get_by_username(user) + else: + raise Exception('You have to provide a user object or a username') + + if getattr(repo, 'repo_id', None): + repo_obj = db.Repository.get(repo.repo_id) + repo_name = repo_obj.repo_name + elif isinstance(repo, str): + repo_name = repo.lstrip('/') + repo_obj = db.Repository.get_by_repo_name(repo_name) + else: + repo_obj = None + repo_name = '' + + user_log = db.UserLog() + user_log.user_id = user_obj.user_id + user_log.username = user_obj.username + user_log.action = action + + user_log.repository = repo_obj + user_log.repository_name = repo_name + + user_log.action_date = datetime.datetime.now() + user_log.user_ip = ipaddr + meta.Session().add(user_log) + + log.info('Logging action:%s on %s by user:%s ip:%s', + action, repo, user_obj, ipaddr) + if commit: + meta.Session().commit()