2009-10-16 00:27:56 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Permission;
|
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\PermissionCheckboxSetField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2012-07-01 07:55:20 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2009-10-16 00:27:56 +02:00
|
|
|
class PermissionTest extends SapphireTest {
|
2012-07-01 07:55:20 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'PermissionTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetCodesGrouped() {
|
2010-04-13 01:41:33 +02:00
|
|
|
$codes = Permission::get_codes();
|
|
|
|
$this->assertArrayNotHasKey('SITETREE_VIEW_ALL', $codes);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetCodesUngrouped() {
|
2015-08-26 16:46:52 +02:00
|
|
|
$codes = Permission::get_codes(false);
|
2010-04-13 01:41:33 +02:00
|
|
|
$this->assertArrayHasKey('SITETREE_VIEW_ALL', $codes);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDirectlyAppliedPermissions() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'author');
|
2009-10-16 00:27:56 +02:00
|
|
|
$this->assertTrue(Permission::checkMember($member, "SITETREE_VIEW_ALL"));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-08-26 16:46:52 +02:00
|
|
|
public function testCMSAccess() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$members = Member::get()->byIDs($this->allFixtureIDs('SilverStripe\\Security\\Member'));
|
2015-08-26 16:46:52 +02:00
|
|
|
foreach ($members as $member) {
|
|
|
|
$this->assertTrue(Permission::checkMember($member, 'CMS_ACCESS'));
|
2016-05-27 05:49:05 +02:00
|
|
|
$this->assertTrue(Permission::checkMember($member, array('CMS_ACCESS', 'CMS_ACCESS_Security')));
|
|
|
|
$this->assertTrue(Permission::checkMember($member, array('CMS_ACCESS_Security', 'CMS_ACCESS')));
|
2015-08-26 16:46:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$member = new Member();
|
|
|
|
$member->update(array(
|
|
|
|
'FirstName' => 'No CMS',
|
|
|
|
'Surname' => 'Access',
|
|
|
|
'Email' => 'no-access@example.com',
|
2015-08-20 20:53:09 +02:00
|
|
|
));
|
|
|
|
$member->write();
|
2015-08-26 16:46:52 +02:00
|
|
|
$this->assertFalse(Permission::checkMember($member, 'CMS_ACCESS'));
|
2016-05-27 05:49:05 +02:00
|
|
|
$this->assertFalse(Permission::checkMember($member, array('CMS_ACCESS', 'CMS_ACCESS_Security')));
|
|
|
|
$this->assertFalse(Permission::checkMember($member, array('CMS_ACCESS_Security', 'CMS_ACCESS')));
|
2015-08-26 16:46:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLeftAndMainAccessAll() {
|
|
|
|
//add user and group
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'leftandmain');
|
2015-08-20 20:53:09 +02:00
|
|
|
|
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin"));
|
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin"));
|
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin"));
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testPermissionAreInheritedFromOneRole() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'author');
|
2011-03-29 06:54:08 +02:00
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin"));
|
2009-10-16 00:27:56 +02:00
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin"));
|
|
|
|
$this->assertFalse(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin"));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testPermissionAreInheritedFromMultipleRoles() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'access');
|
2011-03-29 06:54:08 +02:00
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin"));
|
2009-10-16 00:27:56 +02:00
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin"));
|
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin"));
|
|
|
|
$this->assertTrue(Permission::checkMember($member, "EDIT_PERMISSIONS"));
|
|
|
|
$this->assertFalse(Permission::checkMember($member, "SITETREE_VIEW_ALL"));
|
|
|
|
}
|
2012-07-01 07:55:20 +02:00
|
|
|
|
2015-08-20 20:53:09 +02:00
|
|
|
public function testPermissionsForMember() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'access');
|
2012-07-01 07:55:20 +02:00
|
|
|
$permissions = Permission::permissions_for_member($member->ID);
|
|
|
|
$this->assertEquals(4, count($permissions));
|
|
|
|
$this->assertTrue(in_array('CMS_ACCESS_MyAdmin', $permissions));
|
|
|
|
$this->assertTrue(in_array('CMS_ACCESS_AssetAdmin', $permissions));
|
|
|
|
$this->assertTrue(in_array('CMS_ACCESS_SecurityAdmin', $permissions));
|
|
|
|
$this->assertTrue(in_array('EDIT_PERMISSIONS', $permissions));
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$group = $this->objFromFixture("SilverStripe\\Security\\Group", "access");
|
2012-07-01 07:55:20 +02:00
|
|
|
|
|
|
|
Permission::deny($group->ID, "CMS_ACCESS_MyAdmin");
|
|
|
|
$permissions = Permission::permissions_for_member($member->ID);
|
|
|
|
$this->assertEquals(3, count($permissions));
|
|
|
|
$this->assertFalse(in_array('CMS_ACCESS_MyAdmin', $permissions));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRolesAndPermissionsFromParentGroupsAreInherited() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'globalauthor');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-16 00:27:56 +02:00
|
|
|
// Check that permissions applied to the group are there
|
|
|
|
$this->assertTrue(Permission::checkMember($member, "SITETREE_EDIT_ALL"));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-16 00:27:56 +02:00
|
|
|
// Check that roles from parent groups are there
|
2011-03-29 06:54:08 +02:00
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin"));
|
2009-10-16 00:27:56 +02:00
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin"));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-16 00:27:56 +02:00
|
|
|
// Check that permissions from parent groups are there
|
|
|
|
$this->assertTrue(Permission::checkMember($member, "SITETREE_VIEW_ALL"));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-16 00:27:56 +02:00
|
|
|
// Check that a random permission that shouldn't be there isn't
|
|
|
|
$this->assertFalse(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin"));
|
|
|
|
}
|
2010-04-13 04:00:15 +02:00
|
|
|
/**
|
|
|
|
* Ensure the the get_*_by_permission functions are permission role aware
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGettingMembersByPermission() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$accessMember = $this->objFromFixture('SilverStripe\\Security\\Member', 'access');
|
|
|
|
$accessAuthor = $this->objFromFixture('SilverStripe\\Security\\Member', 'author');
|
2010-04-13 04:14:23 +02:00
|
|
|
|
2010-04-13 04:00:15 +02:00
|
|
|
$result = Permission::get_members_by_permission(array('CMS_ACCESS_SecurityAdmin'));
|
2010-04-13 04:18:14 +02:00
|
|
|
$resultIDs = $result ? $result->column() : array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 04:18:14 +02:00
|
|
|
$this->assertContains($accessMember->ID, $resultIDs,
|
|
|
|
'Member is found via a permission attached to a role');
|
|
|
|
$this->assertNotContains($accessAuthor->ID, $resultIDs);
|
2010-04-13 04:00:15 +02:00
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testHiddenPermissions(){
|
2016-06-23 01:37:22 +02:00
|
|
|
$permissionCheckboxSet = new PermissionCheckboxSetField('Permissions','Permissions','SilverStripe\\Security\\Permission','GroupID');
|
2012-06-19 12:45:44 +02:00
|
|
|
$this->assertContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Security\\Permission', 'hidden_permissions', array('CMS_ACCESS_LeftAndMain'));
|
2012-06-19 12:45:44 +02:00
|
|
|
|
|
|
|
$this->assertNotContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());
|
2013-03-21 19:48:54 +01:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
Config::inst()->remove('SilverStripe\\Security\\Permission', 'hidden_permissions');
|
2012-06-19 12:45:44 +02:00
|
|
|
$this->assertContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());
|
2012-07-01 07:55:20 +02:00
|
|
|
}
|
2016-01-05 09:16:18 +01:00
|
|
|
|
|
|
|
public function testEmptyMemberFails() {
|
|
|
|
$member = new Member();
|
|
|
|
$this->assertFalse($member->exists());
|
|
|
|
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$this->assertFalse(Permission::checkMember($member, 'ADMIN'));
|
|
|
|
$this->assertFalse(Permission::checkMember($member, 'CMS_ACCESS_LeftAndMain'));
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|