Kinldy help me setup multiple session storage for Laminas Authentication
I have 4 classes of users namely :-
- Admin
- Super Admin
- Staff
- Cashier
All of the above have Db Tables (From which i authenticate from) respectively to better manage my users in classes, my authentication works on Adapters which i have created for the above user.
Sample CashierAdapter.php
<?php
namespace Auth\Adapter;
use Laminas\Authentication\Adapter\AdapterInterface;
use Laminas\Authentication\Adapter\DbTable\CredentialTreatmentAdapter as AuthAdapter;
use Laminas\Authentication\AuthenticationService;
use Laminas\Authentication\Result;
use Laminas\Authentication\Validator\Authentication as AuthenticationValidator;
use Auth\Model\TenantStorage;
use Cashier\Model\CashierTable;
class CashierAdapter
{
const SUCCESS = 1;
const FAILURE = 0;
const FAILURE_IDENTITY_NOT_FOUND = -1;
const FAILURE_IDENTITY_AMBIGUOUS = -2;
const FAILURE_CREDENTIAL_INVALID = -3;
const FAILURE_UNCATEGORIZED = -4;
/**
* Sets username and password for authentication
*
* @return void
*/
private $cashierTable;
public function __construct(CashierTable $cashierTable)
{
$this->cashierTable = $cashierTable;
}
/**
* Performs an authentication attempt
*
* @return \Laminas\Authentication\Result
* @throws \Laminas\Authentication\Adapter\Exception\ExceptionInterface
* If authentication cannot be performed
*/
public function logout()
{
$auth = new AuthenticationService();
$auth->clearIdentity();
}
public function authenticate($username, $password)
{
// Perform the authentication query, saving the result
$auth = new AuthenticationService();
$storage = new TenantStorage();
// Configure the instance with setter methods:
$authAdapter = $this->getAdapter();
$authAdapter
->setTableName('cashier')
->setIdentityColumn('email')
->setCredentialColumn('pass');
// Set the input credential values (e.g., from a login form):
/*$bcrypt = new Bcrypt();
$securePass = $bcrypt->create('user password');
*/
$authAdapter
->setIdentity($username)
->setCredential($password);
$result = $auth->authenticate($authAdapter);
//Store Validated responses in a an array
$resp = [];
switch ($result->getCode()) {
case Result::FAILURE_IDENTITY_NOT_FOUND:
/** do stuff for nonexistent identity **/
$resp['status'] = 'error';
$resp['msg'] = '<div class="alert alert-danger"><span class="fas fa-exclamation-circle"></span> That User does not exist</div>';
break;
case Result::FAILURE_CREDENTIAL_INVALID:
/** do stuff for invalid credential **/
$resp['status'] = 'error';
$resp['msg'] = '<div class="alert alert-danger"><span class="fas fa-exclamation-circle"></span> Incorrect Password</div>';
break;
case Result::SUCCESS:
$resp['status'] = 'success';
$resp['route'] = 'cashier-dashboard';
break;
}
return $resp;
}
public function hasIdentity(){
$auth = new AuthenticationService();
$identity = $auth->getIdentity();
if($auth->hasIdentity() && $this->cashierTable->authCheck($identity) > 0){
$username = $this->cashierTable->getUsername($identity);
return ['authentic' => "auth-exists",'username' => $username];
}else{
return ['authentic'=> 'not-auth'];
}
}
public function getIdentity()
{
$auth = new AuthenticationService();
$identity = $auth->getIdentity();
if($auth->hasIdentity() && $this->cashierTable->authCheck($identity) > 0){
return $auth->getIdentity();
}
}
public function ValidateAuth()
{
$service = new AuthenticationService();
$adapter = $this->getAdapter();
$adapter
->setTableName('cashier')
->setIdentityColumn('email')
->setCredentialColumn('pass');
$validator = new AuthenticationValidator([
'service' => $service,
'adapter' => $adapter,
]);
return $validator->isValid();
}
public function getAdapter(){
//Grab DB Adapter
$adapter = new \Laminas\Db\Adapter\Adapter([
'driver' => 'Mysqli',
'database' => 'portal',
'username' => 'rootie',
'password' => 'mypass',
]);
// Configure the instance with setter methods:
$authAdapter = new AuthAdapter($adapter);
return $authAdapter;
}
}
My Controller
My login Logic works ok except some adpaters are authenticated but upon successful login am redirected back to login instead. I believe its the issue of Storage.
/**
* Login Action
*/
public function loginAction(){
$response = "";
if ($_POST) {
//Response
$response = "";
//collect login form data
$this->email = $_POST['email'];
$this->pass = $_POST['password'];
if($this->email != '' && $this->pass != ''){
if(!strpos($this->email,'#sa') && !strpos($this->email,'#fin') && !strpos($this->email,'#cash')){
$role = "tenant";
}
if(strpos($this->email,'#fin')){
$role = "finance";
}
if (strpos($this->email, '#cash')) {
$role = "cashier";
}
if (strpos($this->email, '#sa')) {
$role = "superadmin";
}
//Authenticate SuperAdmin Login
if($role == "superadmin"){
$email = str_replace("#sa", " ", $this->email);
//Authenticate
$msg = $this->sauthService->authenticate($email,$this->pass);
if($msg['status'] !== 'success'){
$response = $msg['msg'];
}else{
$this->redirect()->toRoute($msg['route']);
}
}
//Authenticate SuperAdmin Login
if($role == "cashier"){
$email = str_replace("#cash", " ", $this->email);
//Authenticate
$msg = $this->cashService->authenticate($email,$this->pass);
if($msg['status'] !== 'success'){
$response = $msg['msg'];
}else{
$this->redirect()->toRoute($msg['route']);
}
}
//Authenticate Tenant Login
if($role == "tenant"){
//Authenticate
$msg = $this->authService->authenticate($this->email,$this->pass);
if($msg['status'] !== 'success'){
$response = $msg['msg'];
}else{
$this->redirect()->toRoute($msg['route']);
}
}
//Authenticate Finance Login
if($role == "finance"){
//Authenticate
$email = str_replace("#fin", " ", $this->email);
$msg = $this->finService->authenticate($email,$this->pass);
if($msg['status'] !== 'success'){
$response = $msg['msg'];
}else{
$this->redirect()->toRoute($msg['route']);
}
}
}else{
$response = '<div class="alert alert-danger"><span class="fas fa-exclamation-circle"></span> Please Fill in all fields</div>';
}
}
$view = new ViewModel(['resp_msg' => $response]);
return $view;
}
//Logout user from the system
public function logoutAction()
{
$auth = new AuthenticationService();
$auth->clearIdentity();
return $this->redirect()->toRoute('auth-login');
}
/**
* Get Make Auth Services Global
*
*/
private function getAuthAdapter()
{
$adapter = new \Laminas\Db\Adapter\Adapter([
'driver' => 'Mysqli',
'database' => 'portal',
'username' => 'root',
'password' => 'mypass',
]);
// Instantiate the authentication adapter:
return new DbAuth($adapter);
}