-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds django-rules based permissions for tagging app
Also: * Adds rules requirement and app settings to enable it * Adds mock to test requirements, so we can test system taxonomy rules * ADR: Clarifies that rules will be enforced in the views, not the model or APIs
- Loading branch information
1 parent
6c49368
commit 92f739c
Showing
12 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"""Django rules-based permissions for tagging""" | ||
|
||
import rules | ||
|
||
# Global staff are taxonomy admins. | ||
# (Superusers can already do anything) | ||
is_taxonomy_admin = rules.is_staff | ||
|
||
|
||
@rules.predicate | ||
def can_view_taxonomy(user, taxonomy=None): | ||
""" | ||
Anyone can view an enabled taxonomy, | ||
but only taxonomy admins can view a disabled taxonomy. | ||
""" | ||
return (taxonomy and taxonomy.enabled) or is_taxonomy_admin(user) | ||
|
||
|
||
@rules.predicate | ||
def can_change_taxonomy(user, taxonomy=None): | ||
""" | ||
Even taxonomy admins cannot change system taxonomies. | ||
""" | ||
return is_taxonomy_admin(user) and ( | ||
not taxonomy or not taxonomy or (taxonomy and not taxonomy.system_defined) | ||
) | ||
|
||
|
||
@rules.predicate | ||
def can_change_taxonomy_tag(user, tag=None): | ||
""" | ||
Even taxonomy admins cannot add tags to system taxonomies (their tags are system-defined), or free-text taxonomies | ||
(these don't have predefined tags). | ||
""" | ||
return is_taxonomy_admin(user) and ( | ||
not tag | ||
or not tag.taxonomy | ||
or ( | ||
tag.taxonomy | ||
and not tag.taxonomy.allow_free_text | ||
and not tag.taxonomy.system_defined | ||
) | ||
) | ||
|
||
|
||
@rules.predicate | ||
def can_change_object_tag(user, object_tag=None): | ||
""" | ||
Taxonomy admins can create or modify object tags on enabled taxonomies. | ||
""" | ||
return is_taxonomy_admin(user) and ( | ||
not object_tag | ||
or not object_tag.taxonomy | ||
or (object_tag.taxonomy and object_tag.taxonomy.enabled) | ||
) | ||
|
||
|
||
# Taxonomy | ||
rules.add_perm("oel_tagging.add_taxonomy", can_change_taxonomy) | ||
rules.add_perm("oel_tagging.change_taxonomy", can_change_taxonomy) | ||
rules.add_perm("oel_tagging.delete_taxonomy", can_change_taxonomy) | ||
rules.add_perm("oel_tagging.view_taxonomy", can_view_taxonomy) | ||
|
||
# Tag | ||
rules.add_perm("oel_tagging.add_tag", can_change_taxonomy_tag) | ||
rules.add_perm("oel_tagging.change_tag", can_change_taxonomy_tag) | ||
rules.add_perm("oel_tagging.delete_tag", is_taxonomy_admin) | ||
rules.add_perm("oel_tagging.view_tag", rules.always_allow) | ||
|
||
# ObjectTag | ||
rules.add_perm("oel_tagging.add_object_tag", can_change_object_tag) | ||
rules.add_perm("oel_tagging.change_object_tag", can_change_object_tag) | ||
rules.add_perm("oel_tagging.delete_object_tag", is_taxonomy_admin) | ||
rules.add_perm("oel_tagging.view_object_tag", rules.always_allow) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.