UserProfile.php 2.02 KB
Newer Older
Bertrand Gauthier's avatar
Bertrand Gauthier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace UnicaenAuth\View\Helper;

use BjyAuthorize\Provider\Identity\ProviderInterface as IdentityProvider;

/**
 * Aide de vue permettant d'afficher le profil de l'utilisateur connecté.
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
 */
class UserProfile extends UserAbstract
{
    /**
     * @var IdentityProvider 
     */
    protected $identityProvider;
    
    /**
     * Point d'entrée.
     * 
     * @return UserProfile
     */
    public function __invoke()
    {
        return $this;
    }
    
    /**
     * Retourne le code HTML généré par cette aide de vue.
     *
     * @return string
     */
    public function __toString()
    {
        if (!($identity = $this->getIdentity())) {
            return null;
        }

        if ($identity instanceof \Zend\Permissions\Acl\Role\RoleInterface) {
            $roles = array($identity->getRoleId());
        }
        else {
            $roles = $this->getIdentityRoles();
        }

        $template = '<strong>' . $this->getView()->translate("Profil") . ' :</strong> %s';
        
        $roles = $this->getView()->htmlList($roles, $ordered = false/*, $attribs = false, $escape = false*/);
        $out = sprintf($template, $roles);
        
        return $out;
    }
    
    /**
     * 
     * @return array
     */
    protected function getIdentityRoles()
    {
        $roles = array();
        if ($this->getIdentityProvider()) {
            $roles = $this->getIdentityProvider()->getIdentityRoles();
        }
        if (!$roles) {
            $roles = array($this->getView()->translate("Aucun"));
        }
        return $roles;
    }

    /**
     * 
     * @param IdentityProvider $provider
     * @return \UnicaenAuth\View\Helper\UserProfile
     */
    public function setIdentityProvider(IdentityProvider $provider)
    {
        $this->identityProvider = $provider;
        return $this;
    }
    
    /**
     * 
     * @return IdentityProvider
     */
    public function getIdentityProvider()
    {
        return $this->identityProvider;
    }
}