本文整理汇总了PHP中Symfony\Component\Security\Core\User\UserInterface::getRoles方法的典型用法代码示例。如果您正苦于以下问题:PHP UserInterface::getRoles方法的具体用法?PHP UserInterface::getRoles怎么用?PHP UserInterface::getRoles使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Security\Core\User\UserInterface
的用法示例。
在下文中一共展示了UserInterface::getRoles方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: isGranted
/**
*
* @param string $attribute
* @param Club $club
* @param UserInterface $user
* @return boolean
*/
protected function isGranted($attribute, $club, $user = null)
{
switch ($attribute) {
case self::VIEW:
if (!$club->isPrivate()) {
return true;
}
// make sure there is a user object (i.e. that the user is logged in)
if (!$user instanceof UserInterface) {
return false;
}
if (in_array('ROLE_ADMIN', $user->getRoles())) {
return true;
}
break;
case self::EDIT:
// make sure there is a user object (i.e. that the user is logged in)
if (!$user instanceof UserInterface) {
return false;
}
foreach ($club->getAdministrators() as $administrator) {
if ($administrator->getId() == $user->getId()) {
return true;
}
}
if (in_array('ROLE_ADMIN', $user->getRoles())) {
return true;
}
return false;
case self::CREATE:
break;
}
return false;
}
示例2: isEqualTo
public function isEqualTo(UserInterface $user)
{
if ($user instanceof User) {
// Check that the roles are the same, in any order
$isEqual = count($this->getRoles()) == count($user->getRoles());
if ($isEqual) {
foreach ($this->getRoles() as $role) {
$isEqual = $isEqual && in_array($role, $user->getRoles());
}
}
return $isEqual;
}
return false;
}
示例3: authenticateUser
private function authenticateUser(UserInterface $user)
{
$providerKey = 'secured_area';
// your firewall name
$token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
$this->getSecurityContext()->setToken($token);
}
示例4: logUser
private function logUser(UserInterface $user, $password)
{
$token = new UsernamePasswordToken($user, $password, 'secured_area', $user->getRoles());
$request = $this->getRequest();
$session = $request->getSession();
$session->set('_security_secured_area', serialize($token));
}
示例5: isEqualTo
public function isEqualTo(UserInterface $user)
{
if (!$user instanceof LdapUser || $user->getUsername() !== $this->username || $user->getEmail() !== $this->email || count(array_diff($user->getRoles(), $this->roles)) > 0 || $user->getDn() !== $this->dn) {
return false;
}
return true;
}
示例6: processRecord
/**
* @param array $record
*
* @return array
*/
public function processRecord(array $record)
{
if (is_null($this->user)) {
/* @var TokenStorageInterface $securityTokenStorage */
$securityTokenStorage = $this->container->get('security.token_storage');
if ($securityTokenStorage !== null && $securityTokenStorage->getToken() !== null && $securityTokenStorage->getToken()->getUser() instanceof \Symfony\Component\Security\Core\User\AdvancedUserInterface) {
$this->user = $securityTokenStorage->getToken()->getUser();
$this->record['extra']['user']['username'] = $this->user->getUsername();
$this->record['extra']['user']['roles'] = $this->user->getRoles();
$this->record['extra']['user']['is_account_non_expired'] = $this->user->isAccountNonExpired();
$this->record['extra']['user']['is_account_non_locked'] = $this->user->isAccountNonLocked();
$this->record['extra']['user']['is_credentials_non_expired'] = $this->user->isCredentialsNonExpired();
$this->record['extra']['user']['is_enabled'] = $this->user->isEnabled();
}
}
return array_merge($record, $this->record);
}
示例7: check
/**
* Check whether a user is granted a specific role, respecting the role hierarchy.
*
* @param UserInterface $user
* @param $requiredRole
* @return bool Whether this user is granted the $requiredRole
*/
public function check(UserInterface $user, $requiredRole)
{
$roles = array();
foreach ($user->getRoles() as $roleName) {
$roles[] = new Role($roleName);
}
$token = new AnonymousToken('dummy', 'dummy', $roles);
return static::ACCESS_GRANTED == $this->vote($token, null, array($requiredRole));
}
示例8: __construct
public function __construct(UserInterface $user, $providerKey)
{
parent::__construct($user->getRoles());
if (empty($providerKey)) {
throw new \InvalidArgumentException('$providerKey must not be empty.');
}
$this->providerKey = $providerKey;
$this->setUser($user);
parent::setAuthenticated(true);
}
示例9: __construct
public function __construct($providerKey, UserInterface $user = null)
{
if ($user) {
parent::__construct($user->getRoles());
$this->setUser($user);
} else {
parent::__construct();
}
$this->providerKey = $providerKey;
}
示例10: isGranted
/**
* Returns true if $user is granted $requiredRole
* @param $requiredRole
* @param UserInterface $user
* @return bool
*/
public function isGranted($requiredRole, UserInterface $user)
{
$requiredRole = new Role($requiredRole);
foreach ($user->getRoles() as $role) {
$hierarchy = $this->roleHierarchy->getReachableRoles([new Role($role)]);
if (in_array($requiredRole, $hierarchy)) {
return true;
}
}
return false;
}
示例11: getSecurityRole
/**
* Get security role depending on the roles of the user
*
* @param \Symfony\Component\Security\Core\User\UserInterface $user
* @return string
*/
protected function getSecurityRole(UserInterface $user)
{
$securityRole = 'ROLE_SYSTEM_ADMIN';
foreach ($user->getRoles() as $role) {
if (in_array($role, $this->adminRoles)) {
$securityRole = 'ROLE_ADMIN';
break;
}
}
return $securityRole;
}
示例12: getRoles
/**
* Retrieves roles from user and appends SwitchUserRole if original token contained one.
*
* @param UserInterface $user The user
* @param TokenInterface $token The token
*
* @return array The user roles
*/
private function getRoles(UserInterface $user, TokenInterface $token)
{
$roles = $user->getRoles();
foreach ($token->getRoles() as $role) {
if ($role instanceof SwitchUserRole) {
$roles[] = $role;
break;
}
}
return $roles;
}
示例13: processRecord
/**
* @param array $record
*
* @return array
*/
public function processRecord(array $record)
{
if (is_null($this->user)) {
/* @var SecurityContextInterface $securityContext */
$securityContext = null;
try {
$this->container->get("security.context");
} catch (ServiceCircularReferenceException $e) {
//since the securitycontext is deprecated getting the context from the container results in a log line which tries to use this method again....
}
if ($securityContext !== null && $securityContext->getToken() !== null && $securityContext->getToken()->getUser() instanceof \Symfony\Component\Security\Core\User\AdvancedUserInterface) {
$this->user = $securityContext->getToken()->getUser();
$this->record['extra']['user']['username'] = $this->user->getUsername();
$this->record['extra']['user']['roles'] = $this->user->getRoles();
$this->record['extra']['user']['is_account_non_expired'] = $this->user->isAccountNonExpired();
$this->record['extra']['user']['is_account_non_locked'] = $this->user->isAccountNonLocked();
$this->record['extra']['user']['is_credentials_non_expired'] = $this->user->isCredentialsNonExpired();
$this->record['extra']['user']['is_enabled'] = $this->user->isEnabled();
}
}
return array_merge($record, $this->record);
}
示例14: authenticate
/**
* Creates an authenticated client against a firewall.
*
* @param Symfony\Component\Security\Core\User\UserInterface $user
* @param string $firewall
* @return Symfony\Bundle\FrameworkBundle\Client
*/
protected function authenticate(UserInterface $user, $firewall)
{
$securityKey = sprintf('_security_%s', $firewall);
$client = $this->getClient();
$client->followRedirects(false);
$session = $client->getContainer()->get('session');
$session->start();
$cookie = new Cookie($session->getName(), $session->getId());
$client->getCookieJar()->set($cookie);
$token = new UsernamePasswordToken($user, null, $firewall, $user->getRoles());
$session->set($securityKey, serialize($token));
$session->save();
return $client;
}
示例15: equals
public function equals(UserInterface $user)
{
if (!$user instanceof LdapUser) {
return false;
}
if ($user->getUsername() !== $this->username) {
return false;
}
if ($user->getEmail() !== $this->email) {
return false;
}
if ($user->getRoles() !== $this->roles) {
return false;
}
if ($user->getDn() !== $this->dn) {
return false;
}
return true;
}