Package: masonite-permission
Masonite PermissionIntroductionAssociate users with roles and permissions to control access to your application.Getting StartedInstall the package using pip:pipinstallmasonite-permissionAdd PermissionProvider to your project inconfig/providers.py:# config/providers.py# ...frommasonite_permissionimportPermissionProvider# ...PROVIDERS=[# ...# Third Party ProvidersPermissionProvider,# ...]Publish the package configuration files.pythoncraftpackage:publishmasonite-permissionThis will add migrations and othermasonite-permissionrelated configuration to your project. Run your migrations to create the related database tables.pythoncraftmigrateNow, extendUsermodel class withHasRolesmixin.frommasoniteorm.modelsimportModelfrommasoniteorm.scopesimportSoftDeletesMixinfrommasonite.authenticationimportAuthenticatesfromsrc.masonite_permission.mixinsimport(HasPermissions,HasRoles)classUser(Model,SoftDeletesMixin,Authenticates,HasRoles,HasPermissions):"""User Model."""__fillable__=["name","email","password"]__hidden__=["password"]__auth__="email"UsageWorking with RoleCreating Roles""" Creating RoleArguments:name: The name of the roleslug: The slug of the role, must be unique"""frommasonite_permission.modelsimportRolerole=Role.create({"name":"Admin","slug":"admin"})Add permissions into roles""" Add permissions into rolesAvailable Methods:1. sync_permissions: Syncs the permissions with the rolearguments: Takes a list/tuple of permission slugs2. attach_permission: Adds a permission to a rolearguments: Takes permission model object or permission slug3. detach_permission: Removes a permission from the rolearguments: Takes permission model object or permission slug"""""" Syncing permissions with role, adds provided permissions and removes all other permissionsArguments:permissions: Takes a list/tuple of permission slugs"""role.sync_permissions(["create-post","read-post","update-post","delete-post"])# orrole.sync_permissions("create-post","read-post","update-post","delete-post")# orrole.sync_permissions([])# clears all permissions from role""" Attach permission, this will add new permission into role if already not addedArguments:permission: Takes permission model object or permission slug"""role.attach_permission("create-post")# orrole.attach_permission(Permission.first())""" Detach permission, this will remove permission from role if already addedArguments:permission: Takes permission model object or permission slug"""role.detach_permission("create-post")# orrole.detach_permission(Permission.first())Working with PermissionCreating Permission""" Creating PermissionArguments:name: The name of the permissionslug: The slug of the permission, must be unique"""frommasonite_permission.modelsimportPermissionpermission=Permission.create({"name":"Create Post","slug":"create-post"# must be unique})Add permissions into roles""" Add permissions into rolesAvailable Methods:1. sync_roles: Syncs the roles with the permissionarguments: Takes a list/tuple of role slugs2. attach_role: Adds a permission to a rolearguments: Takes role model object or role slug3. detach_role: Removes a permission from a rolearguments: Takes role model object or role slug"""""" Syncing permissions with role, adds provided roles and removes all other rolesArguments:roles: Takes a list of role ids or role collection"""permission.sync_roles(['admin','editor'])# orpermission.sync_roles('admin','editor')# orpermission.sync_roles([])# clears all role from permission""" Attach role, this will add new permission into role if already not addedArguments:role: Takes role model object or role slug"""role=Role.first()permission.attach_role(role)# orpermission.attach_role('admin')""" Detach role, this will remove permission from role if already addedArguments:role: Takes role model object or role id"""role=Role.first()permission.detach_role(role)# orpermission.detach_role('admin')Working with Useruser=User.first()Adding/Removing single role# Add/Remove single rolerole=Role.first()user.assign_role(role)# or you can pass role iduser.revoke_role(role)# or you can pass role idAdding/Removing multiple roles# add/remove multiple rolesroles=Role.all()user.sync_roles(roles)# or you can also pass list of ids...user.sync_roles([])# clears all roles from userChecking if user has roles# check if user has roleuser.has_role_of("admin")# returns boolean# check if user has any of the rolesuser.has_any_role(["admin","editor"])# returns boolean# oruser.has_any_role("admin","editor")# check if user has all of the rolesuser.has_all_roles(["admin","editor"])# returns boolean# oruser.has_all_roles("admin","editor")Adding/Removing single direct permission# Add/Remove single permissionuser.give_permission_to("read-post","edit-post")# this can be a tuple or a list# oruser.give_permission_to(["read-post","edit-post"])user.revoke_permission_to("read-post","edit-post")# this can be a tuple or a list# oruser.revoke_permission_to(["read-post","edit-post"])Adding/Removing multiple direct permissions# add/remove multiple direct permissionspermissions=Permission.all().pluck("slug")user.sync_permissions(permissions)# tuples or list of permission sluguser.sync_permissions([])# clears all permissions from userChecking if user has permissions# check if user has permission (for single permission)user.has_permission_to("read-post")# returns boolean# check if user has any of the permissionsuser.has_any_permission(["read-post","edit-post"])# returns boolean# oruser.has_any_permission("read-post","edit-post")# check if user has all of the permissionsuser.has_all_permissions(["read-post","edit-post"])# returns boolean# oruser.has_all_permissions("read-post","edit-post")# returns booleanUsing in TemplateIn case of RolesChecking 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 PermissionsChecking 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 %}LicenseMasonite Permission is open-sourced software licensed under theMIT license.