diff --git a/README.md b/README.md index 15794aee..a9436ece 100644 --- a/README.md +++ b/README.md @@ -31,18 +31,16 @@ Installing the plugin is pretty much as with every other CakePHP Plugin. composer require dereuromark/cakephp-tinyauth:dev-master ``` -Then load the plugin: +Then, to load the plugin either run the following command: -```php -Plugin::load('TinyAuth', ['bootstrap' => true]); +```sh +bin/cake plugin load TinyAuth ``` -For `Plugin::loadAll()` it's +or manually add the following line to your app's `config/bootstrap.php` file: ```php -Plugin::loadAll([ - 'TinyAuth' => ['bootstrap' => true] -]); +Plugin::load('TinyAuth'); ``` That's it. It should be up and running. diff --git a/config/bootstrap.php b/config/bootstrap.php deleted file mode 100644 index dd227b27..00000000 --- a/config/bootstrap.php +++ /dev/null @@ -1,5 +0,0 @@ - 'role_id', // name of column in users table holding role id (used for single role/BT only) + 'userColumn' => 'user_id', 'aliasColumn' => 'alias', // name of column in roles table holding role alias/slug 'rolesTable' => 'Roles', // name of Configure key holding available roles OR class name of roles table + 'usersTable' => 'Users', // name of the Users table + 'pivotTablePlugin' => '', // Name of the plugin managing the users table + 'rolesTablePlugin' => '', // Name of the plugin managing the roles table 'multiRole' => false, // true to enables multirole/HABTM authorization (requires a valid join table) 'pivotTable' => null, // Use instead of auto-detect for the multi-role pivot table holding the user's roles 'superAdminRole' => null, // id of super admin role granted access to ALL resources @@ -154,7 +155,7 @@ public function validate($userRoles, Request $request) { // Allow access if user has been granted access to the specific resource if (isset($this->_acl[$iniKey]['actions'])) { - if(array_key_exists($request->action, $this->_acl[$iniKey]['actions']) && !empty($this->_acl[$iniKey]['actions'][$request->action])) { + if (array_key_exists($request->action, $this->_acl[$iniKey]['actions']) && !empty($this->_acl[$iniKey]['actions'][$request->action])) { $matchArray = $this->_acl[$iniKey]['actions'][$request->action]; foreach ($userRoles as $userRole) { if (in_array((string)$userRole, $matchArray)) { @@ -324,7 +325,12 @@ protected function _getAvailableRoles() { } // fetch roles from database - $rolesTable = TableRegistry::get($this->_config['rolesTable']); + $rolesPlugin = $this->_config['rolesTablePlugin']; + $roleTable = $this->_config['rolesTable']; + if (!$rolesPlugin) { + $roleTable = $rolesPlugin . '.' . $roleTable; + } + $rolesTable = TableRegistry::get($roleTable); $roles = $rolesTable->find('all')->formatResults(function ($results) { return $results->combine($this->_config['aliasColumn'], 'id'); @@ -359,9 +365,10 @@ protected function _getUserRoles($user) { // multi-role: reverse engineer name of the pivot table $rolesTableName = $this->_config['rolesTable']; $pivotTableName = $this->_config['pivotTable']; + $usersTableName = $this->_config['usersTable']; if (!$pivotTableName) { $tables = [ - CLASS_USERS, + $usersTableName, $rolesTableName ]; asort($tables); @@ -369,10 +376,14 @@ protected function _getUserRoles($user) { } // fetch roles directly from the pivot table + $pivotTablePlugin = $this->_config['pivotTablePlugin']; + if (!$pivotTablePlugin) { + $pivotTableName = $pivotTablePlugin . '.' . $pivotTableName; + } $pivotTable = TableRegistry::get($pivotTableName); $roleColumn = $this->_config['roleColumn']; $roles = $pivotTable->find('all', [ - 'conditions' => ['user_id' => $user['id']], + 'conditions' => [$this->_config['userColumn'] => $user['id']], 'fields' => $roleColumn ])->extract($roleColumn)->toArray(); diff --git a/tests/TestCase/Auth/TinyAuthorizeTest.php b/tests/TestCase/Auth/TinyAuthorizeTest.php index 6d8b5508..c44d7890 100644 --- a/tests/TestCase/Auth/TinyAuthorizeTest.php +++ b/tests/TestCase/Auth/TinyAuthorizeTest.php @@ -1410,7 +1410,7 @@ protected function _getAcl($path = TMP) { * @return Cake\ORM\Table The User table */ public function getTable() { - $Users = TableRegistry::get(CLASS_USERS); + $Users = TableRegistry::get($this->_config['usersTable']); $Users->belongsTo('Roles'); return $Users;