All Projects → miketheredherring → django-hats

miketheredherring / django-hats

Licence: MIT license
Role-based permissions system for Django. Everyone wears a different hat, some people wear multiple.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to django-hats

rbac
Simple RBAC/ACL for Laravel 8 caching and permission groups.
Stars: ✭ 43 (+104.76%)
Mutual labels:  permissions, roles
Maravel Permissions
Because in the Maravelous univer every user deserves super power
Stars: ✭ 139 (+561.9%)
Mutual labels:  permissions, roles
Laravel Auth
A powerful authentication, authorization and verification package built on top of Laravel. It provides developers with Role Based Access Control, Two-Factor Authentication, Social Authentication, and much more, compatible Laravel’s standard API and fully featured out of the box.
Stars: ✭ 128 (+509.52%)
Mutual labels:  permissions, roles
Sentinel
A framework agnostic authentication & authorization system.
Stars: ✭ 1,354 (+6347.62%)
Mutual labels:  permissions, roles
Adonis Acl
demo app: https://github.com/enniel/adonis-acl-blog-demo
Stars: ✭ 195 (+828.57%)
Mutual labels:  permissions, roles
Accesscontrol
Role and Attribute based Access Control for Node.js
Stars: ✭ 1,723 (+8104.76%)
Mutual labels:  permissions, roles
Laratrust
Handle roles and permissions in your Laravel application
Stars: ✭ 1,799 (+8466.67%)
Mutual labels:  permissions, roles
Policyserver.local
Sample OSS version of PolicyServer
Stars: ✭ 444 (+2014.29%)
Mutual labels:  permissions, roles
nova-permissions
Add Permissions based authorization for your Nova installation via User-based Roles and Permissions. Roles are defined in the database whereas Permissions are defined in the code base.
Stars: ✭ 115 (+447.62%)
Mutual labels:  permissions, roles
Vue Gates
🔒 A Vue.js & Nuxt.js plugin that allows you to use roles and permissions in your components or DOM elements, also compatible as middleware and methods.
Stars: ✭ 184 (+776.19%)
Mutual labels:  permissions, roles
Brandenburg
Laravel Authentication Package
Stars: ✭ 79 (+276.19%)
Mutual labels:  permissions, roles
Bouncer
Eloquent roles and abilities.
Stars: ✭ 2,763 (+13057.14%)
Mutual labels:  permissions, roles
Ngx Permissions
Permission and roles based access control for your angular(angular 2,4,5,6,7,9+) applications(AOT, lazy modules compatible
Stars: ✭ 749 (+3466.67%)
Mutual labels:  permissions, roles
React-Express-JWT-UserPortal
React.js & Express.js User portal Using Core UI, JWT, JWT Token, Refresh Token, Role & Permission management, User manamgenet, Event Log.
Stars: ✭ 22 (+4.76%)
Mutual labels:  permissions, roles
Django Role Permissions
A django app for role based permissions.
Stars: ✭ 465 (+2114.29%)
Mutual labels:  permissions, roles
Laravel Governor
Manage authorization with granular role-based permissions in your Laravel Apps.
Stars: ✭ 131 (+523.81%)
Mutual labels:  permissions, roles
laravel-inforce
A toolset to kickstart your application on top of Laravel Livewire, Laravel Jetstream and Spatie Permissions. LLoadout inforce is created using the TALL stack.
Stars: ✭ 12 (-42.86%)
Mutual labels:  permissions, roles
Shinobi
👺 Simple and light-weight role-based permissions system for Laravel's built in Auth system.
Stars: ✭ 349 (+1561.9%)
Mutual labels:  permissions, roles
Think Authz
An authorization library that supports access control models like ACL, RBAC, ABAC in ThinkPHP 6.0 .
Stars: ✭ 155 (+638.1%)
Mutual labels:  permissions, roles
Vue Router User Roles
A Vue.js plugin that protects routes based on user roles. Add your own authentication.
Stars: ✭ 237 (+1028.57%)
Mutual labels:  permissions, roles

django-hats

Coverage Status PyPI

Role-based permissions system for Django. Everyone wears a different hat, some people wear multiple.

In a nutshell, django-hats is a reusable app, which doesn't try to re-invent the wheel by having Django Groups as its foundation. It provides a compact collection of easy to use patterns/utilities for role, and permission, based checks in CBVs and templates. They have the advantages of being: pragmatically defined, performant, clear syntax, and are well tested/documented!

Quick Start

Install with pip:

pip install django-hats

Or, getting the latest build:

pip install git+git://github.com/miketheredherring/django-hats.git@master

Add django_hats to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'django_hats',
    ...
)

Create roles.py in any registered applications in your Django project:

from django_hats.roles import Role

class Scientist(Role):
    class Meta:
        permissions = ('change_subject', 'change_specimen')

class GeneticCounselor(Role):
    pass

Synchronize your database with defined roles:

python manage.py synchronize_roles

You're ready to go! Start defining permissions and securing your application!

Working with roles

Pragmatically assigning/removing/viewing Permission to role:

>>> Scientist.add_permissions(perm_1, perm_2, ...)
>>> GeneticCounselor.remove_permissions(perm_3)
>>> Scientist.get_permissions()
[<Permission 'change_subject'>, <Permission 'change_specimen'>]

Assigning/removing roles for a user(works with custom user models):

>>> user = User.objects.first()
>>> Scientist.assign(user)
>>> Scientist.remove(user)

Then checking if a user has a role, or multiple roles:

>>> from django_hats.utils import check_membership
>>> Scientist.check_membership(user)
True
>>> GeneticCounselor.check_membership(user)
False
>>> check_membership(user, Scientist)
True
>>> check_membership(user, [Scientist, GeneticCounselor])
False
>>> check_membership(user, [Scientist, GeneticCounselor], any=True)
True

List users with a given role:

>>> Scientist.get_users()
[<User 'Mike Hearing'>, <User 'Scientist_1'>]

Retrieving roles pragmatically:

>>> from django_hats.roles import RoleFinder
...
>>> RoleFinder.by_user(user)
[<class 'Scientist'>, ]
>>> RoleFinder.by_name('genetic_counselor')
<class 'GeneticCounselor'>
>>> RoleFinder.by_group(group)
<class 'Scientist'>

Mixins

Enforcing roles on the view:

from django.views.generic import TemplateView
from django_hats.mixins import RoleRequiredMixin

from app.roles import GeneticCounselor, Scientist

class ProtectedGeneticReport(RoleRequiredMixin, TemplateView):
    role_required = GeneticCounselor
    template_name = 'template.html'


class ProtectedGeneticFiles(RoleRequiredMixin, TemplateView):
    # Works with existing Django `PermissionRequiredMixin`
    permission_required = ('change_subject', 'change_specimen')
    role_required = (GeneticCounselor, Scientist)
    role_required_any = True
    template_name = 'template.html'

Templates

Checking roles in the template like permissions:
NOTE: This is the reccomended way to check for roles in the template

settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'django_hats.context_processors.roles',
    ...
)

template.html

{% if roles.scientist %}PROTECTED CONTENT!{% endif %}

{% if roles.genetic_counselor %}NOTE: Class names are converted to snake_case if not specified in role.Meta.name{% endif %}

Checking roles in the template with filter tag:
NOTE: This works without the context processor, and is not required when using the context processor, if thats your thing

{% load roles %}

{% if user|has_role:'scientist' or user|has_role:genetic_counselor_role %}PROTECTED CONTENT!{% endif %}

Signals

post_synchronize_roles

  • sender: django-hats AppConfig

Management Commands

Synchronize roles/permissions from the database:

python manage.py synchronize_roles

Migrate a role which the class name/name has changed:

python manage.py migrate_role --old=OldRoleClass --new=NewRoleClass

Remove old roles/permissions from the database(only post migration if a name change occured):

python manage.py cleanup_roles
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].