2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
namespace SilverStripe\Security;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
use IntlDateFormatter;
|
2016-08-11 01:40:23 +02:00
|
|
|
use SilverStripe\Admin\LeftAndMain;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\CMS\Controllers\CMSMain;
|
|
|
|
use SilverStripe\Control\Cookie;
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Control\Email\Email;
|
2017-01-13 02:48:46 +01:00
|
|
|
use SilverStripe\Control\Email\Mailer;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Session;
|
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Dev\TestMailer;
|
|
|
|
use SilverStripe\Forms\ConfirmedPasswordField;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
|
|
|
|
use SilverStripe\Forms\ListboxField;
|
|
|
|
use SilverStripe\i18n\i18n;
|
|
|
|
use SilverStripe\MSSQL\MSSQLDatabase;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
|
|
|
use SilverStripe\ORM\HasManyList;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ManyManyList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\SS_List;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\ORM\Map;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\ValidationException;
|
|
|
|
use SilverStripe\ORM\ValidationResult;
|
|
|
|
use SilverStripe\View\SSViewer;
|
|
|
|
use SilverStripe\View\TemplateGlobalProvider;
|
2016-06-23 01:37:22 +02:00
|
|
|
use DateTime;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2007-09-16 02:44:30 +02:00
|
|
|
/**
|
|
|
|
* The member class which represents the users of the system
|
2010-12-11 01:45:05 +01:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @method HasManyList LoggedPasswords()
|
|
|
|
* @method HasManyList RememberLoginHashes()
|
2014-10-06 05:01:33 +02:00
|
|
|
* @property string $FirstName
|
|
|
|
* @property string $Surname
|
|
|
|
* @property string $Email
|
|
|
|
* @property string $Password
|
|
|
|
* @property string $TempIDHash
|
|
|
|
* @property string $TempIDExpired
|
|
|
|
* @property string $AutoLoginHash
|
|
|
|
* @property string $AutoLoginExpired
|
|
|
|
* @property string $PasswordEncryption
|
|
|
|
* @property string $Salt
|
|
|
|
* @property string $PasswordExpiry
|
|
|
|
* @property string $LockedOutUntil
|
|
|
|
* @property string $Locale
|
|
|
|
* @property int $FailedLoginCount
|
|
|
|
* @property string $DateFormat
|
|
|
|
* @property string $TimeFormat
|
2007-09-16 02:44:30 +02:00
|
|
|
*/
|
2017-05-20 06:32:25 +02:00
|
|
|
class Member extends DataObject
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
private static $db = array(
|
|
|
|
'FirstName' => 'Varchar',
|
|
|
|
'Surname' => 'Varchar',
|
|
|
|
'Email' => 'Varchar(254)', // See RFC 5321, Section 4.5.3.1.3. (256 minus the < and > character)
|
|
|
|
'TempIDHash' => 'Varchar(160)', // Temporary id used for cms re-authentication
|
|
|
|
'TempIDExpired' => 'Datetime', // Expiry of temp login
|
|
|
|
'Password' => 'Varchar(160)',
|
|
|
|
'AutoLoginHash' => 'Varchar(160)', // Used to auto-login the user on password reset
|
|
|
|
'AutoLoginExpired' => 'Datetime',
|
|
|
|
// This is an arbitrary code pointing to a PasswordEncryptor instance,
|
|
|
|
// not an actual encryption algorithm.
|
|
|
|
// Warning: Never change this field after its the first password hashing without
|
|
|
|
// providing a new cleartext password as well.
|
|
|
|
'PasswordEncryption' => "Varchar(50)",
|
|
|
|
'Salt' => 'Varchar(50)',
|
|
|
|
'PasswordExpiry' => 'Date',
|
|
|
|
'LockedOutUntil' => 'Datetime',
|
|
|
|
'Locale' => 'Varchar(6)',
|
|
|
|
// handled in registerFailedLogin(), only used if $lock_out_after_incorrect_logins is set
|
|
|
|
'FailedLoginCount' => 'Int',
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $belongs_many_many = array(
|
2017-01-26 05:20:08 +01:00
|
|
|
'Groups' => Group::class,
|
2016-11-23 06:09:10 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
private static $has_many = array(
|
2017-01-26 05:20:08 +01:00
|
|
|
'LoggedPasswords' => MemberPassword::class,
|
|
|
|
'RememberLoginHashes' => RememberLoginHash::class,
|
2016-11-23 06:09:10 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
private static $table_name = "Member";
|
|
|
|
|
|
|
|
private static $default_sort = '"Surname", "FirstName"';
|
|
|
|
|
|
|
|
private static $indexes = array(
|
|
|
|
'Email' => true,
|
|
|
|
//Removed due to duplicate null values causing MSSQL problems
|
|
|
|
//'AutoLoginHash' => Array('type'=>'unique', 'value'=>'AutoLoginHash', 'ignoreNulls'=>true)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private static $notify_password_change = false;
|
2008-04-26 08:32:05 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* All searchable database columns
|
|
|
|
* in this object, currently queried
|
|
|
|
* with a "column LIKE '%keywords%'
|
|
|
|
* statement.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @todo Generic implementation of $searchable_fields on DataObject,
|
|
|
|
* with definition for different searching algorithms
|
|
|
|
* (LIKE, FULLTEXT) and default FormFields to construct a searchform.
|
|
|
|
*/
|
|
|
|
private static $searchable_fields = array(
|
|
|
|
'FirstName',
|
|
|
|
'Surname',
|
|
|
|
'Email',
|
|
|
|
);
|
2013-07-11 01:17:02 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $summary_fields = array(
|
|
|
|
'FirstName',
|
|
|
|
'Surname',
|
|
|
|
'Email',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $casting = array(
|
|
|
|
'Name' => 'Varchar',
|
|
|
|
);
|
2009-10-12 05:27:41 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Internal-use only fields
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $hidden_fields = array(
|
|
|
|
'AutoLoginHash',
|
|
|
|
'AutoLoginExpired',
|
|
|
|
'PasswordEncryption',
|
|
|
|
'PasswordExpiry',
|
|
|
|
'LockedOutUntil',
|
|
|
|
'TempIDHash',
|
|
|
|
'TempIDExpired',
|
|
|
|
'Salt',
|
|
|
|
);
|
2010-10-19 02:55:33 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array See {@link set_title_columns()}
|
|
|
|
*/
|
|
|
|
private static $title_format = null;
|
2014-10-06 05:01:33 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* The unique field used to identify this member.
|
|
|
|
* By default, it's "Email", but another common
|
|
|
|
* field could be Username.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
|
|
|
private static $unique_identifier_field = 'Email';
|
2014-10-06 05:01:33 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Object for validating user's password
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var PasswordValidator
|
|
|
|
*/
|
|
|
|
private static $password_validator = null;
|
2010-10-19 02:55:33 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* The number of days that a password should be valid for.
|
|
|
|
* By default, this is null, which means that passwords never expire
|
|
|
|
*/
|
|
|
|
private static $password_expiry_days = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var Int Number of incorrect logins after which
|
|
|
|
* the user is blocked from further attempts for the timespan
|
|
|
|
* defined in {@link $lock_out_delay_mins}.
|
|
|
|
*/
|
|
|
|
private static $lock_out_after_incorrect_logins = 10;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var integer Minutes of enforced lockout after incorrect password attempts.
|
|
|
|
* Only applies if {@link $lock_out_after_incorrect_logins} greater than 0.
|
|
|
|
*/
|
|
|
|
private static $lock_out_delay_mins = 15;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var String If this is set, then a session cookie with the given name will be set on log-in,
|
|
|
|
* and cleared on logout.
|
|
|
|
*/
|
|
|
|
private static $login_marker_cookie = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates that when a {@link Member} logs in, Member:session_regenerate_id()
|
|
|
|
* should be called as a security precaution.
|
|
|
|
*
|
|
|
|
* This doesn't always work, especially if you're trying to set session cookies
|
|
|
|
* across an entire site using the domain parameter to session_set_cookie_params()
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private static $session_regenerate_id = true;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default lifetime of temporary ids.
|
|
|
|
*
|
|
|
|
* This is the period within which a user can be re-authenticated within the CMS by entering only their password
|
|
|
|
* and without losing their workspace.
|
|
|
|
*
|
|
|
|
* Any session expiration outside of this time will require them to login from the frontend using their full
|
|
|
|
* username and password.
|
|
|
|
*
|
|
|
|
* Defaults to 72 hours. Set to zero to disable expiration.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var int Lifetime in seconds
|
|
|
|
*/
|
|
|
|
private static $temp_id_lifetime = 259200;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure the locale is set to something sensible by default.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function populateDefaults()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
parent::populateDefaults();
|
|
|
|
$this->Locale = i18n::get_closest_translation(i18n::get_locale());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function requireDefaultRecords()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
parent::requireDefaultRecords();
|
|
|
|
// Default groups should've been built by Group->requireDefaultRecords() already
|
|
|
|
static::default_admin();
|
|
|
|
}
|
2008-04-26 08:31:52 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Get the default admin record if it exists, or creates it otherwise if enabled
|
|
|
|
*
|
|
|
|
* @return Member
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function default_admin()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
// Check if set
|
2016-11-29 00:31:16 +01:00
|
|
|
if (!Security::has_default_admin()) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-01-13 02:48:46 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
// Find or create ADMIN group
|
|
|
|
Group::singleton()->requireDefaultRecords();
|
|
|
|
$adminGroup = Permission::get_groups_by_permission('ADMIN')->first();
|
|
|
|
|
|
|
|
// Find member
|
|
|
|
/** @skipUpgrade */
|
|
|
|
$admin = Member::get()
|
|
|
|
->filter('Email', Security::default_admin_username())
|
|
|
|
->first();
|
|
|
|
if (!$admin) {
|
|
|
|
// 'Password' is not set to avoid creating
|
|
|
|
// persistent logins in the database. See Security::setDefaultAdmin().
|
|
|
|
// Set 'Email' to identify this as the default admin
|
|
|
|
$admin = Member::create();
|
2017-05-08 13:34:39 +02:00
|
|
|
$admin->FirstName = _t(__CLASS__.'.DefaultAdminFirstname', 'Default Admin');
|
2016-11-23 06:09:10 +01:00
|
|
|
$admin->Email = Security::default_admin_username();
|
|
|
|
$admin->write();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
// Ensure this user is in the admin group
|
|
|
|
if (!$admin->inGroup($adminGroup)) {
|
|
|
|
// Add member to group instead of adding group to member
|
|
|
|
// This bypasses the privilege escallation code in Member_GroupSet
|
|
|
|
$adminGroup
|
|
|
|
->DirectMembers()
|
|
|
|
->add($admin);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $admin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the passed password matches the stored one (if the member is not locked out).
|
|
|
|
*
|
|
|
|
* @param string $password
|
|
|
|
* @return ValidationResult
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function checkPassword($password)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$result = $this->canLogIn();
|
|
|
|
|
|
|
|
// Short-circuit the result upon failure, no further checks needed.
|
|
|
|
if (!$result->isValid()) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow default admin to login as self
|
|
|
|
if ($this->isDefaultAdmin() && Security::check_default_admin($this->Email, $password)) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check a password is set on this member
|
|
|
|
if (empty($this->Password) && $this->exists()) {
|
2017-05-08 13:34:39 +02:00
|
|
|
$result->addError(_t(__CLASS__.'.NoPassword', 'There is no password on this member.'));
|
2016-11-23 06:09:10 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
$e = PasswordEncryptor::create_for_algorithm($this->PasswordEncryption);
|
|
|
|
if (!$e->check($this->Password, $password, $this->Salt, $this)) {
|
|
|
|
$result->addError(_t(
|
2017-05-08 13:34:39 +02:00
|
|
|
__CLASS__.'.ERRORWRONGCRED',
|
2016-11-23 06:09:10 +01:00
|
|
|
'The provided details don\'t seem to be correct. Please try again.'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this user is the currently configured default admin
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function isDefaultAdmin()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
return Security::has_default_admin()
|
|
|
|
&& $this->Email === Security::default_admin_username();
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Returns a valid {@link ValidationResult} if this member can currently log in, or an invalid
|
|
|
|
* one with error messages to display if the member is locked out.
|
|
|
|
*
|
|
|
|
* You can hook into this with a "canLogIn" method on an attached extension.
|
|
|
|
*
|
|
|
|
* @return ValidationResult
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function canLogIn()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$result = ValidationResult::create();
|
|
|
|
|
|
|
|
if ($this->isLockedOut()) {
|
|
|
|
$result->addError(
|
|
|
|
_t(
|
2017-05-08 13:34:39 +02:00
|
|
|
__CLASS__.'.ERRORLOCKEDOUT2',
|
2016-11-23 06:09:10 +01:00
|
|
|
'Your account has been temporarily disabled because of too many failed attempts at ' .
|
|
|
|
'logging in. Please try again in {count} minutes.',
|
|
|
|
null,
|
2017-04-30 05:17:26 +02:00
|
|
|
array('count' => static::config()->get('lock_out_delay_mins'))
|
2016-11-23 06:09:10 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->extend('canLogIn', $result);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this user is locked out
|
2017-01-26 05:20:08 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
2016-11-23 06:09:10 +01:00
|
|
|
*/
|
2017-04-30 05:17:26 +02:00
|
|
|
protected function isLockedOut()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
if (!$this->LockedOutUntil) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return DBDatetime::now()->getTimestamp() < $this->dbObject('LockedOutUntil')->getTimestamp();
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a {@link PasswordValidator} object to use to validate member's passwords.
|
|
|
|
*
|
|
|
|
* @param PasswordValidator $pv
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function set_password_validator($pv)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
self::$password_validator = $pv;
|
|
|
|
}
|
2012-11-08 04:33:19 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Returns the current {@link PasswordValidator}
|
|
|
|
*
|
|
|
|
* @return PasswordValidator
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function password_validator()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
return self::$password_validator;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function isPasswordExpired()
|
|
|
|
{
|
|
|
|
if (!$this->PasswordExpiry) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-23 06:09:10 +01:00
|
|
|
return strtotime(date('Y-m-d')) >= strtotime($this->PasswordExpiry);
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
2017-05-07 21:11:00 +02:00
|
|
|
* @deprecated Use Security::setCurrentUser() or IdentityStore::logIn()
|
2016-11-23 06:09:10 +01:00
|
|
|
*
|
|
|
|
* @param bool $remember If set to TRUE, the member will be logged in automatically the next time.
|
|
|
|
*/
|
2017-05-07 21:11:00 +02:00
|
|
|
public function logIn()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-05-07 21:11:00 +02:00
|
|
|
user_error("This method is deprecated and now only logs in for the current request", E_USER_WARNING);
|
|
|
|
Security::setCurrentUser($this);
|
|
|
|
}
|
2014-10-06 05:01:33 +02:00
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
/**
|
|
|
|
* Called before a member is logged in via session/cookie/etc
|
|
|
|
*/
|
|
|
|
public function beforeMemberLoggedIn()
|
|
|
|
{
|
|
|
|
// @todo Move to middleware on the AuthenticationRequestFilter IdentityStore
|
|
|
|
$this->extend('beforeMemberLoggedIn');
|
|
|
|
}
|
2014-10-06 05:01:33 +02:00
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
/**
|
|
|
|
* Called after a member is logged in via session/cookie/etc
|
|
|
|
*/
|
|
|
|
public function afterMemberLoggedIn()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
// Clear the incorrect log-in count
|
|
|
|
$this->registerSuccessfulLogin();
|
2014-10-06 05:01:33 +02:00
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->LockedOutUntil = null;
|
2010-01-21 23:59:19 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
$this->regenerateTempID();
|
2010-01-21 23:59:19 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
$this->write();
|
2010-01-21 23:59:19 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
// Audit logging hook
|
2017-04-30 05:17:26 +02:00
|
|
|
$this->extend('afterMemberLoggedIn');
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
2014-10-06 05:01:33 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Trigger regeneration of TempID.
|
|
|
|
*
|
|
|
|
* This should be performed any time the user presents their normal identification (normally Email)
|
|
|
|
* and is successfully authenticated.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function regenerateTempID()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$generator = new RandomGenerator();
|
|
|
|
$this->TempIDHash = $generator->randomToken('sha1');
|
|
|
|
$this->TempIDExpired = self::config()->temp_id_lifetime
|
|
|
|
? date('Y-m-d H:i:s', strtotime(DBDatetime::now()->getValue()) + self::config()->temp_id_lifetime)
|
|
|
|
: null;
|
|
|
|
$this->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the member ID logged in session actually
|
|
|
|
* has a database record of the same ID. If there is
|
|
|
|
* no logged in user, FALSE is returned anyway.
|
|
|
|
*
|
|
|
|
* @return boolean TRUE record found FALSE no record found
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function logged_in_session_exists()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($id = Member::currentUserID()) {
|
2017-01-26 05:20:08 +01:00
|
|
|
if ($member = DataObject::get_by_id(Member::class, $id)) {
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($member->exists()) {
|
|
|
|
return true;
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
2017-05-07 21:11:00 +02:00
|
|
|
* Logs this member out.
|
2016-11-23 06:09:10 +01:00
|
|
|
*/
|
2017-05-07 21:11:00 +02:00
|
|
|
public function logOut()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->extend('beforeMemberLoggedOut');
|
2016-11-23 06:09:10 +01:00
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
Session::clear("loggedInAs");
|
|
|
|
if (Member::config()->login_marker_cookie) {
|
|
|
|
Cookie::set(Member::config()->login_marker_cookie, null, 0);
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
Session::destroy();
|
2016-11-23 06:09:10 +01:00
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->extend('memberLoggedOut');
|
2016-11-23 06:09:10 +01:00
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
// Clears any potential previous hashes for this member
|
|
|
|
RememberLoginHash::clear($this, Cookie::get('alc_device'));
|
2016-11-23 06:09:10 +01:00
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
Cookie::set('alc_enc', null); // // Clear the Remember Me cookie
|
|
|
|
Cookie::force_expiry('alc_enc');
|
|
|
|
Cookie::set('alc_device', null);
|
|
|
|
Cookie::force_expiry('alc_device');
|
2016-11-23 06:09:10 +01:00
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
// Switch back to live in order to avoid infinite loops when
|
|
|
|
// redirecting to the login screen (if this login screen is versioned)
|
|
|
|
Session::clear('readingMode');
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->write();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
// Audit logging hook
|
|
|
|
$this->extend('memberLoggedOut');
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Utility for generating secure password hashes for this member.
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
* @return string
|
|
|
|
* @throws PasswordEncryptor_NotFoundException
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function encryptWithUserSettings($string)
|
|
|
|
{
|
|
|
|
if (!$string) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
// If the algorithm or salt is not available, it means we are operating
|
|
|
|
// on legacy account with unhashed password. Do not hash the string.
|
|
|
|
if (!$this->PasswordEncryption) {
|
|
|
|
return $string;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
// We assume we have PasswordEncryption and Salt available here.
|
|
|
|
$e = PasswordEncryptor::create_for_algorithm($this->PasswordEncryption);
|
|
|
|
return $e->encrypt($string, $this->Salt);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Generate an auto login token which can be used to reset the password,
|
|
|
|
* at the same time hashing it and storing in the database.
|
|
|
|
*
|
|
|
|
* @param int $lifetime The lifetime of the auto login hash in days (by default 2 days)
|
|
|
|
*
|
|
|
|
* @returns string Token that should be passed to the client (but NOT persisted).
|
|
|
|
*
|
|
|
|
* @todo Make it possible to handle database errors such as a "duplicate key" error
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function generateAutologinTokenAndStoreHash($lifetime = 2)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
do {
|
|
|
|
$generator = new RandomGenerator();
|
|
|
|
$token = $generator->randomToken();
|
|
|
|
$hash = $this->encryptWithUserSettings($token);
|
2017-01-26 05:20:08 +01:00
|
|
|
} while (DataObject::get_one(Member::class, array(
|
2016-11-23 06:09:10 +01:00
|
|
|
'"Member"."AutoLoginHash"' => $hash
|
|
|
|
)));
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
$this->AutoLoginHash = $hash;
|
|
|
|
$this->AutoLoginExpired = date('Y-m-d H:i:s', time() + (86400 * $lifetime));
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
$this->write();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
return $token;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Check the token against the member.
|
|
|
|
*
|
|
|
|
* @param string $autologinToken
|
|
|
|
*
|
|
|
|
* @returns bool Is token valid?
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function validateAutoLoginToken($autologinToken)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$hash = $this->encryptWithUserSettings($autologinToken);
|
|
|
|
$member = self::member_from_autologinhash($hash, false);
|
|
|
|
return (bool)$member;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Return the member for the auto login hash
|
|
|
|
*
|
|
|
|
* @param string $hash The hash key
|
|
|
|
* @param bool $login Should the member be logged in?
|
|
|
|
*
|
|
|
|
* @return Member the matching member, if valid
|
|
|
|
* @return Member
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function member_from_autologinhash($hash, $login = false)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
/** @var Member $member */
|
2017-01-26 05:20:08 +01:00
|
|
|
$member = Member::get()->filter([
|
|
|
|
'AutoLoginHash' => $hash,
|
|
|
|
'AutoLoginExpired:GreaterThan' => DBDatetime::now()->getValue(),
|
|
|
|
])->first();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
if ($login && $member) {
|
|
|
|
$member->logIn();
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
return $member;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Find a member record with the given TempIDHash value
|
|
|
|
*
|
|
|
|
* @param string $tempid
|
|
|
|
* @return Member
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function member_from_tempid($tempid)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$members = Member::get()
|
|
|
|
->filter('TempIDHash', $tempid);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
// Exclude expired
|
2017-04-30 05:17:26 +02:00
|
|
|
if (static::config()->get('temp_id_lifetime')) {
|
2016-11-23 06:09:10 +01:00
|
|
|
$members = $members->filter('TempIDExpired:GreaterThan', DBDatetime::now()->getValue());
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
return $members->first();
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Returns the fields for the member form - used in the registration/profile module.
|
|
|
|
* It should return fields that are editable by the admin and the logged-in user.
|
|
|
|
*
|
|
|
|
* @return FieldList Returns a {@link FieldList} containing the fields for
|
|
|
|
* the member form.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function getMemberFormFields()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$fields = parent::getFrontEndFields();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
$fields->replaceField('Password', $this->getMemberPasswordField());
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
$fields->replaceField('Locale', new DropdownField(
|
|
|
|
'Locale',
|
|
|
|
$this->fieldLabel('Locale'),
|
2017-01-26 05:20:08 +01:00
|
|
|
i18n::getSources()->getKnownLocales()
|
2016-11-23 06:09:10 +01:00
|
|
|
));
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-04-30 05:17:26 +02:00
|
|
|
$fields->removeByName(static::config()->get('hidden_fields'));
|
2016-11-23 06:09:10 +01:00
|
|
|
$fields->removeByName('FailedLoginCount');
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
$this->extend('updateMemberFormFields', $fields);
|
|
|
|
return $fields;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Builds "Change / Create Password" field for this member
|
|
|
|
*
|
|
|
|
* @return ConfirmedPasswordField
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function getMemberPasswordField()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$editingPassword = $this->isInDB();
|
|
|
|
$label = $editingPassword
|
2017-05-08 13:34:39 +02:00
|
|
|
? _t(__CLASS__.'.EDIT_PASSWORD', 'New Password')
|
2016-11-23 06:09:10 +01:00
|
|
|
: $this->fieldLabel('Password');
|
|
|
|
/** @var ConfirmedPasswordField $password */
|
|
|
|
$password = ConfirmedPasswordField::create(
|
|
|
|
'Password',
|
|
|
|
$label,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
$editingPassword
|
|
|
|
);
|
|
|
|
|
|
|
|
// If editing own password, require confirmation of existing
|
|
|
|
if ($editingPassword && $this->ID == Member::currentUserID()) {
|
|
|
|
$password->setRequireExistingPassword(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$password->setCanBeEmpty(true);
|
|
|
|
$this->extend('updateMemberPasswordField', $password);
|
|
|
|
return $password;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the {@link RequiredFields} instance for the Member object. This
|
|
|
|
* Validator is used when saving a {@link CMSProfileController} or added to
|
|
|
|
* any form responsible for saving a users data.
|
|
|
|
*
|
|
|
|
* To customize the required fields, add a {@link DataExtension} to member
|
|
|
|
* calling the `updateValidator()` method.
|
|
|
|
*
|
|
|
|
* @return Member_Validator
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function getValidator()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$validator = Member_Validator::create();
|
2016-11-23 06:09:10 +01:00
|
|
|
$validator->setForMember($this);
|
|
|
|
$this->extend('updateValidator', $validator);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
return $validator;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Returns the current logged in user
|
|
|
|
*
|
2017-05-20 06:32:25 +02:00
|
|
|
* @deprecated use Security::getCurrentUser()
|
|
|
|
*
|
2016-11-23 06:09:10 +01:00
|
|
|
* @return Member
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function currentUser()
|
|
|
|
{
|
2017-05-07 21:11:00 +02:00
|
|
|
return Security::getCurrentUser();
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-01-13 04:11:20 +01:00
|
|
|
/**
|
|
|
|
* Temporarily act as the specified user, limited to a $callback, but
|
|
|
|
* without logging in as that user.
|
|
|
|
*
|
|
|
|
* E.g.
|
|
|
|
* <code>
|
|
|
|
* Member::logInAs(Security::findAnAdministrator(), function() {
|
|
|
|
* $record->write();
|
|
|
|
* });
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param Member|null|int $member Member or member ID to log in as.
|
|
|
|
* Set to null or 0 to act as a logged out user.
|
|
|
|
* @param $callback
|
|
|
|
*/
|
|
|
|
public static function actAs($member, $callback)
|
|
|
|
{
|
2017-05-07 21:11:00 +02:00
|
|
|
$previousUser = Security::getCurrentUser();
|
|
|
|
|
|
|
|
// Transform ID to member
|
|
|
|
if (is_numeric($member)) {
|
|
|
|
$member = DataObject::get_by_id(Member::class, $member);
|
|
|
|
}
|
|
|
|
Security::setCurrentUser($member);
|
|
|
|
|
2017-01-13 04:11:20 +01:00
|
|
|
try {
|
|
|
|
return $callback();
|
|
|
|
} finally {
|
2017-05-07 21:11:00 +02:00
|
|
|
Security::setCurrentUser($previousUser);
|
2017-01-13 04:11:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Get the ID of the current logged in user
|
|
|
|
*
|
2017-05-20 06:32:25 +02:00
|
|
|
* @deprecated use Security::getCurrentUser()
|
|
|
|
*
|
2016-11-23 06:09:10 +01:00
|
|
|
* @return int Returns the ID of the current logged in user or 0.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function currentUserID()
|
|
|
|
{
|
2017-05-07 21:11:00 +02:00
|
|
|
if ($member = Security::getCurrentUser()) {
|
|
|
|
return $member->ID;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-13 04:11:20 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/*
|
2008-03-10 22:28:35 +01:00
|
|
|
* Generate a random password, with randomiser to kick in if there's no words file on the
|
2007-09-14 21:10:18 +02:00
|
|
|
* filesystem.
|
|
|
|
*
|
|
|
|
* @return string Returns a random password.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function create_new_password()
|
|
|
|
{
|
2017-02-22 04:14:53 +01:00
|
|
|
$words = Security::config()->uninherited('word_list');
|
2014-08-31 05:49:30 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($words && file_exists($words)) {
|
|
|
|
$words = file($words);
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
list($usec, $sec) = explode(' ', microtime());
|
|
|
|
srand($sec + ((float) $usec * 100000));
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
$word = trim($words[rand(0, sizeof($words)-1)]);
|
|
|
|
$number = rand(10, 999);
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
return $word . $number;
|
|
|
|
} else {
|
|
|
|
$random = rand();
|
|
|
|
$string = md5($random);
|
|
|
|
$output = substr($string, 0, 8);
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Event handler called before writing to the database.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function onBeforeWrite()
|
|
|
|
{
|
|
|
|
if ($this->SetPassword) {
|
|
|
|
$this->Password = $this->SetPassword;
|
|
|
|
}
|
2015-09-07 03:44:16 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
// If a member with the same "unique identifier" already exists with a different ID, don't allow merging.
|
|
|
|
// Note: This does not a full replacement for safeguards in the controller layer (e.g. in a registration form),
|
|
|
|
// but rather a last line of defense against data inconsistencies.
|
|
|
|
$identifierField = Member::config()->unique_identifier_field;
|
|
|
|
if ($this->$identifierField) {
|
|
|
|
// Note: Same logic as Member_Validator class
|
2017-01-26 05:20:08 +01:00
|
|
|
$filter = [
|
|
|
|
"\"Member\".\"$identifierField\"" => $this->$identifierField
|
|
|
|
];
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($this->ID) {
|
|
|
|
$filter[] = array('"Member"."ID" <> ?' => $this->ID);
|
|
|
|
}
|
2017-01-26 05:20:08 +01:00
|
|
|
$existingRecord = DataObject::get_one(Member::class, $filter);
|
2016-11-23 06:09:10 +01:00
|
|
|
|
|
|
|
if ($existingRecord) {
|
|
|
|
throw new ValidationException(_t(
|
2017-05-08 13:34:39 +02:00
|
|
|
__CLASS__.'.ValidationIdentifierFailed',
|
2016-11-23 06:09:10 +01:00
|
|
|
'Can\'t overwrite existing member #{id} with identical identifier ({name} = {value}))',
|
|
|
|
'Values in brackets show "fieldname = value", usually denoting an existing email address',
|
|
|
|
array(
|
|
|
|
'id' => $existingRecord->ID,
|
|
|
|
'name' => $identifierField,
|
|
|
|
'value' => $this->$identifierField
|
|
|
|
)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't send emails out on dev/tests sites to prevent accidentally spamming users.
|
|
|
|
// However, if TestMailer is in use this isn't a risk.
|
2017-01-13 02:48:46 +01:00
|
|
|
if ((Director::isLive() || Injector::inst()->get(Mailer::class) instanceof TestMailer)
|
2016-11-23 06:09:10 +01:00
|
|
|
&& $this->isChanged('Password')
|
|
|
|
&& $this->record['Password']
|
2017-04-30 05:17:26 +02:00
|
|
|
&& static::config()->get('notify_password_change')
|
2016-11-23 06:09:10 +01:00
|
|
|
) {
|
2017-01-13 02:48:46 +01:00
|
|
|
Email::create()
|
|
|
|
->setHTMLTemplate('SilverStripe\\Control\\Email\\ChangePasswordEmail')
|
|
|
|
->setData($this)
|
|
|
|
->setTo($this->Email)
|
2017-05-08 13:34:39 +02:00
|
|
|
->setSubject(_t(__CLASS__.'.SUBJECTPASSWORDCHANGED', "Your password has been changed", 'Email subject'))
|
2017-01-13 02:48:46 +01:00
|
|
|
->send();
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The test on $this->ID is used for when records are initially created.
|
|
|
|
// Note that this only works with cleartext passwords, as we can't rehash
|
|
|
|
// existing passwords.
|
|
|
|
if ((!$this->ID && $this->Password) || $this->isChanged('Password')) {
|
|
|
|
//reset salt so that it gets regenerated - this will invalidate any persistant login cookies
|
|
|
|
// or other information encrypted with this Member's settings (see self::encryptWithUserSettings)
|
|
|
|
$this->Salt = '';
|
|
|
|
// Password was changed: encrypt the password according the settings
|
|
|
|
$encryption_details = Security::encrypt_password(
|
|
|
|
$this->Password, // this is assumed to be cleartext
|
|
|
|
$this->Salt,
|
|
|
|
($this->PasswordEncryption) ?
|
|
|
|
$this->PasswordEncryption : Security::config()->password_encryption_algorithm,
|
|
|
|
$this
|
|
|
|
);
|
|
|
|
|
|
|
|
// Overwrite the Password property with the hashed value
|
|
|
|
$this->Password = $encryption_details['password'];
|
|
|
|
$this->Salt = $encryption_details['salt'];
|
|
|
|
$this->PasswordEncryption = $encryption_details['algorithm'];
|
|
|
|
|
|
|
|
// If we haven't manually set a password expiry
|
|
|
|
if (!$this->isChanged('PasswordExpiry')) {
|
|
|
|
// then set it for us
|
|
|
|
if (self::config()->password_expiry_days) {
|
|
|
|
$this->PasswordExpiry = date('Y-m-d', time() + 86400 * self::config()->password_expiry_days);
|
|
|
|
} else {
|
|
|
|
$this->PasswordExpiry = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// save locale
|
|
|
|
if (!$this->Locale) {
|
|
|
|
$this->Locale = i18n::get_locale();
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function onAfterWrite()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
parent::onAfterWrite();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2017-01-20 03:57:50 +01:00
|
|
|
Permission::reset();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($this->isChanged('Password')) {
|
|
|
|
MemberPassword::log($this);
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function onAfterDelete()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
parent::onAfterDelete();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
//prevent orphaned records remaining in the DB
|
|
|
|
$this->deletePasswordLogs();
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Delete the MemberPassword objects that are associated to this user
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
protected function deletePasswordLogs()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
foreach ($this->LoggedPasswords() as $password) {
|
|
|
|
$password->delete();
|
|
|
|
$password->destroy();
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter out admin groups to avoid privilege escalation,
|
|
|
|
* If any admin groups are requested, deny the whole save operation.
|
|
|
|
*
|
|
|
|
* @param array $ids Database IDs of Group records
|
|
|
|
* @return bool True if the change can be accepted
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function onChangeGroups($ids)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
// unless the current user is an admin already OR the logged in user is an admin
|
|
|
|
if (Permission::check('ADMIN') || Permission::checkMember($this, 'ADMIN')) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
// If there are no admin groups in this set then it's ok
|
|
|
|
$adminGroups = Permission::get_groups_by_permission('ADMIN');
|
|
|
|
$adminGroupIDs = ($adminGroups) ? $adminGroups->column('ID') : array();
|
|
|
|
return count(array_intersect($ids, $adminGroupIDs)) == 0;
|
|
|
|
}
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2010-10-15 05:23:02 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Check if the member is in one of the given groups.
|
|
|
|
*
|
|
|
|
* @param array|SS_List $groups Collection of {@link Group} DataObjects to check
|
|
|
|
* @param boolean $strict Only determine direct group membership if set to true (Default: false)
|
|
|
|
* @return bool Returns TRUE if the member is in one of the given groups, otherwise FALSE.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function inGroups($groups, $strict = false)
|
|
|
|
{
|
|
|
|
if ($groups) {
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
if ($this->inGroup($group, $strict)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Check if the member is in the given group or any parent groups.
|
|
|
|
*
|
|
|
|
* @param int|Group|string $group Group instance, Group Code or ID
|
|
|
|
* @param boolean $strict Only determine direct group membership if set to TRUE (Default: FALSE)
|
|
|
|
* @return bool Returns TRUE if the member is in the given group, otherwise FALSE.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function inGroup($group, $strict = false)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
if (is_numeric($group)) {
|
2017-01-26 05:20:08 +01:00
|
|
|
$groupCheckObj = DataObject::get_by_id(Group::class, $group);
|
2016-11-23 06:09:10 +01:00
|
|
|
} elseif (is_string($group)) {
|
2017-01-26 05:20:08 +01:00
|
|
|
$groupCheckObj = DataObject::get_one(Group::class, array(
|
2016-11-23 06:09:10 +01:00
|
|
|
'"Group"."Code"' => $group
|
|
|
|
));
|
|
|
|
} elseif ($group instanceof Group) {
|
|
|
|
$groupCheckObj = $group;
|
|
|
|
} else {
|
|
|
|
user_error('Member::inGroup(): Wrong format for $group parameter', E_USER_ERROR);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
if (!$groupCheckObj) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
$groupCandidateObjs = ($strict) ? $this->getManyManyComponents("Groups") : $this->Groups();
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($groupCandidateObjs) {
|
|
|
|
foreach ($groupCandidateObjs as $groupCandidateObj) {
|
|
|
|
if ($groupCandidateObj->ID == $groupCheckObj->ID) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Adds the member to a group. This will create the group if the given
|
|
|
|
* group code does not return a valid group object.
|
|
|
|
*
|
|
|
|
* @param string $groupcode
|
|
|
|
* @param string $title Title of the group
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function addToGroupByCode($groupcode, $title = "")
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$group = DataObject::get_one(Group::class, array(
|
2016-11-23 06:09:10 +01:00
|
|
|
'"Group"."Code"' => $groupcode
|
|
|
|
));
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($group) {
|
|
|
|
$this->Groups()->add($group);
|
|
|
|
} else {
|
2016-11-29 00:31:16 +01:00
|
|
|
if (!$title) {
|
|
|
|
$title = $groupcode;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
$group = new Group();
|
|
|
|
$group->Code = $groupcode;
|
|
|
|
$group->Title = $title;
|
|
|
|
$group->write();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
$this->Groups()->add($group);
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Removes a member from a group.
|
|
|
|
*
|
|
|
|
* @param string $groupcode
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function removeFromGroupByCode($groupcode)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$group = Group::get()->filter(array('Code' => $groupcode))->first();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($group) {
|
|
|
|
$this->Groups()->remove($group);
|
|
|
|
}
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* @param array $columns Column names on the Member record to show in {@link getTitle()}.
|
|
|
|
* @param String $sep Separator
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function set_title_columns($columns, $sep = ' ')
|
|
|
|
{
|
|
|
|
if (!is_array($columns)) {
|
|
|
|
$columns = array($columns);
|
|
|
|
}
|
2016-11-23 06:09:10 +01:00
|
|
|
self::config()->title_format = array('columns' => $columns, 'sep' => $sep);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
//------------------- HELPER METHODS -----------------------------------//
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Get the complete name of the member, by default in the format "<Surname>, <FirstName>".
|
|
|
|
* Falls back to showing either field on its own.
|
|
|
|
*
|
|
|
|
* You can overload this getter with {@link set_title_format()}
|
|
|
|
* and {@link set_title_sql()}.
|
|
|
|
*
|
|
|
|
* @return string Returns the first- and surname of the member. If the ID
|
|
|
|
* of the member is equal 0, only the surname is returned.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function getTitle()
|
|
|
|
{
|
2017-04-30 05:17:26 +02:00
|
|
|
$format = static::config()->get('title_format');
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($format) {
|
|
|
|
$values = array();
|
|
|
|
foreach ($format['columns'] as $col) {
|
|
|
|
$values[] = $this->getField($col);
|
|
|
|
}
|
2017-05-20 06:32:25 +02:00
|
|
|
return implode($format['sep'], $values);
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($this->getField('ID') === 0) {
|
2016-11-23 06:09:10 +01:00
|
|
|
return $this->getField('Surname');
|
2016-11-29 00:31:16 +01:00
|
|
|
} else {
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($this->getField('Surname') && $this->getField('FirstName')) {
|
|
|
|
return $this->getField('Surname') . ', ' . $this->getField('FirstName');
|
|
|
|
} elseif ($this->getField('Surname')) {
|
|
|
|
return $this->getField('Surname');
|
|
|
|
} elseif ($this->getField('FirstName')) {
|
|
|
|
return $this->getField('FirstName');
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a SQL CONCAT() fragment suitable for a SELECT statement.
|
|
|
|
* Useful for custom queries which assume a certain member title format.
|
|
|
|
*
|
|
|
|
* @return String SQL
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function get_title_sql()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
// This should be abstracted to SSDatabase concatOperator or similar.
|
|
|
|
$op = (DB::get_conn() instanceof MSSQLDatabase) ? " + " : " || ";
|
|
|
|
|
|
|
|
// Get title_format with fallback to default
|
2017-04-30 05:17:26 +02:00
|
|
|
$format = static::config()->get('title_format');
|
2016-11-23 06:09:10 +01:00
|
|
|
if (!$format) {
|
|
|
|
$format = [
|
|
|
|
'columns' => ['Surname', 'FirstName'],
|
|
|
|
'sep' => ' ',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$columnsWithTablename = array();
|
|
|
|
foreach ($format['columns'] as $column) {
|
|
|
|
$columnsWithTablename[] = static::getSchema()->sqlColumnForField(__CLASS__, $column);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sepSQL = Convert::raw2sql($format['sep'], true);
|
|
|
|
return "(".join(" $op $sepSQL $op ", $columnsWithTablename).")";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the complete name of the member
|
|
|
|
*
|
|
|
|
* @return string Returns the first- and surname of the member.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function getName()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
return ($this->Surname) ? trim($this->FirstName . ' ' . $this->Surname) : $this->FirstName;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Set first- and surname
|
|
|
|
*
|
|
|
|
* This method assumes that the last part of the name is the surname, e.g.
|
|
|
|
* <i>A B C</i> will result in firstname <i>A B</i> and surname <i>C</i>
|
|
|
|
*
|
|
|
|
* @param string $name The name
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function setName($name)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$nameParts = explode(' ', $name);
|
|
|
|
$this->Surname = array_pop($nameParts);
|
|
|
|
$this->FirstName = join(' ', $nameParts);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Alias for {@link setName}
|
|
|
|
*
|
|
|
|
* @param string $name The name
|
|
|
|
* @see setName()
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function splitName($name)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
return $this->setName($name);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
2017-03-30 23:37:21 +02:00
|
|
|
* Return the date format based on the user's chosen locale,
|
|
|
|
* falling back to the default format defined by the {@link i18n.get_locale()} setting.
|
2016-11-23 06:09:10 +01:00
|
|
|
*
|
|
|
|
* @return string ISO date format
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function getDateFormat()
|
|
|
|
{
|
2017-04-03 02:00:59 +02:00
|
|
|
$formatter = new IntlDateFormatter(
|
|
|
|
$this->getLocale(),
|
|
|
|
IntlDateFormatter::MEDIUM,
|
|
|
|
IntlDateFormatter::NONE
|
|
|
|
);
|
|
|
|
$format = $formatter->getPattern();
|
|
|
|
|
2017-03-30 23:37:21 +02:00
|
|
|
$this->extend('updateDateFormat', $format);
|
2017-04-03 02:00:59 +02:00
|
|
|
|
2017-03-30 23:37:21 +02:00
|
|
|
return $format;
|
2017-01-26 05:20:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get user locale
|
|
|
|
*/
|
|
|
|
public function getLocale()
|
|
|
|
{
|
|
|
|
$locale = $this->getField('Locale');
|
|
|
|
if ($locale) {
|
|
|
|
return $locale;
|
|
|
|
}
|
|
|
|
return i18n::get_locale();
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-30 23:37:21 +02:00
|
|
|
* Return the time format based on the user's chosen locale,
|
|
|
|
* falling back to the default format defined by the {@link i18n.get_locale()} setting.
|
2016-11-23 06:09:10 +01:00
|
|
|
*
|
|
|
|
* @return string ISO date format
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function getTimeFormat()
|
|
|
|
{
|
2017-04-03 02:00:59 +02:00
|
|
|
$formatter = new IntlDateFormatter(
|
|
|
|
$this->getLocale(),
|
|
|
|
IntlDateFormatter::NONE,
|
|
|
|
IntlDateFormatter::MEDIUM
|
|
|
|
);
|
|
|
|
$format = $formatter->getPattern();
|
|
|
|
|
2017-03-30 23:37:21 +02:00
|
|
|
$this->extend('updateTimeFormat', $format);
|
|
|
|
|
|
|
|
return $format;
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
//---------------------------------------------------------------------//
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Get a "many-to-many" map that holds for all members their group memberships,
|
|
|
|
* including any parent groups where membership is implied.
|
|
|
|
* Use {@link DirectGroups()} to only retrieve the group relations without inheritance.
|
|
|
|
*
|
|
|
|
* @todo Push all this logic into Member_GroupSet's getIterator()?
|
|
|
|
* @return Member_Groupset
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function Groups()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$groups = Member_GroupSet::create(Group::class, 'Group_Members', 'GroupID', 'MemberID');
|
2016-11-23 06:09:10 +01:00
|
|
|
$groups = $groups->forForeignID($this->ID);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
$this->extend('updateGroups', $groups);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
return $groups;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* @return ManyManyList
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function DirectGroups()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
return $this->getManyManyComponents('Groups');
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Get a member SQLMap of members in specific groups
|
|
|
|
*
|
|
|
|
* If no $groups is passed, all members will be returned
|
|
|
|
*
|
|
|
|
* @param mixed $groups - takes a SS_List, an array or a single Group.ID
|
|
|
|
* @return Map Returns an Map that returns all Member data.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function map_in_groups($groups = null)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$groupIDList = array();
|
|
|
|
|
|
|
|
if ($groups instanceof SS_List) {
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
$groupIDList[] = $group->ID;
|
|
|
|
}
|
|
|
|
} elseif (is_array($groups)) {
|
|
|
|
$groupIDList = $groups;
|
|
|
|
} elseif ($groups) {
|
|
|
|
$groupIDList[] = $groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No groups, return all Members
|
|
|
|
if (!$groupIDList) {
|
|
|
|
return Member::get()->sort(array('Surname'=>'ASC', 'FirstName'=>'ASC'))->map();
|
|
|
|
}
|
|
|
|
|
|
|
|
$membersList = new ArrayList();
|
|
|
|
// This is a bit ineffective, but follow the ORM style
|
|
|
|
foreach (Group::get()->byIDs($groupIDList) as $group) {
|
|
|
|
$membersList->merge($group->Members());
|
|
|
|
}
|
|
|
|
|
|
|
|
$membersList->removeDuplicates('ID');
|
|
|
|
return $membersList->map();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a map of all members in the groups given that have CMS permissions
|
|
|
|
*
|
|
|
|
* If no groups are passed, all groups with CMS permissions will be used.
|
|
|
|
*
|
|
|
|
* @param array $groups Groups to consider or NULL to use all groups with
|
|
|
|
* CMS permissions.
|
|
|
|
* @return Map Returns a map of all members in the groups given that
|
|
|
|
* have CMS permissions.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function mapInCMSGroups($groups = null)
|
|
|
|
{
|
2017-03-10 04:43:10 +01:00
|
|
|
// Check CMS module exists
|
|
|
|
if (!class_exists(LeftAndMain::class)) {
|
|
|
|
return ArrayList::create()->map();
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
if (!$groups || $groups->Count() == 0) {
|
|
|
|
$perms = array('ADMIN', 'CMS_ACCESS_AssetAdmin');
|
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
if (class_exists(CMSMain::class)) {
|
2016-11-23 06:09:10 +01:00
|
|
|
$cmsPerms = CMSMain::singleton()->providePermissions();
|
|
|
|
} else {
|
|
|
|
$cmsPerms = LeftAndMain::singleton()->providePermissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($cmsPerms)) {
|
|
|
|
$perms = array_unique(array_merge($perms, array_keys($cmsPerms)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$permsClause = DB::placeholders($perms);
|
|
|
|
/** @skipUpgrade */
|
|
|
|
$groups = Group::get()
|
|
|
|
->innerJoin("Permission", '"Permission"."GroupID" = "Group"."ID"')
|
|
|
|
->where(array(
|
|
|
|
"\"Permission\".\"Code\" IN ($permsClause)" => $perms
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$groupIDList = array();
|
|
|
|
|
|
|
|
if ($groups instanceof SS_List) {
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
$groupIDList[] = $group->ID;
|
|
|
|
}
|
|
|
|
} elseif (is_array($groups)) {
|
|
|
|
$groupIDList = $groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @skipUpgrade */
|
|
|
|
$members = Member::get()
|
|
|
|
->innerJoin("Group_Members", '"Group_Members"."MemberID" = "Member"."ID"')
|
|
|
|
->innerJoin("Group", '"Group"."ID" = "Group_Members"."GroupID"');
|
|
|
|
if ($groupIDList) {
|
|
|
|
$groupClause = DB::placeholders($groupIDList);
|
|
|
|
$members = $members->where(array(
|
|
|
|
"\"Group\".\"ID\" IN ($groupClause)" => $groupIDList
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $members->sort('"Member"."Surname", "Member"."FirstName"')->map();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the groups in which the member is NOT in
|
|
|
|
*
|
|
|
|
* When passed an array of groups, and a component set of groups, this
|
|
|
|
* function will return the array of groups the member is NOT in.
|
|
|
|
*
|
|
|
|
* @param array $groupList An array of group code names.
|
|
|
|
* @param array $memberGroups A component set of groups (if set to NULL,
|
|
|
|
* $this->groups() will be used)
|
|
|
|
* @return array Groups in which the member is NOT in.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function memberNotInGroups($groupList, $memberGroups = null)
|
|
|
|
{
|
|
|
|
if (!$memberGroups) {
|
|
|
|
$memberGroups = $this->Groups();
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
foreach ($memberGroups as $group) {
|
|
|
|
if (in_array($group->Code, $groupList)) {
|
|
|
|
$index = array_search($group->Code, $groupList);
|
|
|
|
unset($groupList[$index]);
|
|
|
|
}
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
return $groupList;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Return a {@link FieldList} of fields that would appropriate for editing
|
|
|
|
* this member.
|
|
|
|
*
|
|
|
|
* @return FieldList Return a FieldList of fields that would appropriate for
|
|
|
|
* editing this member.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) {
|
2016-11-23 06:09:10 +01:00
|
|
|
/** @var FieldList $mainFields */
|
|
|
|
$mainFields = $fields->fieldByName("Root")->fieldByName("Main")->getChildren();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
// Build change password field
|
2017-01-26 05:20:08 +01:00
|
|
|
$mainFields->replaceField('Password', $this->getMemberPasswordField());
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
$mainFields->replaceField('Locale', new DropdownField(
|
|
|
|
"Locale",
|
2017-05-08 13:34:39 +02:00
|
|
|
_t(__CLASS__.'.INTERFACELANG', "Interface Language", 'Language of the CMS'),
|
2017-01-26 05:20:08 +01:00
|
|
|
i18n::getSources()->getKnownLocales()
|
2016-11-23 06:09:10 +01:00
|
|
|
));
|
2017-04-30 05:17:26 +02:00
|
|
|
$mainFields->removeByName(static::config()->get('hidden_fields'));
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-04-30 05:17:26 +02:00
|
|
|
if (! static::config()->get('lock_out_after_incorrect_logins')) {
|
2016-11-23 06:09:10 +01:00
|
|
|
$mainFields->removeByName('FailedLoginCount');
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
// Groups relation will get us into logical conflicts because
|
|
|
|
// Members are displayed within group edit form in SecurityAdmin
|
|
|
|
$fields->removeByName('Groups');
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
// Members shouldn't be able to directly view/edit logged passwords
|
|
|
|
$fields->removeByName('LoggedPasswords');
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
$fields->removeByName('RememberLoginHashes');
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
if (Permission::check('EDIT_PERMISSIONS')) {
|
|
|
|
$groupsMap = array();
|
|
|
|
foreach (Group::get() as $group) {
|
|
|
|
// Listboxfield values are escaped, use ASCII char instead of »
|
|
|
|
$groupsMap[$group->ID] = $group->getBreadcrumbs(' > ');
|
|
|
|
}
|
|
|
|
asort($groupsMap);
|
2016-11-29 00:31:16 +01:00
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
2017-01-26 05:20:08 +01:00
|
|
|
ListboxField::create('DirectGroups', Group::singleton()->i18n_plural_name())
|
2016-11-23 06:09:10 +01:00
|
|
|
->setSource($groupsMap)
|
|
|
|
->setAttribute(
|
|
|
|
'data-placeholder',
|
2017-05-08 13:34:39 +02:00
|
|
|
_t(__CLASS__.'.ADDGROUP', 'Add group', 'Placeholder text for a dropdown')
|
2016-11-23 06:09:10 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Add permission field (readonly to avoid complicated group assignment logic).
|
|
|
|
// This should only be available for existing records, as new records start
|
|
|
|
// with no permissions until they have a group assignment anyway.
|
2017-01-26 05:20:08 +01:00
|
|
|
if ($this->ID) {
|
2016-11-23 06:09:10 +01:00
|
|
|
$permissionsField = new PermissionCheckboxSetField_Readonly(
|
|
|
|
'Permissions',
|
|
|
|
false,
|
2017-01-26 05:20:08 +01:00
|
|
|
Permission::class,
|
2016-11-23 06:09:10 +01:00
|
|
|
'GroupID',
|
|
|
|
// we don't want parent relationships, they're automatically resolved in the field
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->getManyManyComponents('Groups')
|
2016-11-23 06:09:10 +01:00
|
|
|
);
|
2017-01-26 05:20:08 +01:00
|
|
|
$fields->findOrMakeTab('Root.Permissions', Permission::singleton()->i18n_plural_name());
|
2016-11-23 06:09:10 +01:00
|
|
|
$fields->addFieldToTab('Root.Permissions', $permissionsField);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$permissionsTab = $fields->fieldByName("Root")->fieldByName('Permissions');
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($permissionsTab) {
|
|
|
|
$permissionsTab->addExtraClass('readonly');
|
|
|
|
}
|
2016-11-23 06:09:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return parent::getCMSFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $includerelations Indicate if the labels returned include relation fields
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function fieldLabels($includerelations = true)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$labels = parent::fieldLabels($includerelations);
|
|
|
|
|
2017-05-08 13:34:39 +02:00
|
|
|
$labels['FirstName'] = _t(__CLASS__.'.FIRSTNAME', 'First Name');
|
|
|
|
$labels['Surname'] = _t(__CLASS__.'.SURNAME', 'Surname');
|
2016-11-23 06:09:10 +01:00
|
|
|
/** @skipUpgrade */
|
2017-05-08 13:34:39 +02:00
|
|
|
$labels['Email'] = _t(__CLASS__.'.EMAIL', 'Email');
|
|
|
|
$labels['Password'] = _t(__CLASS__.'.db_Password', 'Password');
|
|
|
|
$labels['PasswordExpiry'] = _t(__CLASS__.'.db_PasswordExpiry', 'Password Expiry Date', 'Password expiry date');
|
|
|
|
$labels['LockedOutUntil'] = _t(__CLASS__.'.db_LockedOutUntil', 'Locked out until', 'Security related date');
|
|
|
|
$labels['Locale'] = _t(__CLASS__.'.db_Locale', 'Interface Locale');
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($includerelations) {
|
2016-11-29 00:31:16 +01:00
|
|
|
$labels['Groups'] = _t(
|
2017-05-08 13:34:39 +02:00
|
|
|
__CLASS__.'.belongs_many_many_Groups',
|
2016-11-29 00:31:16 +01:00
|
|
|
'Groups',
|
|
|
|
'Security Groups this member belongs to'
|
|
|
|
);
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
|
|
|
return $labels;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Users can view their own record.
|
|
|
|
* Otherwise they'll need ADMIN or CMS_ACCESS_SecurityAdmin permissions.
|
|
|
|
* This is likely to be customized for social sites etc. with a looser permission model.
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function canView($member = null)
|
|
|
|
{
|
2016-03-03 09:32:06 +01:00
|
|
|
//get member
|
2017-05-11 11:07:27 +02:00
|
|
|
if (!$member) {
|
2017-05-20 06:32:25 +02:00
|
|
|
$member = Security::getCurrentUser();
|
2016-03-03 09:32:06 +01:00
|
|
|
}
|
|
|
|
//check for extensions, we do this first as they can overrule everything
|
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member);
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($extended !== null) {
|
2016-03-03 09:32:06 +01:00
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
|
|
|
|
//need to be logged in and/or most checks below rely on $member being a Member
|
2016-11-23 06:09:10 +01:00
|
|
|
if (!$member) {
|
2016-03-08 21:50:18 +01:00
|
|
|
return false;
|
2016-03-03 09:32:06 +01:00
|
|
|
}
|
|
|
|
// members can usually view their own record
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($this->ID == $member->ID) {
|
2016-03-03 09:32:06 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//standard check
|
|
|
|
return Permission::checkMember($member, 'CMS_ACCESS_SecurityAdmin');
|
|
|
|
}
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Users can edit their own record.
|
|
|
|
* Otherwise they'll need ADMIN or CMS_ACCESS_SecurityAdmin permissions
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function canEdit($member = null)
|
|
|
|
{
|
2016-03-03 09:32:06 +01:00
|
|
|
//get member
|
2017-05-11 11:07:27 +02:00
|
|
|
if (!$member) {
|
2017-05-20 06:32:25 +02:00
|
|
|
$member = Security::getCurrentUser();
|
2016-03-03 09:32:06 +01:00
|
|
|
}
|
|
|
|
//check for extensions, we do this first as they can overrule everything
|
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member);
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($extended !== null) {
|
2016-03-03 09:32:06 +01:00
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
|
|
|
|
//need to be logged in and/or most checks below rely on $member being a Member
|
2016-11-23 06:09:10 +01:00
|
|
|
if (!$member) {
|
2016-03-08 21:50:18 +01:00
|
|
|
return false;
|
2016-03-03 09:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// HACK: we should not allow for an non-Admin to edit an Admin
|
2016-11-23 06:09:10 +01:00
|
|
|
if (!Permission::checkMember($member, 'ADMIN') && Permission::checkMember($this, 'ADMIN')) {
|
2016-03-03 09:32:06 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// members can usually edit their own record
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($this->ID == $member->ID) {
|
2016-03-03 09:32:06 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//standard check
|
|
|
|
return Permission::checkMember($member, 'CMS_ACCESS_SecurityAdmin');
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Users can edit their own record.
|
|
|
|
* Otherwise they'll need ADMIN or CMS_ACCESS_SecurityAdmin permissions
|
2016-11-23 06:09:10 +01:00
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool
|
2016-03-03 09:32:06 +01:00
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function canDelete($member = null)
|
|
|
|
{
|
2017-05-11 11:07:27 +02:00
|
|
|
if (!$member) {
|
2017-05-20 06:32:25 +02:00
|
|
|
$member = Security::getCurrentUser();
|
2016-03-03 09:32:06 +01:00
|
|
|
}
|
|
|
|
//check for extensions, we do this first as they can overrule everything
|
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member);
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($extended !== null) {
|
2016-03-03 09:32:06 +01:00
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
|
|
|
|
//need to be logged in and/or most checks below rely on $member being a Member
|
2016-11-23 06:09:10 +01:00
|
|
|
if (!$member) {
|
2016-03-08 21:50:18 +01:00
|
|
|
return false;
|
2016-03-03 09:32:06 +01:00
|
|
|
}
|
|
|
|
// Members are not allowed to remove themselves,
|
|
|
|
// since it would create inconsistencies in the admin UIs.
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($this->ID && $member->ID == $this->ID) {
|
2016-03-08 21:50:18 +01:00
|
|
|
return false;
|
2016-03-03 09:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// HACK: if you want to delete a member, you have to be a member yourself.
|
|
|
|
// this is a hack because what this should do is to stop a user
|
|
|
|
// deleting a member who has more privileges (e.g. a non-Admin deleting an Admin)
|
2016-11-23 06:09:10 +01:00
|
|
|
if (Permission::checkMember($this, 'ADMIN')) {
|
|
|
|
if (! Permission::checkMember($member, 'ADMIN')) {
|
2016-03-08 21:50:18 +01:00
|
|
|
return false;
|
2016-03-03 09:32:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//standard check
|
|
|
|
return Permission::checkMember($member, 'CMS_ACCESS_SecurityAdmin');
|
|
|
|
}
|
2008-04-26 08:31:52 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Validate this member object.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function validate()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$valid = parent::validate();
|
|
|
|
|
|
|
|
if (!$this->ID || $this->isChanged('Password')) {
|
|
|
|
if ($this->Password && self::$password_validator) {
|
|
|
|
$valid->combineAnd(self::$password_validator->validate($this->Password, $this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!$this->ID && $this->SetPassword) || $this->isChanged('SetPassword')) {
|
|
|
|
if ($this->SetPassword && self::$password_validator) {
|
|
|
|
$valid->combineAnd(self::$password_validator->validate($this->SetPassword, $this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change password. This will cause rehashing according to
|
|
|
|
* the `PasswordEncryption` property.
|
|
|
|
*
|
|
|
|
* @param string $password Cleartext password
|
|
|
|
* @return ValidationResult
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function changePassword($password)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$this->Password = $password;
|
|
|
|
$valid = $this->validate();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($valid->isValid()) {
|
|
|
|
$this->AutoLoginHash = null;
|
|
|
|
$this->write();
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
return $valid;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Tell this member that someone made a failed attempt at logging in as them.
|
|
|
|
* This can be used to lock the user out temporarily if too many failed attempts are made.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function registerFailedLogin()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
if (self::config()->lock_out_after_incorrect_logins) {
|
|
|
|
// Keep a tally of the number of failed log-ins so that we can lock people out
|
|
|
|
$this->FailedLoginCount = $this->FailedLoginCount + 1;
|
|
|
|
|
|
|
|
if ($this->FailedLoginCount >= self::config()->lock_out_after_incorrect_logins) {
|
|
|
|
$lockoutMins = self::config()->lock_out_delay_mins;
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->LockedOutUntil = date('Y-m-d H:i:s', DBDatetime::now()->getTimestamp() + $lockoutMins*60);
|
2016-11-23 06:09:10 +01:00
|
|
|
$this->FailedLoginCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->extend('registerFailedLogin');
|
|
|
|
$this->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tell this member that a successful login has been made
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function registerSuccessfulLogin()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
if (self::config()->lock_out_after_incorrect_logins) {
|
|
|
|
// Forgive all past login failures
|
|
|
|
$this->FailedLoginCount = 0;
|
|
|
|
$this->write();
|
|
|
|
}
|
|
|
|
}
|
2017-01-13 02:48:46 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Get the HtmlEditorConfig for this user to be used in the CMS.
|
|
|
|
* This is set by the group. If multiple configurations are set,
|
|
|
|
* the one with the highest priority wins.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function getHtmlEditorConfigForCMS()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$currentName = '';
|
|
|
|
$currentPriority = 0;
|
|
|
|
|
|
|
|
foreach ($this->Groups() as $group) {
|
|
|
|
$configName = $group->HtmlEditorConfig;
|
|
|
|
if ($configName) {
|
|
|
|
$config = HTMLEditorConfig::get($group->HtmlEditorConfig);
|
|
|
|
if ($config && $config->getOption('priority') > $currentPriority) {
|
|
|
|
$currentName = $configName;
|
|
|
|
$currentPriority = $config->getOption('priority');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If can't find a suitable editor, just default to cms
|
|
|
|
return $currentName ? $currentName : 'cms';
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|