You can optionally extend this class if you want to inherit the common getter functions.
Inheritance: implements Webiny\Component\Security\User\UserInterface, use trait Webiny\Component\StdLib\StdLibTrait
Example #1
0
 /**
  * This function gets the current user object and needs to validate its access against the required roles.
  * The function must either return ACCESS_GRANTED, ACCESS_ABSTAIN or ACCESS_DENIED.
  *
  * @param AbstractUser $user           Current user instance.
  * @param array        $requestedRoles An array of requested roles for the current access map.
  *
  * @return integer ACCESS_GRANTED, ACCESS_ABSTAIN or ACCESS_DENIED.
  */
 public function vote(AbstractUser $user, array $requestedRoles)
 {
     $result = self::ACCESS_DENIED;
     if ($user->isAuthenticated()) {
         $result = self::ACCESS_GRANTED;
     }
     return $result;
 }
Example #2
0
 /**
  * Stores user data into an array, encrypts it and returns the encrypted string.
  *
  * @param AbstractUser $user Instance of AbstractUser class that holds the pre-filled object from user provider.
  *
  * @return string
  */
 public function encryptUserData(AbstractUser $user)
 {
     // data (we use short syntax to reduce the size of the cookie or session)
     $data = ['u' => $user->getUsername(), 'vu' => $this->tokenRememberMe ? time() + 86400 * 30 : time() + 86400, 'ap' => $user->getAuthProviderName(), 'up' => $user->getUserProviderName()];
     // build and add token to $data
     $token = $this->getCrypt()->encrypt($this->jsonEncode($data), $this->getEncryptionKey());
     $token = urlencode(rtrim($token, '='));
     return $token;
 }
Example #3
0
 /**
  * This function gets the current user object and needs to validate its access against the required roles.
  * The function must either return ACCESS_GRANTED, ACCESS_ABSTAIN or ACCESS_DENIED.
  *
  * @param AbstractUser $user           Current user instance.
  * @param array        $requestedRoles An array of requested roles for the current access map.
  *
  * @return integer ACCESS_GRANTED, ACCESS_ABSTAIN or ACCESS_DENIED.
  */
 public function vote(AbstractUser $user, array $requestedRoles)
 {
     $result = self::ACCESS_ABSTAIN;
     $userRoles = $user->getRoles();
     foreach ($requestedRoles as $role) {
         if (!$this->supportsRole($role)) {
             continue;
         }
         $result = self::ACCESS_DENIED;
         foreach ($userRoles as $ur) {
             /**
              * @var $ur Role
              */
             if ($role->getRole() === $ur->getRole()) {
                 return self::ACCESS_GRANTED;
             }
         }
     }
     return $result;
 }
Example #4
0
 /**
  * Sets roles for current user.
  */
 private function setUserRoles()
 {
     $this->initRoleHierarchy();
     $this->user->setRoles($this->roleHierarchy->getAccessibleRoles($this->user->getRoles()));
 }
 /**
  * Stores user data into an array, encrypts it and returns the encrypted string.
  *
  * @param AbstractUser $user Instance of AbstractUser class that holds the pre-filled object from user provider.
  *
  * @return string
  */
 public function encryptUserData(AbstractUser $user)
 {
     // extract the roles
     $roles = $user->getRoles();
     $roleArray = [];
     foreach ($roles as $r) {
         $roleArray[] = $r->getRole();
     }
     // data (we use short syntax to reduce the size of the cookie or session)
     $data = ['u' => $user->getUsername(), 'r' => $roleArray, 'vu' => time() + 86400 * 30, 'sid' => $this->httpSession()->getSessionId(), 'ap' => $user->getAuthProviderName()];
     // build and add token to $data
     return $this->getCrypt()->encrypt($this->jsonEncode($data), $this->getEncryptionKey());
 }
Example #6
0
 /**
  * Base constructor.
  */
 public function __construct()
 {
     parent::populate('anonymous', '', [], false);
 }