how do I get the user role to pass to the controller to set the redirect to the individual section of the site??
public function login() {
$this->request->allowMethod(['get', 'post']);
$result = $this->Authentication->getResult();
// regardless of POST or GET, redirect if user is logged in
if ($result->isValid()) {
// redirect to /articles after login success
if ($role == 1) :
$redirect = $this->request->getQuery('redirect', [
'controller' => 'Admin',
'action' => 'index',
]);
elseif ($role == 2) :
$redirect = $this->request->getQuery('redirect', [
'controller' => 'Dashboard',
'action' => 'index',
]);
endif;
return $this->redirect($redirect);
}
// display error if user submitted and authentication failed
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('Invalid username or password'));
}
}
This is completely doing my head in! after reading the link, my take on it is as follows:
App>src>Model Entity/User.php add the following:
use Authentication\IdentityInterface;
class User extends Entity implements IdentityInterface
{
/**
* Authentication\IdentityInterface method
*/
public function getIdentifier()
{
return $this->role; // field in the user table
}
In App>src>Controller>UserController.php
public function login()
{
$authentication = $this->request->getAttribute('authentication');
$result = $authentication->getResult();
if ($result->isValid()) {
if ($authentication->identifiers()->get('role')) {
$role = (1); #not sure if it's called this way
$redirect = $this->request->getQuery('redirect', [
'controller' => 'Dashboard',
'action' => 'index',
]);
}
if ($authentication->identifiers()->get('role')) {
$user->role = (2); #not sure if it's called this way either??
$redirect = $this->request->getQuery('redirect', [
'controller' => 'Partners',
'action' => 'index',
]);
}
return $this->redirect($redirect);
}
// display error if user submitted and authentication failed
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('Invalid username or password'));
}
}
not entirely sure how to write that bit, which ever way I try to write it, I get the error of Unknown object “role”