That seems to have worked, but now I’m getting Unknown method "hasRight".
in Entity/User.php, I do have this code:
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\Auth\DefaultPasswordHasher;
/**
* User Entity
*
* @property int $id
* @property string $username
* @property string $email
* @property string $password
* @property \Cake\I18n\FrozenTime $created
*
* @property \App\Model\Entity\Article[] $articles
* @property \App\Model\Entity\UsersRole[] $users_roles
* @property \App\Model\Entity\UsersDetail $users_detail
*/
class User extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array
*/
protected $_accessible = [
'username' => true,
'email' => true,
'password' => true,
'created' => true,
'articles' => true,
'users_roles' => true,
'users_detail' => true
];
/**
* Fields that are excluded from JSON versions of the entity.
*
* @var array
*/
protected $_hidden = [
'password'
];
protected function _setPassword($password) {
if (strlen($password) > 0) {
return (new DefaultPasswordHasher)->hash($password);
}
}
public function hasRight(string $right){
return collection($this->user_roles)->firstMatch([
'{*}.roles.{*}.permissions.' . $right => 1
]) !== null;
}
}
EDIT:
This is my AdminController.php at this moment:
<?php
namespace App\Controller;
class AdminController extends AppController {
public function initialize(){
parent::initialize();
$this->layout = 'admin';
$this->loadModel('UsersRoles');
$this->loadModel('Users');
}
public function index() {
$user_roles = $this->Users->findById($this->Auth->user('id'))->contain(['UsersRoles' => ['Roles' => ['Permissions']]]);
var_dump($user_roles->hasRight("access_cms"));
}
}