UserAbstract.php 2.18 KB
Newer Older
Bertrand Gauthier's avatar
Bertrand Gauthier committed
1
<?php
2

Bertrand Gauthier's avatar
Bertrand Gauthier committed
3
4
namespace UnicaenAuth\View\Helper;

5
use UnicaenAuth\Service\UserContext;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
6
use Laminas\I18n\View\Helper\AbstractTranslatorHelper;
7

Bertrand Gauthier's avatar
Bertrand Gauthier committed
8
9
10
11
12
/**
 * Classe mère des aides de vue concernant l'utilisateur connecté.
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
13
abstract class UserAbstract extends AbstractTranslatorHelper
Bertrand Gauthier's avatar
Bertrand Gauthier committed
14
{
15
16
17
    /**
     * @var UserContext
     */
18
    protected $userContext;
19

20
21
22
23
24
    /**
     * @var array
     */
    static protected $currentIdentity;

Bertrand Gauthier's avatar
Bertrand Gauthier committed
25
26
    /**
     * Constructeur.
27
     *
28
     * @param UserContext|null $userContext
Bertrand Gauthier's avatar
Bertrand Gauthier committed
29
     */
30
    public function __construct(UserContext $userContext = null)
Bertrand Gauthier's avatar
Bertrand Gauthier committed
31
    {
32
33
        if (null !== $userContext) {
            $this->setUserContext($userContext);
Bertrand Gauthier's avatar
Bertrand Gauthier committed
34
35
        }
    }
36

Bertrand Gauthier's avatar
Bertrand Gauthier committed
37
    /**
38
     * Get UserContext Service.
39
     *
40
     * @return UserContext
Bertrand Gauthier's avatar
Bertrand Gauthier committed
41
     */
42
    public function getUserContext()
Bertrand Gauthier's avatar
Bertrand Gauthier committed
43
    {
44
        return $this->userContext;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
45
46
47
    }

    /**
48
     * Set UserContext.
49
     *
50
     * @param UserContext $userContext
Bertrand Gauthier's avatar
Bertrand Gauthier committed
51
52
     * @return UserAbstract
     */
53
    public function setUserContext($userContext)
Bertrand Gauthier's avatar
Bertrand Gauthier committed
54
    {
55
        $this->userContext = $userContext;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
56
57
58
59
60
61
        return $this;
    }

    /**
     * Retourne les données d'identité courante éventuelle.
     *
62
     * @param string $preferedKey
Bertrand Gauthier's avatar
Bertrand Gauthier committed
63
64
     * @return mixed
     */
65
    public function getIdentity($preferedKey = null)
Bertrand Gauthier's avatar
Bertrand Gauthier committed
66
    {
67
68
69
70
71
        if (static::$currentIdentity !== null) {
            return static::$currentIdentity;
        }

        if (! ($identity = $this->getUserContext()->getIdentity())) {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
72
73
74
            return null;
        }

75
        if (is_array($identity)) {
76
            $keys = ['ldap', 'db', 'shib'];
77
78
            if ($preferedKey) {
                // on met la clé spécifiée en tête de liste
79
                $keys = array_merge(($tmp = [$preferedKey]), array_diff($keys, $tmp));
80
            }
81
82
            $found = null;
            foreach ($keys as $key) {
83
                if (array_key_exists($key, $identity) && $identity[$key] !== null) {
84
85
86
                    $found = $identity[$key];
                    break;
                }
87
            }
88
            $identity = $found;
89
        }
90

91
92
93
        static::$currentIdentity = $identity;

        return static::$currentIdentity;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
94
    }
95
}