diff --git a/django/contrib/auth/decorators.py b/django/contrib/auth/decorators.py
index 00a6bce..7840c67 100644
a
|
b
|
import urlparse
|
2 | 2 | from functools import wraps |
3 | 3 | from django.conf import settings |
4 | 4 | from django.contrib.auth import REDIRECT_FIELD_NAME |
| 5 | from django.core.exceptions import PermissionDenied |
5 | 6 | from django.utils.decorators import available_attrs |
6 | 7 | |
7 | 8 | |
… |
… |
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login
|
50 | 51 | def permission_required(perm, login_url=None): |
51 | 52 | """ |
52 | 53 | Decorator for views that checks whether a user has a particular permission |
53 | | enabled, redirecting to the log-in page if necessary. |
| 54 | enabled, redirecting to the log-in page if user is not authenticated. |
| 55 | If user is authenticated and does not have the permission, raise |
| 56 | PermissionDenied. |
54 | 57 | """ |
55 | | return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url) |
| 58 | # return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url) |
| 59 | def check_perms(user): |
| 60 | if user.is_anonymous(): |
| 61 | return False |
| 62 | if user.has_perm(perm): |
| 63 | return True |
| 64 | raise PermissionDenied |
| 65 | return user_passes_test(check_perms, login_url=login_url) |
diff --git a/docs/topics/auth.txt b/docs/topics/auth.txt
index 12d538f..f092d30 100644
a
|
b
|
The permission_required decorator
|
1156 | 1156 | ``"<app label>.<permission codename>"`` (i.e. ``polls.can_vote`` for a |
1157 | 1157 | permission on a model in the ``polls`` application). |
1158 | 1158 | |
| 1159 | If the user is *not* logged in, he will be redirected to the ``login_url``. |
| 1160 | If the user *is* logged in but doesn't have permission, a 403 error response |
| 1161 | (forbidden) will be returned. See :doc:`/topics/http/views/`. |
| 1162 | |
1159 | 1163 | Note that :func:`~django.contrib.auth.decorators.permission_required()` |
1160 | 1164 | also takes an optional ``login_url`` parameter. Example:: |
1161 | 1165 | |
diff --git a/tests/modeltests/test_client/models.py b/tests/modeltests/test_client/models.py
index 16bdd2d..61f2101 100644
a
|
b
|
class ClientTest(TestCase):
|
364 | 364 | login = self.client.login(username='testclient', password='password') |
365 | 365 | self.assertTrue(login, 'Could not log in') |
366 | 366 | |
367 | | # Log in with wrong permissions. Should result in 302. |
| 367 | # Log in with wrong permissions. Should result in 403. |
368 | 368 | response = self.client.get('/test_client/permission_protected_view/') |
369 | | self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_view/') |
| 369 | self.assertEqual(response.status_code, 403) |
370 | 370 | |
371 | 371 | # TODO: Log in with right permissions and request the page again |
372 | 372 | |
… |
… |
class ClientTest(TestCase):
|
381 | 381 | login = self.client.login(username='testclient', password='password') |
382 | 382 | self.assertTrue(login, 'Could not log in') |
383 | 383 | |
384 | | # Log in with wrong permissions. Should result in 302. |
| 384 | # Log in with wrong permissions. Should result in 403. |
385 | 385 | response = self.client.get('/test_client/permission_protected_method_view/') |
386 | | self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_method_view/') |
| 386 | self.assertEqual(response.status_code, 403) |
387 | 387 | |
388 | 388 | # TODO: Log in with right permissions and request the page again |
389 | 389 | |
diff --git a/tests/regressiontests/comment_tests/tests/moderation_view_tests.py b/tests/regressiontests/comment_tests/tests/moderation_view_tests.py
index c9be06a..3d0c138 100644
a
|
b
|
class DeleteViewTests(CommentTestCase):
|
80 | 80 | pk = comments[0].pk |
81 | 81 | self.client.login(username="normaluser", password="normaluser") |
82 | 82 | response = self.client.get("/delete/%d/" % pk) |
83 | | self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/delete/%d/" % pk) |
| 83 | self.assertEqual(response.status_code, 403) |
84 | 84 | |
85 | 85 | makeModerator("normaluser") |
86 | 86 | response = self.client.get("/delete/%d/" % pk) |
… |
… |
class ApproveViewTests(CommentTestCase):
|
124 | 124 | pk = comments[0].pk |
125 | 125 | self.client.login(username="normaluser", password="normaluser") |
126 | 126 | response = self.client.get("/approve/%d/" % pk) |
127 | | self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/approve/%d/" % pk) |
| 127 | self.assertEqual(response.status_code, 403) |
128 | 128 | |
129 | 129 | makeModerator("normaluser") |
130 | 130 | response = self.client.get("/approve/%d/" % pk) |