/** * @test */ public function setRolesWorks() { $roles = [$this->administratorRole, $this->customerRole]; $expectedRoles = [$this->administratorRole->getIdentifier() => $this->administratorRole, $this->customerRole->getIdentifier() => $this->customerRole]; $this->account->setRoles($roles); $this->assertSame($expectedRoles, $this->account->getRoles()); }
public function setUp() { $this->mockRole = $this->getMockBuilder(Role::class)->disableOriginalConstructor()->getMock(); $this->mockRole->expects($this->any())->method('getIdentifier')->will($this->returnValue('Neos.Flow:TestRoleIdentifier')); $this->mockPolicyService = $this->getMockBuilder(PolicyService::class)->disableOriginalConstructor()->getMock(); $this->mockPolicyService->expects($this->any())->method('getRole')->with('Neos.Flow:TestRoleIdentifier')->will($this->returnValue($this->mockRole)); $this->mockHashService = $this->getMockBuilder(HashService::class)->disableOriginalConstructor()->getMock(); $expectedPassword = $this->testKeyClearText; $expectedHashedPasswordAndSalt = $this->testKeyHashed; $this->mockHashService->expects($this->any())->method('validatePassword')->will($this->returnCallback(function ($password, $hashedPasswordAndSalt) use($expectedPassword, $expectedHashedPasswordAndSalt) { return $hashedPasswordAndSalt === $expectedHashedPasswordAndSalt && $password === $expectedPassword; })); $this->mockFileBasedSimpleKeyService = $this->getMockBuilder(FileBasedSimpleKeyService::class)->disableOriginalConstructor()->getMock(); $this->mockFileBasedSimpleKeyService->expects($this->any())->method('getKey')->with('testKey')->will($this->returnValue($this->testKeyHashed)); $this->mockToken = $this->getMockBuilder(PasswordToken::class)->disableOriginalConstructor()->getMock(); }
/** * @dataProvider roleIdentifiersAndPackageKeysAndNames * @test */ public function setNameAndPackageKeyWorks($roleIdentifier, $name, $packageKey) { $role = new Role($roleIdentifier); $this->assertEquals($name, $role->getName()); $this->assertEquals($packageKey, $role->getPackageKey()); }
/** * Parses the global policy configuration and initializes roles and privileges accordingly * * @return void * @throws SecurityException */ protected function initialize() { if ($this->initialized) { return; } $this->policyConfiguration = $this->configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_POLICY); $this->emitConfigurationLoaded($this->policyConfiguration); $this->initializePrivilegeTargets(); $privilegeTargetsForEverybody = $this->privilegeTargets; $this->roles = []; $everybodyRole = new Role('Neos.Flow:Everybody'); $everybodyRole->setAbstract(true); if (isset($this->policyConfiguration['roles'])) { foreach ($this->policyConfiguration['roles'] as $roleIdentifier => $roleConfiguration) { if ($roleIdentifier === 'Neos.Flow:Everybody') { $role = $everybodyRole; } else { $role = new Role($roleIdentifier); if (isset($roleConfiguration['abstract'])) { $role->setAbstract((bool) $roleConfiguration['abstract']); } } if (isset($roleConfiguration['privileges'])) { foreach ($roleConfiguration['privileges'] as $privilegeConfiguration) { $privilegeTargetIdentifier = $privilegeConfiguration['privilegeTarget']; if (!isset($this->privilegeTargets[$privilegeTargetIdentifier])) { throw new SecurityException(sprintf('privilege target "%s", referenced in role configuration "%s" is not defined!', $privilegeTargetIdentifier, $roleIdentifier), 1395869320); } $privilegeTarget = $this->privilegeTargets[$privilegeTargetIdentifier]; if (!isset($privilegeConfiguration['permission'])) { throw new SecurityException(sprintf('No permission set for privilegeTarget "%s" in Role "%s"', $privilegeTargetIdentifier, $roleIdentifier), 1395869331); } $privilegeParameters = isset($privilegeConfiguration['parameters']) ? $privilegeConfiguration['parameters'] : []; try { $privilege = $privilegeTarget->createPrivilege($privilegeConfiguration['permission'], $privilegeParameters); } catch (\Exception $exception) { throw new SecurityException(sprintf('Error for privilegeTarget "%s" in Role "%s": %s', $privilegeTargetIdentifier, $roleIdentifier, $exception->getMessage()), 1401886654, $exception); } $role->addPrivilege($privilege); if ($roleIdentifier === 'Neos.Flow:Everybody') { unset($privilegeTargetsForEverybody[$privilegeTargetIdentifier]); } } } $this->roles[$roleIdentifier] = $role; } } // create ABSTAIN privilege for all uncovered privilegeTargets /** @var PrivilegeTarget $privilegeTarget */ foreach ($privilegeTargetsForEverybody as $privilegeTarget) { if ($privilegeTarget->hasParameters()) { continue; } $everybodyRole->addPrivilege($privilegeTarget->createPrivilege(PrivilegeInterface::ABSTAIN)); } $this->roles['Neos.Flow:Everybody'] = $everybodyRole; // Set parent roles /** @var Role $role */ foreach ($this->roles as $role) { if (isset($this->policyConfiguration['roles'][$role->getIdentifier()]['parentRoles'])) { foreach ($this->policyConfiguration['roles'][$role->getIdentifier()]['parentRoles'] as $parentRoleIdentifier) { $role->addParentRole($this->roles[$parentRoleIdentifier]); } } } $this->emitRolesInitialized($this->roles); $this->initialized = true; }
/** * Adds a role to this account * * @param Role $role * @return void * @throws \InvalidArgumentException * @api */ public function addRole(Role $role) { if ($role->isAbstract()) { throw new \InvalidArgumentException(sprintf('Abstract roles can\'t be assigned to accounts directly, but the role "%s" is marked abstract', $role->getIdentifier()), 1399900657); } $this->initializeRoles(); if (!$this->hasRole($role)) { $roleIdentifier = $role->getIdentifier(); $this->roleIdentifiers[] = $roleIdentifier; $this->roles[$roleIdentifier] = $role; } }
/** * Returns TRUE if the given role is a directly assigned parent of this role. * * @param Role $role * @return boolean */ public function hasParentRole(Role $role) { return isset($this->parentRoles[$role->getIdentifier()]); }