Skip to content
Snippets Groups Projects
Select Git revision
  • master default protected
  • 5.x
  • ll-php8-bs5
  • release_5_bs5
  • ll-php8
  • 4.x
  • laminas_migration
  • release_1.0.0.2
  • release_4.0.0
  • release_3.2.8
  • bootstrap4_migration
  • 1.0.0.3
  • 6.0.7
  • 6.0.6
  • 6.0.5
  • 6.0.4
  • 6.0.3
  • 6.0.2
  • 6.0.1
  • 5.1.1
  • 6.0.0
  • 5.1.0
  • 5.0.0
  • 4.0.2
  • 3.2.11
  • 4.0.1
  • 3.2.10
  • 4.0.0
  • 1.0.0.2
  • 3.2.9
  • 3.2.8
31 results

UserAbstract.php

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    UserAbstract.php 2.34 KiB
    <?php
    namespace UnicaenAuth\View\Helper;
    
    use Zend\I18n\View\Helper\AbstractTranslatorHelper;
    use Zend\Authentication\AuthenticationService;
    use Zend\View\Exception\InvalidArgumentException;
    
    /**
     * Classe mère des aides de vue concernant l'utilisateur connecté.
     *
     * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
     */
    abstract class UserAbstract extends AbstractTranslatorHelper
    {
        protected $authService;
        
        /**
         * Constructeur.
         * 
         * @param AuthenticationService $authService
         */
        public function __construct(AuthenticationService $authService = null)
        {
            if (null !== $authService) {
                $this->setAuthService($authService);
            }
        }
        
        /**
         * Get Authentication Service.
         * 
         * @return \Zend\Authentication\AuthenticationService
         */
        public function getAuthService()
        {
            return $this->authService;
        }
    
        /**
         * Set Authentication Service.
         * 
         * @param \Zend\Authentication\AuthenticationService $authService
         * @return UserAbstract
         */
        public function setAuthService($authService)
        {
            $this->authService = $authService;
            return $this;
        }
    
        /**
         * Retourne les données d'identité courante éventuelle.
         *
         * @param string $preferedKey
         * @return mixed
         */
        public function getIdentity($preferedKey = null)
        {
            if (!$this->getAuthService() || !$this->getAuthService()->hasIdentity()) {
                return null;
            }
    
            $identity = $this->getAuthService()->getIdentity();
            
            if (is_array($identity)) {
                $keys = array('ldap', 'db');
                if ($preferedKey) {
                    // on met la clé spécifiée en tête de liste
                    $keys = array_merge(($tmp = array($preferedKey)), array_diff($keys, $tmp));
                }
                $found = null;
                foreach ($keys as $key) {
                    if (isset($identity[$key])) {
                        $found = $identity[$key];
                        break;
                    }
                }
                if (null === $found) {
                    throw new InvalidArgumentException(
                            "Aucune des clés suivantes n'a été trouvée dans les données d'identité: " . implode(", ", $keys) . ".");
                }
                $identity = $found;
            }
            
            return $identity;
        }
    }