示例#1
0
 /**
  * send
  *
  * @param ResetPassword $resetPassword
  * @param User $user
  * @param array $mailConfig
  *
  * @return mixed
  */
 public function sendRestPasswordEmail(ResetPassword $resetPassword, User $user, $mailConfig)
 {
     $toEmail = $user->getEmail();
     $fromEmail = $mailConfig['fromEmail'];
     $fromName = $mailConfig['fromName'];
     $subject = $mailConfig['subject'];
     $bodyTemplate = $mailConfig['body'];
     //Ignore blank emails
     if (!trim($toEmail)) {
         return;
     }
     $vars = ['name' => '', 'userId' => $user->getId(), 'url' => 'https://' . $_SERVER['HTTP_HOST'] . '/reset-password?fromPasswordResetEmail=1&id=' . $resetPassword->getResetId() . '&key=' . $resetPassword->getHashKey()];
     foreach ($vars as $name => $value) {
         $bodyTemplate = str_replace('__' . $name . '__', $value, $bodyTemplate);
         // Handle BC
         $bodyTemplate = str_replace('{' . $name . '}', $value, $bodyTemplate);
     }
     try {
         $html = new MimePart($bodyTemplate);
         $html->type = "text/html";
         $body = new MimeMessage();
         $body->setParts([$html]);
         $message = new Message();
         $message->setBody($body)->setFrom($fromEmail, $fromName)->setSubject($subject);
         foreach (explode(',', $toEmail) as $email) {
             $message->addTo(trim($email));
         }
         $transport = new \Zend\Mail\Transport\Sendmail();
         $transport->send($message);
     } catch (InvalidArgumentException $e) {
         // nothing
     }
 }
示例#2
0
 /**
  * switchBack
  *
  * @param User  $impersonatorUser
  * @param array $options
  *
  * @return Result
  * @throws \Exception
  */
 public function switchBack(User $impersonatorUser, $options = [])
 {
     // Get current user
     $currentUserId = $this->rcmUserService->getCurrentUser()->getId();
     $impersonatorUserId = $impersonatorUser->getId();
     $result = new Result();
     // Force login as $suUser
     $this->rcmUserService->getUserAuthService()->setIdentity($impersonatorUser);
     // log action
     $this->logAction($impersonatorUserId, $currentUserId, 'SU switched back', true);
     $result->setSuccess(true, 'SU switch back was successful');
     return $result;
 }
示例#3
0
 /**
  * switchBack
  *
  * @param User  $impersonatorUser
  * @param array $options
  *
  * @return Result
  * @throws \Exception
  */
 public function switchBack(User $impersonatorUser, $options = [])
 {
     if (!isset($options['suUserPassword'])) {
         throw new \Exception('suUserPassword required for AuthSwitcher');
     }
     $suUserPassword = $options['suUserPassword'];
     // Get current user
     $currentUserId = $this->rcmUserService->getCurrentUser()->getId();
     $impersonatorUserId = $impersonatorUser->getId();
     $result = new Result();
     $impersonatorUser->setPassword($suUserPassword);
     $authResult = $this->rcmUserService->authenticate($impersonatorUser);
     if (!$authResult->isValid()) {
         // ERROR
         // log action
         $this->logAction($impersonatorUserId, $currentUserId, 'SU attempted to switched back, provided incorrect credentials', true);
         $result->setSuccess(false, $authResult->getMessages()[0]);
         return $result;
     }
     // log action
     $this->logAction($impersonatorUserId, $currentUserId, 'SU switched back', true);
     $result->setSuccess(true, 'SU switch back was successful');
     return $result;
 }
示例#4
0
 /**
  * deleteUser
  *
  * @param User $requestUser requestUser
  *
  * @return mixed|Result
  */
 public function deleteUser(User $requestUser)
 {
     /* <LOW_LEVEL_PREP> */
     // require id
     $id = $requestUser->getId();
     if (empty($id)) {
         return new Result(null, Result::CODE_FAIL, 'User Id required for update.');
     }
     // check if exists
     $existingUserResult = $this->readUser($requestUser);
     if (!$existingUserResult->isSuccess()) {
         // ERROR
         return $existingUserResult;
     }
     $responseUser = new User();
     $responseUser->populate($existingUserResult->getUser());
     $requestUser = new ReadOnlyUser($requestUser);
     /* </LOW_LEVEL_PREP> */
     /* @event beforeDeleteUser */
     $results = $this->getEventManager()->trigger('beforeDeleteUser', $this, ['requestUser' => $requestUser, 'responseUser' => $responseUser], function ($result) {
         return !$result->isSuccess();
     });
     if ($results->stopped()) {
         return $results->last();
     }
     /* @event deleteUser */
     $results = $this->getEventManager()->trigger('deleteUser', $this, ['requestUser' => $requestUser, 'responseUser' => $responseUser], function ($result) {
         return !$result->isSuccess();
     });
     if ($results->stopped()) {
         $result = $results->last();
         $this->getEventManager()->trigger('deleteUserFail', $this, ['result' => $result]);
         return $result;
     }
     $result = new Result($responseUser);
     /* @event deleteUserSuccess */
     $this->getEventManager()->trigger('deleteUserSuccess', $this, ['result' => $result]);
     return $result;
 }
示例#5
0
 /**
  * getUserId
  *
  * @return mixed
  */
 public function getUserId()
 {
     return $this->suUser->getId();
 }
示例#6
0
 /**
  * canUpdate
  *
  * @param User $user user
  *
  * @return bool
  */
 public function canUpdate(User $user)
 {
     $id = $user->getId();
     if (empty($id)) {
         return false;
     }
     return true;
 }
示例#7
0
 /**
  * canRemove
  *
  * @param User   $user user
  * @param string $role role id
  *
  * @return bool
  */
 public function canRemove(User $user, $role)
 {
     $id = $user->getId();
     if (empty($id)) {
         return false;
     }
     return true;
 }
示例#8
0
 /**
  * testSetGet
  *
  * @covers \RcmUser\User\Entity\User
  *
  * @return void
  */
 public function testSetGet()
 {
     $user = new User();
     $value = 'id123';
     $user->setId($value);
     $this->assertEquals($value, $user->getId(), 'Setter or getter failed.');
     $value = 'usernamexxx';
     $user->setUsername($value);
     $this->assertEquals($value, $user->getUsername(), 'Setter or getter failed.');
     $value = '';
     $user->setUsername($value);
     $this->assertNull($user->getUsername(), 'Setter or getter failed.');
     $value = 'passwordxxx';
     $user->setPassword($value);
     $this->assertEquals($value, $user->getPassword(), 'Setter or getter failed.');
     $value = '';
     $user->setPassword($value);
     $this->assertNull($user->getPassword(), 'Setter or getter failed.');
     $value = 'statexxx';
     $user->setState($value);
     $this->assertEquals($value, $user->getState(), 'Setter or getter failed.');
     $value = '';
     $user->setState($value);
     $this->assertNull($user->getState(), 'Setter or getter failed.');
     $value = '*****@*****.**';
     $user->setEmail($value);
     $this->assertEquals($value, $user->getEmail(), 'Setter or getter failed.');
     $value = '';
     $user->setEmail($value);
     $this->assertNull($user->getEmail(), 'Setter or getter failed.');
     $value = 'namesxxx';
     $user->setName($value);
     $this->assertEquals($value, $user->getName(), 'Setter or getter failed.');
     $this->assertEquals($value, $user->get('name', null), 'Getter failed.');
     $value = '';
     $user->setName($value);
     $this->assertNull($user->getName(), 'Setter or getter failed.');
     // cannot set or get iterator
     $hasSet = $user->set('iterator', 'something');
     $this->assertFalse($hasSet, 'Failed to stop iterator property set.');
     $this->assertNull($user->get('iterator', null), 'Getter failed to exclude.');
     $value = null;
     $user->setProperties($value);
     $this->assertTrue(is_array($user->getProperties()), 'Setter or getter failed.');
     $pvalue = ['Y' => 'propertyYYY'];
     $value = 'propertyXXX';
     $user->setProperties($pvalue);
     $this->assertArrayHasKey('Y', $user->getProperties(), 'Setter or getter failed.');
     $user->setProperty('X', $value);
     $this->assertEquals($value, $user->getProperty('X'), 'Setter or getter failed.');
     $this->assertArrayHasKey('Y', $user->getProperties(), 'Setter or getter failed.');
     $this->assertTrue($user->getProperty('nope', 'not_found') === 'not_found', 'Setter or getter failed.');
     $this->assertEquals('propertyYYY', $user->get('Y', null), 'Getter failed.');
     $badPropertyName = 'N*P#_^^^^';
     $hasSet = $user->set($badPropertyName, 'something');
     $this->assertFalse($hasSet, 'Failed to stop bad property set.');
     $hasException = false;
     try {
         $user->setProperty($badPropertyName, 'something');
     } catch (RcmUserException $e) {
         $hasException = true;
         $this->assertInstanceOf('\\RcmUser\\Exception\\RcmUserException', $e);
     }
     if (!$hasException) {
         $this->fail("Expected exception not thrown");
     }
 }
 /**
  * switchToUser
  *
  * @param User  $targetUser
  * @param array $options
  *
  * @return Result
  */
 public function switchToUser(User $targetUser, $options = [])
 {
     // Get current user
     $currentUser = $this->rcmUserService->getCurrentUser();
     $result = new Result();
     if (empty($currentUser)) {
         // ERROR
         $this->logAction('UNKNOWN', $targetUser->getId(), 'SU was attempted by user who is not logged in', false);
         $result->setSuccess(false, 'Access denied');
         return $result;
     }
     // Run restrictions
     $restictionResult = $this->restriction->allowed($currentUser, $targetUser);
     if (!$restictionResult->isAllowed()) {
         // log action
         $this->logAction($currentUser->getId(), $targetUser->getId(), 'SU was attempted by user without access due to restriction', false);
         $result->setSuccess(false, $restictionResult->getMessage());
         return $result;
     }
     return $this->switcher->switchTo($targetUser, $options);
 }
示例#10
0
 /**
  * buildValidRoles
  *
  * @param User  $user  user
  * @param array $roles roles
  *
  * @return array
  */
 public function buildValidRoles(User $user, $roles = [])
 {
     if (!empty($roles)) {
         return $roles;
     }
     $id = $user->getId();
     if (empty($id)) {
         $roles = $this->getDefaultGuestRoleIds()->getData();
     } else {
         $roles = $this->getDefaultUserRoleIds()->getData();
     }
     return $roles;
 }
示例#11
0
文件: User.php 项目: reliv/rcm-user
 /**
  * Merges values of the $user arg into this user if the values are not set
  *
  * @param User $user user
  *
  * @return void
  */
 public function merge(User $user)
 {
     if ($this->getId() === null) {
         $this->setId($user->getId());
     }
     if ($this->getUsername() === null) {
         $this->setUsername($user->getUsername());
     }
     if ($this->getPassword() === null) {
         $this->setPassword($user->getPassword());
     }
     if ($this->getState() === null) {
         $this->setState($user->getState());
     }
     if ($this->getEmail() === null) {
         $this->setEmail($user->getEmail());
     }
     if ($this->getName() === null) {
         $this->setName($user->getName());
     }
     $properties = $user->getProperties();
     foreach ($properties as $key => $property) {
         $userProperty = $this->getProperty($key);
         if (empty($userProperty)) {
             $this->setProperty($key, $property);
         }
     }
 }
示例#12
0
 /**
  * parseDeletedUsername
  *
  * @param User $user user
  *
  * @return null|string
  */
 public function parseDeletedUsername(User $user)
 {
     try {
         $usernameArr = json_decode($user->getUsername(), true);
     } catch (\Exception $e) {
         return null;
     }
     if (count($usernameArr) !== 3) {
         return null;
     }
     if ($usernameArr[1] !== $user->getId()) {
         return null;
     }
     return $usernameArr[2];
 }
 /**
  * read
  *
  * @param User $user user
  *
  * @return Result
  */
 public function read(User $user)
 {
     $userId = $user->getId();
     if (empty($userId)) {
         return new Result([], Result::CODE_FAIL, 'User id required to get user roles.');
     }
     $query = $this->getEntityManager()->createQuery('SELECT userRole.roleId FROM ' . $this->getEntityClass() . ' userRole ' . 'INDEX BY userRole.roleId ' . 'WHERE userRole.userId = ?1');
     $query->setParameter(1, $userId);
     $userRoles = $query->getResult();
     $userAclRoles = [];
     foreach ($userRoles as $userRole) {
         $userAclRoles[] = $userRole['roleId'];
     }
     $message = '';
     if (empty($userAclRoles)) {
         $message = 'No roles found';
     }
     return new Result($userAclRoles, Result::CODE_SUCCESS, $message);
 }