UserProfile.php 2.62 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
48
        $roles = array();
        foreach ($this->getIdentityProvider()->getIdentityRoles() as $role) {
49
50
51
            $lib = '';
            if ($role instanceof NamedRole) {
                $lib = $role->getRoleName();
52
            }
53
54
            elseif ($role instanceof RoleInterface) {
                $lib = $role->getRoleId();
55
56
            }
            else {
57
58
59
60
61
62
63
64
65
66
67
                $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;
68
69
            }
        }
70
71
        if (!$roles) {
            $roles[] = $none;
72
        }
73
74
75
76

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

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

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