2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
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
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage security
|
2014-01-26 04:17:17 +01:00
|
|
|
*
|
|
|
|
* @property string FirstName
|
|
|
|
* @property string Surname
|
|
|
|
* @property string Email
|
|
|
|
* @property string Password
|
|
|
|
* @property string RememberLoginHash
|
|
|
|
* @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
|
|
|
*/
|
2012-02-11 03:08:39 +01:00
|
|
|
class Member extends DataObject implements TemplateGlobalProvider {
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-02-02 00:49:53 +01:00
|
|
|
'FirstName' => 'Varchar',
|
|
|
|
'Surname' => 'Varchar',
|
2011-08-22 08:52:07 +02:00
|
|
|
'Email' => 'Varchar(256)', // See RFC 5321, Section 4.5.3.1.3.
|
2009-11-06 03:23:21 +01:00
|
|
|
'Password' => 'Varchar(160)',
|
2012-11-08 04:33:19 +01:00
|
|
|
'RememberLoginToken' => 'Varchar(160)', // Note: this currently holds a hash, not a token.
|
|
|
|
'AutoLoginHash' => 'Varchar(160)',
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
'AutoLoginExpired' => 'SS_Datetime',
|
2009-11-06 03:23:21 +01:00
|
|
|
// 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)",
|
2010-12-09 09:17:35 +01:00
|
|
|
'Salt' => 'Varchar(50)',
|
2008-04-26 08:31:52 +02:00
|
|
|
'PasswordExpiry' => 'Date',
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
'LockedOutUntil' => 'SS_Datetime',
|
2008-04-26 08:31:52 +02:00
|
|
|
'Locale' => 'Varchar(6)',
|
2009-09-10 04:42:26 +02:00
|
|
|
// handled in registerFailedLogin(), only used if $lock_out_after_incorrect_logins is set
|
2010-10-15 05:23:02 +02:00
|
|
|
'FailedLoginCount' => 'Int',
|
|
|
|
// In ISO format
|
|
|
|
'DateFormat' => 'Varchar(30)',
|
|
|
|
'TimeFormat' => 'Varchar(30)',
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2007-09-15 23:51:37 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $belongs_many_many = array(
|
2009-02-02 00:49:53 +01:00
|
|
|
'Groups' => 'Group',
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many_extraFields = array();
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_sort = '"Surname", "FirstName"';
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $indexes = array(
|
2007-07-19 12:40:28 +02:00
|
|
|
'Email' => true,
|
2012-09-26 23:34:00 +02:00
|
|
|
//Removed due to duplicate null values causing MSSQL problems
|
2014-08-15 08:53:05 +02:00
|
|
|
//'AutoLoginHash' => Array('type'=>'unique', 'value'=>'AutoLoginHash', 'ignoreNulls'=>true)
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private static $notify_password_change = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-10 22:28:35 +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.
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $searchable_fields = array(
|
2008-08-26 03:45:52 +02:00
|
|
|
'FirstName',
|
|
|
|
'Surname',
|
|
|
|
'Email',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $summary_fields = array(
|
2009-03-04 04:44:11 +01:00
|
|
|
'FirstName' => 'First Name',
|
|
|
|
'Surname' => 'Last Name',
|
|
|
|
'Email' => 'Email',
|
2010-04-14 06:08:22 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-14 06:08:22 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2010-04-14 06:08:22 +02:00
|
|
|
* @var Array See {@link set_title_columns()}
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $title_format = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
/**
|
|
|
|
* The unique field used to identify this member.
|
|
|
|
* By default, it's "Email", but another common
|
|
|
|
* field could be Username.
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
|
|
|
* @config
|
2009-02-02 00:49:53 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $unique_identifier_field = 'Email';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2008-04-26 08:31:52 +02:00
|
|
|
* {@link PasswordValidator} object for validating user's password
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $password_validator = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2008-04-26 08:31:52 +02:00
|
|
|
* The number of days that a password should be valid for.
|
|
|
|
* By default, this is null, which means that passwords never expire
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $password_expiry_days = null;
|
2008-04-26 08:32:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
2013-07-11 01:17:02 +02:00
|
|
|
* @var Int Number of incorrect logins after which
|
2014-08-15 08:53:05 +02:00
|
|
|
* the user is blocked from further attempts for the timespan
|
|
|
|
* defined in {@link $lock_out_delay_mins}.
|
2013-03-21 19:48:54 +01:00
|
|
|
*/
|
|
|
|
private static $lock_out_after_incorrect_logins = null;
|
2013-07-11 01:17:02 +02: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
|
|
|
|
2009-10-12 05:27:41 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
|
|
|
* @var String If this is set, then a session cookie with the given name will be set on log-in,
|
2009-10-12 05:27:41 +02:00
|
|
|
* and cleared on logout.
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $login_marker_cookie = null;
|
2009-10-12 05:27:41 +02:00
|
|
|
|
2010-10-19 02:55:33 +02:00
|
|
|
/**
|
|
|
|
* Indicates that when a {@link Member} logs in, Member:session_regenerate_id()
|
|
|
|
* should be called as a security precaution.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-19 02:55:33 +02:00
|
|
|
* 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()
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
|
|
|
* @config
|
2010-10-19 02:55:33 +02:00
|
|
|
* @var boolean
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $session_regenerate_id = true;
|
2010-10-19 02:55:33 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @deprecated 3.2 Use the "Member.session_regenerate_id" config setting instead
|
|
|
|
*/
|
2010-10-19 02:55:33 +02:00
|
|
|
public static function set_session_regenerate_id($bool) {
|
2013-03-21 19:48:54 +01:00
|
|
|
Deprecation::notice('3.2', 'Use the "Member.session_regenerate_id" config setting instead');
|
|
|
|
self::config()->session_regenerate_id = $bool;
|
2010-10-19 02:55:33 +02:00
|
|
|
}
|
|
|
|
|
2010-05-25 06:15:13 +02:00
|
|
|
/**
|
|
|
|
* Ensure the locale is set to something sensible by default.
|
|
|
|
*/
|
|
|
|
public function populateDefaults() {
|
|
|
|
parent::populateDefaults();
|
2014-01-30 03:55:14 +01:00
|
|
|
$this->Locale = i18n::get_closest_translation(i18n::get_locale());
|
2010-05-25 06:15:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function requireDefaultRecords() {
|
2010-12-11 01:45:05 +01:00
|
|
|
parent::requireDefaultRecords();
|
2010-08-03 03:05:27 +02:00
|
|
|
// Default groups should've been built by Group->requireDefaultRecords() already
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-08-03 03:05:27 +02:00
|
|
|
// Find or create ADMIN group
|
2009-11-22 06:16:38 +01:00
|
|
|
$adminGroup = Permission::get_groups_by_permission('ADMIN')->First();
|
|
|
|
if(!$adminGroup) {
|
2010-08-03 03:05:27 +02:00
|
|
|
singleton('Group')->requireDefaultRecords();
|
2009-11-22 06:16:38 +01:00
|
|
|
$adminGroup = Permission::get_groups_by_permission('ADMIN')->First();
|
2010-08-03 03:05:27 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-08-03 03:05:27 +02:00
|
|
|
// Add a default administrator to the first ADMIN group found (most likely the default
|
|
|
|
// group created through Group->requireDefaultRecords()).
|
2009-11-22 06:16:38 +01:00
|
|
|
$admins = Permission::get_members_by_permission('ADMIN')->First();
|
2010-08-03 03:05:27 +02:00
|
|
|
if(!$admins) {
|
|
|
|
// Leave 'Email' and 'Password' are not set to avoid creating
|
|
|
|
// persistent logins in the database. See Security::setDefaultAdmin().
|
2012-04-04 16:59:30 +02:00
|
|
|
$admin = Member::create();
|
2010-08-03 03:05:27 +02:00
|
|
|
$admin->FirstName = _t('Member.DefaultAdminFirstname', 'Default Admin');
|
|
|
|
$admin->write();
|
|
|
|
$admin->Groups()->add($adminGroup);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2010-08-03 03:05:27 +02:00
|
|
|
}
|
2010-05-25 06:15:13 +02:00
|
|
|
|
2009-10-12 05:27:41 +02:00
|
|
|
/**
|
|
|
|
* If this is called, then a session cookie will be set to "1" whenever a user
|
|
|
|
* logs in. This lets 3rd party tools, such as apache's mod_rewrite, detect
|
|
|
|
* whether a user is logged in or not and alter behaviour accordingly.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-10-12 05:27:41 +02:00
|
|
|
* One known use of this is to bypass static caching for logged in users. This is
|
|
|
|
* done by putting this into _config.php
|
|
|
|
* <pre>
|
|
|
|
* Member::set_login_marker_cookie("SS_LOGGED_IN");
|
|
|
|
* </pre>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-10-12 05:27:41 +02:00
|
|
|
* And then adding this condition to each of the rewrite rules that make use of
|
|
|
|
* the static cache.
|
|
|
|
* <pre>
|
|
|
|
* RewriteCond %{HTTP_COOKIE} !SS_LOGGED_IN=1
|
|
|
|
* </pre>
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
|
|
|
* @deprecated 3.2 Use the "Member.login_marker_cookie" config setting instead
|
2009-10-12 05:27:41 +02:00
|
|
|
* @param $cookieName string The name of the cookie to set.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_login_marker_cookie($cookieName) {
|
2013-03-21 19:48:54 +01:00
|
|
|
Deprecation::notice('3.2', 'Use the "Member.login_marker_cookie" config setting instead');
|
|
|
|
self::config()->login_marker_cookie = $cookieName;
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2007-09-15 23:51:37 +02:00
|
|
|
|
2007-09-16 02:32:48 +02:00
|
|
|
/**
|
2010-02-05 01:36:25 +01:00
|
|
|
* Check if the passed password matches the stored one (if the member is not locked out).
|
2007-09-16 02:32:48 +02:00
|
|
|
*
|
2010-02-05 01:36:25 +01:00
|
|
|
* @param string $password
|
|
|
|
* @return ValidationResult
|
2007-09-16 02:32:48 +02:00
|
|
|
*/
|
|
|
|
public function checkPassword($password) {
|
2010-02-05 01:36:25 +01:00
|
|
|
$result = $this->canLogIn();
|
|
|
|
|
2013-07-18 08:01:53 +02:00
|
|
|
// Short-circuit the result upon failure, no further checks needed.
|
|
|
|
if (!$result->valid()) return $result;
|
|
|
|
|
2013-05-30 15:16:12 +02:00
|
|
|
if(empty($this->Password) && $this->exists()) {
|
|
|
|
$result->error(_t('Member.NoPassword','There is no password on this member.'));
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2012-05-07 05:03:53 +02:00
|
|
|
$e = PasswordEncryptor::create_for_algorithm($this->PasswordEncryption);
|
|
|
|
if(!$e->check($this->Password, $password, $this->Salt, $this)) {
|
2010-02-05 01:36:25 +01:00
|
|
|
$result->error(_t (
|
2013-12-19 23:13:13 +01:00
|
|
|
'Member.ERRORWRONGCRED',
|
2013-10-11 00:22:15 +02:00
|
|
|
'The provided details don\'t seem to be correct. Please try again.'
|
2010-02-05 01:36:25 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2008-04-26 08:32:05 +02:00
|
|
|
}
|
2010-02-05 01:36:25 +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
|
|
|
|
*/
|
|
|
|
public function canLogIn() {
|
2013-06-18 16:49:58 +02:00
|
|
|
$result = ValidationResult::create();
|
2010-02-05 01:36:25 +01:00
|
|
|
|
|
|
|
if($this->isLockedOut()) {
|
2013-07-11 01:17:02 +02:00
|
|
|
$result->error(
|
|
|
|
_t(
|
|
|
|
'Member.ERRORLOCKEDOUT2',
|
|
|
|
'Your account has been temporarily disabled because of too many failed attempts at ' .
|
|
|
|
'logging in. Please try again in {count} minutes.',
|
|
|
|
null,
|
|
|
|
array('count' => $this->config()->lock_out_delay_mins)
|
|
|
|
)
|
|
|
|
);
|
2010-02-05 01:36:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->extend('canLogIn', $result);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2008-04-26 08:32:05 +02:00
|
|
|
/**
|
|
|
|
* Returns true if this user is locked out
|
|
|
|
*/
|
|
|
|
public function isLockedOut() {
|
|
|
|
return $this->LockedOutUntil && time() < strtotime($this->LockedOutUntil);
|
2007-09-16 02:32:48 +02:00
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Regenerate the session_id.
|
2014-08-15 08:53:05 +02:00
|
|
|
* This wrapper is here to make it easier to disable calls to session_regenerate_id(), should you need to.
|
2008-03-10 22:28:35 +01:00
|
|
|
* They have caused problems in certain
|
2008-02-25 03:10:37 +01:00
|
|
|
* quirky problems (such as using the Windmill 0.3.6 proxy).
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function session_regenerate_id() {
|
2010-10-19 02:55:33 +02:00
|
|
|
if(!self::$session_regenerate_id) return;
|
|
|
|
|
2008-08-13 03:43:09 +02:00
|
|
|
// This can be called via CLI during testing.
|
|
|
|
if(Director::is_cli()) return;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
$file = '';
|
|
|
|
$line = '';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-21 04:21:05 +02:00
|
|
|
// @ is to supress win32 warnings/notices when session wasn't cleaned up properly
|
|
|
|
// There's nothing we can do about this, because it's an operating system function!
|
|
|
|
if(!headers_sent($file, $line)) @session_regenerate_id(true);
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
/**
|
|
|
|
* Get the field used for uniquely identifying a member
|
|
|
|
* in the database. {@see Member::$unique_identifier_field}
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
|
|
|
* @deprecated 3.2 Use the "Member.unique_identifier_field" config setting instead
|
2009-02-02 00:49:53 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_unique_identifier_field() {
|
2013-03-21 19:48:54 +01:00
|
|
|
Deprecation::notice('3.2', 'Use the "Member.unique_identifier_field" config setting instead');
|
|
|
|
return Member::config()->unique_identifier_field;
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
/**
|
|
|
|
* Set the field used for uniquely identifying a member
|
|
|
|
* in the database. {@see Member::$unique_identifier_field}
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
|
|
|
* @deprecated 3.2 Use the "Member.unique_identifier_field" config setting instead
|
2009-02-02 00:49:53 +01:00
|
|
|
* @param $field The field name to set as the unique field
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_unique_identifier_field($field) {
|
2013-03-21 19:48:54 +01:00
|
|
|
Deprecation::notice('3.2', 'Use the "Member.unique_identifier_field" config setting instead');
|
|
|
|
Member::config()->unique_identifier_field = $field;
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Set a {@link PasswordValidator} object to use to validate member's passwords.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_password_validator($pv) {
|
2008-04-26 08:31:52 +02:00
|
|
|
self::$password_validator = $pv;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-18 05:59:00 +02:00
|
|
|
/**
|
|
|
|
* Returns the current {@link PasswordValidator}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function password_validator() {
|
2008-09-18 05:59:00 +02:00
|
|
|
return self::$password_validator;
|
|
|
|
}
|
2008-04-26 08:31:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the number of days that a password should be valid for.
|
|
|
|
* Set to null (the default) to have passwords never expire.
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
|
|
|
* @deprecated 3.2 Use the "Member.password_expiry_days" config setting instead
|
2008-04-26 08:31:52 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_password_expiry($days) {
|
2013-03-21 19:48:54 +01:00
|
|
|
Deprecation::notice('3.2', 'Use the "Member.password_expiry_days" config setting instead');
|
|
|
|
self::config()->password_expiry_days = $days;
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:32:05 +02:00
|
|
|
/**
|
|
|
|
* Configure the security system to lock users out after this many incorrect logins
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
|
|
|
* @deprecated 3.2 Use the "Member.lock_out_after_incorrect_logins" config setting instead
|
2008-04-26 08:32:05 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function lock_out_after_incorrect_logins($numLogins) {
|
2013-03-21 19:48:54 +01:00
|
|
|
Deprecation::notice('3.2', 'Use the "Member.lock_out_after_incorrect_logins" config setting instead');
|
|
|
|
self::config()->lock_out_after_incorrect_logins = $numLogins;
|
2008-04-26 08:32:05 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isPasswordExpired() {
|
2008-04-26 08:31:52 +02:00
|
|
|
if(!$this->PasswordExpiry) return false;
|
|
|
|
return strtotime(date('Y-m-d')) >= strtotime($this->PasswordExpiry);
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2007-09-16 02:32:48 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Logs this member in
|
|
|
|
*
|
2008-03-10 22:28:35 +01:00
|
|
|
* @param bool $remember If set to TRUE, the member will be logged in automatically the next time.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function logIn($remember = false) {
|
2013-12-18 18:25:51 +01:00
|
|
|
$this->extend('beforeMemberLoggedIn');
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
self::session_regenerate_id();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
Session::set("loggedInAs", $this->ID);
|
2009-10-12 05:27:41 +02:00
|
|
|
// This lets apache rules detect whether the user has logged in
|
2013-03-21 19:48:54 +01:00
|
|
|
if(Member::config()->login_marker_cookie) Cookie::set(Member::config()->login_marker_cookie, 1, 0);
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
if($remember) {
|
2012-11-08 04:33:19 +01:00
|
|
|
// Store the hash and give the client the cookie with the token.
|
2010-12-05 01:39:25 +01:00
|
|
|
$generator = new RandomGenerator();
|
2012-11-08 04:33:19 +01:00
|
|
|
$token = $generator->randomToken('sha1');
|
|
|
|
$hash = $this->encryptWithUserSettings($token);
|
|
|
|
$this->RememberLoginToken = $hash;
|
2009-09-10 05:23:31 +02:00
|
|
|
Cookie::set('alc_enc', $this->ID . ':' . $token, 90, null, null, null, true);
|
2007-09-14 21:10:18 +02:00
|
|
|
} else {
|
|
|
|
$this->RememberLoginToken = null;
|
|
|
|
Cookie::set('alc_enc', null);
|
2013-03-19 10:38:14 +01:00
|
|
|
Cookie::force_expiry('alc_enc');
|
2007-09-14 21:10:18 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:32:05 +02:00
|
|
|
// Clear the incorrect log-in count
|
2013-03-21 19:48:54 +01:00
|
|
|
if(self::config()->lock_out_after_incorrect_logins) {
|
2009-09-10 04:42:26 +02:00
|
|
|
$this->FailedLoginCount = 0;
|
2008-04-26 08:32:05 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
// Don't set column if its not built yet (the login might be precursor to a /dev/build...)
|
2013-06-21 00:32:08 +02:00
|
|
|
if(array_key_exists('LockedOutUntil', DB::field_list('Member'))) {
|
2009-02-02 00:49:53 +01:00
|
|
|
$this->LockedOutUntil = null;
|
|
|
|
}
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-12 22:59:32 +02:00
|
|
|
// Audit logging hook
|
|
|
|
$this->extend('memberLoggedIn');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-29 03:20:24 +02:00
|
|
|
* @return boolean TRUE record found FALSE no record found
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function logged_in_session_exists() {
|
2009-04-29 03:20:24 +02:00
|
|
|
if($id = Member::currentUserID()) {
|
|
|
|
if($member = DataObject::get_by_id('Member', $id)) {
|
|
|
|
if($member->exists()) return true;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
|
|
|
* Log the user in if the "remember login" cookie is set
|
|
|
|
*
|
|
|
|
* The <i>remember login token</i> will be changed on every successful
|
|
|
|
* auto-login.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function autoLogin() {
|
2009-08-11 11:16:34 +02:00
|
|
|
// Don't bother trying this multiple times
|
|
|
|
self::$_already_tried_to_auto_log_in = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-15 23:43:04 +02:00
|
|
|
if(strpos(Cookie::get('alc_enc'), ':') && !Session::get("loggedInAs")) {
|
|
|
|
list($uid, $token) = explode(':', Cookie::get('alc_enc'), 2);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$member = DataObject::get_by_id("Member", $uid);
|
2007-09-15 02:27:40 +02:00
|
|
|
|
2009-03-17 23:25:22 +01:00
|
|
|
// check if autologin token matches
|
2013-01-29 21:17:47 +01:00
|
|
|
if($member) {
|
|
|
|
$hash = $member->encryptWithUserSettings($token);
|
|
|
|
if(!$member->RememberLoginToken || $member->RememberLoginToken !== $hash) {
|
|
|
|
$member = null;
|
|
|
|
}
|
2007-10-02 23:57:12 +02:00
|
|
|
}
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
if($member) {
|
2008-02-25 03:10:37 +01:00
|
|
|
self::session_regenerate_id();
|
2007-09-14 21:10:18 +02:00
|
|
|
Session::set("loggedInAs", $member->ID);
|
2009-10-12 05:27:41 +02:00
|
|
|
// This lets apache rules detect whether the user has logged in
|
2013-07-18 08:01:53 +02:00
|
|
|
if(Member::config()->login_marker_cookie) {
|
|
|
|
Cookie::set(Member::config()->login_marker_cookie, 1, 0, null, null, false, true);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-05 01:39:25 +01:00
|
|
|
$generator = new RandomGenerator();
|
2012-11-08 04:33:19 +01:00
|
|
|
$token = $generator->randomToken('sha1');
|
|
|
|
$hash = $member->encryptWithUserSettings($token);
|
|
|
|
$member->RememberLoginToken = $hash;
|
|
|
|
Cookie::set('alc_enc', $member->ID . ':' . $token, 90, null, null, false, true);
|
2007-09-14 21:10:18 +02:00
|
|
|
$member->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-12 22:59:32 +02:00
|
|
|
// Audit logging hook
|
2008-08-14 05:30:32 +02:00
|
|
|
$member->extend('memberAutoLoggedIn');
|
2007-09-14 21:10:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Logs this member out.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function logOut() {
|
2013-12-18 18:25:51 +01:00
|
|
|
$this->extend('beforeMemberLoggedOut');
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
Session::clear("loggedInAs");
|
2013-03-21 19:48:54 +01:00
|
|
|
if(Member::config()->login_marker_cookie) Cookie::set(Member::config()->login_marker_cookie, null, 0);
|
2013-05-23 03:26:57 +02:00
|
|
|
|
|
|
|
Session::destroy();
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2008-08-12 07:59:15 +02:00
|
|
|
$this->extend('memberLoggedOut');
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
$this->RememberLoginToken = null;
|
2012-02-09 03:58:05 +01:00
|
|
|
Cookie::set('alc_enc', null); // // Clear the Remember Me cookie
|
2013-03-19 10:49:52 +01:00
|
|
|
Cookie::force_expiry('alc_enc');
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
// Switch back to live in order to avoid infinite loops when
|
2013-02-18 14:41:49 +01:00
|
|
|
// redirecting to the login screen (if this login screen is versioned)
|
2012-02-09 03:58:05 +01:00
|
|
|
Session::clear('readingMode');
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
$this->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-12 22:59:32 +02:00
|
|
|
// Audit logging hook
|
|
|
|
$this->extend('memberLoggedOut');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2012-11-08 04:33:19 +01:00
|
|
|
/**
|
|
|
|
* Utility for generating secure password hashes for this member.
|
|
|
|
*/
|
|
|
|
public function encryptWithUserSettings($string) {
|
|
|
|
if (!$string) return null;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We assume we have PasswordEncryption and Salt available here.
|
|
|
|
$e = PasswordEncryptor::create_for_algorithm($this->PasswordEncryption);
|
|
|
|
return $e->encrypt($string, $this->Salt);
|
|
|
|
|
|
|
|
}
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/**
|
2012-11-08 04:33:19 +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.
|
2007-09-16 02:32:48 +02:00
|
|
|
*
|
2008-03-10 22:28:35 +01:00
|
|
|
* @param int $lifetime The lifetime of the auto login hash in days (by default 2 days)
|
2007-09-16 02:32:48 +02:00
|
|
|
*
|
2012-11-08 04:33:19 +01:00
|
|
|
* @returns string Token that should be passed to the client (but NOT persisted).
|
|
|
|
*
|
2008-03-10 22:28:35 +01:00
|
|
|
* @todo Make it possible to handle database errors such as a "duplicate key" error
|
2007-09-14 21:10:18 +02:00
|
|
|
*/
|
2012-11-08 04:33:19 +01:00
|
|
|
public function generateAutologinTokenAndStoreHash($lifetime = 2) {
|
2007-09-16 02:32:48 +02:00
|
|
|
do {
|
2010-12-05 01:39:25 +01:00
|
|
|
$generator = new RandomGenerator();
|
2012-11-08 04:33:19 +01:00
|
|
|
$token = $generator->randomToken();
|
|
|
|
$hash = $this->encryptWithUserSettings($token);
|
2013-06-21 00:32:08 +02:00
|
|
|
} while(DataObject::get_one('Member', array(
|
|
|
|
'"Member"."AutoLoginHash"' => $hash
|
|
|
|
)));
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-16 02:32:48 +02:00
|
|
|
$this->AutoLoginHash = $hash;
|
|
|
|
$this->AutoLoginExpired = date('Y-m-d', time() + (86400 * $lifetime));
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->write();
|
2012-11-08 04:33:19 +01:00
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the token against the member.
|
|
|
|
*
|
|
|
|
* @param string $autologinToken
|
|
|
|
*
|
|
|
|
* @returns bool Is token valid?
|
|
|
|
*/
|
|
|
|
public function validateAutoLoginToken($autologinToken) {
|
|
|
|
$hash = $this->encryptWithUserSettings($autologinToken);
|
2013-06-21 00:32:08 +02:00
|
|
|
$member = self::member_from_autologinhash($hash, false);
|
2012-11-08 04:33:19 +01:00
|
|
|
return (bool)$member;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-16 02:32:48 +02:00
|
|
|
* Return the member for the auto login hash
|
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $hash The hash key
|
2007-09-16 02:32:48 +02:00
|
|
|
* @param bool $login Should the member be logged in?
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @return Member the matching member, if valid
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public static function member_from_autologinhash($hash, $login = false) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$nowExpression = DB::get_conn()->now();
|
|
|
|
$member = DataObject::get_one('Member', array(
|
|
|
|
"\"Member\".\"AutoLoginHash\"" => $hash,
|
|
|
|
"\"Member\".\"AutoLoginExpired\" > $nowExpression" // NOW() can't be parameterised
|
|
|
|
));
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
if($login && $member) $member->logIn();
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $member;
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
2007-12-02 22:28:41 +01:00
|
|
|
* Returns the fields for the member form - used in the registration/profile module.
|
2014-08-15 08:53:05 +02:00
|
|
|
* It should return fields that are editable by the admin and the logged-in user.
|
2007-09-14 21:10:18 +02:00
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @return FieldList Returns a {@link FieldList} containing the fields for
|
2012-12-08 12:20:20 +01:00
|
|
|
* the member form.
|
2007-09-14 21:10:18 +02:00
|
|
|
*/
|
2010-01-21 23:59:19 +01:00
|
|
|
public function getMemberFormFields() {
|
|
|
|
$fields = parent::getFrontendFields();
|
|
|
|
|
|
|
|
$fields->replaceField('Password', $password = new ConfirmedPasswordField (
|
|
|
|
'Password',
|
|
|
|
$this->fieldLabel('Password'),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
(bool) $this->ID
|
|
|
|
));
|
|
|
|
$password->setCanBeEmpty(true);
|
|
|
|
|
|
|
|
$fields->replaceField('Locale', new DropdownField (
|
|
|
|
'Locale',
|
|
|
|
$this->fieldLabel('Locale'),
|
2010-05-25 06:15:13 +02:00
|
|
|
i18n::get_existing_translations()
|
2010-01-21 23:59:19 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
$fields->removeByName('RememberLoginToken');
|
|
|
|
$fields->removeByName('AutoLoginHash');
|
|
|
|
$fields->removeByName('AutoLoginExpired');
|
|
|
|
$fields->removeByName('PasswordEncryption');
|
|
|
|
$fields->removeByName('Salt');
|
|
|
|
$fields->removeByName('PasswordExpiry');
|
|
|
|
$fields->removeByName('FailedLoginCount');
|
|
|
|
$fields->removeByName('LastViewed');
|
|
|
|
$fields->removeByName('LockedOutUntil');
|
|
|
|
|
|
|
|
$this->extend('updateMemberFormFields', $fields);
|
2007-12-02 22:28:41 +01:00
|
|
|
return $fields;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2013-10-16 00:29:43 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getValidator() {
|
2013-10-16 00:29:43 +02:00
|
|
|
$validator = Injector::inst()->create('Member_Validator');
|
|
|
|
$this->extend('updateValidator', $validator);
|
|
|
|
|
|
|
|
return $validator;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Returns the current logged in user
|
|
|
|
*
|
2014-01-12 04:07:24 +01:00
|
|
|
* @return Member|null
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function currentUser() {
|
2009-08-11 11:16:34 +02:00
|
|
|
$id = Member::currentUserID();
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
if($id) {
|
2014-01-12 04:07:24 +01:00
|
|
|
return Member::get()->byId($id);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
|
|
|
* Get the ID of the current logged in user
|
|
|
|
*
|
|
|
|
* @return int Returns the ID of the current logged in user or 0.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function currentUserID() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$id = Session::get("loggedInAs");
|
2009-08-11 11:16:34 +02:00
|
|
|
if(!$id && !self::$_already_tried_to_auto_log_in) {
|
2007-09-14 21:10:18 +02:00
|
|
|
self::autoLogin();
|
|
|
|
$id = Session::get("loggedInAs");
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return is_numeric($id) ? $id : 0;
|
|
|
|
}
|
2009-08-11 11:16:34 +02:00
|
|
|
private static $_already_tried_to_auto_log_in = false;
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02: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.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function create_new_password() {
|
2007-10-25 03:51:53 +02:00
|
|
|
if(file_exists(Security::get_word_list())) {
|
|
|
|
$words = file(Security::get_word_list());
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
list($usec, $sec) = explode(' ', microtime());
|
|
|
|
srand($sec + ((float) $usec * 100000));
|
|
|
|
|
|
|
|
$word = trim($words[rand(0,sizeof($words)-1)]);
|
|
|
|
$number = rand(10,999);
|
|
|
|
|
|
|
|
return $word . $number;
|
|
|
|
} else {
|
2012-12-08 12:20:20 +01:00
|
|
|
$random = rand();
|
|
|
|
$string = md5($random);
|
|
|
|
$output = substr($string, 0, 6);
|
|
|
|
return $output;
|
2007-09-14 21:10:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-02 00:49:53 +01:00
|
|
|
* Event handler called before writing to the database.
|
2007-09-14 21:10:18 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function onBeforeWrite() {
|
2008-03-11 02:30:29 +01:00
|
|
|
if($this->SetPassword) $this->Password = $this->SetPassword;
|
2010-03-09 05:10:38 +01:00
|
|
|
|
|
|
|
// If a member with the same "unique identifier" already exists with a different ID, don't allow merging.
|
2014-08-15 08:53:05 +02:00
|
|
|
// Note: This does not a full replacement for safeguards in the controller layer (e.g. in a registration form),
|
2010-03-09 05:10:38 +01:00
|
|
|
// but rather a last line of defense against data inconsistencies.
|
2013-03-21 19:48:54 +01:00
|
|
|
$identifierField = Member::config()->unique_identifier_field;
|
2009-02-02 00:49:53 +01:00
|
|
|
if($this->$identifierField) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-03-09 05:10:38 +01:00
|
|
|
// Note: Same logic as Member_Validator class
|
2013-06-21 00:32:08 +02:00
|
|
|
$filter = array("\"$identifierField\"" => $this->$identifierField);
|
|
|
|
if($this->ID) {
|
|
|
|
$filter[] = array('"Member"."ID" <> ?' => $this->ID);
|
|
|
|
}
|
|
|
|
$existingRecord = DataObject::get_one('Member', $filter);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($existingRecord) {
|
2013-06-18 16:49:58 +02:00
|
|
|
throw new ValidationException(ValidationResult::create(false, _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Member.ValidationIdentifierFailed',
|
|
|
|
'Can\'t overwrite existing member #{id} with identical identifier ({name} = {value}))',
|
2012-09-26 23:34:00 +02:00
|
|
|
'Values in brackets show "fieldname = value", usually denoting an existing email address',
|
2012-05-01 21:44:54 +02:00
|
|
|
array(
|
|
|
|
'id' => $existingRecord->ID,
|
|
|
|
'name' => $identifierField,
|
|
|
|
'value' => $this->$identifierField
|
|
|
|
)
|
2010-10-13 05:42:49 +02:00
|
|
|
)));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2010-03-09 05:10:38 +01:00
|
|
|
|
2008-07-21 00:36:58 +02:00
|
|
|
// 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.
|
2008-10-01 20:32:55 +02:00
|
|
|
if(
|
2014-08-15 08:53:05 +02:00
|
|
|
(Director::isLive() || Email::mailer() instanceof TestMailer)
|
2009-05-27 02:09:23 +02:00
|
|
|
&& $this->isChanged('Password')
|
2014-08-15 08:53:05 +02:00
|
|
|
&& $this->record['Password']
|
2013-03-21 19:48:54 +01:00
|
|
|
&& $this->config()->notify_password_change
|
2008-10-01 20:32:55 +02:00
|
|
|
) {
|
2012-05-05 00:03:17 +02:00
|
|
|
$e = Member_ChangePasswordEmail::create();
|
2012-05-10 03:53:45 +02:00
|
|
|
$e->populateTemplate($this);
|
|
|
|
$e->setTo($this->Email);
|
2012-05-05 00:03:17 +02:00
|
|
|
$e->send();
|
2008-10-01 20:32:55 +02:00
|
|
|
}
|
2010-03-09 05:10:38 +01:00
|
|
|
|
2009-11-06 03:23:21 +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.
|
2010-10-15 05:52:38 +02:00
|
|
|
if((!$this->ID && $this->Password) || $this->isChanged('Password')) {
|
2008-03-11 02:30:29 +01:00
|
|
|
// Password was changed: encrypt the password according the settings
|
2009-11-06 03:23:21 +01:00
|
|
|
$encryption_details = Security::encrypt_password(
|
|
|
|
$this->Password, // this is assumed to be cleartext
|
|
|
|
$this->Salt,
|
2013-07-18 08:01:53 +02:00
|
|
|
($this->PasswordEncryption) ?
|
|
|
|
$this->PasswordEncryption : Security::config()->password_encryption_algorithm,
|
2009-11-06 03:23:21 +01:00
|
|
|
$this
|
|
|
|
);
|
2010-10-15 05:52:38 +02:00
|
|
|
|
2009-11-06 03:23:21 +01:00
|
|
|
// Overwrite the Password property with the hashed value
|
2010-10-19 02:37:43 +02:00
|
|
|
$this->Password = $encryption_details['password'];
|
2008-03-11 02:30:29 +01:00
|
|
|
$this->Salt = $encryption_details['salt'];
|
|
|
|
$this->PasswordEncryption = $encryption_details['algorithm'];
|
2010-03-09 05:10:38 +01:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
// If we haven't manually set a password expiry
|
2009-05-27 02:09:23 +02:00
|
|
|
if(!$this->isChanged('PasswordExpiry')) {
|
2008-04-26 08:31:52 +02:00
|
|
|
// then set it for us
|
2013-03-21 19:48:54 +01:00
|
|
|
if(self::config()->password_expiry_days) {
|
|
|
|
$this->PasswordExpiry = date('Y-m-d', time() + 86400 * self::config()->password_expiry_days);
|
2008-04-26 08:31:52 +02:00
|
|
|
} else {
|
|
|
|
$this->PasswordExpiry = null;
|
|
|
|
}
|
|
|
|
}
|
2008-03-11 02:30:29 +01:00
|
|
|
}
|
2010-03-09 05:10:38 +01:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
// save locale
|
|
|
|
if(!$this->Locale) {
|
|
|
|
$this->Locale = i18n::get_locale();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::onBeforeWrite();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function onAfterWrite() {
|
2008-04-26 08:31:52 +02:00
|
|
|
parent::onAfterWrite();
|
|
|
|
|
2009-05-27 02:09:23 +02:00
|
|
|
if($this->isChanged('Password')) {
|
2008-04-26 08:31:52 +02:00
|
|
|
MemberPassword::log($this);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-09 03:49:41 +01:00
|
|
|
/**
|
|
|
|
* If any admin groups are requested, deny the whole save operation.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-09 03:49:41 +01:00
|
|
|
* @param Array $ids Database IDs of Group records
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function onChangeGroups($ids) {
|
2012-04-27 02:22:58 +02:00
|
|
|
// Filter out admin groups to avoid privilege escalation,
|
|
|
|
// unless the current user is an admin already OR the logged in user is an admin
|
|
|
|
if(!(Permission::check('ADMIN') || Permission::checkMember($this, 'ADMIN'))) {
|
2011-03-09 03:49:41 +01:00
|
|
|
$adminGroups = Permission::get_groups_by_permission('ADMIN');
|
|
|
|
$adminGroupIDs = ($adminGroups) ? $adminGroups->column('ID') : array();
|
|
|
|
return count(array_intersect($ids, $adminGroupIDs)) == 0;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-11-03 02:57:16 +01:00
|
|
|
* Check if the member is in one of the given groups.
|
2007-09-14 21:10:18 +02:00
|
|
|
*
|
2011-10-26 08:09:04 +02:00
|
|
|
* @param array|SS_List $groups Collection of {@link Group} DataObjects to check
|
2008-11-03 02:57:16 +01:00
|
|
|
* @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.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-11-03 02:57:16 +01:00
|
|
|
public function inGroups($groups, $strict = false) {
|
2008-11-03 14:48:04 +01:00
|
|
|
if($groups) foreach($groups as $group) {
|
2008-11-03 02:57:16 +01:00
|
|
|
if($this->inGroup($group, $strict)) return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-03 02:57:16 +01:00
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
2008-11-03 02:57:16 +01:00
|
|
|
* Check if the member is in the given group or any parent groups.
|
2007-09-14 21:10:18 +02:00
|
|
|
*
|
2008-09-22 18:07:28 +02:00
|
|
|
* @param int|Group|string $group Group instance, Group Code or ID
|
2008-11-03 02:57:16 +01:00
|
|
|
* @param boolean $strict Only determine direct group membership if set to TRUE (Default: FALSE)
|
2008-09-22 18:07:28 +02:00
|
|
|
* @return bool Returns TRUE if the member is in the given group, otherwise FALSE.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-11-03 02:57:16 +01:00
|
|
|
public function inGroup($group, $strict = false) {
|
2008-09-22 18:07:28 +02:00
|
|
|
if(is_numeric($group)) {
|
|
|
|
$groupCheckObj = DataObject::get_by_id('Group', $group);
|
|
|
|
} elseif(is_string($group)) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$groupCheckObj = DataObject::get_one('Group', array(
|
|
|
|
'"Group"."Code"' => $group
|
|
|
|
));
|
2008-11-03 02:57:16 +01:00
|
|
|
} elseif($group instanceof Group) {
|
2008-09-22 18:07:28 +02:00
|
|
|
$groupCheckObj = $group;
|
|
|
|
} else {
|
|
|
|
user_error('Member::inGroup(): Wrong format for $group parameter', E_USER_ERROR);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 02:07:39 +02:00
|
|
|
if(!$groupCheckObj) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-03 02:57:16 +01:00
|
|
|
$groupCandidateObjs = ($strict) ? $this->getManyManyComponents("Groups") : $this->Groups();
|
2008-11-03 14:48:04 +01:00
|
|
|
if($groupCandidateObjs) foreach($groupCandidateObjs as $groupCandidateObj) {
|
2008-11-03 02:57:16 +01:00
|
|
|
if($groupCandidateObj->ID == $groupCheckObj->ID) return true;
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:00:48 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Adds the member to a group. This will create the group if the given
|
|
|
|
* group code does not return a valid group object.
|
2010-10-15 05:00:48 +02:00
|
|
|
*
|
|
|
|
* @param string $groupcode
|
|
|
|
* @param string Title of the group
|
|
|
|
*/
|
|
|
|
public function addToGroupByCode($groupcode, $title = "") {
|
2013-06-21 00:32:08 +02:00
|
|
|
$group = DataObject::get_one('Group', array(
|
|
|
|
'"Group"."Code"' => $groupcode
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:00:48 +02:00
|
|
|
if($group) {
|
|
|
|
$this->Groups()->add($group);
|
2013-06-21 00:32:08 +02:00
|
|
|
} else {
|
2010-10-15 05:00:48 +02:00
|
|
|
if(!$title) $title = $groupcode;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:00:48 +02:00
|
|
|
$group = new Group();
|
|
|
|
$group->Code = $groupcode;
|
|
|
|
$group->Title = $title;
|
|
|
|
$group->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:00:48 +02:00
|
|
|
$this->Groups()->add($group);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-24 22:57:00 +02:00
|
|
|
/**
|
|
|
|
* Removes a member from a group.
|
|
|
|
*
|
|
|
|
* @param string $groupcode
|
|
|
|
*/
|
|
|
|
public function removeFromGroupByCode($groupcode) {
|
2013-11-01 12:22:13 +01:00
|
|
|
$group = Group::get()->filter(array('Code' => $groupcode))->first();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-24 22:57:00 +02:00
|
|
|
if($group) {
|
|
|
|
$this->Groups()->remove($group);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-14 06:08:22 +02:00
|
|
|
/**
|
|
|
|
* @param Array $columns Column names on the Member record to show in {@link getTitle()}.
|
|
|
|
* @param String $sep Separator
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_title_columns($columns, $sep = ' ') {
|
2010-04-14 06:08:22 +02:00
|
|
|
if (!is_array($columns)) $columns = array($columns);
|
2013-03-21 19:48:54 +01:00
|
|
|
self::config()->title_format = array('columns' => $columns, 'sep' => $sep);
|
2010-04-14 06:08:22 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
//------------------- HELPER METHODS -----------------------------------//
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
2010-04-14 06:08:22 +02:00
|
|
|
* Get the complete name of the member, by default in the format "<Surname>, <FirstName>".
|
|
|
|
* Falls back to showing either field on its own.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-04-14 06:08:22 +02:00
|
|
|
* You can overload this getter with {@link set_title_format()}
|
|
|
|
* and {@link set_title_sql()}.
|
2007-09-14 21:10:18 +02:00
|
|
|
*
|
|
|
|
* @return string Returns the first- and surname of the member. If the ID
|
2010-04-14 06:08:22 +02:00
|
|
|
* of the member is equal 0, only the surname is returned.
|
2007-09-14 21:10:18 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function getTitle() {
|
2013-03-21 19:48:54 +01:00
|
|
|
$format = $this->config()->title_format;
|
|
|
|
if ($format) {
|
2010-04-14 06:08:22 +02:00
|
|
|
$values = array();
|
2013-03-21 19:48:54 +01:00
|
|
|
foreach($format['columns'] as $col) {
|
2010-04-14 06:08:22 +02:00
|
|
|
$values[] = $this->getField($col);
|
|
|
|
}
|
2013-03-21 19:48:54 +01:00
|
|
|
return join($format['sep'], $values);
|
2010-04-14 06:08:22 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->getField('ID') === 0)
|
|
|
|
return $this->getField('Surname');
|
2009-04-17 08:00:32 +02:00
|
|
|
else{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
}
|
2010-10-15 05:23:02 +02:00
|
|
|
|
2010-04-14 06:08:22 +02:00
|
|
|
/**
|
|
|
|
* Return a SQL CONCAT() fragment suitable for a SELECT statement.
|
|
|
|
* Useful for custom queries which assume a certain member title format.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-04-14 06:08:22 +02:00
|
|
|
* @param String $tableName
|
|
|
|
* @return String SQL
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_title_sql($tableName = 'Member') {
|
2010-10-15 04:48:51 +02:00
|
|
|
// This should be abstracted to SSDatabase concatOperator or similar.
|
2013-06-21 00:32:08 +02:00
|
|
|
$op = (DB::get_conn() instanceof MSSQLDatabase) ? " + " : " || ";
|
2010-10-15 04:48:51 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
$format = self::config()->title_format;
|
|
|
|
if ($format) {
|
2010-04-14 06:08:51 +02:00
|
|
|
$columnsWithTablename = array();
|
2013-03-21 19:48:54 +01:00
|
|
|
foreach($format['columns'] as $column) {
|
2010-10-15 01:50:36 +02:00
|
|
|
$columnsWithTablename[] = "\"$tableName\".\"$column\"";
|
2010-04-14 06:08:51 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
return "(".join(" $op '".$format['sep']."' $op ", $columnsWithTablename).")";
|
2010-04-14 06:08:22 +02:00
|
|
|
} else {
|
2010-10-15 04:48:51 +02:00
|
|
|
return "(\"$tableName\".\"Surname\" $op ' ' $op \"$tableName\".\"FirstName\")";
|
2010-04-14 06:08:22 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the complete name of the member
|
|
|
|
*
|
|
|
|
* @return string Returns the first- and surname of the member.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function getName() {
|
2010-10-19 03:24:43 +02:00
|
|
|
return ($this->Surname) ? trim($this->FirstName . ' ' . $this->Surname) : $this->FirstName;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02: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
|
|
|
|
*/
|
|
|
|
public function setName($name) {
|
|
|
|
$nameParts = explode(' ', $name);
|
|
|
|
$this->Surname = array_pop($nameParts);
|
|
|
|
$this->FirstName = join(' ', $nameParts);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for {@link setName}
|
|
|
|
*
|
|
|
|
* @param string $name The name
|
|
|
|
* @see setName()
|
|
|
|
*/
|
|
|
|
public function splitName($name) {
|
|
|
|
return $this->setName($name);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2010-10-15 05:23:02 +02:00
|
|
|
/**
|
|
|
|
* Override the default getter for DateFormat so the
|
|
|
|
* default format for the user's locale is used
|
|
|
|
* if the user has not defined their own.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:23:02 +02:00
|
|
|
* @return string ISO date format
|
|
|
|
*/
|
|
|
|
public function getDateFormat() {
|
|
|
|
if($this->getField('DateFormat')) {
|
|
|
|
return $this->getField('DateFormat');
|
|
|
|
} else {
|
2014-02-11 12:12:37 +01:00
|
|
|
return Config::inst()->get('i18n', 'date_format');
|
2010-10-15 05:23:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the default getter for TimeFormat so the
|
|
|
|
* default format for the user's locale is used
|
|
|
|
* if the user has not defined their own.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:23:02 +02:00
|
|
|
* @return string ISO date format
|
|
|
|
*/
|
|
|
|
public function getTimeFormat() {
|
|
|
|
if($this->getField('TimeFormat')) {
|
|
|
|
return $this->getField('TimeFormat');
|
|
|
|
} else {
|
2014-02-11 12:12:37 +01:00
|
|
|
return Config::inst()->get('i18n', 'time_format');
|
2010-10-15 05:23:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
//---------------------------------------------------------------------//
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/**
|
2012-03-06 23:44:38 +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.
|
2007-09-15 02:23:44 +02:00
|
|
|
*
|
2009-11-22 06:30:14 +01:00
|
|
|
* @todo Push all this logic into Member_GroupSet's getIterator()?
|
2007-09-14 21:10:18 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function Groups() {
|
2013-05-22 03:56:36 +02:00
|
|
|
$groups = Member_GroupSet::create('Group', 'Group_Members', 'GroupID', 'MemberID');
|
2012-07-23 00:31:47 +02:00
|
|
|
$groups = $groups->forForeignID($this->ID);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-01 17:54:54 +01:00
|
|
|
$this->extend('updateGroups', $groups);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-11-22 06:30:14 +01:00
|
|
|
return $groups;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-03-06 23:44:38 +01:00
|
|
|
/**
|
|
|
|
* @return ManyManyList
|
|
|
|
*/
|
|
|
|
public function DirectGroups() {
|
|
|
|
return $this->getManyManyComponents('Groups');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-10 03:53:45 +02:00
|
|
|
/**
|
|
|
|
* Get a member SQLMap of members in specific groups
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-10 03:53:45 +02:00
|
|
|
* If no $groups is passed, all members will be returned
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-10 03:53:45 +02:00
|
|
|
* @param mixed $groups - takes a SS_List, an array or a single Group.ID
|
|
|
|
* @return SQLMap Returns an SQLMap that returns all Member data.
|
|
|
|
* @see map()
|
|
|
|
*/
|
|
|
|
public static function map_in_groups($groups = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDList = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-10 03:53:45 +02:00
|
|
|
if($groups instanceof SS_List) {
|
|
|
|
foreach( $groups as $group ) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDList[] = $group->ID;
|
2012-05-10 03:53:45 +02:00
|
|
|
}
|
2007-09-14 21:10:18 +02:00
|
|
|
} elseif(is_array($groups)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDList = $groups;
|
2012-05-10 03:53:45 +02:00
|
|
|
} elseif($groups) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDList[] = $groups;
|
2007-09-14 21:10:18 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-10 03:53:45 +02:00
|
|
|
// No groups, return all Members
|
|
|
|
if(!$groupIDList) {
|
|
|
|
return Member::get()->sort(array('Surname'=>'ASC', 'FirstName'=>'ASC'))->map();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-10 03:53:45 +02:00
|
|
|
$membersList = new ArrayList();
|
|
|
|
// This is a bit ineffective, but follow the ORM style
|
|
|
|
foreach(Group::get()->byIDs($groupIDList) as $group) {
|
|
|
|
$membersList->merge($group->Members());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-10 03:53:45 +02:00
|
|
|
$membersList->removeDuplicates('ID');
|
|
|
|
return $membersList->map();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* 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.
|
2013-06-21 00:32:08 +02:00
|
|
|
* @return SS_Map Returns a map of all members in the groups given that
|
2007-09-14 21:10:18 +02:00
|
|
|
* have CMS permissions.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-14 21:10:18 +02:00
|
|
|
public static function mapInCMSGroups($groups = null) {
|
|
|
|
if(!$groups || $groups->Count() == 0) {
|
2008-08-13 00:14:36 +02:00
|
|
|
$perms = array('ADMIN', 'CMS_ACCESS_AssetAdmin');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-19 12:45:44 +02:00
|
|
|
if(class_exists('CMSMain')) {
|
|
|
|
$cmsPerms = singleton('CMSMain')->providePermissions();
|
|
|
|
} else {
|
|
|
|
$cmsPerms = singleton('LeftAndMain')->providePermissions();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-13 00:14:36 +02:00
|
|
|
if(!empty($cmsPerms)) {
|
|
|
|
$perms = array_unique(array_merge($perms, array_keys($cmsPerms)));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$permsClause = DB::placeholders($perms);
|
2012-09-26 23:34:00 +02:00
|
|
|
$groups = DataObject::get('Group')
|
2013-06-21 00:32:08 +02:00
|
|
|
->innerJoin("Permission", '"Permission"."GroupID" = "Group"."ID"')
|
|
|
|
->where(array(
|
|
|
|
"\"Permission\".\"Code\" IN ($permsClause)" => $perms
|
|
|
|
));
|
2007-09-14 21:10:18 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDList = array();
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2011-10-26 08:09:04 +02:00
|
|
|
if(is_a($groups, 'SS_List')) {
|
2009-02-02 00:49:53 +01:00
|
|
|
foreach($groups as $group) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDList[] = $group->ID;
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2007-09-14 21:10:18 +02:00
|
|
|
} elseif(is_array($groups)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDList = $groups;
|
2007-09-14 21:10:18 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$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(
|
2014-08-15 08:53:05 +02:00
|
|
|
"\"Group\".\"ID\" IN ($groupClause)" => $groupIDList
|
2013-06-21 00:32:08 +02:00
|
|
|
));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
return $members->sort('"Member"."Surname", "Member"."FirstName"')->map();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-09-14 20:23:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* 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,
|
2012-12-08 12:20:20 +01:00
|
|
|
* $this->groups() will be used)
|
2007-09-14 21:10:18 +02:00
|
|
|
* @return array Groups in which the member is NOT in.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-14 21:10:18 +02:00
|
|
|
public function memberNotInGroups($groupList, $memberGroups = null){
|
2009-02-02 00:49:53 +01:00
|
|
|
if(!$memberGroups) $memberGroups = $this->Groups();
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
foreach($memberGroups as $group) {
|
|
|
|
if(in_array($group->Code, $groupList)) {
|
|
|
|
$index = array_search($group->Code, $groupList);
|
2007-07-19 12:40:28 +02:00
|
|
|
unset($groupList[$index]);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $groupList;
|
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Return a {@link FieldList} of fields that would appropriate for editing
|
2007-09-14 21:10:18 +02:00
|
|
|
* this member.
|
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @return FieldList Return a FieldList of fields that would appropriate for
|
2012-12-08 12:20:20 +01:00
|
|
|
* editing this member.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-04-13 15:46:47 +02:00
|
|
|
public function getCMSFields() {
|
2014-02-16 20:36:51 +01:00
|
|
|
require_once 'Zend/Date.php';
|
|
|
|
|
|
|
|
$self = $this;
|
|
|
|
$this->beforeUpdateCMSFields(function($fields) use ($self) {
|
|
|
|
$mainFields = $fields->fieldByName("Root")->fieldByName("Main")->Children;
|
|
|
|
|
|
|
|
$password = new ConfirmedPasswordField(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Password',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
2014-02-16 20:36:51 +01:00
|
|
|
true // showOnClick
|
|
|
|
);
|
|
|
|
$password->setCanBeEmpty(true);
|
|
|
|
if( ! $self->ID) $password->showOnClick = false;
|
|
|
|
$mainFields->replaceField('Password', $password);
|
|
|
|
|
|
|
|
$mainFields->replaceField('Locale', new DropdownField(
|
2014-08-15 08:53:05 +02:00
|
|
|
"Locale",
|
|
|
|
_t('Member.INTERFACELANG', "Interface Language", 'Language of the CMS'),
|
2014-02-16 20:36:51 +01:00
|
|
|
i18n::get_existing_translations()
|
|
|
|
));
|
2010-04-13 03:49:24 +02:00
|
|
|
|
2014-02-16 20:36:51 +01:00
|
|
|
$mainFields->removeByName('RememberLoginToken');
|
|
|
|
$mainFields->removeByName('AutoLoginHash');
|
|
|
|
$mainFields->removeByName('AutoLoginExpired');
|
|
|
|
$mainFields->removeByName('PasswordEncryption');
|
|
|
|
$mainFields->removeByName('PasswordExpiry');
|
|
|
|
$mainFields->removeByName('LockedOutUntil');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-02-16 20:36:51 +01:00
|
|
|
if( ! $self->config()->lock_out_after_incorrect_logins) {
|
|
|
|
$mainFields->removeByName('FailedLoginCount');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-02-16 20:36:51 +01:00
|
|
|
$mainFields->removeByName('Salt');
|
2012-09-28 16:32:16 +02:00
|
|
|
|
2014-02-16 20:36:51 +01:00
|
|
|
// Groups relation will get us into logical conflicts because
|
|
|
|
// Members are displayed within group edit form in SecurityAdmin
|
|
|
|
$fields->removeByName('Groups');
|
2012-08-07 06:40:19 +02:00
|
|
|
|
2014-02-16 20:36:51 +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);
|
|
|
|
$fields->addFieldToTab('Root.Main',
|
|
|
|
ListboxField::create('DirectGroups', singleton('Group')->i18n_plural_name())
|
|
|
|
->setMultiple(true)
|
|
|
|
->setSource($groupsMap)
|
|
|
|
->setAttribute(
|
2014-08-15 08:53:05 +02:00
|
|
|
'data-placeholder',
|
2014-02-16 20:36:51 +01:00
|
|
|
_t('Member.ADDGROUP', 'Add group', 'Placeholder text for a dropdown')
|
|
|
|
)
|
2010-02-22 05:37:36 +01:00
|
|
|
);
|
2012-03-02 18:11:02 +01:00
|
|
|
|
|
|
|
|
2014-02-16 20:36:51 +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.
|
|
|
|
if($self->ID) {
|
|
|
|
$permissionsField = new PermissionCheckboxSetField_Readonly(
|
|
|
|
'Permissions',
|
|
|
|
false,
|
|
|
|
'Permission',
|
|
|
|
'GroupID',
|
|
|
|
// we don't want parent relationships, they're automatically resolved in the field
|
|
|
|
$self->getManyManyComponents('Groups')
|
|
|
|
);
|
|
|
|
$fields->findOrMakeTab('Root.Permissions', singleton('Permission')->i18n_plural_name());
|
|
|
|
$fields->addFieldToTab('Root.Permissions', $permissionsField);
|
|
|
|
}
|
2010-02-22 05:37:36 +01:00
|
|
|
}
|
2012-08-07 06:40:19 +02:00
|
|
|
|
2014-02-16 20:36:51 +01:00
|
|
|
$permissionsTab = $fields->fieldByName("Root")->fieldByName('Permissions');
|
|
|
|
if($permissionsTab) $permissionsTab->addExtraClass('readonly');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-02-16 20:36:51 +01:00
|
|
|
$defaultDateFormat = Zend_Locale_Format::getDateFormat(new Zend_Locale($self->Locale));
|
|
|
|
$dateFormatMap = array(
|
|
|
|
'MMM d, yyyy' => Zend_Date::now()->toString('MMM d, yyyy'),
|
|
|
|
'yyyy/MM/dd' => Zend_Date::now()->toString('yyyy/MM/dd'),
|
|
|
|
'MM/dd/yyyy' => Zend_Date::now()->toString('MM/dd/yyyy'),
|
|
|
|
'dd/MM/yyyy' => Zend_Date::now()->toString('dd/MM/yyyy'),
|
|
|
|
);
|
|
|
|
$dateFormatMap[$defaultDateFormat] = Zend_Date::now()->toString($defaultDateFormat)
|
|
|
|
. sprintf(' (%s)', _t('Member.DefaultDateTime', 'default'));
|
|
|
|
$mainFields->push(
|
|
|
|
$dateFormatField = new MemberDatetimeOptionsetField(
|
|
|
|
'DateFormat',
|
|
|
|
$self->fieldLabel('DateFormat'),
|
|
|
|
$dateFormatMap
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$dateFormatField->setValue($self->DateFormat);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-02-16 20:36:51 +01:00
|
|
|
$defaultTimeFormat = Zend_Locale_Format::getTimeFormat(new Zend_Locale($self->Locale));
|
|
|
|
$timeFormatMap = array(
|
|
|
|
'h:mm a' => Zend_Date::now()->toString('h:mm a'),
|
|
|
|
'H:mm' => Zend_Date::now()->toString('H:mm'),
|
|
|
|
);
|
|
|
|
$timeFormatMap[$defaultTimeFormat] = Zend_Date::now()->toString($defaultTimeFormat)
|
|
|
|
. sprintf(' (%s)', _t('Member.DefaultDateTime', 'default'));
|
|
|
|
$mainFields->push(
|
|
|
|
$timeFormatField = new MemberDatetimeOptionsetField(
|
|
|
|
'TimeFormat',
|
|
|
|
$self->fieldLabel('TimeFormat'),
|
|
|
|
$timeFormatMap
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$timeFormatField->setValue($self->TimeFormat);
|
|
|
|
});
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-02-16 20:36:51 +01:00
|
|
|
return parent::getCMSFields();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 02:07:39 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-29 02:07:39 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function fieldLabels($includerelations = true) {
|
2009-04-29 02:07:39 +02:00
|
|
|
$labels = parent::fieldLabels($includerelations);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 03:49:24 +02:00
|
|
|
$labels['FirstName'] = _t('Member.FIRSTNAME', 'First Name');
|
|
|
|
$labels['Surname'] = _t('Member.SURNAME', 'Surname');
|
|
|
|
$labels['Email'] = _t('Member.EMAIL', 'Email');
|
2008-11-02 21:04:10 +01:00
|
|
|
$labels['Password'] = _t('Member.db_Password', 'Password');
|
2012-04-14 00:16:22 +02:00
|
|
|
$labels['PasswordExpiry'] = _t('Member.db_PasswordExpiry', 'Password Expiry Date', 'Password expiry date');
|
|
|
|
$labels['LockedOutUntil'] = _t('Member.db_LockedOutUntil', 'Locked out until', 'Security related date');
|
2008-11-02 21:04:10 +01:00
|
|
|
$labels['Locale'] = _t('Member.db_Locale', 'Interface Locale');
|
2012-08-15 20:30:30 +02:00
|
|
|
$labels['DateFormat'] = _t('Member.DATEFORMAT', 'Date format');
|
|
|
|
$labels['TimeFormat'] = _t('Member.TIMEFORMAT', 'Time format');
|
2009-04-29 02:07:39 +02:00
|
|
|
if($includerelations){
|
2012-09-26 23:34:00 +02:00
|
|
|
$labels['Groups'] = _t('Member.belongs_many_many_Groups', 'Groups',
|
|
|
|
'Security Groups this member belongs to');
|
2009-04-29 02:07:39 +02:00
|
|
|
}
|
2008-10-03 20:30:27 +02:00
|
|
|
return $labels;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-04 00:33:28 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canView($member = null) {
|
2009-02-04 00:33:28 +01:00
|
|
|
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
// extended access checks
|
2009-02-04 00:33:28 +01:00
|
|
|
$results = $this->extend('canView', $member);
|
2009-12-16 06:39:39 +01:00
|
|
|
if($results && is_array($results)) {
|
|
|
|
if(!min($results)) return false;
|
|
|
|
else return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-04 00:33:28 +01:00
|
|
|
// members can usually edit their own record
|
2009-12-16 06:39:39 +01:00
|
|
|
if($member && $this->ID == $member->ID) return true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-04 00:33:28 +01:00
|
|
|
if(
|
|
|
|
Permission::checkMember($member, 'ADMIN')
|
|
|
|
|| Permission::checkMember($member, 'CMS_ACCESS_SecurityAdmin')
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-04 00:33:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-04 00:33:28 +01:00
|
|
|
/**
|
|
|
|
* Users can edit their own record.
|
|
|
|
* Otherwise they'll need ADMIN or CMS_ACCESS_SecurityAdmin permissions
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canEdit($member = null) {
|
2009-02-04 00:33:28 +01:00
|
|
|
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
// extended access checks
|
2009-02-04 00:33:28 +01:00
|
|
|
$results = $this->extend('canEdit', $member);
|
2009-12-16 06:39:39 +01:00
|
|
|
if($results && is_array($results)) {
|
|
|
|
if(!min($results)) return false;
|
|
|
|
else return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-21 02:43:16 +01:00
|
|
|
// No member found
|
|
|
|
if(!($member && $member->exists())) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 04:46:26 +02:00
|
|
|
// If the requesting member is not an admin, but has access to manage members,
|
|
|
|
// he still can't edit other members with ADMIN permission.
|
|
|
|
// This is a bit weak, strictly speaking he shouldn't be allowed to
|
|
|
|
// perform any action that could change the password on a member
|
2014-08-15 08:53:05 +02:00
|
|
|
// with "higher" permissions than himself, but thats hard to determine.
|
2010-10-19 04:46:26 +02:00
|
|
|
if(!Permission::checkMember($member, 'ADMIN') && Permission::checkMember($this, 'ADMIN')) return false;
|
|
|
|
|
2009-02-04 00:33:28 +01:00
|
|
|
return $this->canView($member);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-04 00:33:28 +01:00
|
|
|
/**
|
|
|
|
* Users can edit their own record.
|
|
|
|
* Otherwise they'll need ADMIN or CMS_ACCESS_SecurityAdmin permissions
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canDelete($member = null) {
|
2009-02-04 00:33:28 +01:00
|
|
|
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser();
|
2012-12-15 20:23:50 +01:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
// extended access checks
|
2009-02-04 00:33:28 +01:00
|
|
|
$results = $this->extend('canDelete', $member);
|
2009-12-16 06:39:39 +01:00
|
|
|
if($results && is_array($results)) {
|
|
|
|
if(!min($results)) return false;
|
|
|
|
else return true;
|
|
|
|
}
|
2012-12-15 20:23:50 +01:00
|
|
|
|
2009-11-21 02:43:16 +01:00
|
|
|
// No member found
|
|
|
|
if(!($member && $member->exists())) return false;
|
2012-12-15 20:23:50 +01:00
|
|
|
|
|
|
|
// Members are not allowed to remove themselves,
|
|
|
|
// since it would create inconsistencies in the admin UIs.
|
|
|
|
if($this->ID && $member->ID == $this->ID) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-04 00:33:28 +01:00
|
|
|
return $this->canEdit($member);
|
2007-11-09 02:10:45 +01:00
|
|
|
}
|
2008-04-26 08:31:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate this member object.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function validate() {
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid = parent::validate();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-27 02:09:23 +02:00
|
|
|
if(!$this->ID || $this->isChanged('Password')) {
|
2008-10-02 04:45:58 +02:00
|
|
|
if($this->Password && self::$password_validator) {
|
2008-04-26 08:31:52 +02:00
|
|
|
$valid->combineAnd(self::$password_validator->validate($this->Password, $this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-27 02:09:23 +02:00
|
|
|
if((!$this->ID && $this->SetPassword) || $this->isChanged('SetPassword')) {
|
2008-10-02 04:45:58 +02:00
|
|
|
if($this->SetPassword && self::$password_validator) {
|
2008-04-26 08:36:03 +02:00
|
|
|
$valid->combineAnd(self::$password_validator->validate($this->SetPassword, $this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
return $valid;
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
2009-11-06 03:23:21 +01:00
|
|
|
/**
|
|
|
|
* Change password. This will cause rehashing according to
|
|
|
|
* the `PasswordEncryption` property.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-11-06 03:23:21 +01:00
|
|
|
* @param String $password Cleartext password
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function changePassword($password) {
|
2008-04-26 08:31:52 +02:00
|
|
|
$this->Password = $password;
|
|
|
|
$valid = $this->validate();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
if($valid->valid()) {
|
|
|
|
$this->AutoLoginHash = null;
|
|
|
|
$this->write();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
return $valid;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:32:05 +02: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.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function registerFailedLogin() {
|
2013-03-21 19:48:54 +01:00
|
|
|
if(self::config()->lock_out_after_incorrect_logins) {
|
2008-04-26 08:32:05 +02:00
|
|
|
// Keep a tally of the number of failed log-ins so that we can lock people out
|
2009-09-10 04:42:26 +02:00
|
|
|
$this->FailedLoginCount = $this->FailedLoginCount + 1;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
if($this->FailedLoginCount >= self::config()->lock_out_after_incorrect_logins) {
|
2013-07-11 01:17:02 +02:00
|
|
|
$lockoutMins = self::config()->lock_out_delay_mins;
|
|
|
|
$this->LockedOutUntil = date('Y-m-d H:i:s', time() + $lockoutMins*60);
|
2013-10-17 19:42:50 +02:00
|
|
|
$this->FailedLoginCount = 0;
|
2008-04-26 08:32:05 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-17 19:16:57 +02:00
|
|
|
$this->extend('registerFailedLogin');
|
|
|
|
$this->write();
|
2008-04-26 08:32:05 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-10 06:32:39 +02:00
|
|
|
/**
|
|
|
|
* Get the HtmlEditorConfig for this user to be used in the CMS.
|
2010-02-22 10:38:15 +01:00
|
|
|
* This is set by the group. If multiple configurations are set,
|
|
|
|
* the one with the highest priority wins.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-08-10 06:32:39 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getHtmlEditorConfigForCMS() {
|
2009-08-10 06:32:39 +02:00
|
|
|
$currentName = '';
|
|
|
|
$currentPriority = 0;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-10 06:32:39 +02:00
|
|
|
foreach($this->Groups() as $group) {
|
|
|
|
$configName = $group->HtmlEditorConfig;
|
2010-02-22 10:38:15 +01:00
|
|
|
if($configName) {
|
|
|
|
$config = HtmlEditorConfig::get($group->HtmlEditorConfig);
|
|
|
|
if($config && $config->getOption('priority') > $currentPriority) {
|
|
|
|
$currentName = $configName;
|
2013-05-22 21:31:19 +02:00
|
|
|
$currentPriority = $config->getOption('priority');
|
2010-02-22 10:38:15 +01:00
|
|
|
}
|
2009-08-10 06:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-10 06:32:39 +02:00
|
|
|
// If can't find a suitable editor, just default to cms
|
|
|
|
return $currentName ? $currentName : 'cms';
|
|
|
|
}
|
2012-02-11 03:08:39 +01:00
|
|
|
|
2012-02-21 01:36:34 +01:00
|
|
|
public static function get_template_global_variables() {
|
2012-02-11 03:08:39 +01:00
|
|
|
return array(
|
|
|
|
'CurrentMember' => 'currentUser',
|
2012-03-16 02:14:45 +01:00
|
|
|
'currentUser',
|
2012-02-11 03:08:39 +01:00
|
|
|
);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-11-22 06:30:14 +01:00
|
|
|
* Represents a set of Groups attached to a member.
|
|
|
|
* Handles the hierarchy logic.
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage security
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-11-22 06:30:14 +01:00
|
|
|
class Member_GroupSet extends ManyManyList {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-14 23:31:03 +02:00
|
|
|
protected function linkJoinTable() {
|
|
|
|
// Do not join the table directly
|
|
|
|
if($this->extraFields) {
|
|
|
|
user_error('Member_GroupSet does not support many_many_extraFields', E_USER_ERROR);
|
|
|
|
}
|
2009-11-22 06:30:14 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-11-22 06:30:14 +01:00
|
|
|
* Link this group set to a specific member.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* Recursively selects all groups applied to this member, as well as any
|
|
|
|
* parent groups of any applied groups
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param array|integer $id (optional) An ID or an array of IDs - if not provided, will use the current
|
|
|
|
* ids as per getForeignID
|
|
|
|
* @return array Condition In array(SQL => parameters format)
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-12-12 05:22:45 +01:00
|
|
|
public function foreignIDFilter($id = null) {
|
|
|
|
if ($id === null) $id = $this->getForeignID();
|
|
|
|
|
2009-11-22 06:30:14 +01:00
|
|
|
// Find directly applied groups
|
2012-12-12 05:22:45 +01:00
|
|
|
$manyManyFilter = parent::foreignIDFilter($id);
|
2013-06-21 00:32:08 +02:00
|
|
|
$query = new SQLSelect('"Group_Members"."GroupID"', '"Group_Members"', $manyManyFilter);
|
|
|
|
$groupIDs = $query->execute()->column();
|
2009-11-22 06:30:14 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Get all ancestors, iteratively merging these into the master set
|
2009-11-22 06:30:14 +01:00
|
|
|
$allGroupIDs = array();
|
|
|
|
while($groupIDs) {
|
|
|
|
$allGroupIDs = array_merge($allGroupIDs, $groupIDs);
|
|
|
|
$groupIDs = DataObject::get("Group")->byIDs($groupIDs)->column("ParentID");
|
|
|
|
$groupIDs = array_filter($groupIDs);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:30:14 +01:00
|
|
|
// Add a filter to this DataList
|
2013-06-21 00:32:08 +02:00
|
|
|
if(!empty($allGroupIDs)) {
|
|
|
|
$allGroupIDsPlaceholders = DB::placeholders($allGroupIDs);
|
|
|
|
return array("\"Group\".\"ID\" IN ($allGroupIDsPlaceholders)" => $allGroupIDs);
|
|
|
|
} else {
|
|
|
|
return array('"Group"."ID"' => 0);
|
2012-12-12 05:22:45 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
public function foreignIDWriteFilter($id = null) {
|
2013-06-21 00:32:08 +02:00
|
|
|
// Use the ManyManyList::foreignIDFilter rather than the one
|
|
|
|
// in this class, otherwise we end up selecting all inherited groups
|
2012-12-12 05:22:45 +01:00
|
|
|
return parent::foreignIDFilter($id);
|
2009-11-22 06:30:14 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
|
|
|
* Class used as template to send an email saying that the password has been
|
2013-10-16 00:29:43 +02:00
|
|
|
* changed.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage security
|
2007-09-14 21:10:18 +02:00
|
|
|
*/
|
2008-06-12 11:29:05 +02:00
|
|
|
class Member_ChangePasswordEmail extends Email {
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
protected $from = ''; // setting a blank from address uses the site's default administrator email
|
|
|
|
protected $subject = '';
|
|
|
|
protected $ss_template = 'ChangePasswordEmail';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
public function __construct() {
|
2009-09-18 05:07:15 +02:00
|
|
|
parent::__construct();
|
2013-10-16 00:29:43 +02:00
|
|
|
|
|
|
|
$this->subject = _t('Member.SUBJECTPASSWORDCHANGED', "Your password has been changed", 'Email subject');
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class used as template to send the forgot password email
|
2013-10-16 00:29:43 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage security
|
2007-09-14 21:10:18 +02:00
|
|
|
*/
|
2008-06-12 11:29:05 +02:00
|
|
|
class Member_ForgotPasswordEmail extends Email {
|
2012-12-08 12:20:20 +01:00
|
|
|
protected $from = ''; // setting a blank from address uses the site's default administrator email
|
|
|
|
protected $subject = '';
|
|
|
|
protected $ss_template = 'ForgotPasswordEmail';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
public function __construct() {
|
2009-09-18 05:07:15 +02:00
|
|
|
parent::__construct();
|
2013-10-16 00:29:43 +02:00
|
|
|
|
|
|
|
$this->subject = _t('Member.SUBJECTPASSWORDRESET', "Your password reset link", 'Email subject');
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
|
|
|
* Member Validator
|
2013-10-16 00:29:43 +02:00
|
|
|
*
|
|
|
|
* Custom validation for the Member object can be achieved either through an
|
|
|
|
* {@link DataExtension} on the Member object or, by specifying a subclass of
|
|
|
|
* {@link Member_Validator} through the {@link Injector} API.
|
|
|
|
*
|
|
|
|
* {@see Member::getValidator()}
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage security
|
2007-09-14 21:10:18 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Member_Validator extends RequiredFields {
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2013-10-16 00:29:43 +02:00
|
|
|
protected $customRequired = array(
|
|
|
|
'FirstName',
|
|
|
|
'Email'
|
|
|
|
);
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function __construct() {
|
|
|
|
$required = func_get_args();
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($required[0]) && is_array($required[0])) {
|
|
|
|
$required = $required[0];
|
|
|
|
}
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$required = array_merge($required, $this->customRequired);
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
parent::__construct($required);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
2007-09-16 18:55:47 +02:00
|
|
|
* Check if the submitted member data is valid (server-side)
|
2007-09-14 21:10:18 +02:00
|
|
|
*
|
|
|
|
* Check if a member with that email doesn't already exist, or if it does
|
|
|
|
* that it is this member.
|
|
|
|
*
|
|
|
|
* @param array $data Submitted data
|
|
|
|
* @return bool Returns TRUE if the submitted data is valid, otherwise
|
|
|
|
* FALSE.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function php($data) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$valid = parent::php($data);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
$identifierField = Member::config()->unique_identifier_field;
|
2013-06-21 00:32:08 +02:00
|
|
|
$member = DataObject::get_one('Member', array(
|
|
|
|
"\"$identifierField\"" => $data[$identifierField]
|
|
|
|
));
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
// if we are in a complex table field popup, use ctf[childID], else use ID
|
|
|
|
if(isset($_REQUEST['ctf']['childID'])) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$id = $_REQUEST['ctf']['childID'];
|
2009-02-02 00:49:53 +01:00
|
|
|
} elseif(isset($_REQUEST['ID'])) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$id = $_REQUEST['ID'];
|
2009-02-02 00:49:53 +01:00
|
|
|
} else {
|
2008-02-25 03:10:37 +01:00
|
|
|
$id = null;
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
|
|
|
if($id && is_object($member) && $member->ID != $id) {
|
2012-10-07 22:26:28 +02:00
|
|
|
$uniqueField = $this->form->Fields()->dataFieldByName($identifierField);
|
2009-02-02 00:49:53 +01:00
|
|
|
$this->validationError(
|
|
|
|
$uniqueField->id(),
|
2012-05-01 21:44:54 +02:00
|
|
|
_t(
|
|
|
|
'Member.VALIDATIONMEMBEREXISTS',
|
|
|
|
'A member already exists with the same %s',
|
|
|
|
array('identifier' => strtolower($identifierField))
|
2009-02-02 00:49:53 +01:00
|
|
|
),
|
|
|
|
'required'
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
$valid = false;
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-16 18:55:47 +02:00
|
|
|
// Execute the validators on the extensions
|
|
|
|
if($this->extension_instances) {
|
|
|
|
foreach($this->extension_instances as $extension) {
|
2009-11-03 03:33:53 +01:00
|
|
|
if(method_exists($extension, 'hasMethod') && $extension->hasMethod('updatePHP')) {
|
2007-09-16 18:55:47 +02:00
|
|
|
$valid &= $extension->updatePHP($data, $this->form);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $valid;
|
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2012-03-09 00:12:33 +01:00
|
|
|
}
|