Skip to content

Commit c709f1d

Browse files
Add is_anonymous, is_active, and is_superuser as built-in user attribute permissions (#215)
1 parent 93e49e2 commit c709f1d

File tree

6 files changed

+40
-2
lines changed

6 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
1818

1919
## [Unreleased]
2020

21+
### Added
22+
23+
- Added `is_anonymous`, `is_active`, and `is_superuser` as built-in user attribute permission checks, joining the existing `is_authenticated` and `is_staff`.
24+
2125
## [0.14.0]
2226

2327
### Added

docs/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Permissions are evaluated in order. **All** must pass (AND logic).
9797

9898
| Permission type | How it's checked |
9999
|---|---|
100-
| `"is_authenticated"`, `"is_staff"`, `"is_superuser"` | Read as a boolean attribute on `request.user`. |
100+
| `"is_anonymous"`, `"is_authenticated"`, `"is_active"`, `"is_staff"`, `"is_superuser"` | Read as a boolean attribute on `request.user`. |
101101
| `"app.codename"` (any other string) | Checked via `request.user.has_perm()`. |
102102
| Callable | Called with `request`; must return `bool`. |
103103

docs/usage.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ NavItem(title="Dashboard", url="/dashboard/", permissions=["is_authenticated"])
1515
Use a string matching a boolean attribute on `request.user`:
1616

1717
```python
18+
NavItem(title="Login", url="/login/", permissions=["is_anonymous"])
1819
NavItem(title="Dashboard", url="/dashboard/", permissions=["is_authenticated"])
20+
NavItem(title="Dashboard", url="/dashboard/", permissions=["is_active"])
1921
NavItem(title="Staff Area", url="/staff/", permissions=["is_staff"])
2022
```
2123

example/navigation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ class PermissionsNav(Nav):
5757
template_name = "navs/basic.html"
5858
items = [
5959
NavItem(title="Everyone can see this link", url="#"),
60+
NavItem(
61+
title="You are not logged in",
62+
url="#",
63+
permissions=["is_anonymous"],
64+
),
6065
NavItem(
6166
title="You are authenticated",
6267
url="#",

src/django_simple_nav/nav.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727

2828
logger = logging.getLogger(__name__)
2929

30+
USER_ATTRIBUTE_PERMISSIONS = frozenset({
31+
"is_anonymous",
32+
"is_authenticated",
33+
"is_active",
34+
"is_staff",
35+
"is_superuser",
36+
})
37+
3038

3139
class NavItemContext(dict):
3240
"""A dict subclass that can render itself as HTML in templates.
@@ -272,7 +280,7 @@ def check_permissions(self, request: HttpRequest) -> bool:
272280
break
273281
elif callable(perm):
274282
has_perm = perm(request)
275-
elif perm in ["is_authenticated", "is_staff"]:
283+
elif perm in USER_ATTRIBUTE_PERMISSIONS:
276284
has_perm = getattr(user, perm, False)
277285
else:
278286
has_perm = user.has_perm(perm)

tests/test_navitem.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,14 @@ def test_get_items(req):
240240
"permissions,expected",
241241
[
242242
([], True),
243+
(["is_anonymous"], True),
243244
(["is_authenticated"], False),
245+
(["is_active"], False),
244246
(["is_staff"], False),
245247
(["is_superuser"], False),
246248
(["is_authenticated", "is_staff"], False),
247249
(["is_authenticated", "is_superuser"], False),
250+
(["is_anonymous", "is_authenticated"], False),
248251
],
249252
)
250253
def test_check_permissions_anonymous(permissions, expected, req):
@@ -259,7 +262,9 @@ def test_check_permissions_anonymous(permissions, expected, req):
259262
"permissions,expected",
260263
[
261264
([], True),
265+
(["is_anonymous"], False),
262266
(["is_authenticated"], True),
267+
(["is_active"], True),
263268
(["is_staff"], False),
264269
(["is_superuser"], False),
265270
(["is_authenticated", "is_staff"], False),
@@ -278,7 +283,9 @@ def test_check_permissions_is_authenticated(permissions, expected, req):
278283
"permissions,expected",
279284
[
280285
([], True),
286+
(["is_anonymous"], False),
281287
(["is_authenticated"], True),
288+
(["is_active"], True),
282289
(["is_staff"], True),
283290
(["is_superuser"], False),
284291
(["is_authenticated", "is_staff"], True),
@@ -297,7 +304,9 @@ def test_check_permissions_is_staff(permissions, expected, req):
297304
"permissions,expected",
298305
[
299306
([], True),
307+
(["is_anonymous"], True),
300308
(["is_authenticated"], True),
309+
(["is_active"], True),
301310
(["is_staff"], True),
302311
(["is_superuser"], True),
303312
(["is_authenticated", "is_staff"], True),
@@ -316,7 +325,9 @@ def test_check_permissions_is_superuser(permissions, expected, req):
316325
"permissions,expected",
317326
[
318327
([], True),
328+
(["is_anonymous"], False),
319329
(["is_authenticated"], False),
330+
(["is_active"], False),
320331
(["is_staff"], False),
321332
(["is_superuser"], False),
322333
(["is_authenticated", "is_staff"], False),
@@ -333,7 +344,9 @@ def test_check_permissions_no_request_user(permissions, expected, req):
333344
"permissions,expected",
334345
[
335346
([], True),
347+
(["is_anonymous"], True),
336348
(["is_authenticated"], True),
349+
(["is_active"], True),
337350
(["is_staff"], True),
338351
(["is_superuser"], True),
339352
(["is_authenticated", "is_staff"], True),
@@ -381,7 +394,9 @@ def check_is_authenticated(request):
381394
"permissions,expected",
382395
[
383396
([], True),
397+
(["is_anonymous"], False),
384398
(["is_authenticated"], True),
399+
(["is_active"], True),
385400
(["is_staff"], False),
386401
(["is_superuser"], False),
387402
(["is_authenticated", "is_staff"], False),
@@ -414,7 +429,9 @@ def test_check_permissions_auth_permission_is_authenticated(permissions, expecte
414429
"permissions,expected",
415430
[
416431
([], True),
432+
(["is_anonymous"], False),
417433
(["is_authenticated"], True),
434+
(["is_active"], True),
418435
(["is_staff"], True),
419436
(["is_superuser"], False),
420437
(["is_authenticated", "is_staff"], True),
@@ -447,7 +464,9 @@ def test_check_permissions_auth_permission_is_staff(permissions, expected, req):
447464
"permissions,expected",
448465
[
449466
([], True),
467+
(["is_anonymous"], True),
450468
(["is_authenticated"], True),
469+
(["is_active"], True),
451470
(["is_staff"], True),
452471
(["is_superuser"], True),
453472
(["is_authenticated", "is_staff"], True),

0 commit comments

Comments
 (0)