Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

<?php

declare(strict_types=1);

namespace LeadersLinked\Plugin;

use Laminas\Mvc\Controller\Plugin\AbstractPlugin;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Authentication\AuthenticationService;
use LeadersLinked\Model\Device;
use LeadersLinked\Mapper\DeviceMapper;
use LeadersLinked\Model\UserType;


class CurrentUserPlugin extends AbstractPlugin
{
    
    /**
     * 
     * @var AuthenticationService
     */
    protected $auth;
    
    /**
     *
     * @var boolean
     */
    protected $hasIdentity;
    
    
    /**
     *
     * @return \LeadersLinked\Model\User
     */
    protected $user;
    
    /**
     * 
     * @var string
     */
    protected $deviceId;
    
    /**
     * 
     * @var Device
     */
    protected $device;
    
    /**
     *
     * @param AdapterInterface $adapter
     */
    public function __construct($adapter) 
    {
        

        $this->auth = new \Laminas\Authentication\AuthenticationService();
        $this->hasIdentity = false;
        if($this->auth->hasIdentity()) {
            $identity = $this->auth->getIdentity();
            $this->deviceId = isset($identity['device_id']) ? $identity['device_id'] : '';
            
            $userMapper     = \LeadersLinked\Mapper\UserMapper::getInstance($adapter);
            $this->user     = $userMapper->fetchOne($identity['user_id']);
     
            if($this->user) {
                $this->hasIdentity = true;
                
                if($this->deviceId) {
                    $deviceMapper = DeviceMapper::getInstance($adapter);
                    $this->device = $deviceMapper->fetchOne($identity['device_id']);
                }
            }
        }
    }


    /**
     * 
     * @return \LeadersLinked\Model\User
     */
    public function getUser() 
    {
        
        return $this->user;
    }
    
    /**
     * 
     * @return string
     */
    public function getDeviceId()
    {
        return $this->deviceId;
    }

    /**
     * 
     * @return Device
     */
    public function getDevice()
    {
        return $this->device;
    }
    
    /**
     * 
     * @return int
     */
    public function getUserId()
    {
        if($this->hasIdentity) {
            return $this->user->id;
        } else {
            return 0;
        }
    }
    

    /**
     * @return string
     */
    public function getUserTypeId()
    {
        if($this->hasIdentity) {
           return $this->user->usertype_id;
        } else {
            return UserType::GUEST;
        }
    }
    
    
   
    /**
     * 
     * @return boolean
     */
    public function hasIdentity() 
    {
        return $this->user ? true : false;
    }
    
    public function clearIdentity()
    {
        $this->auth->clearIdentity();
    }

   
}