Download the PHP package n0nag0n/fatfree-permissions without Composer
On this page you can find all versions of the php package n0nag0n/fatfree-permissions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download n0nag0n/fatfree-permissions
More information about n0nag0n/fatfree-permissions
Files in n0nag0n/fatfree-permissions
Package fatfree-permissions
Short Description A permissions module for your Fat-Free app to help with custom permissions.
License MIT
Informations about the package fatfree-permissions
Fat-Free Permissions
This is a permissions module that can be used in your projects if you have multiple roles in your app and each role has a little bit different functionality. This module allows you to define permissions for each role and then check if the current user has the permission to access a certain page or perform a certain action. This module is designed to be used with the Fat-Free Framework. It is not a standalone module. This module pairs very nicely with xfra35/f3-access
Installation
Run composer require n0nag0n/fatfree-permissions
and you're on your way!
Configuration
There is very little configuration you need to do to get this started. It actually is wired up to accept configs, but it's not actually used at the moment.
Usage
First you need to setup your permissions, then you tell the your app what the permissions mean. Ultimately you will check your permissions with $Permissions->has()
, ->can()
, or is()
. They all have the same functionality, but are named differently to make your code more readable.
Simple Usage
Then in your template or controller you can do something like this:
Advanced Usage
You might have something more advanced where you have some functionality available to one role, but not another role surrounding the same thing. I'll show you what I mean.
The permissions defined in this context are completely customizable. If you want view
, update
, archive
, soft-delete
, and like
permissions, you can totally customize it that way. Whatever strings you append to the array can be used to check if the user has that permission.
Now the fun part comes when you want to check if a user has a certain permission regarding orders. You can do something like this:
Injecting dependencies
As you can see in the example above, you can inject dependencies into the closure that defines the permissions. This is useful if you have some sort of toggle that you want to check against. The same works for Class->Method type calls, except you define the method as such:
Shortcuts with classes
You can also use classes to define your permissions. This is useful if you have a lot of permissions and you want to keep your code clean. You can do something like this:
The cool part is that there is also a shortcut that you can use (that is also cached!!!) where you just tell the permissions class to map all methods in a class into permissions. So if you have a method named order()
and a method named company()
, these will automatically be mapped so you can just run $Permissions->has('order.read')
or $Permissions->has('company.read')
and it will work. Defining this is very difficult, so stay with me here. You just need to do this:
And away you go!