CakePHP TinyAuth plugin for an easy and fast user authentication and authorization. Single or multi role. DB or config file based.
A CakePHP plugin to handle authentication and user authorization the easy way.
This branch is for CakePHP 5.0+. For details see version map.
What are public actions, which ones need login?
Once you are logged in, what actions can you see with your role(s)?
isPublic()
check on current other other actions.Default CakePHP authentication and authorization depends on code changes in at least each controller, maybe more classes.
This plugin hooks in with a single line of change and manages all that using config files and there is no need to touch all those controllers, including plugin controllers.
It is also possible to manage the config files without the need to code.
And it can with adapters also be moved completely to the DB and managed by CRUD backend.
Ask yourself: Do you need the overhead and complexity involved with the full blown (RBAC DB) ACL? See also my post acl-access-control-lists-revised/.
If not, then this plugin could very well be your answer and a super quick solution to your auth problem 😃
But even if you don’t leverage the authentication or authorization, the available AuthUserComponent and AuthUserHelper
can be very useful when dealing with role based decisions in your controller or view level. They also work stand-alone.
See https://sandbox.dereuromark.de/auth-sandbox
Define the public actions (accessible by anyone) per controller:
Users = index,view
Admin/Maintenance = pingCheck
PluginName.SomeController = *
MyPlugin.Api/V1 = *
Define what actions may be accessed by what logged-in user role:
[Users]
index = *
add,edit = user,super-user
[Admin/Users]
* = admin
[Translate.Admin/Languages]
* = *
$currentId = $this->AuthUser->id();
$isMe = $this->AuthUser->isMe($userEntity->id);
if ($this->AuthUser->hasRole('mod')) {
}
if ($this->AuthUser->hasAccess(['action' => 'secretArea'])) {
}
// Helper only
echo $this->AuthUser->link('Admin Backend', ['prefix' => 'Admin', 'action' => 'index']);
echo $this->AuthUser->postLink('Delete', ['action' => 'delete', $id], ['confirm' => 'Sure?']);
Including the plugin is pretty much as with every other CakePHP plugin:
composer require dereuromark/cakephp-tinyauth
Then, to load the plugin either run the following command:
bin/cake plugin load TinyAuth
or manually add the following line to your app’s src/Application.php
file’s bootstrap()
function:
$this->addPlugin('TinyAuth');
That’s it. It should be up and running.
For setup and usage see Docs.
Also note the original blog post and how it all started.