/** * setUsername * * @param string $username username * * @return void * @throws \RcmUser\Exception\RcmUserReadOnlyException */ public function setUsername($username) { if (!$this->locked) { return parent::setUsername($username); } throw new RcmUserReadOnlyException('Object is READ ONLY'); }
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; }
/** * 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); }
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())); } }
/** * 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"); } }
/** * 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(); }
/** * delete * * @param User $requestUser requestUser * @param User $responseUser responseUser * * @return mixed|Result */ public function delete(User $requestUser, User $responseUser) { /* VALIDATE */ if (!$this->canDelete($requestUser)) { // error, cannot update return new Result($responseUser, Result::CODE_FAIL, 'User cannot be deleted, id required for delete.'); } /* PREPARE */ $responseUser->setUsername($this->buildDeletedUsername($responseUser)); $responseUser->setState(self::USER_DELETED_STATE); /* SAVE */ $responseUser = $this->getValidInstance($responseUser); // @todo if error, fail with null $this->getEntityManager()->merge($responseUser); /* by default, we should not support true delete $this->getEntityManager()->remove($user); */ $this->getEntityManager()->flush(); return new Result($responseUser); }