UserProfile.php 2.71 KB
Newer Older
Bertrand Gauthier's avatar
Bertrand Gauthier committed
1
2
3
<?php
namespace UnicaenAuth\View\Helper;

4
5
6
7
use BjyAuthorize\Provider\Identity\ProviderInterface;
use UnicaenAuth\Acl\NamedRole;
use Zend\Permissions\Acl\Role\RoleInterface;

Bertrand Gauthier's avatar
Bertrand Gauthier committed
8
9
10
11
12
13
14
/**
 * Aide de vue permettant d'afficher le profil de l'utilisateur connecté.
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
 */
class UserProfile extends UserAbstract
{
15
    /**
16
     * @var ProviderInterface 
17
18
19
     */
    protected $identityProvider;
    
Bertrand Gauthier's avatar
Bertrand Gauthier committed
20
21
22
    /**
     * Point d'entrée.
     * 
23
     * @return self
Bertrand Gauthier's avatar
Bertrand Gauthier committed
24
25
26
27
28
29
30
31
32
33
34
35
36
     */
    public function __invoke()
    {
        return $this;
    }
    
    /**
     * Retourne le code HTML généré par cette aide de vue.
     *
     * @return string
     */
    public function __toString()
    {
37
38
39
40
41
42
43
44
45
46
        $title   = _("Profil utilisateur");
        $unknown = _("Inconnu");
        $none    = _("Aucun");

        if ($this->getTranslator()) {
            $title   = $this->getTranslator()->translate($title, $this->getTranslatorTextDomain());
            $unknown = $this->getTranslator()->translate($unknown, $this->getTranslatorTextDomain());
            $none    = $this->getTranslator()->translate($none, $this->getTranslatorTextDomain());
        }

47
        $roles = array();
48
49
        $identityRoles = $this->getIdentityProvider() ? $this->getIdentityProvider()->getIdentityRoles() : array();
        foreach ($identityRoles as $role) {
50
51
52
            $lib = '';
            if ($role instanceof NamedRole) {
                $lib = $role->getRoleName();
53
            }
54
55
            elseif ($role instanceof RoleInterface) {
                $lib = $role->getRoleId();
56
57
            }
            else {
58
59
60
61
62
63
64
65
66
67
68
                $fallback = null;
                set_error_handler(function() use (&$fallback, $unknown) { $fallback = $unknown; });
                $lib = (string) $role;
                restore_error_handler();
                $lib = $lib ?: $fallback;
            }
            if ($lib) {
                if ($this->getTranslator()) {
                    $lib = $this->getTranslator()->translate($lib, $this->getTranslatorTextDomain());
                }
                $roles[] = $lib;
69
70
            }
        }
71
72
        if (!$roles) {
            $roles[] = $none;
73
        }
74
75
76
77

        $html = "<strong>$title :</strong>" . PHP_EOL;
        $html .= $this->getView()->htmlList($roles);

78
79
80
81
82
        return $html;
    }
    
    /**
     * 
83
     * @return ProviderInterface
84
85
86
87
88
89
90
91
     */
    public function getIdentityProvider()
    {
        return $this->identityProvider;
    }

    /**
     * 
92
     * @param ProviderInterface $identityProvider
93
94
     * @return self
     */
95
    public function setIdentityProvider(ProviderInterface $identityProvider = null)
96
97
98
    {
        $this->identityProvider = $identityProvider;
        return $this;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
99
    }
100
}