2008-04-26 08:31:52 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Security;
|
|
|
|
use SilverStripe\Security\MemberPassword;
|
|
|
|
use SilverStripe\Security\Group;
|
|
|
|
use SilverStripe\Security\Permission;
|
|
|
|
use SilverStripe\Security\PasswordEncryptor_Blowfish;
|
|
|
|
use SilverStripe\Security\RememberLoginHash;
|
|
|
|
use SilverStripe\Security\Member_Validator;
|
|
|
|
use SilverStripe\Security\PasswordValidator;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2008-06-15 15:33:53 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-06-15 15:33:53 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2009-12-16 06:39:39 +01:00
|
|
|
class MemberTest extends FunctionalTest {
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'MemberTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-03-09 05:10:38 +01:00
|
|
|
protected $orig = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
protected $local = null;
|
|
|
|
|
2010-10-19 05:01:29 +02:00
|
|
|
protected $illegalExtensions = array(
|
2016-06-23 01:37:22 +02:00
|
|
|
'SilverStripe\\Security\\Member' => array(
|
2010-10-19 05:01:29 +02:00
|
|
|
// TODO Coupling with modules, this should be resolved by automatically
|
|
|
|
// removing all applied extensions before a unit test
|
|
|
|
'ForumRole',
|
|
|
|
'OpenIDAuthenticatedRole'
|
|
|
|
)
|
|
|
|
);
|
2010-10-19 00:33:41 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct() {
|
2010-10-19 00:33:41 +02:00
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
//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
|
2014-08-15 08:53:05 +02:00
|
|
|
//and this setup occurs before the setUp method is run
|
2010-10-19 00:33:41 +02:00
|
|
|
$this->local = i18n::default_locale();
|
|
|
|
i18n::set_default_locale('en_US');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __destruct() {
|
2012-12-08 12:20:20 +01:00
|
|
|
i18n::set_default_locale($this->local);
|
|
|
|
}
|
2010-10-19 00:33:41 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUp() {
|
2009-11-06 03:23:21 +01:00
|
|
|
parent::setUp();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
$this->orig['Member_unique_identifier_field'] = Member::config()->unique_identifier_field;
|
|
|
|
Member::config()->unique_identifier_field = 'Email';
|
2009-11-06 03:23:21 +01:00
|
|
|
Member::set_password_validator(null);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function tearDown() {
|
2013-03-21 19:48:54 +01:00
|
|
|
Member::config()->unique_identifier_field = $this->orig['Member_unique_identifier_field'];
|
2010-03-09 05:10:38 +01:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2013-10-16 00:29:43 +02:00
|
|
|
|
|
|
|
|
2010-03-09 05:10:38 +01:00
|
|
|
/**
|
2016-06-15 06:03:16 +02:00
|
|
|
* @expectedException SilverStripe\ORM\ValidationException
|
2010-03-09 05:10:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testWriteDoesntMergeNewRecordWithExistingMember() {
|
2010-03-09 05:10:38 +01:00
|
|
|
$m1 = new Member();
|
|
|
|
$m1->Email = 'member@test.com';
|
|
|
|
$m1->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-03-09 05:10:38 +01:00
|
|
|
$m2 = new Member();
|
|
|
|
$m2->Email = 'member@test.com';
|
|
|
|
$m2->write();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-03-09 05:10:38 +01:00
|
|
|
/**
|
2016-06-15 06:03:16 +02:00
|
|
|
* @expectedException SilverStripe\ORM\ValidationException
|
2010-03-09 05:10:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testWriteDoesntMergeExistingMemberOnIdentifierChange() {
|
2010-03-09 05:10:38 +01:00
|
|
|
$m1 = new Member();
|
|
|
|
$m1->Email = 'member@test.com';
|
|
|
|
$m1->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-03-09 05:10:38 +01:00
|
|
|
$m2 = new Member();
|
|
|
|
$m2->Email = 'member_new@test.com';
|
|
|
|
$m2->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-03-09 05:10:38 +01:00
|
|
|
$m2->Email = 'member@test.com';
|
|
|
|
$m2->write();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDefaultPasswordEncryptionOnMember() {
|
2010-10-15 05:52:38 +02:00
|
|
|
$memberWithPassword = new Member();
|
|
|
|
$memberWithPassword->Password = 'mypassword';
|
|
|
|
$memberWithPassword->write();
|
2009-11-06 03:23:21 +01:00
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
$memberWithPassword->PasswordEncryption,
|
2013-03-21 19:48:54 +01:00
|
|
|
Security::config()->password_encryption_algorithm,
|
2010-10-15 05:52:38 +02:00
|
|
|
'Password encryption is set for new member records on first write (with setting "Password")'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:52:38 +02:00
|
|
|
$memberNoPassword = new Member();
|
|
|
|
$memberNoPassword->write();
|
|
|
|
$this->assertNull(
|
|
|
|
$memberNoPassword->PasswordEncryption,
|
|
|
|
'Password encryption is not set for new member records on first write, when not setting a "Password")'
|
2009-11-06 03:23:21 +01:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDefaultPasswordEncryptionDoesntChangeExistingMembers() {
|
2009-11-06 03:23:21 +01:00
|
|
|
$member = new Member();
|
|
|
|
$member->Password = 'mypassword';
|
|
|
|
$member->PasswordEncryption = 'sha1_v2.4';
|
|
|
|
$member->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
$origAlgo = Security::config()->password_encryption_algorithm;
|
|
|
|
Security::config()->password_encryption_algorithm = 'none';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-06 03:23:21 +01:00
|
|
|
$member->Password = 'mynewpassword';
|
|
|
|
$member->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-06 03:23:21 +01:00
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
$member->PasswordEncryption,
|
2009-11-06 03:23:21 +01:00
|
|
|
'sha1_v2.4'
|
|
|
|
);
|
2010-02-23 05:05:34 +01:00
|
|
|
$result = $member->checkPassword('mynewpassword');
|
|
|
|
$this->assertTrue($result->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
Security::config()->password_encryption_algorithm = $origAlgo;
|
2009-11-06 03:23:21 +01:00
|
|
|
}
|
2013-01-06 21:20:02 +01:00
|
|
|
|
|
|
|
public function testKeepsEncryptionOnEmptyPasswords() {
|
|
|
|
$member = new Member();
|
|
|
|
$member->Password = 'mypassword';
|
|
|
|
$member->PasswordEncryption = 'sha1_v2.4';
|
|
|
|
$member->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-01-06 21:20:02 +01:00
|
|
|
$member->Password = '';
|
|
|
|
$member->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-01-06 21:20:02 +01:00
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
$member->PasswordEncryption,
|
2013-01-06 21:20:02 +01:00
|
|
|
'sha1_v2.4'
|
|
|
|
);
|
|
|
|
$result = $member->checkPassword('');
|
|
|
|
$this->assertTrue($result->valid());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetPassword() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'test');
|
2009-11-06 03:23:21 +01:00
|
|
|
$member->Password = "test1";
|
|
|
|
$member->write();
|
2010-02-23 05:05:34 +01:00
|
|
|
$result = $member->checkPassword('test1');
|
|
|
|
$this->assertTrue($result->valid());
|
2009-11-06 03:23:21 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Test that password changes are logged properly
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testPasswordChangeLogging() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'test');
|
2008-08-11 07:17:37 +02:00
|
|
|
$this->assertNotNull($member);
|
2008-04-26 08:31:52 +02:00
|
|
|
$member->Password = "test1";
|
|
|
|
$member->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$member->Password = "test2";
|
|
|
|
$member->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$member->Password = "test3";
|
|
|
|
$member->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$passwords = DataObject::get("SilverStripe\\Security\\MemberPassword", "\"MemberID\" = $member->ID", "\"Created\" DESC, \"ID\" DESC")
|
2012-09-26 23:34:00 +02:00
|
|
|
->getIterator();
|
2008-04-26 08:31:52 +02:00
|
|
|
$this->assertNotNull($passwords);
|
2011-05-03 04:16:40 +02:00
|
|
|
$passwords->rewind();
|
|
|
|
$this->assertTrue($passwords->current()->checkPassword('test3'), "Password test3 not found in MemberRecord");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-03 04:16:40 +02:00
|
|
|
$passwords->next();
|
|
|
|
$this->assertTrue($passwords->current()->checkPassword('test2'), "Password test2 not found in MemberRecord");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-03 04:16:40 +02:00
|
|
|
$passwords->next();
|
|
|
|
$this->assertTrue($passwords->current()->checkPassword('test1'), "Password test1 not found in MemberRecord");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-03 04:16:40 +02:00
|
|
|
$passwords->next();
|
2016-06-15 06:03:16 +02:00
|
|
|
$this->assertInstanceOf('SilverStripe\\ORM\\DataObject', $passwords->current());
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertTrue($passwords->current()->checkPassword('1nitialPassword'),
|
|
|
|
"Password 1nitialPassword not found in MemberRecord");
|
2015-06-24 22:04:23 +02:00
|
|
|
|
|
|
|
//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);
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:32:42 +02:00
|
|
|
/**
|
|
|
|
* Test that changed passwords will send an email
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testChangedPasswordEmaling() {
|
2016-06-23 01:37:22 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Security\\Member', 'notify_password_change', true);
|
2016-04-04 11:48:56 +02:00
|
|
|
|
2008-04-26 08:32:42 +02:00
|
|
|
$this->clearEmails();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'test');
|
2008-08-11 07:17:37 +02:00
|
|
|
$this->assertNotNull($member);
|
2008-04-26 08:32:42 +02:00
|
|
|
$valid = $member->changePassword('32asDF##$$%%');
|
|
|
|
$this->assertTrue($valid->valid());
|
2016-04-04 11:48:56 +02:00
|
|
|
|
2016-04-11 13:46:41 +02:00
|
|
|
$this->assertEmailSent('testuser@example.com', null, 'Your password has been changed',
|
|
|
|
'/testuser@example\.com/');
|
2016-04-04 11:48:56 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that triggering "forgotPassword" sends an Email with a reset link
|
2016-04-29 07:50:55 +02:00
|
|
|
*/
|
2016-04-04 11:48:56 +02:00
|
|
|
public function testForgotPasswordEmaling() {
|
|
|
|
$this->clearEmails();
|
|
|
|
$this->autoFollowRedirection = false;
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'test');
|
2016-04-04 11:48:56 +02:00
|
|
|
$this->assertNotNull($member);
|
|
|
|
|
|
|
|
// Initiate a password-reset
|
|
|
|
$response = $this->post('Security/LostPasswordForm', array('Email' => $member->Email));
|
|
|
|
|
|
|
|
$this->assertEquals($response->getStatusCode(), 302);
|
|
|
|
|
|
|
|
// We should get redirected to Security/passwordsent
|
2016-04-11 13:46:41 +02:00
|
|
|
$this->assertContains('Security/passwordsent/testuser@example.com',
|
2016-04-04 11:48:56 +02:00
|
|
|
urldecode($response->getHeader('Location')));
|
|
|
|
|
|
|
|
// Check existance of reset link
|
2016-04-11 13:46:41 +02:00
|
|
|
$this->assertEmailSent("testuser@example.com", null, 'Your password reset link',
|
2016-06-03 10:51:02 +02:00
|
|
|
'/Security\/changepassword\?m='.$member->ID.'&t=[^"]+/');
|
2008-04-26 08:32:42 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02: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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testValidatePassword() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'test');
|
2008-08-11 07:17:37 +02:00
|
|
|
$this->assertNotNull($member);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-02 00:28:04 +01:00
|
|
|
Member::set_password_validator(new MemberTest_PasswordValidator());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
// BAD PASSWORDS
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('shorty');
|
|
|
|
$this->assertFalse($valid->valid());
|
|
|
|
$this->assertContains("TOO_SHORT", $valid->codeList());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('longone');
|
|
|
|
$this->assertNotContains("TOO_SHORT", $valid->codeList());
|
|
|
|
$this->assertContains("LOW_CHARACTER_STRENGTH", $valid->codeList());
|
|
|
|
$this->assertFalse($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('w1thNumb3rs');
|
|
|
|
$this->assertNotContains("LOW_CHARACTER_STRENGTH", $valid->codeList());
|
|
|
|
$this->assertTrue($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
// Clear out the MemberPassword table to ensure that the system functions properly in that situation
|
2008-11-24 10:31:14 +01:00
|
|
|
DB::query("DELETE FROM \"MemberPassword\"");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
// GOOD PASSWORDS
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('withSym###Ls');
|
|
|
|
$this->assertNotContains("LOW_CHARACTER_STRENGTH", $valid->codeList());
|
|
|
|
$this->assertTrue($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('withSym###Ls2');
|
|
|
|
$this->assertTrue($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('withSym###Ls3');
|
|
|
|
$this->assertTrue($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('withSym###Ls4');
|
|
|
|
$this->assertTrue($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('withSym###Ls5');
|
|
|
|
$this->assertTrue($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('withSym###Ls6');
|
|
|
|
$this->assertTrue($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('withSym###Ls7');
|
|
|
|
$this->assertTrue($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
// CAN'T USE PASSWORDS 2-7, but I can use pasword 1
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('withSym###Ls2');
|
|
|
|
$this->assertFalse($valid->valid());
|
|
|
|
$this->assertContains("PREVIOUS_PASSWORD", $valid->codeList());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('withSym###Ls5');
|
|
|
|
$this->assertFalse($valid->valid());
|
|
|
|
$this->assertContains("PREVIOUS_PASSWORD", $valid->codeList());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('withSym###Ls7');
|
|
|
|
$this->assertFalse($valid->valid());
|
|
|
|
$this->assertContains("PREVIOUS_PASSWORD", $valid->codeList());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('withSym###Ls');
|
|
|
|
$this->assertTrue($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
// HAVING DONE THAT, PASSWORD 2 is now available from the list
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('withSym###Ls2');
|
|
|
|
$this->assertTrue($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('withSym###Ls3');
|
|
|
|
$this->assertTrue($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword('withSym###Ls4');
|
|
|
|
$this->assertTrue($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-05-15 10:46:12 +02:00
|
|
|
Member::set_password_validator(null);
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Test that the PasswordExpiry date is set when passwords are changed
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testPasswordExpirySetting() {
|
2013-03-21 19:48:54 +01:00
|
|
|
Member::config()->password_expiry_days = 90;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'test');
|
2008-08-11 07:17:37 +02:00
|
|
|
$this->assertNotNull($member);
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword("Xx?1234234");
|
|
|
|
$this->assertTrue($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
$expiryDate = date('Y-m-d', time() + 90*86400);
|
2008-04-26 08:31:52 +02:00
|
|
|
$this->assertEquals($expiryDate, $member->PasswordExpiry);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
Member::config()->password_expiry_days = null;
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = $member->changePassword("Xx?1234235");
|
|
|
|
$this->assertTrue($valid->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
$this->assertNull($member->PasswordExpiry);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testIsPasswordExpired() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'test');
|
2008-08-11 07:17:37 +02:00
|
|
|
$this->assertNotNull($member);
|
2008-04-26 08:31:52 +02:00
|
|
|
$this->assertFalse($member->isPasswordExpired());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'noexpiry');
|
2008-04-26 08:31:52 +02:00
|
|
|
$member->PasswordExpiry = null;
|
|
|
|
$this->assertFalse($member->isPasswordExpired());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'expiredpassword');
|
2008-04-26 08:31:52 +02:00
|
|
|
$this->assertTrue($member->isPasswordExpired());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
// Check the boundary conditions
|
|
|
|
// If PasswordExpiry == today, then it's expired
|
|
|
|
$member->PasswordExpiry = date('Y-m-d');
|
|
|
|
$this->assertTrue($member->isPasswordExpired());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
// If PasswordExpiry == tomorrow, then it's not
|
|
|
|
$member->PasswordExpiry = date('Y-m-d', time() + 86400);
|
|
|
|
$this->assertFalse($member->isPasswordExpired());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testMemberWithNoDateFormatFallsbackToGlobalLocaleDefaultFormat() {
|
2014-02-11 12:12:37 +01:00
|
|
|
Config::inst()->update('i18n', 'date_format', 'yyyy-MM-dd');
|
|
|
|
Config::inst()->update('i18n', 'time_format', 'H:mm');
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'noformatmember');
|
2014-02-11 12:12:37 +01:00
|
|
|
$this->assertEquals('yyyy-MM-dd', $member->DateFormat);
|
|
|
|
$this->assertEquals('H:mm', $member->TimeFormat);
|
2010-10-15 05:23:02 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testInGroups() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$staffmember = $this->objFromFixture('SilverStripe\\Security\\Member', 'staffmember');
|
|
|
|
$managementmember = $this->objFromFixture('SilverStripe\\Security\\Member', 'managementmember');
|
|
|
|
$accountingmember = $this->objFromFixture('SilverStripe\\Security\\Member', 'accountingmember');
|
|
|
|
$ceomember = $this->objFromFixture('SilverStripe\\Security\\Member', 'ceomember');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$staffgroup = $this->objFromFixture('SilverStripe\\Security\\Group', 'staffgroup');
|
|
|
|
$managementgroup = $this->objFromFixture('SilverStripe\\Security\\Group', 'managementgroup');
|
|
|
|
$accountinggroup = $this->objFromFixture('SilverStripe\\Security\\Group', 'accountinggroup');
|
|
|
|
$ceogroup = $this->objFromFixture('SilverStripe\\Security\\Group', 'ceogroup');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-03 02:57:16 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$staffmember->inGroups(array($staffgroup, $managementgroup)),
|
|
|
|
'inGroups() succeeds if a membership is detected on one of many passed groups'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$staffmember->inGroups(array($ceogroup, $managementgroup)),
|
|
|
|
'inGroups() fails if a membership is detected on none of the passed groups'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$ceomember->inGroups(array($staffgroup, $managementgroup), true),
|
|
|
|
'inGroups() fails if no direct membership is detected on any of the passed groups (in strict mode)'
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAddToGroupByCode() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$grouplessMember = $this->objFromFixture('SilverStripe\\Security\\Member', 'grouplessmember');
|
|
|
|
$memberlessGroup = $this->objFromFixture('SilverStripe\\Security\\Group','memberlessgroup');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:00:48 +02:00
|
|
|
$this->assertFalse($grouplessMember->Groups()->exists());
|
|
|
|
$this->assertFalse($memberlessGroup->Members()->exists());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:00:48 +02:00
|
|
|
$grouplessMember->addToGroupByCode('memberless');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:00:48 +02:00
|
|
|
$this->assertEquals($memberlessGroup->Members()->Count(), 1);
|
|
|
|
$this->assertEquals($grouplessMember->Groups()->Count(), 1);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:00:48 +02:00
|
|
|
$grouplessMember->addToGroupByCode('somegroupthatwouldneverexist', 'New Group');
|
|
|
|
$this->assertEquals($grouplessMember->Groups()->Count(), 2);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$group = DataObject::get_one('SilverStripe\\Security\\Group', array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'"Group"."Code"' => 'somegroupthatwouldneverexist'
|
|
|
|
));
|
2010-10-15 05:00:48 +02:00
|
|
|
$this->assertNotNull($group);
|
|
|
|
$this->assertEquals($group->Code, 'somegroupthatwouldneverexist');
|
|
|
|
$this->assertEquals($group->Title, 'New Group');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:00:48 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-01 03:05:13 +01:00
|
|
|
public function testRemoveFromGroupByCode() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$grouplessMember = $this->objFromFixture('SilverStripe\\Security\\Member', 'grouplessmember');
|
|
|
|
$memberlessGroup = $this->objFromFixture('SilverStripe\\Security\\Group','memberlessgroup');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-01 03:05:13 +01:00
|
|
|
$this->assertFalse($grouplessMember->Groups()->exists());
|
|
|
|
$this->assertFalse($memberlessGroup->Members()->exists());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-01 03:05:13 +01:00
|
|
|
$grouplessMember->addToGroupByCode('memberless');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-01 03:05:13 +01:00
|
|
|
$this->assertEquals($memberlessGroup->Members()->Count(), 1);
|
|
|
|
$this->assertEquals($grouplessMember->Groups()->Count(), 1);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-01 03:05:13 +01:00
|
|
|
$grouplessMember->addToGroupByCode('somegroupthatwouldneverexist', 'New Group');
|
|
|
|
$this->assertEquals($grouplessMember->Groups()->Count(), 2);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$group = DataObject::get_one('SilverStripe\\Security\\Group', "\"Code\" = 'somegroupthatwouldneverexist'");
|
2013-11-01 03:05:13 +01:00
|
|
|
$this->assertNotNull($group);
|
|
|
|
$this->assertEquals($group->Code, 'somegroupthatwouldneverexist');
|
|
|
|
$this->assertEquals($group->Title, 'New Group');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-01 03:05:13 +01:00
|
|
|
$grouplessMember->removeFromGroupByCode('memberless');
|
|
|
|
$this->assertEquals($memberlessGroup->Members()->Count(), 0);
|
|
|
|
$this->assertEquals($grouplessMember->Groups()->Count(), 1);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-01 03:05:13 +01:00
|
|
|
$grouplessMember->removeFromGroupByCode('somegroupthatwouldneverexist');
|
|
|
|
$this->assertEquals($grouplessMember->Groups()->Count(), 0);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testInGroup() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$staffmember = $this->objFromFixture('SilverStripe\\Security\\Member', 'staffmember');
|
|
|
|
$managementmember = $this->objFromFixture('SilverStripe\\Security\\Member', 'managementmember');
|
|
|
|
$accountingmember = $this->objFromFixture('SilverStripe\\Security\\Member', 'accountingmember');
|
|
|
|
$ceomember = $this->objFromFixture('SilverStripe\\Security\\Member', 'ceomember');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$staffgroup = $this->objFromFixture('SilverStripe\\Security\\Group', 'staffgroup');
|
|
|
|
$managementgroup = $this->objFromFixture('SilverStripe\\Security\\Group', 'managementgroup');
|
|
|
|
$accountinggroup = $this->objFromFixture('SilverStripe\\Security\\Group', 'accountinggroup');
|
|
|
|
$ceogroup = $this->objFromFixture('SilverStripe\\Security\\Group', 'ceogroup');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-03 02:57:16 +01:00
|
|
|
$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'
|
|
|
|
);
|
2009-04-29 02:07:39 +02:00
|
|
|
$this->assertFalse(
|
|
|
|
$staffmember->inGroup('does-not-exist'),
|
|
|
|
'Non-existant group returns false'
|
|
|
|
);
|
2008-11-03 02:57:16 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-16 06:39:39 +01:00
|
|
|
/**
|
|
|
|
* 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() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$extensions = $this->removeExtensions(Object::get_extensions('SilverStripe\\Security\\Member'));
|
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'test');
|
|
|
|
$member2 = $this->objFromFixture('SilverStripe\\Security\\Member', 'staffmember');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-16 06:39:39 +01:00
|
|
|
$this->session()->inst_set('loggedInAs', null);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-16 06:39:39 +01:00
|
|
|
/* Not logged in, you can't view, delete or edit the record */
|
|
|
|
$this->assertFalse($member->canView());
|
|
|
|
$this->assertFalse($member->canDelete());
|
|
|
|
$this->assertFalse($member->canEdit());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-16 06:39:39 +01:00
|
|
|
/* Logged in users can edit their own record */
|
|
|
|
$this->session()->inst_set('loggedInAs', $member->ID);
|
|
|
|
$this->assertTrue($member->canView());
|
2012-12-15 20:23:50 +01:00
|
|
|
$this->assertFalse($member->canDelete());
|
2009-12-16 06:39:39 +01:00
|
|
|
$this->assertTrue($member->canEdit());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-16 06:39:39 +01:00
|
|
|
/* Other uses cannot view, delete or edit others records */
|
|
|
|
$this->session()->inst_set('loggedInAs', $member2->ID);
|
|
|
|
$this->assertFalse($member->canView());
|
|
|
|
$this->assertFalse($member->canDelete());
|
|
|
|
$this->assertFalse($member->canEdit());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-16 06:39:39 +01:00
|
|
|
$this->addExtensions($extensions);
|
|
|
|
$this->session()->inst_set('loggedInAs', null);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-16 06:39:39 +01:00
|
|
|
public function testAuthorisedMembersCanManipulateOthersRecords() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$extensions = $this->removeExtensions(Object::get_extensions('SilverStripe\\Security\\Member'));
|
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'test');
|
|
|
|
$member2 = $this->objFromFixture('SilverStripe\\Security\\Member', 'staffmember');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-16 06:39:39 +01:00
|
|
|
/* Group members with SecurityAdmin permissions can manipulate other records */
|
|
|
|
$this->session()->inst_set('loggedInAs', $member->ID);
|
|
|
|
$this->assertTrue($member2->canView());
|
|
|
|
$this->assertTrue($member2->canDelete());
|
|
|
|
$this->assertTrue($member2->canEdit());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-16 06:39:39 +01:00
|
|
|
$this->addExtensions($extensions);
|
|
|
|
$this->session()->inst_set('loggedInAs', null);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
public function testExtendedCan() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$extensions = $this->removeExtensions(Object::get_extensions('SilverStripe\\Security\\Member'));
|
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'test');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-16 06:39:57 +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());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
/* Apply a extension that allows viewing in any case (most likely the case for member profiles) */
|
2012-10-09 05:55:37 +02:00
|
|
|
Member::add_extension('MemberTest_ViewingAllowedExtension');
|
2016-06-23 01:37:22 +02:00
|
|
|
$member2 = $this->objFromFixture('SilverStripe\\Security\\Member', 'staffmember');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-16 06:39:57 +01:00
|
|
|
$this->assertTrue($member2->canView());
|
|
|
|
$this->assertFalse($member2->canDelete());
|
|
|
|
$this->assertFalse($member2->canEdit());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
/* Apply a extension that denies viewing of the Member */
|
2012-10-09 23:30:40 +02:00
|
|
|
Member::remove_extension('MemberTest_ViewingAllowedExtension');
|
2012-10-09 05:55:37 +02:00
|
|
|
Member::add_extension('MemberTest_ViewingDeniedExtension');
|
2016-06-23 01:37:22 +02:00
|
|
|
$member3 = $this->objFromFixture('SilverStripe\\Security\\Member', 'managementmember');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-16 06:39:57 +01:00
|
|
|
$this->assertFalse($member3->canView());
|
|
|
|
$this->assertFalse($member3->canDelete());
|
|
|
|
$this->assertFalse($member3->canEdit());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
/* Apply a extension that allows viewing and editing but denies deletion */
|
2012-10-09 23:30:40 +02:00
|
|
|
Member::remove_extension('MemberTest_ViewingDeniedExtension');
|
2012-10-09 05:55:37 +02:00
|
|
|
Member::add_extension('MemberTest_EditingAllowedDeletingDeniedExtension');
|
2016-06-23 01:37:22 +02:00
|
|
|
$member4 = $this->objFromFixture('SilverStripe\\Security\\Member', 'accountingmember');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-16 06:39:57 +01:00
|
|
|
$this->assertTrue($member4->canView());
|
|
|
|
$this->assertFalse($member4->canDelete());
|
|
|
|
$this->assertTrue($member4->canEdit());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-09 23:30:40 +02:00
|
|
|
Member::remove_extension('MemberTest_EditingAllowedDeletingDeniedExtension');
|
2009-12-16 06:39:57 +01:00
|
|
|
$this->addExtensions($extensions);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 03:24:04 +02:00
|
|
|
/**
|
|
|
|
* Tests for {@link Member::getName()} and {@link Member::setName()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testName() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'test');
|
2010-10-19 03:24:04 +02:00
|
|
|
$member->setName('Test Some User');
|
|
|
|
$this->assertEquals('Test Some User', $member->getName());
|
|
|
|
$member->setName('Test');
|
|
|
|
$this->assertEquals('Test', $member->getName());
|
|
|
|
$member->FirstName = 'Test';
|
|
|
|
$member->Surname = '';
|
|
|
|
$this->assertEquals('Test', $member->getName());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testMembersWithSecurityAdminAccessCantEditAdminsUnlessTheyreAdminsThemselves() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$adminMember = $this->objFromFixture('SilverStripe\\Security\\Member', 'admin');
|
|
|
|
$otherAdminMember = $this->objFromFixture('SilverStripe\\Security\\Member', 'other-admin');
|
|
|
|
$securityAdminMember = $this->objFromFixture('SilverStripe\\Security\\Member', 'test');
|
|
|
|
$ceoMember = $this->objFromFixture('SilverStripe\\Security\\Member', 'ceomember');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 04:46:26 +02:00
|
|
|
// Careful: Don't read as english language.
|
|
|
|
// More precisely this should read canBeEditedBy()
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 04:46:26 +02:00
|
|
|
$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');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 04:46:26 +02:00
|
|
|
$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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testOnChangeGroups() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$staffGroup = $this->objFromFixture('SilverStripe\\Security\\Group', 'staffgroup');
|
|
|
|
$staffMember = $this->objFromFixture('SilverStripe\\Security\\Member', 'staffmember');
|
|
|
|
$adminMember = $this->objFromFixture('SilverStripe\\Security\\Member', 'admin');
|
2011-03-09 03:49:41 +01:00
|
|
|
$newAdminGroup = new Group(array('Title' => 'newadmin'));
|
|
|
|
$newAdminGroup->write();
|
|
|
|
Permission::grant($newAdminGroup->ID, 'ADMIN');
|
|
|
|
$newOtherGroup = new Group(array('Title' => 'othergroup'));
|
|
|
|
$newOtherGroup->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-09 03:49:41 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$staffMember->onChangeGroups(array($staffGroup->ID)),
|
|
|
|
'Adding existing non-admin group relation is allowed for non-admin members'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$staffMember->onChangeGroups(array($newOtherGroup->ID)),
|
|
|
|
'Adding new non-admin group relation is allowed for non-admin members'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$staffMember->onChangeGroups(array($newAdminGroup->ID)),
|
|
|
|
'Adding new admin group relation is not allowed for non-admin members'
|
|
|
|
);
|
2012-04-27 02:22:58 +02:00
|
|
|
|
|
|
|
$this->session()->inst_set('loggedInAs', $adminMember->ID);
|
|
|
|
$this->assertTrue(
|
|
|
|
$staffMember->onChangeGroups(array($newAdminGroup->ID)),
|
|
|
|
'Adding new admin group relation is allowed for normal users, when granter is logged in as admin'
|
|
|
|
);
|
|
|
|
$this->session()->inst_set('loggedInAs', null);
|
|
|
|
|
2011-03-09 03:49:41 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$adminMember->onChangeGroups(array($newAdminGroup->ID)),
|
|
|
|
'Adding new admin group relation is allowed for admin members'
|
|
|
|
);
|
|
|
|
}
|
2014-02-16 20:36:51 +01:00
|
|
|
|
2015-09-07 03:44:16 +02:00
|
|
|
/**
|
|
|
|
* Test Member_GroupSet::add
|
|
|
|
*/
|
|
|
|
public function testOnChangeGroupsByAdd() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$staffMember = $this->objFromFixture('SilverStripe\\Security\\Member', 'staffmember');
|
|
|
|
$adminMember = $this->objFromFixture('SilverStripe\\Security\\Member', 'admin');
|
2015-09-07 03:44:16 +02:00
|
|
|
|
|
|
|
// Setup new admin group
|
|
|
|
$newAdminGroup = new Group(array('Title' => 'newadmin'));
|
|
|
|
$newAdminGroup->write();
|
|
|
|
Permission::grant($newAdminGroup->ID, 'ADMIN');
|
|
|
|
|
|
|
|
// Setup non-admin group
|
|
|
|
$newOtherGroup = new Group(array('Title' => 'othergroup'));
|
|
|
|
$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
|
|
|
|
$this->session()->inst_set('loggedInAs', null);
|
|
|
|
$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() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$staffMember = $this->objFromFixture('SilverStripe\\Security\\Member', 'staffmember');
|
2015-09-07 03:44:16 +02:00
|
|
|
|
|
|
|
// Setup new admin group
|
|
|
|
$newAdminGroup = new Group(array('Title' => 'newadmin'));
|
|
|
|
$newAdminGroup->write();
|
|
|
|
Permission::grant($newAdminGroup->ID, 'ADMIN');
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-09-07 03:44:16 +02:00
|
|
|
// Test staff member can't be added to admin groups
|
|
|
|
$this->assertFalse($staffMember->inGroup($newAdminGroup));
|
|
|
|
$staffMember->Groups()->setByIDList(array($newAdminGroup->ID));
|
|
|
|
$this->assertFalse(
|
|
|
|
$staffMember->inGroup($newAdminGroup),
|
|
|
|
'Adding new admin group relation is not allowed for non-admin members'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-02-16 20:36:51 +01:00
|
|
|
/**
|
|
|
|
* Test that extensions using updateCMSFields() are applied correctly
|
|
|
|
*/
|
|
|
|
public function testUpdateCMSFields() {
|
|
|
|
Member::add_extension('MemberTest_FieldsExtension');
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = singleton('SilverStripe\\Security\\Member');
|
2014-02-16 20:36:51 +01:00
|
|
|
$fields = $member->getCMSFields();
|
|
|
|
|
|
|
|
$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('MemberTest_FieldsExtension');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-10 03:53:45 +02:00
|
|
|
/**
|
|
|
|
* Test that all members are returned
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testMap_in_groupsReturnsAll() {
|
2012-05-10 03:53:45 +02:00
|
|
|
$members = Member::map_in_groups();
|
2016-01-21 21:29:53 +01:00
|
|
|
$this->assertEquals(13, $members->count(), 'There are 12 members in the mock plus a fake admin');
|
2012-05-10 03:53:45 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-10 03:53:45 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Test that only admin members are returned
|
2012-05-10 03:53:45 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testMap_in_groupsReturnsAdmins() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$adminID = $this->objFromFixture('SilverStripe\\Security\\Group', 'admingroup')->ID;
|
2016-01-21 21:29:53 +01:00
|
|
|
$members = Member::map_in_groups($adminID)->toArray();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'admin');
|
|
|
|
$otherAdmin = $this->objFromFixture('SilverStripe\\Security\\Member', 'other-admin');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertTrue(in_array($admin->getTitle(), $members),
|
|
|
|
$admin->getTitle().' should be in the returned list.');
|
|
|
|
$this->assertTrue(in_array($otherAdmin->getTitle(), $members),
|
|
|
|
$otherAdmin->getTitle().' should be in the returned list.');
|
2012-05-10 03:53:45 +02:00
|
|
|
$this->assertEquals(2, count($members), 'There should be 2 members from the admin group');
|
|
|
|
}
|
2010-10-19 03:24:04 +02:00
|
|
|
|
2009-12-16 06:39:39 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-12-16 06:39:39 +01:00
|
|
|
* @param array $extensions
|
|
|
|
* @return array The added extensions
|
|
|
|
*/
|
|
|
|
protected function addExtensions($extensions) {
|
|
|
|
if($extensions) foreach($extensions as $extension) {
|
2012-10-09 05:55:37 +02:00
|
|
|
Member::add_extension($extension);
|
2009-12-16 06:39:39 +01:00
|
|
|
}
|
|
|
|
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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-12-16 06:39:39 +01:00
|
|
|
* @param array $extensions
|
|
|
|
* @return array The removed extensions
|
|
|
|
*/
|
|
|
|
protected function removeExtensions($extensions) {
|
|
|
|
if($extensions) foreach($extensions as $extension) {
|
2012-10-09 23:30:40 +02:00
|
|
|
Member::remove_extension($extension);
|
2009-12-16 06:39:39 +01:00
|
|
|
}
|
|
|
|
return $extensions;
|
|
|
|
}
|
|
|
|
|
2012-11-08 04:33:19 +01:00
|
|
|
public function testGenerateAutologinTokenAndStoreHash() {
|
|
|
|
$enc = new PasswordEncryptor_Blowfish();
|
|
|
|
|
|
|
|
$m = new Member();
|
|
|
|
$m->PasswordEncryption = 'blowfish';
|
|
|
|
$m->Salt = $enc->salt('123');
|
|
|
|
|
|
|
|
$token = $m->generateAutologinTokenAndStoreHash();
|
|
|
|
|
|
|
|
$this->assertEquals($m->encryptWithUserSettings($token), $m->AutoLoginHash, 'Stores the token as ahash.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateAutoLoginToken() {
|
|
|
|
$enc = new PasswordEncryptor_Blowfish();
|
|
|
|
|
|
|
|
$m1 = new Member();
|
|
|
|
$m1->PasswordEncryption = 'blowfish';
|
|
|
|
$m1->Salt = $enc->salt('123');
|
|
|
|
$m1Token = $m1->generateAutologinTokenAndStoreHash();
|
|
|
|
|
|
|
|
$m2 = new Member();
|
|
|
|
$m2->PasswordEncryption = 'blowfish';
|
|
|
|
$m2->Salt = $enc->salt('456');
|
|
|
|
$m2Token = $m2->generateAutologinTokenAndStoreHash();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-08 04:33:19 +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.');
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:21:06 +01:00
|
|
|
public function testRememberMeHashGeneration() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$m1 = $this->objFromFixture('SilverStripe\\Security\\Member', 'grouplessmember');
|
2016-02-04 23:21:06 +01:00
|
|
|
|
|
|
|
$m1->login(true);
|
|
|
|
$hashes = RememberLoginHash::get()->filter('MemberID', $m1->ID);
|
|
|
|
$this->assertEquals($hashes->Count(), 1);
|
|
|
|
$firstHash = $hashes->First();
|
|
|
|
$this->assertNotNull($firstHash->DeviceID);
|
|
|
|
$this->assertNotNull($firstHash->Hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRememberMeHashAutologin() {
|
2016-06-23 01:37:22 +02:00
|
|
|
/** @var Member $m1 */
|
|
|
|
$m1 = $this->objFromFixture('SilverStripe\\Security\\Member', 'noexpiry');
|
2016-02-04 23:21:06 +01:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$m1->logIn(true);
|
2016-02-04 23:21:06 +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(
|
2016-03-08 21:50:18 +01:00
|
|
|
'Security/login',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2016-02-04 23:21:06 +01:00
|
|
|
array(
|
|
|
|
'alc_enc' => $m1->ID.':'.$token,
|
|
|
|
'alc_device' => $firstHash->DeviceID
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$message = _t(
|
|
|
|
'Member.LOGGEDINAS',
|
|
|
|
"You're logged in as {name}.",
|
|
|
|
array('name' => $m1->FirstName)
|
|
|
|
);
|
|
|
|
$this->assertContains($message, $response->getBody());
|
|
|
|
|
|
|
|
$this->session()->inst_set('loggedInAs', null);
|
|
|
|
|
2016-03-08 21:50:18 +01:00
|
|
|
// A wrong token or a wrong device ID should not let us autologin
|
2016-02-04 23:21:06 +01:00
|
|
|
$response = $this->get(
|
2016-03-08 21:50:18 +01:00
|
|
|
'Security/login',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2016-02-04 23:21:06 +01:00
|
|
|
array(
|
|
|
|
'alc_enc' => $m1->ID.':'.str_rot13($token),
|
|
|
|
'alc_device' => $firstHash->DeviceID
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertNotContains($message, $response->getBody());
|
|
|
|
|
|
|
|
$response = $this->get(
|
2016-03-08 21:50:18 +01:00
|
|
|
'Security/login',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2016-02-04 23:21:06 +01:00
|
|
|
array(
|
|
|
|
'alc_enc' => $m1->ID.':'.$token,
|
|
|
|
'alc_device' => str_rot13($firstHash->DeviceID)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertNotContains($message, $response->getBody());
|
|
|
|
|
2016-03-08 21:50:18 +01:00
|
|
|
// Re-logging (ie 'alc_enc' has expired), and not checking the "Remember Me" option
|
2016-02-04 23:21:06 +01:00
|
|
|
// should remove all previous hashes for this device
|
|
|
|
$response = $this->post(
|
2016-03-08 21:50:18 +01:00
|
|
|
'Security/LoginForm',
|
2016-02-04 23:21:06 +01:00
|
|
|
array(
|
|
|
|
'Email' => $m1->Email,
|
|
|
|
'Password' => '1nitialPassword',
|
2016-06-23 01:37:22 +02:00
|
|
|
'AuthenticationMethod' => 'SilverStripe\\Security\\MemberAuthenticator',
|
2016-02-04 23:21:06 +01:00
|
|
|
'action_dologin' => 'action_dologin'
|
|
|
|
),
|
|
|
|
null,
|
|
|
|
$this->session(),
|
2016-03-08 21:50:18 +01:00
|
|
|
null,
|
2016-02-04 23:21:06 +01:00
|
|
|
array(
|
|
|
|
'alc_device' => $firstHash->DeviceID
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertContains($message, $response->getBody());
|
|
|
|
$this->assertEquals(RememberLoginHash::get()->filter('MemberID', $m1->ID)->Count(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExpiredRememberMeHashAutologin() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$m1 = $this->objFromFixture('SilverStripe\\Security\\Member', 'noexpiry');
|
2016-02-04 23:21:06 +01:00
|
|
|
|
|
|
|
$m1->login(true);
|
|
|
|
$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();
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
DBDateTime::set_mock_now('1999-12-31 23:59:59');
|
2016-02-04 23:21:06 +01:00
|
|
|
|
|
|
|
$response = $this->get(
|
2016-03-08 21:50:18 +01:00
|
|
|
'Security/login',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2016-02-04 23:21:06 +01:00
|
|
|
array(
|
|
|
|
'alc_enc' => $m1->ID.':'.$token,
|
|
|
|
'alc_device' => $firstHash->DeviceID
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$message = _t(
|
|
|
|
'Member.LOGGEDINAS',
|
|
|
|
"You're logged in as {name}.",
|
|
|
|
array('name' => $m1->FirstName)
|
|
|
|
);
|
|
|
|
$this->assertContains($message, $response->getBody());
|
|
|
|
|
|
|
|
$this->session()->inst_set('loggedInAs', null);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
DBDateTime::set_mock_now('2000-01-01 00:00:01');
|
2016-02-04 23:21:06 +01:00
|
|
|
|
|
|
|
$response = $this->get(
|
2016-03-08 21:50:18 +01:00
|
|
|
'Security/login',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2016-02-04 23:21:06 +01:00
|
|
|
array(
|
|
|
|
'alc_enc' => $m1->ID.':'.$token,
|
|
|
|
'alc_device' => $firstHash->DeviceID
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertNotContains($message, $response->getBody());
|
|
|
|
$this->session()->inst_set('loggedInAs', null);
|
2016-06-15 06:03:16 +02:00
|
|
|
DBDatetime::clear_mock_now();
|
2016-02-04 23:21:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRememberMeMultipleDevices() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$m1 = $this->objFromFixture('SilverStripe\\Security\\Member', 'noexpiry');
|
2016-02-04 23:21:06 +01:00
|
|
|
|
|
|
|
// First device
|
|
|
|
$m1->login(true);
|
|
|
|
Cookie::set('alc_device', null);
|
|
|
|
// Second device
|
|
|
|
$m1->login(true);
|
|
|
|
|
|
|
|
// Hash of first device
|
|
|
|
$firstHash = RememberLoginHash::get()->filter('MemberID', $m1->ID)->First();
|
|
|
|
$this->assertNotNull($firstHash);
|
|
|
|
|
|
|
|
// Hash of second device
|
|
|
|
$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(
|
2016-03-08 21:50:18 +01:00
|
|
|
'Security/login',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2016-02-04 23:21:06 +01:00
|
|
|
array(
|
|
|
|
'alc_enc' => $m1->ID.':'.$firstToken,
|
|
|
|
'alc_device' => $firstHash->DeviceID
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$message = _t(
|
|
|
|
'Member.LOGGEDINAS',
|
|
|
|
"You're logged in as {name}.",
|
|
|
|
array('name' => $m1->FirstName)
|
|
|
|
);
|
|
|
|
$this->assertContains($message, $response->getBody());
|
|
|
|
|
|
|
|
$this->session()->inst_set('loggedInAs', null);
|
|
|
|
|
|
|
|
// Accessing the login page from the second device
|
|
|
|
$response = $this->get(
|
2016-03-08 21:50:18 +01:00
|
|
|
'Security/login',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2016-02-04 23:21:06 +01:00
|
|
|
array(
|
|
|
|
'alc_enc' => $m1->ID.':'.$secondToken,
|
|
|
|
'alc_device' => $secondHash->DeviceID
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertContains($message, $response->getBody());
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$logout_across_devices = Config::inst()->get('SilverStripe\\Security\\RememberLoginHash', 'logout_across_devices');
|
2016-02-04 23:21:06 +01:00
|
|
|
|
|
|
|
// Logging out from the second device - only one device being logged out
|
2016-06-23 01:37:22 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Security\\RememberLoginHash', 'logout_across_devices', false);
|
2016-02-04 23:21:06 +01:00
|
|
|
$response = $this->get(
|
2016-03-08 21:50:18 +01:00
|
|
|
'Security/logout',
|
|
|
|
$this->session(),
|
|
|
|
null,
|
2016-02-04 23:21:06 +01:00
|
|
|
array(
|
|
|
|
'alc_enc' => $m1->ID.':'.$secondToken,
|
|
|
|
'alc_device' => $secondHash->DeviceID
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2016-03-08 21:50:18 +01:00
|
|
|
RememberLoginHash::get()->filter(array('MemberID'=>$m1->ID, 'DeviceID'=>$firstHash->DeviceID))->Count(),
|
2016-02-04 23:21:06 +01:00
|
|
|
1
|
|
|
|
);
|
|
|
|
|
|
|
|
// Logging out from any device when all login hashes should be removed
|
2016-06-23 01:37:22 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Security\\RememberLoginHash', 'logout_across_devices', true);
|
2016-02-04 23:21:06 +01:00
|
|
|
$m1->login(true);
|
|
|
|
$response = $this->get('Security/logout', $this->session());
|
|
|
|
$this->assertEquals(
|
2016-03-08 21:50:18 +01:00
|
|
|
RememberLoginHash::get()->filter('MemberID', $m1->ID)->Count(),
|
2016-02-04 23:21:06 +01:00
|
|
|
0
|
|
|
|
);
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Security\\RememberLoginHash', 'logout_across_devices', $logout_across_devices);
|
2016-02-04 23:21:06 +01:00
|
|
|
}
|
|
|
|
|
2012-12-15 20:23:50 +01:00
|
|
|
public function testCanDelete() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$admin1 = $this->objFromFixture('SilverStripe\\Security\\Member', 'admin');
|
|
|
|
$admin2 = $this->objFromFixture('SilverStripe\\Security\\Member', 'other-admin');
|
|
|
|
$member1 = $this->objFromFixture('SilverStripe\\Security\\Member', 'grouplessmember');
|
|
|
|
$member2 = $this->objFromFixture('SilverStripe\\Security\\Member', 'noformatmember');
|
2012-12-15 20:23:50 +01:00
|
|
|
|
|
|
|
$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'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-10-17 19:42:50 +02:00
|
|
|
public function testFailedLoginCount() {
|
|
|
|
$maxFailedLoginsAllowed = 3;
|
|
|
|
//set up the config variables to enable login lockouts
|
|
|
|
Config::nest();
|
2016-06-23 01:37:22 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Security\\Member', 'lock_out_after_incorrect_logins', $maxFailedLoginsAllowed);
|
2013-10-17 19:42:50 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'test');
|
2013-10-17 19:42:50 +02:00
|
|
|
$failedLoginCount = $member->FailedLoginCount;
|
|
|
|
|
|
|
|
for ($i = 1; $i < $maxFailedLoginsAllowed; ++$i) {
|
|
|
|
$member->registerFailedLogin();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
++$failedLoginCount,
|
|
|
|
$member->FailedLoginCount,
|
|
|
|
'Failed to increment $member->FailedLoginCount'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$member->isLockedOut(),
|
|
|
|
"Member has been locked out too early"
|
|
|
|
);
|
2013-10-23 18:26:05 +02:00
|
|
|
}
|
2013-10-23 14:53:29 +02:00
|
|
|
}
|
2013-10-17 19:42:50 +02:00
|
|
|
|
2016-02-24 13:23:38 +01:00
|
|
|
public function testMemberValidator()
|
|
|
|
{
|
|
|
|
// clear custom requirements for this test
|
2016-06-23 01:37:22 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Security\\Member_Validator', 'customRequired', null);
|
|
|
|
$memberA = $this->objFromFixture('SilverStripe\\Security\\Member', 'admin');
|
|
|
|
$memberB = $this->objFromFixture('SilverStripe\\Security\\Member', 'test');
|
2016-02-24 13:23:38 +01:00
|
|
|
|
|
|
|
// 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(array(
|
|
|
|
'FirstName' => 'Test',
|
|
|
|
'Email' => $memberA->Email
|
|
|
|
));
|
|
|
|
|
|
|
|
$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(array(
|
|
|
|
'FirstName' => 'Test',
|
|
|
|
'Email' => $memberA->Email
|
|
|
|
));
|
|
|
|
|
|
|
|
// Simulate update to a new Email address
|
|
|
|
$pass1 = $validator->php(array(
|
|
|
|
'FirstName' => 'Test',
|
|
|
|
'Email' => 'membervalidatortest@testing.com'
|
|
|
|
));
|
|
|
|
|
|
|
|
// Pass in the same Email address that the member already has. Ensure that case is valid
|
|
|
|
$pass2 = $validator->php(array(
|
|
|
|
'FirstName' => 'Test',
|
|
|
|
'Surname' => 'User',
|
|
|
|
'Email' => $memberB->Email
|
|
|
|
));
|
|
|
|
|
|
|
|
$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
|
2016-06-23 01:37:22 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Security\\Member_Validator', 'customRequired', null);
|
2016-02-24 13:23:38 +01:00
|
|
|
|
|
|
|
// create a blank form
|
|
|
|
$form = new MemberTest_ValidatorForm();
|
|
|
|
|
|
|
|
// Test extensions
|
|
|
|
Member_Validator::add_extension('MemberTest_MemberValidator_SurnameMustMatchFirstNameExtension');
|
|
|
|
$validator = new Member_Validator();
|
|
|
|
$validator->setForm($form);
|
|
|
|
|
|
|
|
// This test should fail, since the extension enforces FirstName == Surname
|
|
|
|
$fail = $validator->php(array(
|
|
|
|
'FirstName' => 'Test',
|
|
|
|
'Surname' => 'User',
|
|
|
|
'Email' => 'test-member-validator-extension@testing.com'
|
|
|
|
));
|
|
|
|
|
|
|
|
$pass = $validator->php(array(
|
|
|
|
'FirstName' => 'Test',
|
|
|
|
'Surname' => 'Test',
|
|
|
|
'Email' => 'test-member-validator-extension@testing.com'
|
|
|
|
));
|
|
|
|
|
|
|
|
$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_MemberValidator_AlwaysFailsExtension');
|
|
|
|
$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(array(
|
|
|
|
'FirstName' => 'Test',
|
|
|
|
'Surname' => 'Test',
|
|
|
|
'Email' => 'test-member-validator-extension@testing.com'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$fail,
|
|
|
|
'Member_Validator must fail because of added extensions.'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Remove added extensions
|
|
|
|
Member_Validator::remove_extension('MemberTest_MemberValidator_AlwaysFailsExtension');
|
|
|
|
Member_Validator::remove_extension('MemberTest_MemberValidator_SurnameMustMatchFirstNameExtension');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCustomMemberValidator()
|
|
|
|
{
|
|
|
|
// clear custom requirements for this test
|
2016-06-23 01:37:22 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Security\\Member_Validator', 'customRequired', null);
|
2016-02-24 13:23:38 +01:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'admin');
|
2013-10-17 19:42:50 +02:00
|
|
|
|
2013-10-16 00:29:43 +02:00
|
|
|
$form = new MemberTest_ValidatorForm();
|
|
|
|
$form->loadDataFrom($member);
|
|
|
|
|
|
|
|
$validator = new Member_Validator();
|
|
|
|
$validator->setForm($form);
|
|
|
|
|
|
|
|
$pass = $validator->php(array(
|
|
|
|
'FirstName' => 'Borris',
|
|
|
|
'Email' => 'borris@silverstripe.com'
|
|
|
|
));
|
|
|
|
|
|
|
|
$fail = $validator->php(array(
|
|
|
|
'Email' => 'borris@silverstripe.com',
|
|
|
|
'Surname' => ''
|
|
|
|
));
|
|
|
|
|
2016-02-24 13:23:38 +01:00
|
|
|
$this->assertTrue($pass, 'Validator requires a FirstName and Email');
|
2013-10-16 00:29:43 +02:00
|
|
|
$this->assertFalse($fail, 'Missing FirstName');
|
|
|
|
|
|
|
|
$ext = new MemberTest_ValidatorExtension();
|
|
|
|
$ext->updateValidator($validator);
|
|
|
|
|
|
|
|
$pass = $validator->php(array(
|
|
|
|
'FirstName' => 'Borris',
|
|
|
|
'Email' => 'borris@silverstripe.com'
|
|
|
|
));
|
|
|
|
|
|
|
|
$fail = $validator->php(array(
|
|
|
|
'Email' => 'borris@silverstripe.com'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($pass, 'Missing surname');
|
|
|
|
$this->assertFalse($fail, 'Missing surname value');
|
|
|
|
|
|
|
|
$fail = $validator->php(array(
|
|
|
|
'Email' => 'borris@silverstripe.com',
|
|
|
|
'Surname' => 'Silverman'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($fail, 'Passes with email and surname now (no firstname)');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class MemberTest_ValidatorForm extends Form implements TestOnly {
|
2013-10-17 19:42:50 +02:00
|
|
|
|
2013-10-16 00:29:43 +02:00
|
|
|
public function __construct() {
|
|
|
|
parent::__construct(Controller::curr(), __CLASS__, new FieldList(
|
|
|
|
new TextField('Email'),
|
|
|
|
new TextField('Surname'),
|
|
|
|
new TextField('ID'),
|
|
|
|
new TextField('FirstName')
|
|
|
|
), new FieldList(
|
|
|
|
new FormAction('someAction')
|
|
|
|
));
|
2013-10-17 19:42:50 +02:00
|
|
|
}
|
2009-12-16 06:39:57 +01:00
|
|
|
}
|
2013-10-16 00:29:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class MemberTest_ValidatorExtension extends DataExtension implements TestOnly {
|
2013-10-17 19:42:50 +02:00
|
|
|
|
2013-10-16 00:29:43 +02:00
|
|
|
public function updateValidator(&$validator) {
|
|
|
|
$validator->addRequiredField('Surname');
|
|
|
|
$validator->removeRequiredField('FirstName');
|
|
|
|
}
|
2009-12-16 06:39:57 +01:00
|
|
|
}
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2016-02-24 13:23:38 +01:00
|
|
|
/**
|
|
|
|
* Extension that adds additional validation criteria
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class MemberTest_MemberValidator_SurnameMustMatchFirstNameExtension extends DataExtension implements TestOnly
|
|
|
|
{
|
|
|
|
public function updatePHP($data, $form) {
|
|
|
|
return $data['FirstName'] == $data['Surname'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension that adds additional validation criteria
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class MemberTest_MemberValidator_AlwaysFailsExtension extends DataExtension implements TestOnly
|
|
|
|
{
|
|
|
|
public function updatePHP($data, $form) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 00:29:43 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2011-04-15 11:35:30 +02:00
|
|
|
class MemberTest_ViewingAllowedExtension extends DataExtension implements TestOnly {
|
2009-12-16 06:39:57 +01:00
|
|
|
|
2012-03-27 06:04:11 +02:00
|
|
|
public function canView($member = null) {
|
2009-12-16 06:39:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-10-16 00:29:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2011-04-15 11:35:30 +02:00
|
|
|
class MemberTest_ViewingDeniedExtension extends DataExtension implements TestOnly {
|
2009-12-16 06:39:57 +01:00
|
|
|
|
2012-03-27 06:04:11 +02:00
|
|
|
public function canView($member = null) {
|
2009-12-16 06:39:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2014-02-16 20:36:51 +01:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class MemberTest_FieldsExtension extends DataExtension implements TestOnly {
|
|
|
|
|
|
|
|
public function updateCMSFields(FieldList $fields) {
|
|
|
|
$fields->addFieldToTab('Root.Main', new TextField('TestMemberField', 'Test'));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-16 00:29:43 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2011-04-15 11:35:30 +02:00
|
|
|
class MemberTest_EditingAllowedDeletingDeniedExtension extends DataExtension implements TestOnly {
|
2009-12-16 06:39:57 +01:00
|
|
|
|
2012-03-27 06:04:11 +02:00
|
|
|
public function canView($member = null) {
|
2009-12-16 06:39:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-27 06:04:11 +02:00
|
|
|
public function canEdit($member = null) {
|
2009-12-16 06:39:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-27 06:04:11 +02:00
|
|
|
public function canDelete($member = null) {
|
2009-12-16 06:39:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-02 00:28:04 +01:00
|
|
|
}
|
|
|
|
|
2013-10-16 00:29:43 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2012-03-02 00:28:04 +01:00
|
|
|
class MemberTest_PasswordValidator extends PasswordValidator {
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct() {
|
2012-03-02 00:28:04 +01:00
|
|
|
parent::__construct();
|
|
|
|
$this->minLength(7);
|
|
|
|
$this->checkHistoricalPasswords(6);
|
|
|
|
$this->characterStrength(3, array('lowercase','uppercase','digits','punctuation'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 05:28:28 +01:00
|
|
|
}
|