|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Security\Acl\Domain; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
|
15 |
use Symfony\Component\Security\Core\User\UserInterface; |
|
|
16 |
use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface; |
|
|
17 |
|
|
|
18 |
/** |
|
|
19 |
* A SecurityIdentity implementation used for actual users |
|
|
20 |
* |
|
|
21 |
* @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
|
22 |
*/ |
|
|
23 |
final class UserSecurityIdentity implements SecurityIdentityInterface |
|
|
24 |
{ |
|
|
25 |
private $username; |
|
|
26 |
private $class; |
|
|
27 |
|
|
|
28 |
/** |
|
|
29 |
* Constructor |
|
|
30 |
* |
|
|
31 |
* @param string $username the username representation |
|
|
32 |
* @param string $class the user's fully qualified class name |
|
|
33 |
*/ |
|
|
34 |
public function __construct($username, $class) |
|
|
35 |
{ |
|
|
36 |
if (empty($username)) { |
|
|
37 |
throw new \InvalidArgumentException('$username must not be empty.'); |
|
|
38 |
} |
|
|
39 |
if (empty($class)) { |
|
|
40 |
throw new \InvalidArgumentException('$class must not be empty.'); |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
$this->username = (string) $username; |
|
|
44 |
$this->class = $class; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
/** |
|
|
48 |
* Creates a user security identity from an UserInterface |
|
|
49 |
* |
|
|
50 |
* @param UserInterface $user |
|
|
51 |
* @return UserSecurityIdentity |
|
|
52 |
*/ |
|
|
53 |
static public function fromAccount(UserInterface $user) |
|
|
54 |
{ |
|
|
55 |
return new self($user->getUsername(), get_class($user)); |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
/** |
|
|
59 |
* Creates a user security identity from a TokenInterface |
|
|
60 |
* |
|
|
61 |
* @param TokenInterface $token |
|
|
62 |
* @return UserSecurityIdentity |
|
|
63 |
*/ |
|
|
64 |
static public function fromToken(TokenInterface $token) |
|
|
65 |
{ |
|
|
66 |
$user = $token->getUser(); |
|
|
67 |
|
|
|
68 |
if ($user instanceof UserInterface) { |
|
|
69 |
return self::fromAccount($user); |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
return new self((string) $user, is_object($user)? get_class($user) : get_class($token)); |
|
|
73 |
} |
|
|
74 |
|
|
|
75 |
/** |
|
|
76 |
* Returns the username |
|
|
77 |
* |
|
|
78 |
* @return string |
|
|
79 |
*/ |
|
|
80 |
public function getUsername() |
|
|
81 |
{ |
|
|
82 |
return $this->username; |
|
|
83 |
} |
|
|
84 |
|
|
|
85 |
/** |
|
|
86 |
* Returns the user's class name |
|
|
87 |
* |
|
|
88 |
* @return string |
|
|
89 |
*/ |
|
|
90 |
public function getClass() |
|
|
91 |
{ |
|
|
92 |
return $this->class; |
|
|
93 |
} |
|
|
94 |
|
|
|
95 |
/** |
|
|
96 |
* {@inheritDoc} |
|
|
97 |
*/ |
|
|
98 |
public function equals(SecurityIdentityInterface $sid) |
|
|
99 |
{ |
|
|
100 |
if (!$sid instanceof UserSecurityIdentity) { |
|
|
101 |
return false; |
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
return $this->username === $sid->getUsername() |
|
|
105 |
&& $this->class === $sid->getClass(); |
|
|
106 |
} |
|
|
107 |
|
|
|
108 |
/** |
|
|
109 |
* A textual representation of this security identity. |
|
|
110 |
* |
|
|
111 |
* This is not used for equality comparison, but only for debugging. |
|
|
112 |
* |
|
|
113 |
* @return string |
|
|
114 |
*/ |
|
|
115 |
public function __toString() |
|
|
116 |
{ |
|
|
117 |
return sprintf('UserSecurityIdentity(%s, %s)', $this->username, $this->class); |
|
|
118 |
} |
|
|
119 |
} |