File management solution for Masonite
Project description
Masonite Permission
Introduction
Associate users with roles and permissions
Installation
pip install masonite-permission
Configuration
Add PermissionProvider to your project in config/providers.py
:
# config/providers.py
# ...
from masonite_permission import PermissionProvider
# ...
PROVIDERS = [
# ...
# Third Party Providers
PermissionProvider,
# ...
]
Then you can publish the package resources (if needed) by doing:
python craft package:publish masonite-permission
Finally, extend User
model class with HasRoles
.
from masoniteorm.models import Model
from masoniteorm.scopes import SoftDeletesMixin
from masonite.authentication import Authenticates
from masonite_permission.models.has_roles import HasRoles
class User(Model, SoftDeletesMixin, Authenticates, HasRoles):
"""User Model."""
__fillable__ = ["name", "email", "password"]
__hidden__ = ["password"]
__auth__ = "email"
Usage
Role
Methods that can be used in role model object:
""" Creating Role """
role = Role.create({
"name": "Admin",
"slug": "admin" # must be unique
})
""" collection can be synced """
permission_collection = Permission.all()
role.sync_permissions(permission_collection)
""" id will also work """
permission_ids = [1,2,3,4,5]
role.sync_permissions(permission_ids)
""" clear related data """
role.sync_permissions([])
""" Attach/Detatch Permission """
permission = Permission.first()
role.attach_permission(permission) # add permission to role, ignores if permission already exists
role.detach_permission(permission) # remove permission from role, ignores if permission doesn't exist
""" this can also be done """
role.attach_permission(1) # passing permission id instead of object, ignores if permission already exists
role.detatch_permission(1) # passing permission id instead of object, ignores if permission doesn't exist
Permission
Methods that can be used in permission model object:
""" Creating Permission """
permission = Permission.create({
"name": "Create Post",
"slug": "create-post" # must be unique
})
""" collection can be synced """
roles_collection = Role.all()
permission.sync_roles(role_collection)
""" id will also work """
role_ids = [1,2,3,4,5]
permission.sync_roles(role_ids)
""" clear related data """
permissioin.sync_roles([])
""" Attach/Detatch Role """
role = Role.first()
permission.attach_role(role) # add role to permission, ignores if role already exists
permission.detach_role(role) # remove role from permission, ignores if role doesn't exist
""" this can also be done """
permission.attach_role(1) # passing role id instead of object, ignores if role already exists
permission.detatch_role(1) # passing role id instead of object, ignores if role doesn't exist
User
Methods that can be used in user model object:
user = User.first()
# Add/Remove single role
role = Role.first()
user.assign_role(role) # or you can pass role id
user.remove_role(role) # or you can pass role id
# if you want to add multiple roles
roles = Role.all()
user.sync_roles(roles) # or you can also pass list of ids...
# remove all roles from user at once
user.sync_roles([])
# check if user has role
user.has_role("role-1") # returns boolean
# check if user has any of the roles
user.has_any_role(["role-1", "role-2"]) # returns boolean
# check if user has all of the roles
user.has_all_roles(["role-1", "role-2"]) # returns boolean
# check if user has permission
user.has_permission("permission-1") # returns boolean
# check if user has any of the permissions
user.has_any_permission(["permission-1", "permission-2"]) # returns boolean
# check if user has all of the permissions
user.has_all_permissions(["permission-1", "permission-2"]) # returns boolean
Using in Template
In case of Roles Checking if user has role.
{% if user.is_("admin") %}
<p>You are an admin</p>
{% endif %}
Checking if user has any of the roles
{% if user.is_("admin|editor|truck-driver") %}
<p>You can be either admin, editor, truck driver or all of those</p>
{% endif %}
Checking if user has all of the roles
{% if user.is_("admin,editor,truck-driver") %}
<p>You are an admin, editor and also truck-driver</p>
{% endif %}
In case of Permissions Checking if user can do {permission}.
{% if user.can_("edit-post") %}
<p>You can edit post</p>
{% endif %}
Checking if user can do any one or more of the {permissions}
{% if user.can_("edit-post|delete-post") %}
<p>You can either edit-post, delete-post or both.</p>
{% endif %}
Checking if user can do all of the {permissions}
{% if user.can_("edit-post,delete-post") %}
<p>You can edit post and also delete post.</p>
{% endif %}
Contributing
Please read the Contributing Documentation here.
Maintainers
License
Masonite Permission is open-sourced software licensed under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for masonite-permission-0.1.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc3f8ecba021353f34fb09d105e9ce36c952553e509d343825ba52eeb3e35367 |
|
MD5 | 97e90a6f4960ca5af3fd5be633231842 |
|
BLAKE2b-256 | 14b620fd6d875b54a9bf3d9029a22e46003bce96249aefafc0b70c7848d85017 |
Hashes for masonite_permission-0.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0f6221ef18769b29e3ed9615e1ca7ee921c9fdc22d49fc2a25bbc7bbbea2116 |
|
MD5 | 781b9e29edd766419f3076e014d051d4 |
|
BLAKE2b-256 | aa0a080c4f9cee2cd197c0dcadb1d5749cdb653642afb7ba7f4e6f387e7c50dc |