Commit 92b1f15f authored by Michal Čihař's avatar Michal Čihař

Add decorator to test for multiple permissions

parent 17a61f75
# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2013 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <http://weblate.org/>
#
# 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/>.
#
from django.contrib.auth.decorators import user_passes_test
def any_permission_required(*args):
'''
A decorator which checks user has any of the given permissions.
permission required can not be used in its place as that takes only a
single permission.
'''
def test_func(user):
for perm in args:
if user.has_perm(perm):
return True
return False
return user_passes_test(test_func)
...@@ -23,9 +23,7 @@ from django.views.decorators.cache import cache_page ...@@ -23,9 +23,7 @@ from django.views.decorators.cache import cache_page
from weblate.trans import appsettings from weblate.trans import appsettings
from django.template import RequestContext from django.template import RequestContext
from django.http import HttpResponse from django.http import HttpResponse
from django.contrib.auth.decorators import ( from django.contrib.auth.decorators import permission_required
login_required, permission_required, user_passes_test
)
from django.db.models import Q from django.db.models import Q
from weblate.trans.models import ( from weblate.trans.models import (
...@@ -33,6 +31,7 @@ from weblate.trans.models import ( ...@@ -33,6 +31,7 @@ from weblate.trans.models import (
Dictionary Dictionary
) )
from weblate.trans.views.edit import parse_search_url from weblate.trans.views.edit import parse_search_url
from weblate.trans.decorators import any_permission_required
from whoosh.analysis import StandardAnalyzer, StemmingAnalyzer from whoosh.analysis import StandardAnalyzer, StemmingAnalyzer
import logging import logging
...@@ -140,7 +139,6 @@ def get_dictionary(request, unit_id): ...@@ -140,7 +139,6 @@ def get_dictionary(request, unit_id):
})) }))
@login_required
@permission_required('trans.ignore_check') @permission_required('trans.ignore_check')
def ignore_check(request, check_id): def ignore_check(request, check_id):
obj = get_object_or_404(Check, pk=int(check_id)) obj = get_object_or_404(Check, pk=int(check_id))
...@@ -155,7 +153,7 @@ def ignore_check(request, check_id): ...@@ -155,7 +153,7 @@ def ignore_check(request, check_id):
return HttpResponse('ok') return HttpResponse('ok')
@user_passes_test(lambda u: u.has_perm('trans.commit_translation') or u.has_perm('trans.update_translation')) @any_permission_required('trans.commit_translation', 'trans.update_translation')
def git_status_project(request, project): def git_status_project(request, project):
obj = get_object_or_404(Project, slug=project) obj = get_object_or_404(Project, slug=project)
obj.check_acl(request) obj.check_acl(request)
...@@ -165,7 +163,7 @@ def git_status_project(request, project): ...@@ -165,7 +163,7 @@ def git_status_project(request, project):
})) }))
@user_passes_test(lambda u: u.has_perm('trans.commit_translation') or u.has_perm('trans.update_translation')) @any_permission_required('trans.commit_translation', 'trans.update_translation')
def git_status_subproject(request, project, subproject): def git_status_subproject(request, project, subproject):
obj = get_object_or_404(SubProject, slug=subproject, project__slug=project) obj = get_object_or_404(SubProject, slug=subproject, project__slug=project)
obj.check_acl(request) obj.check_acl(request)
...@@ -175,7 +173,7 @@ def git_status_subproject(request, project, subproject): ...@@ -175,7 +173,7 @@ def git_status_subproject(request, project, subproject):
})) }))
@user_passes_test(lambda u: u.has_perm('trans.commit_translation') or u.has_perm('trans.update_translation')) @any_permission_required('trans.commit_translation', 'trans.update_translation')
def git_status_translation(request, project, subproject, lang): def git_status_translation(request, project, subproject, lang):
obj = get_object_or_404( obj = get_object_or_404(
Translation, Translation,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment