dereuromark/cakephp-tinyauth

A CakePHP plugin to handle user authentication and authorization the easy way.

Installs: 67 360

Dependents: 7

Suggesters: 1

Stars: 101

Watchers: 6

Forks: 36

Open Issues: 4

Type:cakephp-plugin

2.0.1 2019-11-09 13:16 UTC

README

Build Status Latest Stable Version Coverage Status Minimum PHP Version License Total Downloads Coding Standards

A CakePHP plugin to handle authentication and user authorization the easy way.

This branch is for CakePHP 3.7+. For details see version map.

Features

Authentication

What are public actions, which ones need login?

  • Powerful wildcard (*) operator.
  • Quick Setup for 5 minute integration.

Authorization

Once you are logged in, what actions can you see with your role(s)?

  • Single-role: 1 user has 1 role (users and roles table for example)
  • Multi-role: 1 user can have 1...n roles (users, roles and a "roles_users" pivot table for example)
  • Quick Setup for 5 minute integration.

Useful helpers

  • AuthUser Component and Helper for stateful and stateless "auth data" access.
  • Authentication Component and Helper for isPublic() check on current other other actions.
  • Auth DebugKit panel for detailed insights into current URL and auth status.

What's the idea?

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.

Demo

See https://sandbox.dereuromark.de/auth-sandbox

auth_allow.ini

Define the public actions (accessible by anyone) per controller:

Users = index,view
admin/Maintenance = pingCheck
PluginName.SomeController = *
MyPlugin.api/V1 = *

auth_acl.ini

Define what actions may be accessed by what logged-in user role:

[Users]
index = *
add,edit = user,mod

[admin/Users]
* = admin

[Translate.admin/Languages]
* = *

AuthUser component and helper

$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?']);

Installation

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.

Docs

For setup and usage see Docs.

Also note the original blog post and how it all started.

Branching strategy

The master branch is the currently active and maintained one and works with the current 3.x stable version. Please see the original Tools plugin if you need TinyAuth for CakePHP 2.x versions.