Вход Регистрация
Файл: symfony-2.7/src/Symfony/Component/Security/Acl/Tests/Domain/PermissionGrantingStrategyTest.php
Строк: 242
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace SymfonyComponentSecurityAclTestsDomain;

use 
SymfonyComponentSecurityAclDomainObjectIdentity;
use 
SymfonyComponentSecurityAclDomainRoleSecurityIdentity;
use 
SymfonyComponentSecurityAclDomainAcl;
use 
SymfonyComponentSecurityAclDomainUserSecurityIdentity;
use 
SymfonyComponentSecurityAclDomainPermissionGrantingStrategy;
use 
SymfonyComponentSecurityAclExceptionNoAceFoundException;

class 
PermissionGrantingStrategyTest extends PHPUnit_Framework_TestCase
{
    public function 
testIsGrantedObjectAcesHavePriority()
    {
        
$strategy = new PermissionGrantingStrategy();
        
$acl $this->getAcl($strategy);
        
$sid = new UserSecurityIdentity('johannes''Foo');

        
$acl->insertClassAce($sid1);
        
$acl->insertObjectAce($sid10false);
        
$this->assertFalse($strategy->isGranted($acl, array(1), array($sid)));
    }

    public function 
testIsGrantedFallsBackToClassAcesIfNoApplicableObjectAceWasFound()
    {
        
$strategy = new PermissionGrantingStrategy();
        
$acl $this->getAcl($strategy);
        
$sid = new UserSecurityIdentity('johannes''Foo');

        
$acl->insertClassAce($sid1);
        
$this->assertTrue($strategy->isGranted($acl, array(1), array($sid)));
    }

    public function 
testIsGrantedFavorsLocalAcesOverParentAclAces()
    {
        
$strategy = new PermissionGrantingStrategy();
        
$sid = new UserSecurityIdentity('johannes''Foo');

        
$acl $this->getAcl($strategy);
        
$acl->insertClassAce($sid1);

        
$parentAcl $this->getAcl($strategy);
        
$acl->setParentAcl($parentAcl);
        
$parentAcl->insertClassAce($sid10false);

        
$this->assertTrue($strategy->isGranted($acl, array(1), array($sid)));
    }

    public function 
testIsGrantedFallsBackToParentAcesIfNoLocalAcesAreApplicable()
    {
        
$strategy = new PermissionGrantingStrategy();
        
$sid = new UserSecurityIdentity('johannes''Foo');
        
$anotherSid = new UserSecurityIdentity('ROLE_USER''Foo');

        
$acl $this->getAcl($strategy);
        
$acl->insertClassAce($anotherSid10false);

        
$parentAcl $this->getAcl($strategy);
        
$acl->setParentAcl($parentAcl);
        
$parentAcl->insertClassAce($sid1);

        
$this->assertTrue($strategy->isGranted($acl, array(1), array($sid)));
    }

    
/**
     * @expectedException SymfonyComponentSecurityAclExceptionNoAceFoundException
     */
    
public function testIsGrantedReturnsExceptionIfNoAceIsFound()
    {
        
$strategy = new PermissionGrantingStrategy();
        
$acl $this->getAcl($strategy);
        
$sid = new UserSecurityIdentity('johannes''Foo');

        
$strategy->isGranted($acl, array(1), array($sid));
    }

    public function 
testIsGrantedFirstApplicableEntryMakesUltimateDecisionForPermissionIdentityCombination()
    {
        
$strategy = new PermissionGrantingStrategy();
        
$acl $this->getAcl($strategy);
        
$sid = new UserSecurityIdentity('johannes''Foo');
        
$aSid = new RoleSecurityIdentity('ROLE_USER');

        
$acl->insertClassAce($aSid1);
        
$acl->insertClassAce($sid11false);
        
$acl->insertClassAce($sid12);
        
$this->assertFalse($strategy->isGranted($acl, array(1), array($sid$aSid)));

        
$acl->insertObjectAce($sid10false);
        
$acl->insertObjectAce($aSid11);
        
$this->assertFalse($strategy->isGranted($acl, array(1), array($sid$aSid)));
    }

    public function 
testIsGrantedCallsAuditLoggerOnGrant()
    {
        
$strategy = new PermissionGrantingStrategy();
        
$acl $this->getAcl($strategy);
        
$sid = new UserSecurityIdentity('johannes''Foo');

        
$logger $this->getMock('SymfonyComponentSecurityAclModelAuditLoggerInterface');
        
$logger
            
->expects($this->once())
            ->
method('logIfNeeded')
        ;
        
$strategy->setAuditLogger($logger);

        
$acl->insertObjectAce($sid1);
        
$acl->updateObjectAuditing(0truefalse);

        
$this->assertTrue($strategy->isGranted($acl, array(1), array($sid)));
    }

    public function 
testIsGrantedCallsAuditLoggerOnDeny()
    {
        
$strategy = new PermissionGrantingStrategy();
        
$acl $this->getAcl($strategy);
        
$sid = new UserSecurityIdentity('johannes''Foo');

        
$logger $this->getMock('SymfonyComponentSecurityAclModelAuditLoggerInterface');
        
$logger
            
->expects($this->once())
            ->
method('logIfNeeded')
        ;
        
$strategy->setAuditLogger($logger);

        
$acl->insertObjectAce($sid10false);
        
$acl->updateObjectAuditing(0falsetrue);

        
$this->assertFalse($strategy->isGranted($acl, array(1), array($sid)));
    }

    
/**
     * @dataProvider getAllStrategyTests
     */
    
public function testIsGrantedStrategies($maskStrategy$aceMask$requiredMask$result)
    {
        
$strategy = new PermissionGrantingStrategy();
        
$acl $this->getAcl($strategy);
        
$sid = new UserSecurityIdentity('johannes''Foo');

        
$acl->insertObjectAce($sid$aceMask0true$maskStrategy);

        if (
false === $result) {
            try {
                
$strategy->isGranted($acl, array($requiredMask), array($sid));
                
$this->fail('The ACE is not supposed to match.');
            } catch (
NoAceFoundException $noAce) {
            }
        } else {
            
$this->assertTrue($strategy->isGranted($acl, array($requiredMask), array($sid)));
        }
    }

    public function 
getAllStrategyTests()
    {
        return array(
            array(
'all'<< << 1<< 0true),
            array(
'all'<< << 1<< 2false),
            array(
'all'<< << 10<< << 10true),
            array(
'all'<< << 1<< << || << 2false),
            array(
'any'<< << 1<< 0true),
            array(
'any'<< << 1<< << 2true),
            array(
'any'<< << 1<< 2false),
            array(
'equal'<< << 1<< 0false),
            array(
'equal'<< << 1<< 1false),
            array(
'equal'<< << 1<< << 1true),
        );
    }

    protected function 
getAcl($strategy)
    {
        static 
$id 1;

        return new 
Acl($id++, new ObjectIdentity(1'Foo'), $strategy, array(), true);
    }
}
Онлайн: 2
Реклама