Skip to content

Commit 3a5a5f0

Browse files
committed
Added head revision of django-permissions and django-workflows. See ietf-tools#535
- Legacy-Id: 2598
1 parent 8f02f16 commit 3a5a5f0

25 files changed

Lines changed: 3448 additions & 0 deletions

File tree

ietf/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@
118118
'django.contrib.admin',
119119
'django.contrib.humanize',
120120
'south',
121+
'workflows',
122+
'permissions',
121123
'ietf.announcements',
122124
'ietf.idindex',
123125
'ietf.idtracker',

permissions/__init__.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import permissions.utils
2+
3+
class PermissionBase(object):
4+
"""Mix-in class for permissions.
5+
"""
6+
def grant_permission(self, role, permission):
7+
"""Grants passed permission to passed role. Returns True if the
8+
permission was able to be added, otherwise False.
9+
10+
**Parameters:**
11+
12+
role
13+
The role for which the permission should be granted.
14+
15+
permission
16+
The permission which should be granted. Either a permission
17+
object or the codename of a permission.
18+
"""
19+
return permissions.utils.grant_permission(self, role, permission)
20+
21+
def remove_permission(self, role, permission):
22+
"""Removes passed permission from passed role. Returns True if the
23+
permission has been removed.
24+
25+
**Parameters:**
26+
27+
role
28+
The role for which a permission should be removed.
29+
30+
permission
31+
The permission which should be removed. Either a permission object
32+
or the codename of a permission.
33+
"""
34+
return permissions.utils.remove_permission(self, role, permission)
35+
36+
def has_permission(self, user, permission, roles=[]):
37+
"""Returns True if the passed user has passed permission for this
38+
instance. Otherwise False.
39+
40+
**Parameters:**
41+
42+
permission
43+
The permission's codename which should be checked. Must be a
44+
string with a valid codename.
45+
46+
user
47+
The user for which the permission should be checked.
48+
49+
roles
50+
If passed, these roles will be assigned to the user temporarily
51+
before the permissions are checked.
52+
"""
53+
return permissions.utils.has_permission(self, user, permission, roles)
54+
55+
def check_permission(self, user, permission, roles=[]):
56+
"""Raise Unauthorized if the the passed user hasn't passed permission
57+
for this instance.
58+
59+
**Parameters:**
60+
61+
permission
62+
The permission's codename which should be checked. Must be a
63+
string with a valid codename.
64+
65+
user
66+
The user for which the permission should be checked.
67+
68+
roles
69+
If passed, these roles will be assigned to the user temporarily
70+
before the permissions are checked.
71+
"""
72+
if not self.has_permission(user, permission, roles):
73+
raise Unauthorized("User %s doesn't have permission %s for object %s" % (user, permission, obj.slug))
74+
75+
def add_inheritance_block(self, permission):
76+
"""Adds an inheritance block for the passed permission.
77+
78+
**Parameters:**
79+
80+
permission
81+
The permission for which an inheritance block should be added.
82+
Either a permission object or the codename of a permission.
83+
"""
84+
return permissions.utils.add_inheritance_block(self, permission)
85+
86+
def remove_inheritance_block(self, permission):
87+
"""Removes a inheritance block for the passed permission.
88+
89+
**Parameters:**
90+
91+
permission
92+
The permission for which an inheritance block should be removed.
93+
Either a permission object or the codename of a permission.
94+
"""
95+
return permissions.utils.remove_inheritance_block(self, permission)
96+
97+
def is_inherited(self, codename):
98+
"""Returns True if the passed permission is inherited.
99+
100+
**Parameters:**
101+
102+
codename
103+
The permission which should be checked. Must be the codename of
104+
the permission.
105+
"""
106+
return permissions.utils.is_inherited(self, codename)
107+
108+
def add_role(self, principal, role):
109+
"""Adds a local role for the principal.
110+
111+
**Parameters:**
112+
113+
principal
114+
The principal (user or group) which gets the role.
115+
116+
role
117+
The role which is assigned.
118+
"""
119+
return permissions.utils.add_local_role(self, principal, role)
120+
121+
def get_roles(self, principal):
122+
"""Returns *direct* local roles for passed principal (user or group).
123+
"""
124+
return permissions.utils.get_local_roles(self, principal)
125+
126+
def remove_role(self, principal, role):
127+
"""Adds a local role for the principal to the object.
128+
129+
**Parameters:**
130+
131+
principal
132+
The principal (user or group) from which the role is removed.
133+
134+
role
135+
The role which is removed.
136+
"""
137+
return permissions.utils.remove_local_role(self, principal, role)
138+
139+
def remove_roles(self, principal):
140+
"""Removes all local roles for the passed principal from the object.
141+
142+
**Parameters:**
143+
144+
principal
145+
The principal (user or group) from which all local roles are
146+
removed.
147+
"""
148+
return permissions.utils.remove_local_roles(self, principal)

permissions/admin.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from django.contrib import admin
2+
3+
from permissions.models import ObjectPermission
4+
admin.site.register(ObjectPermission)
5+
6+
from permissions.models import Permission
7+
admin.site.register(Permission)
8+
9+
from permissions.models import Role
10+
admin.site.register(Role)
11+
12+
from permissions.models import PrincipalRoleRelation
13+
admin.site.register(PrincipalRoleRelation)

permissions/backend.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# permissions imports
2+
import permissions.utils
3+
4+
class ObjectPermissionsBackend(object):
5+
"""Django backend for object permissions. Needs Django 1.2.
6+
7+
8+
Use it together with the default ModelBackend like so::
9+
10+
AUTHENTICATION_BACKENDS = (
11+
'django.contrib.auth.backends.ModelBackend',
12+
'permissions.backend.ObjectPermissionsBackend',
13+
)
14+
15+
Then you can use it like:
16+
17+
user.has_perm("view", your_object)
18+
19+
"""
20+
supports_object_permissions = True
21+
supports_anonymous_user = True
22+
23+
def authenticate(self, username, password):
24+
return None
25+
26+
def has_perm(self, user_obj, perm, obj=None):
27+
"""Checks whether the passed user has passed permission for passed
28+
object (obj).
29+
30+
This should be the primary method to check wether a user has a certain
31+
permission.
32+
33+
Parameters
34+
==========
35+
36+
perm
37+
The permission's codename which should be checked.
38+
39+
user_obj
40+
The user for which the permission should be checked.
41+
42+
obj
43+
The object for which the permission should be checked.
44+
"""
45+
return permissions.utils.has_permission(obj, user_obj, perm)

permissions/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Unauthorized(Exception):
2+
def __init__(self, str):
3+
super(Unauthorized, self).__init__(str)

permissions/fixtures/initial.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<django-objects version="1.0">
3+
<object pk="1" model="permissions.permission">
4+
<field type="CharField" name="name">View</field>
5+
<field type="CharField" name="codename">view</field>
6+
</object>
7+
<object pk="2" model="permissions.permission">
8+
<field type="CharField" name="name">Edit</field>
9+
<field type="CharField" name="codename">edit</field>
10+
</object>
11+
<object pk="3" model="permissions.permission">
12+
<field type="CharField" name="name">Delete</field>
13+
<field type="CharField" name="codename">delete</field>
14+
</object>
15+
<object pk="4" model="permissions.permission">
16+
<field type="CharField" name="name">Cut</field>
17+
<field type="CharField" name="codename">cut</field>
18+
</object>
19+
<object pk="5" model="permissions.permission">
20+
<field type="CharField" name="name">Copy</field>
21+
<field type="CharField" name="codename">copy</field>
22+
</object>
23+
</django-objects>
701 Bytes
Binary file not shown.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# German translations for django-permissions
2+
# Copyright (C) 2010 Kai Diefenbach
3+
# This file is distributed under the same license as the PACKAGE package.
4+
# Kai Diefenbach <kai.diefenbach@iqpp.de>, 2010.
5+
#
6+
msgid ""
7+
msgstr ""
8+
"Project-Id-Version: 1.0\n"
9+
"Report-Msgid-Bugs-To: \n"
10+
"POT-Creation-Date: 2010-03-30 23:12+0200\n"
11+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
12+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
13+
"Language-Team: LANGUAGE <LL@li.org>\n"
14+
"MIME-Version: 1.0\n"
15+
"Content-Type: text/plain; charset=UTF-8\n"
16+
"Content-Transfer-Encoding: 8bit\n"
17+
18+
#: models.py:154
19+
msgid "Name"
20+
msgstr "Name"
21+
22+
#: models.py:155
23+
msgid "Codename"
24+
msgstr "Codename"
25+
26+
#: models.py:156
27+
msgid "Content Types"
28+
msgstr "Inhaltstypen"
29+
30+
#: models.py:175 models.py:280
31+
msgid "Role"
32+
msgstr "Rolle"
33+
34+
#: models.py:176 models.py:216
35+
msgid "Permission"
36+
msgstr "Recht"
37+
38+
#: models.py:178 models.py:218 models.py:282
39+
msgid "Content type"
40+
msgstr "Inhaltstyp"
41+
42+
#: models.py:179 models.py:219 models.py:283
43+
msgid "Content id"
44+
msgstr "Inhalts-ID"
45+
46+
#: models.py:278
47+
msgid "User"
48+
msgstr "Benutzer"
49+
50+
#: models.py:279
51+
msgid "Group"
52+
msgstr "Gruppe"

0 commit comments

Comments
 (0)