2008-04-26 08:31:52 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Security\Tests;
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
use SilverStripe\Control\Cookie;
|
2017-12-14 16:17:33 +01:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2016-11-23 06:09:10 +01:00
|
|
|
use SilverStripe\Core\Convert;
|
2017-05-20 06:32:25 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
2018-04-11 03:04:22 +02:00
|
|
|
use SilverStripe\Forms\ListboxField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\i18n\i18n;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2017-06-15 04:20:12 +02:00
|
|
|
use SilverStripe\ORM\ValidationException;
|
2017-10-03 06:57:52 +02:00
|
|
|
use SilverStripe\ORM\ValidationResult;
|
2017-06-15 04:20:12 +02:00
|
|
|
use SilverStripe\Security\Group;
|
|
|
|
use SilverStripe\Security\IdentityStore;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Member;
|
2017-06-15 04:20:12 +02:00
|
|
|
use SilverStripe\Security\Member_Validator;
|
|
|
|
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;
|
2017-06-09 06:25:40 +02:00
|
|
|
use SilverStripe\Security\MemberAuthenticator\SessionAuthenticationHandler;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\MemberPassword;
|
|
|
|
use SilverStripe\Security\PasswordEncryptor_Blowfish;
|
2018-11-14 10:54:17 +01:00
|
|
|
use SilverStripe\Security\PasswordValidator;
|
2017-06-15 04:20:12 +02:00
|
|
|
use SilverStripe\Security\Permission;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\RememberLoginHash;
|
2017-06-15 04:20:12 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Security\Tests\MemberTest\FieldsExtension;
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class MemberTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'MemberTest.yml';
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
protected $orig = [];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-17 07:40:13 +02:00
|
|
|
protected static $illegal_extensions = [
|
|
|
|
Member::class => '*',
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
public static function setUpBeforeClass()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
parent::setUpBeforeClass();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
//Setting the locale has to happen in the constructor (using the setUp and tearDown methods doesn't work)
|
|
|
|
//This is because the test relies on the yaml file being interpreted according to a particular date format
|
|
|
|
//and this setup occurs before the setUp method is run
|
2017-02-22 04:15:08 +01:00
|
|
|
i18n::config()->set('default_locale', 'en_US');
|
2019-04-05 00:22:27 +02:00
|
|
|
|
|
|
|
// Set the default authenticator to use for these tests
|
|
|
|
Injector::inst()->load([
|
|
|
|
Security::class => [
|
|
|
|
'properties' => [
|
|
|
|
'Authenticators' => [
|
|
|
|
'default' => '%$' . MemberAuthenticator::class,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function setUp()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
Member::config()->set('unique_identifier_field', 'Email');
|
2018-11-14 10:54:17 +01:00
|
|
|
|
2018-12-03 00:04:34 +01:00
|
|
|
PasswordValidator::singleton()
|
|
|
|
->setMinLength(0)
|
|
|
|
->setTestNames([]);
|
2018-09-28 16:25:10 +02:00
|
|
|
|
|
|
|
i18n::set_locale('en_US');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 16:17:33 +01:00
|
|
|
public function testPasswordEncryptionUpdatedOnChangedPassword()
|
|
|
|
{
|
|
|
|
Config::modify()->set(Security::class, 'password_encryption_algorithm', 'none');
|
|
|
|
$member = Member::create();
|
|
|
|
$member->Password = 'password';
|
|
|
|
$member->write();
|
|
|
|
$this->assertEquals('password', $member->Password);
|
|
|
|
$this->assertEquals('none', $member->PasswordEncryption);
|
|
|
|
Config::modify()->set(Security::class, 'password_encryption_algorithm', 'blowfish');
|
|
|
|
$member->Password = 'newpassword';
|
|
|
|
$member->write();
|
|
|
|
$this->assertNotEquals('password', $member->Password);
|
|
|
|
$this->assertNotEquals('newpassword', $member->Password);
|
|
|
|
$this->assertEquals('blowfish', $member->PasswordEncryption);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testWriteDoesntMergeNewRecordWithExistingMember()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
$this->expectException(ValidationException::class);
|
2016-12-16 05:34:21 +01:00
|
|
|
$m1 = new Member();
|
|
|
|
$m1->Email = 'member@test.com';
|
|
|
|
$m1->write();
|
|
|
|
|
|
|
|
$m2 = new Member();
|
|
|
|
$m2->Email = 'member@test.com';
|
|
|
|
$m2->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \SilverStripe\ORM\ValidationException
|
|
|
|
*/
|
|
|
|
public function testWriteDoesntMergeExistingMemberOnIdentifierChange()
|
|
|
|
{
|
|
|
|
$m1 = new Member();
|
|
|
|
$m1->Email = 'member@test.com';
|
|
|
|
$m1->write();
|
|
|
|
|
|
|
|
$m2 = new Member();
|
|
|
|
$m2->Email = 'member_new@test.com';
|
|
|
|
$m2->write();
|
|
|
|
|
|
|
|
$m2->Email = 'member@test.com';
|
|
|
|
$m2->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefaultPasswordEncryptionOnMember()
|
|
|
|
{
|
|
|
|
$memberWithPassword = new Member();
|
|
|
|
$memberWithPassword->Password = 'mypassword';
|
|
|
|
$memberWithPassword->write();
|
|
|
|
$this->assertEquals(
|
2017-06-15 04:20:12 +02:00
|
|
|
Security::config()->get('password_encryption_algorithm'),
|
2017-12-14 16:17:33 +01:00
|
|
|
$memberWithPassword->PasswordEncryption,
|
2016-12-16 05:34:21 +01:00
|
|
|
'Password encryption is set for new member records on first write (with setting "Password")'
|
|
|
|
);
|
|
|
|
|
|
|
|
$memberNoPassword = new Member();
|
|
|
|
$memberNoPassword->write();
|
2018-02-06 14:56:46 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
Security::config()->get('password_encryption_algorithm'),
|
2016-12-16 05:34:21 +01:00
|
|
|
$memberNoPassword->PasswordEncryption,
|
|
|
|
'Password encryption is not set for new member records on first write, when not setting a "Password")'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testKeepsEncryptionOnEmptyPasswords()
|
|
|
|
{
|
|
|
|
$member = new Member();
|
|
|
|
$member->Password = 'mypassword';
|
|
|
|
$member->PasswordEncryption = 'sha1_v2.4';
|
|
|
|
$member->write();
|
|
|
|
|
|
|
|
$member->Password = '';
|
|
|
|
$member->write();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2017-12-14 16:17:33 +01:00
|
|
|
Security::config()->get('password_encryption_algorithm'),
|
|
|
|
$member->PasswordEncryption
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2017-06-15 04:20:12 +02:00
|
|
|
$auth = new MemberAuthenticator();
|
|
|
|
$result = $auth->checkPassword($member, '');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue($result->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetPassword()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $member */
|
2016-11-23 06:09:10 +01:00
|
|
|
$member = $this->objFromFixture(Member::class, 'test');
|
2016-12-16 05:34:21 +01:00
|
|
|
$member->Password = "test1";
|
|
|
|
$member->write();
|
2017-06-15 04:20:12 +02:00
|
|
|
$auth = new MemberAuthenticator();
|
|
|
|
$result = $auth->checkPassword($member, 'test1');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue($result->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that password changes are logged properly
|
|
|
|
*/
|
|
|
|
public function testPasswordChangeLogging()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $member */
|
2016-12-16 05:34:21 +01:00
|
|
|
$member = $this->objFromFixture(Member::class, 'test');
|
|
|
|
$this->assertNotNull($member);
|
|
|
|
$member->Password = "test1";
|
|
|
|
$member->write();
|
|
|
|
|
|
|
|
$member->Password = "test2";
|
|
|
|
$member->write();
|
|
|
|
|
|
|
|
$member->Password = "test3";
|
|
|
|
$member->write();
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
$passwords = DataObject::get(MemberPassword::class, "\"MemberID\" = $member->ID", "\"Created\" DESC, \"ID\" DESC")
|
2016-12-16 05:34:21 +01:00
|
|
|
->getIterator();
|
|
|
|
$this->assertNotNull($passwords);
|
|
|
|
$passwords->rewind();
|
|
|
|
$this->assertTrue($passwords->current()->checkPassword('test3'), "Password test3 not found in MemberRecord");
|
|
|
|
|
|
|
|
$passwords->next();
|
|
|
|
$this->assertTrue($passwords->current()->checkPassword('test2'), "Password test2 not found in MemberRecord");
|
|
|
|
|
|
|
|
$passwords->next();
|
|
|
|
$this->assertTrue($passwords->current()->checkPassword('test1'), "Password test1 not found in MemberRecord");
|
|
|
|
|
|
|
|
$passwords->next();
|
|
|
|
$this->assertInstanceOf('SilverStripe\\ORM\\DataObject', $passwords->current());
|
|
|
|
$this->assertTrue(
|
|
|
|
$passwords->current()->checkPassword('1nitialPassword'),
|
|
|
|
"Password 1nitialPassword not found in MemberRecord"
|
|
|
|
);
|
|
|
|
|
|
|
|
//check we don't retain orphaned records when a member is deleted
|
|
|
|
$member->delete();
|
|
|
|
|
|
|
|
$passwords = MemberPassword::get()->filter('MemberID', $member->OldID);
|
|
|
|
|
|
|
|
$this->assertCount(0, $passwords);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that changed passwords will send an email
|
|
|
|
*/
|
|
|
|
public function testChangedPasswordEmaling()
|
|
|
|
{
|
|
|
|
Member::config()->update('notify_password_change', true);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->clearEmails();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $member */
|
2016-12-16 05:34:21 +01:00
|
|
|
$member = $this->objFromFixture(Member::class, 'test');
|
|
|
|
$this->assertNotNull($member);
|
|
|
|
$valid = $member->changePassword('32asDF##$$%%');
|
|
|
|
$this->assertTrue($valid->isValid());
|
|
|
|
|
|
|
|
$this->assertEmailSent(
|
|
|
|
'testuser@example.com',
|
|
|
|
null,
|
|
|
|
'Your password has been changed',
|
|
|
|
'/testuser@example\.com/'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that triggering "forgotPassword" sends an Email with a reset link
|
|
|
|
*/
|
|
|
|
public function testForgotPasswordEmaling()
|
|
|
|
{
|
|
|
|
$this->clearEmails();
|
|
|
|
$this->autoFollowRedirection = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $member */
|
2016-12-16 05:34:21 +01:00
|
|
|
$member = $this->objFromFixture(Member::class, 'test');
|
|
|
|
$this->assertNotNull($member);
|
|
|
|
|
|
|
|
// Initiate a password-reset
|
2020-04-20 19:58:09 +02:00
|
|
|
$response = $this->post('Security/lostpassword/LostPasswordForm', ['Email' => $member->Email]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals($response->getStatusCode(), 302);
|
|
|
|
|
|
|
|
// We should get redirected to Security/passwordsent
|
|
|
|
$this->assertContains(
|
2018-06-26 14:29:59 +02:00
|
|
|
'Security/lostpassword/passwordsent',
|
2016-12-16 05:34:21 +01:00
|
|
|
urldecode($response->getHeader('Location'))
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check existance of reset link
|
|
|
|
$this->assertEmailSent(
|
|
|
|
"testuser@example.com",
|
|
|
|
null,
|
|
|
|
'Your password reset link',
|
2018-01-16 19:39:30 +01:00
|
|
|
'/Security\/changepassword\?m=' . $member->ID . '&t=[^"]+/'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that passwords validate against NZ e-government guidelines
|
|
|
|
* - don't allow the use of the last 6 passwords
|
|
|
|
* - require at least 3 of lowercase, uppercase, digits and punctuation
|
|
|
|
* - at least 7 characters long
|
|
|
|
*/
|
|
|
|
public function testValidatePassword()
|
|
|
|
{
|
|
|
|
/**
|
2018-01-24 23:10:09 +01:00
|
|
|
* @var Member $member
|
2019-08-15 07:11:43 +02:00
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
$member = $this->objFromFixture(Member::class, 'test');
|
|
|
|
$this->assertNotNull($member);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
Member::set_password_validator(new MemberTest\TestPasswordValidator());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// BAD PASSWORDS
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('shorty');
|
|
|
|
$this->assertFalse($result->isValid());
|
|
|
|
$this->assertArrayHasKey("TOO_SHORT", $result->getMessages());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('longone');
|
|
|
|
$this->assertArrayNotHasKey("TOO_SHORT", $result->getMessages());
|
|
|
|
$this->assertArrayHasKey("LOW_CHARACTER_STRENGTH", $result->getMessages());
|
|
|
|
$this->assertFalse($result->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('w1thNumb3rs');
|
|
|
|
$this->assertArrayNotHasKey("LOW_CHARACTER_STRENGTH", $result->getMessages());
|
|
|
|
$this->assertTrue($result->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Clear out the MemberPassword table to ensure that the system functions properly in that situation
|
|
|
|
DB::query("DELETE FROM \"MemberPassword\"");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// GOOD PASSWORDS
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('withSym###Ls');
|
|
|
|
$this->assertArrayNotHasKey("LOW_CHARACTER_STRENGTH", $result->getMessages());
|
|
|
|
$this->assertTrue($result->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('withSym###Ls2');
|
|
|
|
$this->assertTrue($result->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('withSym###Ls3');
|
|
|
|
$this->assertTrue($result->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('withSym###Ls4');
|
|
|
|
$this->assertTrue($result->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('withSym###Ls5');
|
|
|
|
$this->assertTrue($result->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('withSym###Ls6');
|
|
|
|
$this->assertTrue($result->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('withSym###Ls7');
|
|
|
|
$this->assertTrue($result->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// CAN'T USE PASSWORDS 2-7, but I can use pasword 1
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('withSym###Ls2');
|
|
|
|
$this->assertFalse($result->isValid());
|
|
|
|
$this->assertArrayHasKey("PREVIOUS_PASSWORD", $result->getMessages());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('withSym###Ls5');
|
|
|
|
$this->assertFalse($result->isValid());
|
|
|
|
$this->assertArrayHasKey("PREVIOUS_PASSWORD", $result->getMessages());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('withSym###Ls7');
|
|
|
|
$this->assertFalse($result->isValid());
|
|
|
|
$this->assertArrayHasKey("PREVIOUS_PASSWORD", $result->getMessages());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('withSym###Ls');
|
|
|
|
$this->assertTrue($result->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// HAVING DONE THAT, PASSWORD 2 is now available from the list
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('withSym###Ls2');
|
|
|
|
$this->assertTrue($result->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('withSym###Ls3');
|
|
|
|
$this->assertTrue($result->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $member->changePassword('withSym###Ls4');
|
|
|
|
$this->assertTrue($result->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
Member::set_password_validator(null);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Test that the PasswordExpiry date is set when passwords are changed
|
|
|
|
*/
|
|
|
|
public function testPasswordExpirySetting()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
Member::config()->set('password_expiry_days', 90);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $member */
|
2016-12-16 05:34:21 +01:00
|
|
|
$member = $this->objFromFixture(Member::class, 'test');
|
|
|
|
$this->assertNotNull($member);
|
|
|
|
$valid = $member->changePassword("Xx?1234234");
|
|
|
|
$this->assertTrue($valid->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$expiryDate = date('Y-m-d', time() + 90*86400);
|
|
|
|
$this->assertEquals($expiryDate, $member->PasswordExpiry);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
Member::config()->set('password_expiry_days', null);
|
2016-12-16 05:34:21 +01:00
|
|
|
$valid = $member->changePassword("Xx?1234235");
|
|
|
|
$this->assertTrue($valid->isValid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNull($member->PasswordExpiry);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testIsPasswordExpired()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $member */
|
2016-12-16 05:34:21 +01:00
|
|
|
$member = $this->objFromFixture(Member::class, 'test');
|
|
|
|
$this->assertNotNull($member);
|
|
|
|
$this->assertFalse($member->isPasswordExpired());
|
|
|
|
|
|
|
|
$member = $this->objFromFixture(Member::class, 'noexpiry');
|
|
|
|
$member->PasswordExpiry = null;
|
|
|
|
$this->assertFalse($member->isPasswordExpired());
|
|
|
|
|
|
|
|
$member = $this->objFromFixture(Member::class, 'expiredpassword');
|
|
|
|
$this->assertTrue($member->isPasswordExpired());
|
|
|
|
|
|
|
|
// Check the boundary conditions
|
|
|
|
// If PasswordExpiry == today, then it's expired
|
|
|
|
$member->PasswordExpiry = date('Y-m-d');
|
|
|
|
$this->assertTrue($member->isPasswordExpired());
|
|
|
|
|
|
|
|
// If PasswordExpiry == tomorrow, then it's not
|
|
|
|
$member->PasswordExpiry = date('Y-m-d', time() + 86400);
|
|
|
|
$this->assertFalse($member->isPasswordExpired());
|
|
|
|
}
|
2019-04-05 00:22:27 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testInGroups()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $staffmember */
|
2016-12-16 05:34:21 +01:00
|
|
|
$staffmember = $this->objFromFixture(Member::class, 'staffmember');
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $ceomember */
|
2016-12-16 05:34:21 +01:00
|
|
|
$ceomember = $this->objFromFixture(Member::class, 'ceomember');
|
|
|
|
|
|
|
|
$staffgroup = $this->objFromFixture(Group::class, 'staffgroup');
|
|
|
|
$managementgroup = $this->objFromFixture(Group::class, 'managementgroup');
|
|
|
|
$ceogroup = $this->objFromFixture(Group::class, 'ceogroup');
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2020-04-20 19:58:09 +02:00
|
|
|
$staffmember->inGroups([$staffgroup, $managementgroup]),
|
2016-12-16 05:34:21 +01:00
|
|
|
'inGroups() succeeds if a membership is detected on one of many passed groups'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
2020-04-20 19:58:09 +02:00
|
|
|
$staffmember->inGroups([$ceogroup, $managementgroup]),
|
2016-12-16 05:34:21 +01:00
|
|
|
'inGroups() fails if a membership is detected on none of the passed groups'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
2020-04-20 19:58:09 +02:00
|
|
|
$ceomember->inGroups([$staffgroup, $managementgroup], true),
|
2016-12-16 05:34:21 +01:00
|
|
|
'inGroups() fails if no direct membership is detected on any of the passed groups (in strict mode)'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-02-09 14:52:07 +01:00
|
|
|
/**
|
|
|
|
* Assertions to check that Member_GroupSet is functionally equivalent to ManyManyList
|
|
|
|
*/
|
|
|
|
public function testRemoveGroups()
|
|
|
|
{
|
|
|
|
$staffmember = $this->objFromFixture(Member::class, 'staffmember');
|
|
|
|
|
|
|
|
$staffgroup = $this->objFromFixture(Group::class, 'staffgroup');
|
|
|
|
$managementgroup = $this->objFromFixture(Group::class, 'managementgroup');
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2020-04-20 19:58:09 +02:00
|
|
|
$staffmember->inGroups([$staffgroup, $managementgroup]),
|
2018-02-09 14:52:07 +01:00
|
|
|
'inGroups() succeeds if a membership is detected on one of many passed groups'
|
|
|
|
);
|
|
|
|
|
|
|
|
$staffmember->Groups()->remove($managementgroup);
|
|
|
|
$this->assertFalse(
|
|
|
|
$staffmember->inGroup($managementgroup),
|
|
|
|
'member was not removed from group using ->Groups()->remove()'
|
|
|
|
);
|
|
|
|
|
|
|
|
$staffmember->Groups()->removeAll();
|
|
|
|
$this->assertCount(
|
|
|
|
0,
|
|
|
|
$staffmember->Groups(),
|
|
|
|
'member was not removed from all groups using ->Groups()->removeAll()'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testAddToGroupByCode()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $grouplessMember */
|
2016-12-16 05:34:21 +01:00
|
|
|
$grouplessMember = $this->objFromFixture(Member::class, 'grouplessmember');
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Group $memberlessGroup */
|
2016-12-16 05:34:21 +01:00
|
|
|
$memberlessGroup = $this->objFromFixture(Group::class, 'memberlessgroup');
|
|
|
|
|
|
|
|
$this->assertFalse($grouplessMember->Groups()->exists());
|
|
|
|
$this->assertFalse($memberlessGroup->Members()->exists());
|
|
|
|
|
|
|
|
$grouplessMember->addToGroupByCode('memberless');
|
|
|
|
|
|
|
|
$this->assertEquals($memberlessGroup->Members()->count(), 1);
|
|
|
|
$this->assertEquals($grouplessMember->Groups()->count(), 1);
|
|
|
|
|
|
|
|
$grouplessMember->addToGroupByCode('somegroupthatwouldneverexist', 'New Group');
|
|
|
|
$this->assertEquals($grouplessMember->Groups()->count(), 2);
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Group $group */
|
2016-12-16 05:34:21 +01:00
|
|
|
$group = DataObject::get_one(
|
|
|
|
Group::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'"Group"."Code"' => 'somegroupthatwouldneverexist'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertNotNull($group);
|
|
|
|
$this->assertEquals($group->Code, 'somegroupthatwouldneverexist');
|
|
|
|
$this->assertEquals($group->Title, 'New Group');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveFromGroupByCode()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $grouplessMember */
|
2016-12-16 05:34:21 +01:00
|
|
|
$grouplessMember = $this->objFromFixture(Member::class, 'grouplessmember');
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Group $memberlessGroup */
|
2016-12-16 05:34:21 +01:00
|
|
|
$memberlessGroup = $this->objFromFixture(Group::class, 'memberlessgroup');
|
|
|
|
|
|
|
|
$this->assertFalse($grouplessMember->Groups()->exists());
|
|
|
|
$this->assertFalse($memberlessGroup->Members()->exists());
|
|
|
|
|
|
|
|
$grouplessMember->addToGroupByCode('memberless');
|
|
|
|
|
|
|
|
$this->assertEquals($memberlessGroup->Members()->count(), 1);
|
|
|
|
$this->assertEquals($grouplessMember->Groups()->count(), 1);
|
|
|
|
|
|
|
|
$grouplessMember->addToGroupByCode('somegroupthatwouldneverexist', 'New Group');
|
|
|
|
$this->assertEquals($grouplessMember->Groups()->count(), 2);
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Group $group */
|
2016-12-16 05:34:21 +01:00
|
|
|
$group = DataObject::get_one(Group::class, "\"Code\" = 'somegroupthatwouldneverexist'");
|
|
|
|
$this->assertNotNull($group);
|
|
|
|
$this->assertEquals($group->Code, 'somegroupthatwouldneverexist');
|
|
|
|
$this->assertEquals($group->Title, 'New Group');
|
|
|
|
|
|
|
|
$grouplessMember->removeFromGroupByCode('memberless');
|
|
|
|
$this->assertEquals($memberlessGroup->Members()->count(), 0);
|
|
|
|
$this->assertEquals($grouplessMember->Groups()->count(), 1);
|
|
|
|
|
|
|
|
$grouplessMember->removeFromGroupByCode('somegroupthatwouldneverexist');
|
|
|
|
$this->assertEquals($grouplessMember->Groups()->count(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInGroup()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $staffmember */
|
2016-12-16 05:34:21 +01:00
|
|
|
$staffmember = $this->objFromFixture(Member::class, 'staffmember');
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $managementmember */
|
2016-12-16 05:34:21 +01:00
|
|
|
$managementmember = $this->objFromFixture(Member::class, 'managementmember');
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $accountingmember */
|
2016-12-16 05:34:21 +01:00
|
|
|
$accountingmember = $this->objFromFixture(Member::class, 'accountingmember');
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $ceomember */
|
2016-12-16 05:34:21 +01:00
|
|
|
$ceomember = $this->objFromFixture(Member::class, 'ceomember');
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Group $staffgroup */
|
2016-12-16 05:34:21 +01:00
|
|
|
$staffgroup = $this->objFromFixture(Group::class, 'staffgroup');
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Group $managementgroup */
|
2016-12-16 05:34:21 +01:00
|
|
|
$managementgroup = $this->objFromFixture(Group::class, 'managementgroup');
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Group $ceogroup */
|
2016-12-16 05:34:21 +01:00
|
|
|
$ceogroup = $this->objFromFixture(Group::class, 'ceogroup');
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$staffmember->inGroup($staffgroup),
|
|
|
|
'Direct group membership is detected'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$managementmember->inGroup($staffgroup),
|
|
|
|
'Users of child group are members of a direct parent group (if not in strict mode)'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$accountingmember->inGroup($staffgroup),
|
|
|
|
'Users of child group are members of a direct parent group (if not in strict mode)'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$ceomember->inGroup($staffgroup),
|
|
|
|
'Users of indirect grandchild group are members of a parent group (if not in strict mode)'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$ceomember->inGroup($ceogroup, true),
|
|
|
|
'Direct group membership is dected (if in strict mode)'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$ceomember->inGroup($staffgroup, true),
|
|
|
|
'Users of child group are not members of a direct parent group (if in strict mode)'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$staffmember->inGroup($managementgroup),
|
|
|
|
'Users of parent group are not members of a direct child group'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$staffmember->inGroup($ceogroup),
|
|
|
|
'Users of parent group are not members of an indirect grandchild group'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$accountingmember->inGroup($managementgroup),
|
|
|
|
'Users of group are not members of any siblings'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$staffmember->inGroup('does-not-exist'),
|
|
|
|
'Non-existant group returns false'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that the user is able to view their own record, and in turn, they can
|
|
|
|
* edit and delete their own record too.
|
|
|
|
*/
|
|
|
|
public function testCanManipulateOwnRecord()
|
|
|
|
{
|
|
|
|
$member = $this->objFromFixture(Member::class, 'test');
|
|
|
|
$member2 = $this->objFromFixture(Member::class, 'staffmember');
|
|
|
|
|
|
|
|
/* Not logged in, you can't view, delete or edit the record */
|
|
|
|
$this->assertFalse($member->canView());
|
|
|
|
$this->assertFalse($member->canDelete());
|
|
|
|
$this->assertFalse($member->canEdit());
|
|
|
|
|
|
|
|
/* Logged in users can edit their own record */
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->logInAs($member);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue($member->canView());
|
|
|
|
$this->assertFalse($member->canDelete());
|
|
|
|
$this->assertTrue($member->canEdit());
|
|
|
|
|
|
|
|
/* Other uses cannot view, delete or edit others records */
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->logInAs($member2);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse($member->canView());
|
|
|
|
$this->assertFalse($member->canDelete());
|
|
|
|
$this->assertFalse($member->canEdit());
|
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->logOut();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAuthorisedMembersCanManipulateOthersRecords()
|
|
|
|
{
|
|
|
|
$member = $this->objFromFixture(Member::class, 'test');
|
|
|
|
$member2 = $this->objFromFixture(Member::class, 'staffmember');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/* Group members with SecurityAdmin permissions can manipulate other records */
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->logInAs($member);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue($member2->canView());
|
|
|
|
$this->assertTrue($member2->canDelete());
|
|
|
|
$this->assertTrue($member2->canEdit());
|
2017-05-07 21:11:00 +02:00
|
|
|
|
|
|
|
$this->logOut();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testExtendedCan()
|
|
|
|
{
|
|
|
|
$member = $this->objFromFixture(Member::class, 'test');
|
2016-11-09 13:45:40 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/* Normal behaviour is that you can't view a member unless canView() on an extension returns true */
|
|
|
|
$this->assertFalse($member->canView());
|
|
|
|
$this->assertFalse($member->canDelete());
|
|
|
|
$this->assertFalse($member->canEdit());
|
|
|
|
|
|
|
|
/* Apply a extension that allows viewing in any case (most likely the case for member profiles) */
|
|
|
|
Member::add_extension(MemberTest\ViewingAllowedExtension::class);
|
|
|
|
$member2 = $this->objFromFixture(Member::class, 'staffmember');
|
|
|
|
|
|
|
|
$this->assertTrue($member2->canView());
|
|
|
|
$this->assertFalse($member2->canDelete());
|
|
|
|
$this->assertFalse($member2->canEdit());
|
|
|
|
|
|
|
|
/* Apply a extension that denies viewing of the Member */
|
|
|
|
Member::remove_extension(MemberTest\ViewingAllowedExtension::class);
|
|
|
|
Member::add_extension(MemberTest\ViewingDeniedExtension::class);
|
|
|
|
$member3 = $this->objFromFixture(Member::class, 'managementmember');
|
|
|
|
|
|
|
|
$this->assertFalse($member3->canView());
|
|
|
|
$this->assertFalse($member3->canDelete());
|
|
|
|
$this->assertFalse($member3->canEdit());
|
|
|
|
|
|
|
|
/* Apply a extension that allows viewing and editing but denies deletion */
|
|
|
|
Member::remove_extension(MemberTest\ViewingDeniedExtension::class);
|
|
|
|
Member::add_extension(MemberTest\EditingAllowedDeletingDeniedExtension::class);
|
|
|
|
$member4 = $this->objFromFixture(Member::class, 'accountingmember');
|
|
|
|
|
|
|
|
$this->assertTrue($member4->canView());
|
|
|
|
$this->assertFalse($member4->canDelete());
|
|
|
|
$this->assertTrue($member4->canEdit());
|
|
|
|
|
|
|
|
Member::remove_extension(MemberTest\EditingAllowedDeletingDeniedExtension::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for {@link Member::getName()} and {@link Member::setName()}
|
|
|
|
*/
|
|
|
|
public function testName()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $member */
|
2016-12-16 05:34:21 +01:00
|
|
|
$member = $this->objFromFixture(Member::class, 'test');
|
|
|
|
$member->setName('Test Some User');
|
|
|
|
$this->assertEquals('Test Some User', $member->getName());
|
2018-10-05 00:09:00 +02:00
|
|
|
$this->assertEquals('Test Some', $member->FirstName);
|
|
|
|
$this->assertEquals('User', $member->Surname);
|
2016-12-16 05:34:21 +01:00
|
|
|
$member->setName('Test');
|
|
|
|
$this->assertEquals('Test', $member->getName());
|
|
|
|
$member->FirstName = 'Test';
|
|
|
|
$member->Surname = '';
|
2018-10-05 00:09:00 +02:00
|
|
|
$this->assertEquals('Test', $member->FirstName);
|
|
|
|
$this->assertEquals('', $member->Surname);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals('Test', $member->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMembersWithSecurityAdminAccessCantEditAdminsUnlessTheyreAdminsThemselves()
|
|
|
|
{
|
|
|
|
$adminMember = $this->objFromFixture(Member::class, 'admin');
|
|
|
|
$otherAdminMember = $this->objFromFixture(Member::class, 'other-admin');
|
|
|
|
$securityAdminMember = $this->objFromFixture(Member::class, 'test');
|
|
|
|
$ceoMember = $this->objFromFixture(Member::class, 'ceomember');
|
|
|
|
|
|
|
|
// Careful: Don't read as english language.
|
|
|
|
// More precisely this should read canBeEditedBy()
|
|
|
|
|
|
|
|
$this->assertTrue($adminMember->canEdit($adminMember), 'Admins can edit themselves');
|
|
|
|
$this->assertTrue($otherAdminMember->canEdit($adminMember), 'Admins can edit other admins');
|
|
|
|
$this->assertTrue($securityAdminMember->canEdit($adminMember), 'Admins can edit other members');
|
|
|
|
|
|
|
|
$this->assertTrue($securityAdminMember->canEdit($securityAdminMember), 'Security-Admins can edit themselves');
|
|
|
|
$this->assertFalse($adminMember->canEdit($securityAdminMember), 'Security-Admins can not edit other admins');
|
|
|
|
$this->assertTrue($ceoMember->canEdit($securityAdminMember), 'Security-Admins can edit other members');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOnChangeGroups()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Group $staffGroup */
|
2016-12-16 05:34:21 +01:00
|
|
|
$staffGroup = $this->objFromFixture(Group::class, 'staffgroup');
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $staffMember */
|
2016-12-16 05:34:21 +01:00
|
|
|
$staffMember = $this->objFromFixture(Member::class, 'staffmember');
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $adminMember */
|
2016-12-16 05:34:21 +01:00
|
|
|
$adminMember = $this->objFromFixture(Member::class, 'admin');
|
2018-04-11 03:04:22 +02:00
|
|
|
|
|
|
|
// Construct admin and non-admin gruops
|
2020-04-20 19:58:09 +02:00
|
|
|
$newAdminGroup = new Group(['Title' => 'newadmin']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$newAdminGroup->write();
|
|
|
|
Permission::grant($newAdminGroup->ID, 'ADMIN');
|
2020-04-20 19:58:09 +02:00
|
|
|
$newOtherGroup = new Group(['Title' => 'othergroup']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$newOtherGroup->write();
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2020-04-20 19:58:09 +02:00
|
|
|
$staffMember->onChangeGroups([$staffGroup->ID]),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Adding existing non-admin group relation is allowed for non-admin members'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
2020-04-20 19:58:09 +02:00
|
|
|
$staffMember->onChangeGroups([$newOtherGroup->ID]),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Adding new non-admin group relation is allowed for non-admin members'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
2020-04-20 19:58:09 +02:00
|
|
|
$staffMember->onChangeGroups([$newAdminGroup->ID]),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Adding new admin group relation is not allowed for non-admin members'
|
|
|
|
);
|
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->logInAs($adminMember);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue(
|
2020-04-20 19:58:09 +02:00
|
|
|
$staffMember->onChangeGroups([$newAdminGroup->ID]),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Adding new admin group relation is allowed for normal users, when granter is logged in as admin'
|
|
|
|
);
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->logOut();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2020-04-20 19:58:09 +02:00
|
|
|
$adminMember->onChangeGroups([$newAdminGroup->ID]),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Adding new admin group relation is allowed for admin members'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-11 03:04:22 +02:00
|
|
|
/**
|
|
|
|
* Ensure DirectGroups listbox disallows admin-promotion
|
|
|
|
*/
|
|
|
|
public function testAllowedGroupsListbox()
|
|
|
|
{
|
|
|
|
/** @var Group $adminGroup */
|
|
|
|
$adminGroup = $this->objFromFixture(Group::class, 'admingroup');
|
|
|
|
/** @var Member $staffMember */
|
|
|
|
$staffMember = $this->objFromFixture(Member::class, 'staffmember');
|
|
|
|
/** @var Member $adminMember */
|
|
|
|
$adminMember = $this->objFromFixture(Member::class, 'admin');
|
|
|
|
|
|
|
|
// Ensure you can see the DirectGroups box
|
|
|
|
$this->logInWithPermission('EDIT_PERMISSIONS');
|
|
|
|
|
|
|
|
// Non-admin member field contains non-admin groups
|
|
|
|
/** @var ListboxField $staffListbox */
|
|
|
|
$staffListbox = $staffMember->getCMSFields()->dataFieldByName('DirectGroups');
|
|
|
|
$this->assertArrayNotHasKey($adminGroup->ID, $staffListbox->getSource());
|
|
|
|
|
|
|
|
// admin member field contains admin group
|
|
|
|
/** @var ListboxField $adminListbox */
|
|
|
|
$adminListbox = $adminMember->getCMSFields()->dataFieldByName('DirectGroups');
|
|
|
|
$this->assertArrayHasKey($adminGroup->ID, $adminListbox->getSource());
|
|
|
|
|
|
|
|
// If logged in as admin, staff listbox has admin group
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$staffListbox = $staffMember->getCMSFields()->dataFieldByName('DirectGroups');
|
|
|
|
$this->assertArrayHasKey($adminGroup->ID, $staffListbox->getSource());
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Test Member_GroupSet::add
|
|
|
|
*/
|
|
|
|
public function testOnChangeGroupsByAdd()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $staffMember */
|
2016-12-16 05:34:21 +01:00
|
|
|
$staffMember = $this->objFromFixture(Member::class, 'staffmember');
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $adminMember */
|
2016-12-16 05:34:21 +01:00
|
|
|
$adminMember = $this->objFromFixture(Member::class, 'admin');
|
|
|
|
|
|
|
|
// Setup new admin group
|
2020-04-20 19:58:09 +02:00
|
|
|
$newAdminGroup = new Group(['Title' => 'newadmin']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$newAdminGroup->write();
|
|
|
|
Permission::grant($newAdminGroup->ID, 'ADMIN');
|
|
|
|
|
|
|
|
// Setup non-admin group
|
2020-04-20 19:58:09 +02:00
|
|
|
$newOtherGroup = new Group(['Title' => 'othergroup']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$newOtherGroup->write();
|
|
|
|
|
|
|
|
// Test staff can be added to other group
|
|
|
|
$this->assertFalse($staffMember->inGroup($newOtherGroup));
|
|
|
|
$staffMember->Groups()->add($newOtherGroup);
|
|
|
|
$this->assertTrue(
|
|
|
|
$staffMember->inGroup($newOtherGroup),
|
|
|
|
'Adding new non-admin group relation is allowed for non-admin members'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test staff member can't be added to admin groups
|
|
|
|
$this->assertFalse($staffMember->inGroup($newAdminGroup));
|
|
|
|
$staffMember->Groups()->add($newAdminGroup);
|
|
|
|
$this->assertFalse(
|
|
|
|
$staffMember->inGroup($newAdminGroup),
|
|
|
|
'Adding new admin group relation is not allowed for non-admin members'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test staff member can be added to admin group by admins
|
|
|
|
$this->logInAs($adminMember);
|
|
|
|
$staffMember->Groups()->add($newAdminGroup);
|
|
|
|
$this->assertTrue(
|
|
|
|
$staffMember->inGroup($newAdminGroup),
|
|
|
|
'Adding new admin group relation is allowed for normal users, when granter is logged in as admin'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test staff member can be added if they are already admin
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->logOut();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse($adminMember->inGroup($newAdminGroup));
|
|
|
|
$adminMember->Groups()->add($newAdminGroup);
|
|
|
|
$this->assertTrue(
|
|
|
|
$adminMember->inGroup($newAdminGroup),
|
|
|
|
'Adding new admin group relation is allowed for admin members'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Member_GroupSet::add
|
|
|
|
*/
|
|
|
|
public function testOnChangeGroupsBySetIDList()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $staffMember */
|
2016-12-16 05:34:21 +01:00
|
|
|
$staffMember = $this->objFromFixture(Member::class, 'staffmember');
|
|
|
|
|
|
|
|
// Setup new admin group
|
2020-04-20 19:58:09 +02:00
|
|
|
$newAdminGroup = new Group(['Title' => 'newadmin']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$newAdminGroup->write();
|
|
|
|
Permission::grant($newAdminGroup->ID, 'ADMIN');
|
|
|
|
|
|
|
|
// Test staff member can't be added to admin groups
|
|
|
|
$this->assertFalse($staffMember->inGroup($newAdminGroup));
|
2020-04-20 19:58:09 +02:00
|
|
|
$staffMember->Groups()->setByIDList([$newAdminGroup->ID]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$staffMember->inGroup($newAdminGroup),
|
|
|
|
'Adding new admin group relation is not allowed for non-admin members'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that extensions using updateCMSFields() are applied correctly
|
|
|
|
*/
|
|
|
|
public function testUpdateCMSFields()
|
|
|
|
{
|
|
|
|
Member::add_extension(FieldsExtension::class);
|
|
|
|
|
|
|
|
$member = Member::singleton();
|
|
|
|
$fields = $member->getCMSFields();
|
|
|
|
|
|
|
|
/**
|
2019-04-05 00:22:27 +02:00
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNotNull($fields->dataFieldByName('Email'), 'Scaffolded fields are retained');
|
|
|
|
$this->assertNull($fields->dataFieldByName('Salt'), 'Field modifications run correctly');
|
|
|
|
$this->assertNotNull($fields->dataFieldByName('TestMemberField'), 'Extension is applied correctly');
|
|
|
|
|
|
|
|
Member::remove_extension(FieldsExtension::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that all members are returned
|
|
|
|
*/
|
|
|
|
public function testMap_in_groupsReturnsAll()
|
|
|
|
{
|
|
|
|
$members = Member::map_in_groups();
|
|
|
|
$this->assertEquals(13, $members->count(), 'There are 12 members in the mock plus a fake admin');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that only admin members are returned
|
|
|
|
*/
|
|
|
|
public function testMap_in_groupsReturnsAdmins()
|
|
|
|
{
|
|
|
|
$adminID = $this->objFromFixture(Group::class, 'admingroup')->ID;
|
|
|
|
$members = Member::map_in_groups($adminID)->toArray();
|
|
|
|
|
|
|
|
$admin = $this->objFromFixture(Member::class, 'admin');
|
|
|
|
$otherAdmin = $this->objFromFixture(Member::class, 'other-admin');
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
in_array($admin->getTitle(), $members),
|
2018-01-16 19:39:30 +01:00
|
|
|
$admin->getTitle() . ' should be in the returned list.'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
in_array($otherAdmin->getTitle(), $members),
|
2018-01-16 19:39:30 +01:00
|
|
|
$otherAdmin->getTitle() . ' should be in the returned list.'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(2, count($members), 'There should be 2 members from the admin group');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the given array of member extensions as class names.
|
|
|
|
* This is useful for re-adding extensions after being removed
|
|
|
|
* in a test case to produce an unbiased test.
|
|
|
|
*
|
|
|
|
* @param array $extensions
|
|
|
|
* @return array The added extensions
|
|
|
|
*/
|
|
|
|
protected function addExtensions($extensions)
|
|
|
|
{
|
|
|
|
if ($extensions) {
|
|
|
|
foreach ($extensions as $extension) {
|
|
|
|
Member::add_extension($extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove given extensions from Member. This is useful for
|
|
|
|
* removing extensions that could produce a biased
|
|
|
|
* test result, as some extensions applied by project
|
|
|
|
* code or modules can do this.
|
|
|
|
*
|
|
|
|
* @param array $extensions
|
|
|
|
* @return array The removed extensions
|
|
|
|
*/
|
|
|
|
protected function removeExtensions($extensions)
|
|
|
|
{
|
|
|
|
if ($extensions) {
|
|
|
|
foreach ($extensions as $extension) {
|
|
|
|
Member::remove_extension($extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateAutologinTokenAndStoreHash()
|
|
|
|
{
|
|
|
|
$m = new Member();
|
2018-02-06 14:56:46 +01:00
|
|
|
$m->write();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$token = $m->generateAutologinTokenAndStoreHash();
|
|
|
|
|
2018-02-06 14:56:46 +01:00
|
|
|
$this->assertEquals($m->encryptWithUserSettings($token), $m->AutoLoginHash, 'Stores the token as a hash.');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateAutoLoginToken()
|
|
|
|
{
|
|
|
|
$enc = new PasswordEncryptor_Blowfish();
|
|
|
|
|
|
|
|
$m1 = new Member();
|
2018-02-06 14:56:46 +01:00
|
|
|
$m1->write();
|
2016-12-16 05:34:21 +01:00
|
|
|
$m1Token = $m1->generateAutologinTokenAndStoreHash();
|
|
|
|
|
|
|
|
$m2 = new Member();
|
2018-02-06 14:56:46 +01:00
|
|
|
$m2->write();
|
2017-06-15 04:20:12 +02:00
|
|
|
$m2->generateAutologinTokenAndStoreHash();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertTrue($m1->validateAutoLoginToken($m1Token), 'Passes token validity test against matching member.');
|
|
|
|
$this->assertFalse($m2->validateAutoLoginToken($m1Token), 'Fails token validity test against other member.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRememberMeHashGeneration()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $m1 */
|
2016-12-16 05:34:21 +01:00
|
|
|
$m1 = $this->objFromFixture(Member::class, 'grouplessmember');
|
|
|
|
|
2017-05-30 09:42:00 +02:00
|
|
|
Injector::inst()->get(IdentityStore::class)->logIn($m1, true);
|
2017-05-07 21:11:00 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$hashes = RememberLoginHash::get()->filter('MemberID', $m1->ID);
|
|
|
|
$this->assertEquals($hashes->count(), 1);
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var RememberLoginHash $firstHash */
|
2016-12-16 05:34:21 +01:00
|
|
|
$firstHash = $hashes->first();
|
|
|
|
$this->assertNotNull($firstHash->DeviceID);
|
|
|
|
$this->assertNotNull($firstHash->Hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRememberMeHashAutologin()
|
|
|
|
{
|
2018-02-06 14:56:46 +01:00
|
|
|
/** @var Member $m1 */
|
2016-12-16 05:34:21 +01:00
|
|
|
$m1 = $this->objFromFixture(Member::class, 'noexpiry');
|
|
|
|
|
2017-05-30 09:42:00 +02:00
|
|
|
Injector::inst()->get(IdentityStore::class)->logIn($m1, true);
|
2017-05-07 21:11:00 +02:00
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var RememberLoginHash $firstHash */
|
2016-12-16 05:34:21 +01:00
|
|
|
$firstHash = RememberLoginHash::get()->filter('MemberID', $m1->ID)->first();
|
|
|
|
$this->assertNotNull($firstHash);
|
|
|
|
|
|
|
|
// re-generates the hash so we can get the token
|
|
|
|
$firstHash->Hash = $firstHash->getNewHash($m1);
|
|
|
|
$token = $firstHash->getToken();
|
|
|
|
$firstHash->write();
|
|
|
|
|
|
|
|
$response = $this->get(
|
|
|
|
'Security/login',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2018-01-16 19:39:30 +01:00
|
|
|
'alc_enc' => $m1->ID . ':' . $token,
|
2016-12-16 05:34:21 +01:00
|
|
|
'alc_device' => $firstHash->DeviceID
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$message = Convert::raw2xml(
|
|
|
|
_t(
|
2017-04-20 03:15:24 +02:00
|
|
|
'SilverStripe\\Security\\Member.LOGGEDINAS',
|
2016-12-16 05:34:21 +01:00
|
|
|
"You're logged in as {name}.",
|
2020-04-20 19:58:09 +02:00
|
|
|
['name' => $m1->FirstName]
|
2016-12-16 05:34:21 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertContains($message, $response->getBody());
|
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->logOut();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// A wrong token or a wrong device ID should not let us autologin
|
|
|
|
$response = $this->get(
|
|
|
|
'Security/login',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2018-01-16 19:39:30 +01:00
|
|
|
'alc_enc' => $m1->ID . ':asdfasd' . str_rot13($token),
|
2016-12-16 05:34:21 +01:00
|
|
|
'alc_device' => $firstHash->DeviceID
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertNotContains($message, $response->getBody());
|
|
|
|
|
|
|
|
$response = $this->get(
|
|
|
|
'Security/login',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2018-01-16 19:39:30 +01:00
|
|
|
'alc_enc' => $m1->ID . ':' . $token,
|
2016-12-16 05:34:21 +01:00
|
|
|
'alc_device' => str_rot13($firstHash->DeviceID)
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertNotContains($message, $response->getBody());
|
|
|
|
|
|
|
|
// Re-logging (ie 'alc_enc' has expired), and not checking the "Remember Me" option
|
|
|
|
// should remove all previous hashes for this device
|
|
|
|
$response = $this->post(
|
2017-04-23 05:30:33 +02:00
|
|
|
'Security/login/default/LoginForm',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Email' => $m1->Email,
|
|
|
|
'Password' => '1nitialPassword',
|
2017-04-23 05:30:33 +02:00
|
|
|
'action_doLogin' => 'action_doLogin'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
null,
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'alc_device' => $firstHash->DeviceID
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertContains($message, $response->getBody());
|
|
|
|
$this->assertEquals(RememberLoginHash::get()->filter('MemberID', $m1->ID)->count(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExpiredRememberMeHashAutologin()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $m1 */
|
2016-12-16 05:34:21 +01:00
|
|
|
$m1 = $this->objFromFixture(Member::class, 'noexpiry');
|
2017-05-30 09:42:00 +02:00
|
|
|
Injector::inst()->get(IdentityStore::class)->logIn($m1, true);
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var RememberLoginHash $firstHash */
|
2016-12-16 05:34:21 +01:00
|
|
|
$firstHash = RememberLoginHash::get()->filter('MemberID', $m1->ID)->first();
|
|
|
|
$this->assertNotNull($firstHash);
|
|
|
|
|
|
|
|
// re-generates the hash so we can get the token
|
|
|
|
$firstHash->Hash = $firstHash->getNewHash($m1);
|
|
|
|
$token = $firstHash->getToken();
|
|
|
|
$firstHash->ExpiryDate = '2000-01-01 00:00:00';
|
|
|
|
$firstHash->write();
|
|
|
|
|
|
|
|
DBDatetime::set_mock_now('1999-12-31 23:59:59');
|
|
|
|
|
|
|
|
$response = $this->get(
|
|
|
|
'Security/login',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2018-01-16 19:39:30 +01:00
|
|
|
'alc_enc' => $m1->ID . ':' . $token,
|
2016-12-16 05:34:21 +01:00
|
|
|
'alc_device' => $firstHash->DeviceID
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$message = Convert::raw2xml(
|
|
|
|
_t(
|
2017-04-20 03:15:24 +02:00
|
|
|
'SilverStripe\\Security\\Member.LOGGEDINAS',
|
2016-12-16 05:34:21 +01:00
|
|
|
"You're logged in as {name}.",
|
2020-04-20 19:58:09 +02:00
|
|
|
['name' => $m1->FirstName]
|
2016-12-16 05:34:21 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertContains($message, $response->getBody());
|
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->logOut();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// re-generates the hash so we can get the token
|
|
|
|
$firstHash->Hash = $firstHash->getNewHash($m1);
|
|
|
|
$token = $firstHash->getToken();
|
|
|
|
$firstHash->ExpiryDate = '2000-01-01 00:00:00';
|
|
|
|
$firstHash->write();
|
|
|
|
|
|
|
|
DBDatetime::set_mock_now('2000-01-01 00:00:01');
|
|
|
|
|
|
|
|
$response = $this->get(
|
|
|
|
'Security/login',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2018-01-16 19:39:30 +01:00
|
|
|
'alc_enc' => $m1->ID . ':' . $token,
|
2016-12-16 05:34:21 +01:00
|
|
|
'alc_device' => $firstHash->DeviceID
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertNotContains($message, $response->getBody());
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->logOut();
|
2016-12-16 05:34:21 +01:00
|
|
|
DBDatetime::clear_mock_now();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRememberMeMultipleDevices()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $m1 */
|
2016-12-16 05:34:21 +01:00
|
|
|
$m1 = $this->objFromFixture(Member::class, 'noexpiry');
|
|
|
|
|
|
|
|
// First device
|
2017-05-30 09:42:00 +02:00
|
|
|
Injector::inst()->get(IdentityStore::class)->logIn($m1, true);
|
2016-12-16 05:34:21 +01:00
|
|
|
Cookie::set('alc_device', null);
|
|
|
|
// Second device
|
2017-05-30 09:42:00 +02:00
|
|
|
Injector::inst()->get(IdentityStore::class)->logIn($m1, true);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Hash of first device
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var RememberLoginHash $firstHash */
|
2016-12-16 05:34:21 +01:00
|
|
|
$firstHash = RememberLoginHash::get()->filter('MemberID', $m1->ID)->first();
|
|
|
|
$this->assertNotNull($firstHash);
|
|
|
|
|
|
|
|
// Hash of second device
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var RememberLoginHash $secondHash */
|
2016-12-16 05:34:21 +01:00
|
|
|
$secondHash = RememberLoginHash::get()->filter('MemberID', $m1->ID)->last();
|
|
|
|
$this->assertNotNull($secondHash);
|
|
|
|
|
|
|
|
// DeviceIDs are different
|
|
|
|
$this->assertNotEquals($firstHash->DeviceID, $secondHash->DeviceID);
|
|
|
|
|
|
|
|
// re-generates the hashes so we can get the tokens
|
|
|
|
$firstHash->Hash = $firstHash->getNewHash($m1);
|
|
|
|
$firstToken = $firstHash->getToken();
|
|
|
|
$firstHash->write();
|
|
|
|
|
|
|
|
$secondHash->Hash = $secondHash->getNewHash($m1);
|
|
|
|
$secondToken = $secondHash->getToken();
|
|
|
|
$secondHash->write();
|
|
|
|
|
|
|
|
// Accessing the login page should show the user's name straight away
|
|
|
|
$response = $this->get(
|
|
|
|
'Security/login',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2018-01-16 19:39:30 +01:00
|
|
|
'alc_enc' => $m1->ID . ':' . $firstToken,
|
2016-12-16 05:34:21 +01:00
|
|
|
'alc_device' => $firstHash->DeviceID
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$message = Convert::raw2xml(
|
|
|
|
_t(
|
2017-04-20 03:15:24 +02:00
|
|
|
'SilverStripe\\Security\\Member.LOGGEDINAS',
|
2016-12-16 05:34:21 +01:00
|
|
|
"You're logged in as {name}.",
|
2020-04-20 19:58:09 +02:00
|
|
|
['name' => $m1->FirstName]
|
2016-12-16 05:34:21 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertContains($message, $response->getBody());
|
|
|
|
|
2017-06-09 06:25:40 +02:00
|
|
|
// Test that removing session but not cookie keeps user
|
|
|
|
/** @var SessionAuthenticationHandler $sessionHandler */
|
|
|
|
$sessionHandler = Injector::inst()->get(SessionAuthenticationHandler::class);
|
|
|
|
$sessionHandler->logOut();
|
|
|
|
Security::setCurrentUser(null);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Accessing the login page from the second device
|
|
|
|
$response = $this->get(
|
|
|
|
'Security/login',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2018-01-16 19:39:30 +01:00
|
|
|
'alc_enc' => $m1->ID . ':' . $secondToken,
|
2016-12-16 05:34:21 +01:00
|
|
|
'alc_device' => $secondHash->DeviceID
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertContains($message, $response->getBody());
|
|
|
|
|
|
|
|
// Logging out from the second device - only one device being logged out
|
|
|
|
RememberLoginHash::config()->update('logout_across_devices', false);
|
2017-06-15 04:20:12 +02:00
|
|
|
$this->get(
|
2016-12-16 05:34:21 +01:00
|
|
|
'Security/logout',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2018-01-16 19:39:30 +01:00
|
|
|
'alc_enc' => $m1->ID . ':' . $secondToken,
|
2016-12-16 05:34:21 +01:00
|
|
|
'alc_device' => $secondHash->DeviceID
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
RememberLoginHash::get()->filter(['MemberID'=>$m1->ID, 'DeviceID'=>$firstHash->DeviceID])->count(),
|
2016-12-16 05:34:21 +01:00
|
|
|
1
|
|
|
|
);
|
|
|
|
|
|
|
|
// Logging out from any device when all login hashes should be removed
|
|
|
|
RememberLoginHash::config()->update('logout_across_devices', true);
|
2017-05-30 09:42:00 +02:00
|
|
|
Injector::inst()->get(IdentityStore::class)->logIn($m1, true);
|
2017-06-15 04:20:12 +02:00
|
|
|
$this->get('Security/logout', $this->session());
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
RememberLoginHash::get()->filter('MemberID', $m1->ID)->count(),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanDelete()
|
|
|
|
{
|
|
|
|
$admin1 = $this->objFromFixture(Member::class, 'admin');
|
|
|
|
$admin2 = $this->objFromFixture(Member::class, 'other-admin');
|
|
|
|
$member1 = $this->objFromFixture(Member::class, 'grouplessmember');
|
|
|
|
$member2 = $this->objFromFixture(Member::class, 'noformatmember');
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$admin1->canDelete($admin2),
|
|
|
|
'Admins can delete other admins'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$member1->canDelete($admin2),
|
|
|
|
'Admins can delete non-admins'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$admin1->canDelete($admin1),
|
|
|
|
'Admins can not delete themselves'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$member1->canDelete($member2),
|
|
|
|
'Non-admins can not delete other non-admins'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$member1->canDelete($member1),
|
|
|
|
'Non-admins can not delete themselves'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailedLoginCount()
|
|
|
|
{
|
|
|
|
$maxFailedLoginsAllowed = 3;
|
|
|
|
//set up the config variables to enable login lockouts
|
|
|
|
Member::config()->update('lock_out_after_incorrect_logins', $maxFailedLoginsAllowed);
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $member */
|
2016-12-16 05:34:21 +01:00
|
|
|
$member = $this->objFromFixture(Member::class, 'test');
|
|
|
|
$failedLoginCount = $member->FailedLoginCount;
|
|
|
|
|
|
|
|
for ($i = 1; $i < $maxFailedLoginsAllowed; ++$i) {
|
|
|
|
$member->registerFailedLogin();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
++$failedLoginCount,
|
|
|
|
$member->FailedLoginCount,
|
|
|
|
'Failed to increment $member->FailedLoginCount'
|
|
|
|
);
|
|
|
|
|
2017-04-30 05:17:26 +02:00
|
|
|
$this->assertTrue(
|
2017-06-15 04:20:12 +02:00
|
|
|
$member->canLogin(),
|
2016-12-16 05:34:21 +01:00
|
|
|
"Member has been locked out too early"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMemberValidator()
|
|
|
|
{
|
|
|
|
// clear custom requirements for this test
|
|
|
|
Member_Validator::config()->update('customRequired', null);
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $memberA */
|
2016-12-16 05:34:21 +01:00
|
|
|
$memberA = $this->objFromFixture(Member::class, 'admin');
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var Member $memberB */
|
2016-12-16 05:34:21 +01:00
|
|
|
$memberB = $this->objFromFixture(Member::class, 'test');
|
|
|
|
|
|
|
|
// create a blank form
|
|
|
|
$form = new MemberTest\ValidatorForm();
|
|
|
|
|
|
|
|
$validator = new Member_Validator();
|
|
|
|
$validator->setForm($form);
|
|
|
|
|
|
|
|
// Simulate creation of a new member via form, but use an existing member identifier
|
|
|
|
$fail = $validator->php(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'FirstName' => 'Test',
|
|
|
|
'Email' => $memberA->Email
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$fail,
|
|
|
|
'Member_Validator must fail when trying to create new Member with existing Email.'
|
|
|
|
);
|
|
|
|
|
|
|
|
// populate the form with values from another member
|
|
|
|
$form->loadDataFrom($memberB);
|
|
|
|
|
|
|
|
// Assign the validator to an existing member
|
|
|
|
// (this is basically the same as passing the member ID with the form data)
|
|
|
|
$validator->setForMember($memberB);
|
|
|
|
|
|
|
|
// Simulate update of a member via form and use an existing member Email
|
|
|
|
$fail = $validator->php(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'FirstName' => 'Test',
|
|
|
|
'Email' => $memberA->Email
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Simulate update to a new Email address
|
|
|
|
$pass1 = $validator->php(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'FirstName' => 'Test',
|
|
|
|
'Email' => 'membervalidatortest@testing.com'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Pass in the same Email address that the member already has. Ensure that case is valid
|
|
|
|
$pass2 = $validator->php(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'FirstName' => 'Test',
|
|
|
|
'Surname' => 'User',
|
|
|
|
'Email' => $memberB->Email
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$fail,
|
|
|
|
'Member_Validator must fail when trying to update existing member with existing Email.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$pass1,
|
|
|
|
'Member_Validator must pass when Email is updated to a value that\'s not in use.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$pass2,
|
|
|
|
'Member_Validator must pass when Member updates his own Email to the already existing value.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMemberValidatorWithExtensions()
|
|
|
|
{
|
|
|
|
// clear custom requirements for this test
|
|
|
|
Member_Validator::config()->update('customRequired', null);
|
|
|
|
|
|
|
|
// create a blank form
|
|
|
|
$form = new MemberTest\ValidatorForm();
|
|
|
|
|
|
|
|
// Test extensions
|
|
|
|
Member_Validator::add_extension(MemberTest\SurnameMustMatchFirstNameExtension::class);
|
|
|
|
$validator = new Member_Validator();
|
|
|
|
$validator->setForm($form);
|
|
|
|
|
|
|
|
// This test should fail, since the extension enforces FirstName == Surname
|
|
|
|
$fail = $validator->php(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'FirstName' => 'Test',
|
|
|
|
'Surname' => 'User',
|
|
|
|
'Email' => 'test-member-validator-extension@testing.com'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$pass = $validator->php(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'FirstName' => 'Test',
|
|
|
|
'Surname' => 'Test',
|
|
|
|
'Email' => 'test-member-validator-extension@testing.com'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$fail,
|
|
|
|
'Member_Validator must fail because of added extension.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$pass,
|
|
|
|
'Member_Validator must succeed, since it meets all requirements.'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add another extension that always fails. This ensures that all extensions are considered in the validation
|
|
|
|
Member_Validator::add_extension(MemberTest\AlwaysFailExtension::class);
|
|
|
|
$validator = new Member_Validator();
|
|
|
|
$validator->setForm($form);
|
|
|
|
|
|
|
|
// Even though the data is valid, This test should still fail, since one extension always returns false
|
|
|
|
$fail = $validator->php(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'FirstName' => 'Test',
|
|
|
|
'Surname' => 'Test',
|
|
|
|
'Email' => 'test-member-validator-extension@testing.com'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$fail,
|
|
|
|
'Member_Validator must fail because of added extensions.'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Remove added extensions
|
|
|
|
Member_Validator::remove_extension(MemberTest\AlwaysFailExtension::class);
|
|
|
|
Member_Validator::remove_extension(MemberTest\SurnameMustMatchFirstNameExtension::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCustomMemberValidator()
|
|
|
|
{
|
|
|
|
// clear custom requirements for this test
|
|
|
|
Member_Validator::config()->update('customRequired', null);
|
|
|
|
|
|
|
|
$member = $this->objFromFixture(Member::class, 'admin');
|
|
|
|
|
|
|
|
$form = new MemberTest\ValidatorForm();
|
|
|
|
$form->loadDataFrom($member);
|
|
|
|
|
|
|
|
$validator = new Member_Validator();
|
|
|
|
$validator->setForm($form);
|
|
|
|
|
|
|
|
$pass = $validator->php(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'FirstName' => 'Borris',
|
|
|
|
'Email' => 'borris@silverstripe.com'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$fail = $validator->php(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Email' => 'borris@silverstripe.com',
|
|
|
|
'Surname' => ''
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue($pass, 'Validator requires a FirstName and Email');
|
|
|
|
$this->assertFalse($fail, 'Missing FirstName');
|
|
|
|
|
|
|
|
$ext = new MemberTest\ValidatorExtension();
|
|
|
|
$ext->updateValidator($validator);
|
|
|
|
|
|
|
|
$pass = $validator->php(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'FirstName' => 'Borris',
|
|
|
|
'Email' => 'borris@silverstripe.com'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$fail = $validator->php(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Email' => 'borris@silverstripe.com'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse($pass, 'Missing surname');
|
|
|
|
$this->assertFalse($fail, 'Missing surname value');
|
|
|
|
|
|
|
|
$fail = $validator->php(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Email' => 'borris@silverstripe.com',
|
|
|
|
'Surname' => 'Silverman'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue($fail, 'Passes with email and surname now (no firstname)');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCurrentUser()
|
|
|
|
{
|
2017-05-20 06:32:25 +02:00
|
|
|
$this->assertNull(Security::getCurrentUser());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$adminMember = $this->objFromFixture(Member::class, 'admin');
|
|
|
|
$this->logInAs($adminMember);
|
|
|
|
|
2017-05-20 06:32:25 +02:00
|
|
|
$userFromSession = Security::getCurrentUser();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($adminMember->ID, $userFromSession->ID);
|
|
|
|
}
|
2017-01-13 04:11:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \SilverStripe\Security\Member::actAs()
|
|
|
|
*/
|
|
|
|
public function testActAsUserPermissions()
|
|
|
|
{
|
2017-05-20 06:32:25 +02:00
|
|
|
$this->assertNull(Security::getCurrentUser());
|
2017-01-13 04:11:20 +01:00
|
|
|
|
|
|
|
/** @var Member $adminMember */
|
|
|
|
$adminMember = $this->objFromFixture(Member::class, 'admin');
|
|
|
|
|
|
|
|
// Check acting as admin when not logged in
|
|
|
|
$checkAdmin = Member::actAs($adminMember, function () {
|
|
|
|
return Permission::check('ADMIN');
|
|
|
|
});
|
|
|
|
$this->assertTrue($checkAdmin);
|
|
|
|
|
|
|
|
// Check nesting
|
|
|
|
$checkAdmin = Member::actAs($adminMember, function () {
|
|
|
|
return Member::actAs(null, function () {
|
|
|
|
return Permission::check('ADMIN');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
$this->assertFalse($checkAdmin);
|
|
|
|
|
|
|
|
// Check logging in as non-admin user
|
|
|
|
$this->logInWithPermission('TEST_PERMISSION');
|
|
|
|
|
|
|
|
$hasPerm = Member::actAs(null, function () {
|
|
|
|
return Permission::check('TEST_PERMISSION');
|
|
|
|
});
|
|
|
|
$this->assertFalse($hasPerm);
|
|
|
|
|
|
|
|
// Check permissions can be promoted
|
|
|
|
$checkAdmin = Member::actAs($adminMember, function () {
|
|
|
|
return Permission::check('ADMIN');
|
|
|
|
});
|
|
|
|
$this->assertTrue($checkAdmin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \SilverStripe\Security\Member::actAs()
|
|
|
|
*/
|
|
|
|
public function testActAsUser()
|
|
|
|
{
|
2017-05-20 06:32:25 +02:00
|
|
|
$this->assertNull(Security::getCurrentUser());
|
2017-01-13 04:11:20 +01:00
|
|
|
|
|
|
|
/** @var Member $adminMember */
|
|
|
|
$adminMember = $this->objFromFixture(Member::class, 'admin');
|
2017-05-30 09:42:00 +02:00
|
|
|
$member = Member::actAs($adminMember, function () {
|
|
|
|
return Security::getCurrentUser();
|
2017-01-13 04:11:20 +01:00
|
|
|
});
|
2017-05-30 09:42:00 +02:00
|
|
|
$this->assertEquals($adminMember->ID, $member->ID);
|
2017-01-13 04:11:20 +01:00
|
|
|
|
|
|
|
// Check nesting
|
2017-05-30 09:42:00 +02:00
|
|
|
$member = Member::actAs($adminMember, function () {
|
2017-01-13 04:11:20 +01:00
|
|
|
return Member::actAs(null, function () {
|
2017-05-30 09:42:00 +02:00
|
|
|
return Security::getCurrentUser();
|
2017-01-13 04:11:20 +01:00
|
|
|
});
|
|
|
|
});
|
2017-05-30 09:42:00 +02:00
|
|
|
$this->assertEmpty($member);
|
2017-01-13 04:11:20 +01:00
|
|
|
}
|
2017-10-03 06:57:52 +02:00
|
|
|
|
|
|
|
public function testChangePasswordWithExtensionsThatModifyValidationResult()
|
|
|
|
{
|
|
|
|
// Default behaviour
|
2018-04-11 03:04:22 +02:00
|
|
|
/** @var Member $member */
|
2017-10-03 06:57:52 +02:00
|
|
|
$member = $this->objFromFixture(Member::class, 'admin');
|
|
|
|
$result = $member->changePassword('my-secret-new-password');
|
|
|
|
$this->assertInstanceOf(ValidationResult::class, $result);
|
|
|
|
$this->assertTrue($result->isValid());
|
|
|
|
|
|
|
|
// With an extension added
|
|
|
|
Member::add_extension(MemberTest\ExtendedChangePasswordExtension::class);
|
|
|
|
$member = $this->objFromFixture(Member::class, 'admin');
|
|
|
|
$result = $member->changePassword('my-second-secret-password');
|
|
|
|
$this->assertInstanceOf(ValidationResult::class, $result);
|
|
|
|
$this->assertFalse($result->isValid());
|
|
|
|
}
|
2018-09-28 16:25:10 +02:00
|
|
|
|
|
|
|
public function testNewMembersReceiveTheDefaultLocale()
|
|
|
|
{
|
|
|
|
// Set a different current locale to the default
|
|
|
|
i18n::set_locale('de_DE');
|
|
|
|
|
|
|
|
$newMember = Member::create();
|
|
|
|
$newMember->update([
|
|
|
|
'FirstName' => 'Leslie',
|
|
|
|
'Surname' => 'Longly',
|
|
|
|
'Email' => 'longly.leslie@example.com',
|
|
|
|
]);
|
|
|
|
$newMember->write();
|
|
|
|
|
|
|
|
$this->assertSame('en_US', $newMember->Locale, 'New members receive the default locale');
|
|
|
|
}
|
2019-08-15 07:11:43 +02:00
|
|
|
|
2019-08-15 23:49:36 +02:00
|
|
|
public function testChangePasswordOnlyValidatesPlaintext()
|
2019-08-15 07:11:43 +02:00
|
|
|
{
|
|
|
|
// This validator requires passwords to be 17 characters long
|
|
|
|
Member::set_password_validator(new MemberTest\VerySpecificPasswordValidator());
|
|
|
|
|
|
|
|
// This algorithm will never return a 17 character hash
|
|
|
|
Security::config()->set('password_encryption_algorithm', 'blowfish');
|
|
|
|
|
|
|
|
/** @var Member $member */
|
|
|
|
$member = $this->objFromFixture(Member::class, 'test');
|
|
|
|
$result = $member->changePassword('Password123456789'); // 17 characters long
|
|
|
|
$this->assertTrue($result->isValid());
|
|
|
|
}
|
2019-09-06 21:12:27 +02:00
|
|
|
|
|
|
|
public function testGetLastName()
|
|
|
|
{
|
|
|
|
$member = new Member();
|
|
|
|
$member->Surname = 'Johnson';
|
|
|
|
|
|
|
|
$this->assertSame('Johnson', $member->getLastName(), 'getLastName should proxy to Surname');
|
|
|
|
}
|
2013-10-16 00:29:43 +02:00
|
|
|
}
|