示例#1
0
 /**
  * setPassword
  *
  * @param string $password password
  *
  * @return void
  * @throws \RcmUser\Exception\RcmUserReadOnlyException
  */
 public function setPassword($password)
 {
     if (!$this->locked) {
         return parent::setPassword($password);
     }
     throw new RcmUserReadOnlyException('Object is READ ONLY');
 }
示例#2
0
 protected function getNewUser($prefix = 'A')
 {
     $user = new User();
     $user->setId($prefix . '_id');
     $user->setUsername($prefix . '_username');
     $user->setPassword($prefix . '_password');
     $user->setState($prefix . '_state');
     $user->setProperties(['property1', $prefix . '_property1']);
     $user->setProperty('property2', $prefix . '_property2');
     return $user;
 }
示例#3
0
 /**
  * testConstruct
  *
  * @covers \RcmUser\User\Entity\ReadOnlyUser::__construct
  * @covers \RcmUser\User\Entity\ReadOnlyUser::populate
  *
  * @return void
  */
 public function testConstruct()
 {
     $user = new User();
     $user->setId('id');
     $user->setUsername('username');
     $user->setPassword('password');
     $user->setState('disabled');
     $user->setName('name');
     $user->setEmail('*****@*****.**');
     $user->setProperties(['A' => 'something']);
     return new ReadOnlyUser($user);
 }
示例#4
0
 /**
  * prepareUserUpdate
  *
  * @param User $requestUser  requestUser
  * @param User $responseUser responseUser
  * @param User $existingUser existingUser
  *
  * @return Result
  */
 public function prepareUserUpdate(User $requestUser, User $responseUser, User $existingUser)
 {
     // PASSWORD CHECKS
     $requestPassword = $requestUser->getPassword();
     $existingPassword = $existingUser->getPassword();
     //$hashedPassword = $existingPassword;
     // if password changed
     if ($existingPassword !== $requestPassword) {
         $hashedPassword = $this->getEncryptor()->create($requestPassword);
         $responseUser->setPassword($hashedPassword);
     }
     // STATE
     $requestState = $requestUser->getState();
     $existingState = $existingUser->getState();
     if ($requestState !== $existingState) {
         $responseUser->setState($requestState);
     }
     return new Result($responseUser);
 }
示例#5
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;
 }
示例#6
0
 protected function addUsers($username, $password, $fullname, $email)
 {
     $sm = $this->getServiceLocator();
     /** @var \RcmUser\Service\RcmUserService $userService */
     $userService = $sm->get('RcmUser\\Service\\RcmUserService');
     /** @var \RcmUser\User\Service\UserRoleService $userRoleService */
     $userRoleService = $sm->get('RcmUser\\User\\Service\\UserRoleService');
     /** @var \RcmUser\Acl\Service\AclDataService $aclDataService */
     $aclDataService = $sm->get('RcmUser\\Acl\\AclDataService');
     $user = new User();
     $user->setUsername($username);
     $user->setPassword($password);
     $user->setEmail($email);
     $user->setName($fullname);
     $user->setState('enabled');
     $response = $userService->createUser($user);
     if (!$response->isSuccess()) {
         throw new \Exception(implode("<br />", $response->getMessages()));
     }
     $user = $response->getData();
     $suRoleIdResponse = $aclDataService->getSuperAdminRoleId();
     $suRoleId = $suRoleIdResponse->getData();
     $suRole = new AclRole();
     $suRole->setRoleId($suRoleId);
     $suRole->setDescription('Super Admin Role');
     $response = $aclDataService->createRole($suRole);
     if (!$response->isSuccess()) {
         throw new \Exception(implode("<br />", $response->getMessages()));
     }
     $guestRoleIdResponse = $aclDataService->getGuestRoleId();
     $guestRoleId = $guestRoleIdResponse->getData();
     $guestRole = new AclRole();
     $guestRole->setRoleId($guestRoleId);
     $guestRole->setDescription('Default Guest');
     $response = $aclDataService->createRole($guestRole);
     if (!$response->isSuccess()) {
         throw new \Exception(implode("<br />", $response->getMessages()));
     }
     $response = $userRoleService->addRole($user, $suRoleId);
     if (!$response->isSuccess()) {
         throw new \Exception(implode("<br />", $response->getMessages()));
     }
 }
 /**
  * prepareUser
  *
  * @param User $user user
  *
  * @return User
  */
 public function prepareUser(User $user)
 {
     if ($this->getObfuscatePassword()) {
         $user->setPassword(User::PASSWORD_OBFUSCATE);
     }
     return $user;
 }
示例#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");
     }
 }
示例#9
0
文件: Tester.php 项目: reliv/rcm-user
 /**
  * testCase3
  *
  * @param ServiceLocatorInterface $serviceLocator serviceLocator
  * @param array                   $params         params
  *
  * @return string
  */
 public static function testCase3(ServiceLocatorInterface $serviceLocator, $params = [])
 {
     $startTime = time();
     $tester = new Tester($serviceLocator);
     $tester->testId = __FUNCTION__;
     $testUserId = null;
     $user = self::parseParam($params, 'user');
     $password = self::parseParam($params, 'userPlainTextPassword', 'pass_testCase_3_word1');
     $userRoles = self::parseParam($params, 'userRoles', ['admin']);
     // build new user if
     if (empty($user)) {
         $user = new User();
         $user->setUsername('testCase_3');
         $user->setPassword($password);
         $tester->addMessage("Create test user: "******"->buildUser result: " . json_encode($user, true));
         $user = $tester->testCreateUser($user);
         if (empty($user)) {
             $tester->addMessage("TEST FAILED");
             return $tester->getMessage();
         }
         $testUserId = $user->getId();
     }
     $resource = self::parseParam($params, 'resource', RcmUserAclResourceProvider::RESOURCE_ID_ROOT);
     $privilege = self::parseParam($params, 'privilege', '');
     $user->setPassword($password);
     $tester->addMessage("Log in user: "******"TEST FAILED");
         return $tester->getMessage();
     }
     $tester->addMessage("Verify logged in: ");
     $user = $tester->rcmUserService->getIdentity();
     if (empty($user->getId())) {
         $tester->addMessage("TEST FAILED");
         return $tester->getMessage();
     }
     $properties = $user->getProperty(UserRoleProperty::PROPERTY_KEY, 'NOT SET');
     if ($properties === 'NOT SET') {
         $tester->addMessage("TEST FAILED");
         return $tester->getMessage();
     }
     $tester->addMessage("Current user roles: " . json_encode($properties, true));
     /* ACL VALUES */
     $tester->addMessage("ACL Roles: " . json_encode($tester->authorizeService->getAcl(RcmUserAclResourceProvider::RESOURCE_ID_ROOT, 'RcmUser')->getRoles(), true));
     $tester->addMessage("ACL Resources: " . json_encode($tester->authorizeService->getAcl(RcmUserAclResourceProvider::RESOURCE_ID_ROOT, 'RcmUser')->getResources(), true));
     /* ACL CHECK *
        /* RcmUser */
     $tester->addMessage("ACL CHECK: rcmUserService->rcmUserIsAllowed({$resource}, {$privilege}) = " . json_encode($tester->rcmUserService->IsAllowed($resource, $privilege)));
     /* *
        $tester->addMessage(
            "ACL CHECK: viewHelper->rcmUserIsAllowed($resource, $privilege) = " .
            json_encode(
                $tester->rcmUserIsAllowed($resource, $privilege)
            )
        );
        $tester->addMessage(
            "ACL CHECK: ".
            "controllerPlugin->rcmUserIsAllowed($resource, $privilege) = " .
            json_encode(
                $tester->userController->rcmUserIsAllowed($resource, $privilege)
            )
        );
        /* */
     // clean up user if we created it
     if ($testUserId !== null) {
         $tester->addMessage("Clean up test user:"******"TEST FAILED");
             return $tester->getMessage();
         }
     }
     $tester->addMessage("TEST SUCCESS: [" . __FUNCTION__ . "] Time to complete:" . (time() - $startTime) . "sec");
     return $tester->getMessage();
 }
示例#10
0
 /**
  * testAuth
  *
  * @return void
  */
 public function testAuth()
 {
     $userAdapter = $this->testBuildUserAdapter();
     $user = new User('123');
     $user->setPassword('badpass');
     $userAdapter->setUser($user);
     $result = $userAdapter->authenticate();
     $this->assertFalse($result->isValid(), 'Username not set should return false');
     $user->setUsername('badusername');
     $userAdapter->setUser($user);
     $result = $userAdapter->authenticate();
     $this->assertFalse($result->isValid(), 'Bad username should return false');
     $user->setUsername('testusername');
     $userAdapter->setUser($user);
     $result = $userAdapter->authenticate();
     $this->assertFalse($result->isValid(), 'Bad password should return false');
     $user->setPassword('goodpass');
     $userAdapter->setUser($user);
     $result = $userAdapter->authenticate();
     $this->assertTrue($result->isValid(), 'Good password should return true');
 }