Rev 1 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpdeclare(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;/**** @var boolean*/protected $hasImpersonate;/**** @return \LeadersLinked\Model\User*/protected $user;/**** @return \LeadersLinked\Model\User*/protected $impersonateUser;/**** @var string*/protected $deviceId;/**** @var Device*/protected $device;/**** @param AdapterInterface $adapter*/public function __construct($adapter){$this->auth = new \Laminas\Authentication\AuthenticationService();$this->impersonate_user = null;$this->hasIdentity = false;$this->hasImpersonate = 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->user->impersonate_user_id) {$this->hasImpersonate = true;$this->impersonate_user = $userMapper->fetchOne($this->user->impersonate_user_id);}if($this->deviceId) {$deviceMapper = DeviceMapper::getInstance($adapter);$this->device = $deviceMapper->fetchOne($identity['device_id']);}}}}/**** @return \LeadersLinked\Model\User*/public function getUser(){if($this->hasImpersonate) {return $this->impersonate_user;} else {return $this->user;}}/**** @return \LeadersLinked\Model\User*/public function getRawUser(){return $this->user;}/**** @return \LeadersLinked\Model\User*/public function getImpersonateUser(){return $this->impersonate_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) {if($this->hasImpersonate) {return $this->impersonate_user->id;} else {return $this->user->id;}} else {return 0;}}/*** @return string*/public function getUserTypeId(){if($this->hasIdentity) {if($this->hasImpersonate) {return $this->impersonate_user->usertype_id;} else {return $this->user->usertype_id;}} else {return UserType::GUEST;}}/**** @return boolean*/public function hasImpersonate(){return $this->hasImpersonate;}/**** @return boolean*/public function hasIdentity(){return $this->hasIdentity;}public function clearIdentity(){$this->auth->clearIdentity();}}