Skip to main content

Masonite Permission

GitHub Workflow Status PyPI issues Python Version GitHub release (latest by date including pre-releases) License star downloads Code style: black

Introduction

Associate users with roles and permissions

Getting Started

Install the package using pip:

pip install masonite-permission

Add PermissionProvider to your project in config/providers.py:

# config/providers.py
# ...
from masonite_permission import PermissionProvider

# ...
PROVIDERS = [
    # ...
    # Third Party Providers
    PermissionProvider,
    # ...
]

Publish the package configuration files.

python craft package:publish masonite-permission

This will add migrations and other masonite-permission related configuration to your project. Run your migrations to create the related database tables.

python craft migrate

Now, extend User model class with HasRoles mixin.

from masoniteorm.models import Model
from masoniteorm.scopes import SoftDeletesMixin
from masonite.authentication import Authenticates
from masonite_permission.mixins import HasRoles

class User(Model, SoftDeletesMixin, Authenticates, HasRoles):
    """User Model."""

    __fillable__ = ["name", "email", "password"]
    __hidden__ = ["password"]
    __auth__ = "email"

Usage

Working with Role

Creating Role
""" Creating Role
    Arguments:
        name: The name of the role
        slug: The slug of the role, must be unique
"""
from masonite_permission.models import Role

role = Role.create({
    "name": "Admin",
    "slug": "admin"
})
Add permissions into roles
""" Add permissions into roles
    Available Methods:
        1. sync_permissions: Syncs the permissions with the role
            arguments: Takes a list of permission ids or permission collection
        2. attach_permission: Adds a permission to a role
            arguments: Takes permission model object or permission id
        3. detach_permission: Removes a permission from the role
            arguments: Takes permission model object or permission id
"""
""" Syncing permissions with role, adds provided permissions and removes all other permissions
    Arguments:
        permissions: Takes a list of permission ids or permission collection
"""
permission_collection = Permission.all()
permission_ids = [1, 2, 3, 4, ...]

role.sync_permissions(permission_collection)
# or
role.sync_permissions(permission_ids)
# or
role.sync_permissions([]) # clears all permissions from role
""" Attach permission, this will add new permission into role if already not added
    Arguments:
        permission: Takes permission model object or permission id
"""
permission = Permission.first()

role.attach_permission(permission)
# or
role.attach_permission(1)
""" Detach permission, this will remove permission from role if already added
    Arguments:
        permission: Takes permission model object or permission id
"""
permission = Permission.first()

role.detach_permission(permission)
# or
role.detach_permission(1)

Working with Permission

Creating Permission
""" Creating Permission
    Arguments:
        name: The name of the permission
        slug: The slug of the permission, must be unique
"""
from masonite_permission.models import Permission
permission = Permission.create({
  "name": "Create Post",
  "slug": "create-post" # must be unique
})
Add permissions into roles
""" Add permissions into roles
    Available Methods:
        1. sync_roles: Syncs the roles with the permission
            arguments: Takes a list of role ids or role collection
        2. attach_role: Adds a permission to a role
            arguments: Takes role model object or role id
        3. detach_role: Removes a permission from a role
            arguments: Takes role model object or role id
"""
""" Syncing permissions with role, adds provided roles and removes all other roles
    Arguments:
        roles: Takes a list of role ids or role collection
"""
role_collection = Role.all()
role_ids = [1, 2, 3, 4, ...]

permission.sync_roles(role_collection)
# or
permission.sync_roles(role_ids)
# or
permission.sync_roles([]) # clears all role from permission
""" Attach role, this will add new permission into role if already not added
    Arguments:
        role: Takes role model object or role id
"""
role = Role.first()

permission.attach_role(role)
# or
permission.attach_role(1)
""" Detach role, this will remove permission from role if already added
    Arguments:
        role: Takes role model object or role id
"""
role = Role.first()

permission.detach_role(role)
# or
permission.detach_role(1)

Working with User

user = User.first()
# Add/Remove single role
role = Role.first()

user.assign_role(role) # or you can pass role id
user.revoke_role(role) # or you can pass role id
# add/remove multiple roles
roles = Role.all()

user.sync_roles(roles) # or you can also pass list of ids...
user.sync_roles([]) # clears all roles from user
# 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.


Release files for masonite-permission 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for masonite-permission 0.1.4
File Size Uploaded
masonite-permission-0.1.4.tar.gz 83.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for masonite-permission 0.1.4
File Interpreter ABI Platform
masonite_permission-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 94.4 kB

Release files / masonite-permission-0.1.4.tar.gz

Download URL masonite-permission-0.1.4.tar.gz
Size 83.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7b58a7ddd422c24c401cb358c4a0877d7179517b804b33c5026c4480215ef82b
BLAKE2b-256 checksum
How to use checksums
353a6defafea223b10ea58fc47a6aaefabf46cfc274d61971153f3894db15026
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release files / masonite_permission-0.1.4-py3-none-any.whl

Download URL masonite_permission-0.1.4-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6ed23d5c88a7ec1b48c38be0f154caf7bde9231a2664019fa0022b5788055e6e
BLAKE2b-256 checksum
How to use checksums
16fc0a0c9ab8f59559a3c2550cc1ffef32e175878c4e7e2370ced8c614a0f8fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release history Release notifications | RSS feed

1.0.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page