2010-04-12 23:16:26 +02:00
|
|
|
<?php
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Security\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-06-15 04:20:12 +02:00
|
|
|
use SilverStripe\Security\Member;
|
2018-02-06 14:56:46 +01:00
|
|
|
use SilverStripe\Security\PasswordEncryptor;
|
2017-06-15 04:20:12 +02:00
|
|
|
use SilverStripe\Security\Permission;
|
|
|
|
use SilverStripe\Security\DefaultAdminService;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class SecurityDefaultAdminTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
|
|
|
protected $defaultUsername = null;
|
2017-06-15 04:20:12 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $defaultPassword = null;
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// TODO Workaround to force database clearing with no fixture present,
|
|
|
|
// and avoid sideeffects from other tests
|
2017-06-22 12:50:45 +02:00
|
|
|
if (!static::$tempDB->isUsed()) {
|
|
|
|
static::$tempDB->build();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2017-06-22 12:50:45 +02:00
|
|
|
static::$tempDB->clearAllData();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
if (DefaultAdminService::hasDefaultAdmin()) {
|
|
|
|
$this->defaultUsername = DefaultAdminService::getDefaultAdminUsername();
|
|
|
|
$this->defaultPassword = DefaultAdminService::getDefaultAdminPassword();
|
|
|
|
DefaultAdminService::clearDefaultAdmin();
|
|
|
|
} else {
|
|
|
|
$this->defaultUsername = null;
|
|
|
|
$this->defaultPassword = null;
|
|
|
|
}
|
|
|
|
DefaultAdminService::setDefaultAdmin('admin', 'password');
|
2017-01-20 03:57:50 +01:00
|
|
|
Permission::reset();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function tearDown(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
DefaultAdminService::clearDefaultAdmin();
|
|
|
|
if ($this->defaultUsername) {
|
|
|
|
DefaultAdminService::setDefaultAdmin($this->defaultUsername, $this->defaultPassword);
|
|
|
|
}
|
2017-01-20 03:57:50 +01:00
|
|
|
Permission::reset();
|
2016-12-16 05:34:21 +01:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckDefaultAdmin()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
$this->assertTrue(DefaultAdminService::hasDefaultAdmin());
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue(
|
2017-06-15 04:20:12 +02:00
|
|
|
DefaultAdminService::isDefaultAdminCredentials('admin', 'password'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Succeeds with correct username and password'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
2017-06-15 04:20:12 +02:00
|
|
|
DefaultAdminService::isDefaultAdminCredentials('wronguser', 'password'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Fails with incorrect username'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
2017-06-15 04:20:12 +02:00
|
|
|
DefaultAdminService::isDefaultAdminCredentials('admin', 'wrongpassword'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Fails with incorrect password'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindAnAdministratorCreatesNewUser()
|
|
|
|
{
|
|
|
|
$adminMembers = Permission::get_members_by_permission('ADMIN');
|
|
|
|
$this->assertEquals(0, $adminMembers->count());
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
$admin = DefaultAdminService::singleton()->findOrCreateDefaultAdmin();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertInstanceOf(Member::class, $admin);
|
|
|
|
$this->assertTrue(Permission::checkMember($admin, 'ADMIN'));
|
2017-06-15 04:20:12 +02:00
|
|
|
$this->assertEquals($admin->Email, DefaultAdminService::getDefaultAdminUsername());
|
|
|
|
$this->assertTrue(DefaultAdminService::isDefaultAdmin($admin->Email));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNull($admin->Password);
|
2018-02-06 14:56:46 +01:00
|
|
|
$this->assertArrayHasKey($admin->PasswordEncryption, PasswordEncryptor::get_encryptors());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2017-07-06 03:29:27 +02:00
|
|
|
public function testFindOrCreateAdmin()
|
|
|
|
{
|
|
|
|
$adminMembers = Permission::get_members_by_permission('ADMIN');
|
|
|
|
$this->assertEquals(0, $adminMembers->count());
|
|
|
|
|
|
|
|
$admin = DefaultAdminService::singleton()->findOrCreateAdmin('newadmin@example.com', 'Admin Name');
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Member::class, $admin);
|
|
|
|
$this->assertTrue(Permission::checkMember($admin, 'ADMIN'));
|
|
|
|
$this->assertEquals('newadmin@example.com', $admin->Email);
|
|
|
|
$this->assertEquals('Admin Name', $admin->FirstName);
|
|
|
|
$this->assertNull($admin->Password);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFindAnAdministratorWithoutDefaultAdmin()
|
|
|
|
{
|
|
|
|
// Clear default admin
|
2017-06-15 04:20:12 +02:00
|
|
|
$service = DefaultAdminService::singleton();
|
2017-06-13 11:04:43 +02:00
|
|
|
DefaultAdminService::clearDefaultAdmin();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$adminMembers = Permission::get_members_by_permission('ADMIN');
|
|
|
|
$this->assertEquals(0, $adminMembers->count());
|
|
|
|
|
2017-06-13 11:04:43 +02:00
|
|
|
$admin = $service->findOrCreateDefaultAdmin();
|
|
|
|
$this->assertNull($admin);
|
2017-06-15 04:20:12 +02:00
|
|
|
|
2017-06-13 11:04:43 +02:00
|
|
|
// When clearing the admin, it will not re-instate it anymore
|
|
|
|
DefaultAdminService::setDefaultAdmin('admin', 'password');
|
2017-06-15 04:20:12 +02:00
|
|
|
$admin = $service->findOrCreateDefaultAdmin();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue(Permission::checkMember($admin, 'ADMIN'));
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
// User should have Email but no Password
|
|
|
|
$this->assertEquals('admin', $admin->Email);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEmpty($admin->Password);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefaultAdmin()
|
|
|
|
{
|
|
|
|
$adminMembers = Permission::get_members_by_permission('ADMIN');
|
|
|
|
$this->assertEquals(0, $adminMembers->count());
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
$admin = DefaultAdminService::singleton()->findOrCreateDefaultAdmin();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertInstanceOf(Member::class, $admin);
|
|
|
|
$this->assertTrue(Permission::checkMember($admin, 'ADMIN'));
|
2017-06-15 04:20:12 +02:00
|
|
|
$this->assertEquals($admin->Email, DefaultAdminService::getDefaultAdminUsername());
|
|
|
|
$this->assertTrue(DefaultAdminService::isDefaultAdmin($admin->Email));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNull($admin->Password);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|