Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15461 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php
declare(strict_types=1);

namespace LeadersLinked\Controller;

use Laminas\Authentication\Result as AuthResult;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Log\LoggerInterface;

use LeadersLinked\Authentication\AuthOneTimePasswordAdapter;
use Laminas\Authentication\AuthenticationService;

use Laminas\View\Model\JsonModel;
use Laminas\View\Model\ViewModel;


class AuthController extends AbstractActionController
{
    /**
     *
     * @var AdapterInterface
     */
    private $adapter;
    
    
    /**
     *
     * @var AbstractAdapter
     */
    private $cache;
    
    /**
     *
     * @var  LoggerInterface
     */
    private $logger;

    /**
     * 
     * @var array
     */
    private $config;
    
    
    
    
    /**
     * 
     * @param AdapterInterface $adapter
     * @param AbstractAdapter $cache
     * @param LoggerInterface $logger
     * @param array $config
     */
    public function __construct($adapter, $cache , $logger, $config)
    {
        $this->adapter      = $adapter;
        $this->cache        = $cache;
        $this->logger       = $logger;
        $this->config       = $config;
    }
    
    public function indexAction()
    {
        $this->layout()->setTemplate('layout/auth');
        $viewModel = new ViewModel();
        $viewModel->setTemplate('leaders-linked/auth/index.phtml');

        return $viewModel ;
    }
    
    public function signoutAction() 
    {
        $auth = new AuthenticationService();
        $auth->clearIdentity(); 
        
        return $this->redirect()->toRoute('home');
    } 
    
    public function signinAdminAction()
    {

        
        $request = $this->getRequest();
        if($request->isGet()) {
            $user_uuid  = filter_var($this->params()->fromQuery('user_uuid'), FILTER_SANITIZE_STRING);
            $rand       = filter_var($this->params()->fromQuery('rand'), FILTER_SANITIZE_NUMBER_INT);
            $timestamp  = filter_var($this->params()->fromQuery('time'), FILTER_SANITIZE_NUMBER_INT);
            $password   = filter_var($this->params()->fromQuery('password'), FILTER_SANITIZE_STRING);
            
            
            if(!$user_uuid || !$rand || !$timestamp || !$password ) {
                throw new \Exception('ERROR_PARAMETERS_ARE_INVALID');
            }
            

            $authAdapter = new AuthOneTimePasswordAdapter ($this->adapter, $this->config);
            $authAdapter->setDataAdmin($user_uuid, $password, $timestamp, $rand);
    
            $authService = new AuthenticationService();
            $result = $authService->authenticate($authAdapter);
            
    
            if($result->getCode() == AuthResult::SUCCESS) {
                return $this->redirect()->toRoute('dashboard');
            } else {
                throw new \Exception($result->getMessages()[0]);
            }
        }

        return new JsonModel([
            'success' => false,
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
        ]);
 
    }
    
    public function signinCompanyAction()
    {
        $request = $this->getRequest();
        if($request->isGet()) {
            $company_uuid = filter_var($this->params()->fromQuery('company_uuid'), FILTER_SANITIZE_STRING);
            $user_uuid  = filter_var($this->params()->fromQuery('user_uuid'), FILTER_SANITIZE_STRING);
            $timestamp   = filter_var($this->params()->fromQuery('time'), FILTER_SANITIZE_STRING);
            $password   = filter_var($this->params()->fromQuery('password'), FILTER_SANITIZE_STRING);
            $rand       = filter_var($this->params()->fromQuery('rand'), FILTER_SANITIZE_NUMBER_INT);
            
            if(empty($user_uuid)  || empty($company_uuid) || empty($user_uuid) || empty($timestamp)  || empty($password) || empty($rand)) {
                return new JsonModel([
                    'success' => false,
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID'
                ]);
            }
            
            $authAdapter = new AuthOneTimePasswordAdapter ($this->adapter, $this->config);
            $authAdapter->setDataCompany($user_uuid, $password, $timestamp, $rand, $company_uuid);
            
            $authService = new AuthenticationService();
            $result = $authService->authenticate($authAdapter);
            
            
            if($result->getCode() == AuthResult::SUCCESS) {
                return $this->redirect()->toRoute('dashboard');
            } else {
                throw new \Exception($result->getMessages()[0]);
            }
        }
        
        return new JsonModel([
            'success' => false,
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
        ]);
    }
}