Simple way for restricting access to views
-
- Add 'locksmith' to installed apps in your settings.py:
INSTALLED_APPS = (
...
'locksmith'
)
and install it:
./manage.py syncdb locksmith
-
- Configure your key names in settings.py:
LOCKSMITH_KEYS = (
'articles',
'users'
)
-
- Update your keys (you can do it every time you add a key)
./manage.py updatekeys
-
- Update your custom user model, add locksmith mixin:
class User(locksmith.models.LocksmithMixin, models.Model):
...
-
- Add key to users keychain (currently no admin interface):
from locksmith.models import Keychain, Key
...
user = Users.objects.get(pk=1) # your user
# create keychain for this user
keychain = Keychain(expiration_date="2020-12-31")
keychain.save()
user.keychain = keychain
user.keychain.add(Key.objects.get(name='articles')
-
- Configure your views
from locksmith.decorators import key_required
...
@key_required('articles')
def action(request, *args, **kwargs):
And that's it!