2007-09-11 02:26:55 +02:00
|
|
|
<?php
|
2013-05-10 10:23:06 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DataObjectInterface;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
/**
|
2012-06-26 15:03:11 +02:00
|
|
|
* Two masked input fields, checks for matching passwords.
|
2013-05-10 10:23:06 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* Optionally hides the fields by default and shows a link to toggle their
|
2013-05-10 10:23:06 +02:00
|
|
|
* visibility.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
2007-09-11 02:26:55 +02:00
|
|
|
*/
|
|
|
|
class ConfirmedPasswordField extends FormField {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Minimum character length of the password.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2008-04-26 08:35:50 +02:00
|
|
|
public $minLength = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Maximum character length of the password.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2008-04-26 08:35:50 +02:00
|
|
|
public $maxLength = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Enforces at least one digit and one alphanumeric
|
|
|
|
* character (in addition to {$minLength} and {$maxLength}
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2007-09-11 02:26:55 +02:00
|
|
|
public $requireStrongPassword = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Allow empty fields in serverside validation
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2007-09-11 02:26:55 +02:00
|
|
|
public $canBeEmpty = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* If set to TRUE, the "password" and "confirm password" form fields will
|
|
|
|
* be hidden via CSS and JavaScript by default, and triggered by a link.
|
2013-05-10 10:23:06 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* An additional hidden field determines if showing the fields has been
|
2013-05-10 10:23:06 +02:00
|
|
|
* triggered and just validates/saves the input in this case.
|
|
|
|
*
|
2008-11-22 04:33:00 +01:00
|
|
|
* This behaviour works unobtrusively, without JavaScript enabled
|
|
|
|
* the fields show, validate and save by default.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-11-22 04:33:00 +01:00
|
|
|
* @param boolean $showOnClick
|
2007-12-19 02:45:22 +01:00
|
|
|
*/
|
|
|
|
protected $showOnClick = false;
|
2014-01-07 05:16:58 +01:00
|
|
|
|
2016-05-13 06:23:38 +02:00
|
|
|
/**
|
|
|
|
* Check if the existing password should be entered first
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $requireExistingPassword = false;
|
|
|
|
|
2014-01-07 05:16:58 +01:00
|
|
|
|
|
|
|
/**
|
2016-05-13 06:23:38 +02:00
|
|
|
* A place to temporarily store the confirm password value
|
|
|
|
*
|
2014-01-07 05:16:58 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $confirmValue;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-13 06:23:38 +02:00
|
|
|
/**
|
|
|
|
* Store value of "Current Password" field
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $currentPasswordValue;
|
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
2013-05-10 10:23:06 +02:00
|
|
|
* Title for the link that triggers the visibility of password fields.
|
2007-12-19 02:45:22 +01:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2008-11-22 04:33:00 +01:00
|
|
|
public $showOnClickTitle;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-24 16:25:13 +02:00
|
|
|
/**
|
|
|
|
* Child fields (_Password, _ConfirmPassword)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-04-24 16:25:13 +02:00
|
|
|
* @var FieldList
|
|
|
|
*/
|
2013-08-21 08:54:05 +02:00
|
|
|
public $children;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-03-21 11:12:23 +01:00
|
|
|
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_STRUCTURAL;
|
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param string $title
|
|
|
|
* @param mixed $value
|
|
|
|
* @param Form $form
|
|
|
|
* @param boolean $showOnClick
|
2008-04-09 13:29:29 +02:00
|
|
|
* @param string $titleConfirmField Alternate title (not localizeable)
|
2007-12-19 02:45:22 +01:00
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
public function __construct($name, $title = null, $value = "", $form = null, $showOnClick = false,
|
|
|
|
$titleConfirmField = null) {
|
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
// naming with underscores to prevent values from actually being saved somewhere
|
2011-05-11 09:51:54 +02:00
|
|
|
$this->children = new FieldList(
|
2008-04-09 13:29:29 +02:00
|
|
|
new PasswordField(
|
2014-08-15 08:53:05 +02:00
|
|
|
"{$name}[_Password]",
|
2010-12-08 23:07:37 +01:00
|
|
|
(isset($title)) ? $title : _t('Member.PASSWORD', 'Password')
|
|
|
|
),
|
2008-04-09 13:29:29 +02:00
|
|
|
new PasswordField(
|
|
|
|
"{$name}[_ConfirmPassword]",
|
|
|
|
(isset($titleConfirmField)) ? $titleConfirmField : _t('Member.CONFIRMPASSWORD', 'Confirm Password')
|
|
|
|
)
|
2007-09-11 02:26:55 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
// has to be called in constructor because Field() isn't triggered upon saving the instance
|
|
|
|
if($showOnClick) {
|
2007-12-19 02:45:22 +01:00
|
|
|
$this->children->push(new HiddenField("{$name}[_PasswordFieldVisible]"));
|
|
|
|
}
|
2013-05-10 10:32:59 +02:00
|
|
|
|
|
|
|
// disable auto complete
|
|
|
|
foreach($this->children as $child) {
|
2016-05-13 06:23:38 +02:00
|
|
|
/** @var FormField $child */
|
2013-05-10 10:32:59 +02:00
|
|
|
$child->setAttribute('autocomplete', 'off');
|
|
|
|
}
|
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
$this->showOnClick = $showOnClick;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
// we have labels for the subfields
|
|
|
|
$title = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-13 06:23:38 +02:00
|
|
|
parent::__construct($name, $title);
|
2009-06-27 06:00:10 +02:00
|
|
|
$this->setValue($value);
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 10:23:06 +02:00
|
|
|
/**
|
|
|
|
* @param array $properties
|
|
|
|
*
|
2016-06-03 10:51:02 +02:00
|
|
|
* @return string
|
2013-05-10 10:23:06 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
|
2016-04-19 11:04:43 +02:00
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/client/dist/js/ConfirmedPasswordField.js');
|
|
|
|
Requirements::css(FRAMEWORK_DIR . '/client/dist/styles/ConfirmedPasswordField.css');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
$content = '';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
if($this->showOnClick) {
|
2008-11-22 04:33:00 +01:00
|
|
|
if($this->showOnClickTitle) {
|
|
|
|
$title = $this->showOnClickTitle;
|
|
|
|
} else {
|
|
|
|
$title = _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'ConfirmedPasswordField.SHOWONCLICKTITLE',
|
|
|
|
'Change Password',
|
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
'Label of the link which triggers display of the "change password" formfields'
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
$content .= "<div class=\"showOnClick\">\n";
|
2012-03-07 17:45:14 +01:00
|
|
|
$content .= "<a href=\"#\">{$title}</a>\n";
|
2007-12-19 02:45:22 +01:00
|
|
|
$content .= "<div class=\"showOnClickContainer\">";
|
|
|
|
}
|
2008-11-22 04:33:00 +01:00
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
foreach($this->children as $field) {
|
2016-05-13 06:23:38 +02:00
|
|
|
/** @var FormField $field */
|
2014-08-15 08:53:05 +02:00
|
|
|
$field->setDisabled($this->isDisabled());
|
2009-04-29 01:55:53 +02:00
|
|
|
$field->setReadonly($this->isReadonly());
|
2013-05-10 10:32:59 +02:00
|
|
|
|
2012-12-11 22:57:45 +01:00
|
|
|
if(count($this->attributes)) {
|
|
|
|
foreach($this->attributes as $name => $value) {
|
|
|
|
$field->setAttribute($name, $value);
|
|
|
|
}
|
|
|
|
}
|
2013-05-10 10:32:59 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
$content .= $field->FieldHolder();
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->showOnClick) {
|
|
|
|
$content .= "</div>\n";
|
|
|
|
$content .= "</div>\n";
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
return $content;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-17 10:29:21 +02:00
|
|
|
/**
|
|
|
|
* Returns the children of this field for use in templating.
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getChildren() {
|
|
|
|
return $this->children;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
/**
|
2013-05-10 10:23:06 +02:00
|
|
|
* Can be empty is a flag that turns on / off empty field checking.
|
|
|
|
*
|
2007-09-11 02:26:55 +02:00
|
|
|
* For example, set this to false (the default) when creating a user account,
|
2013-05-10 10:23:06 +02:00
|
|
|
* and true when displaying on an edit form.
|
|
|
|
*
|
|
|
|
* @param boolean $value
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-10 10:23:06 +02:00
|
|
|
* @return ConfirmedPasswordField
|
2007-09-11 02:26:55 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setCanBeEmpty($value) {
|
2007-09-11 02:26:55 +02:00
|
|
|
$this->canBeEmpty = (bool)$value;
|
2013-05-10 10:23:06 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* The title on the link which triggers display of the "password" and
|
|
|
|
* "confirm password" formfields. Only used if {@link setShowOnClick()}
|
2013-05-10 10:23:06 +02:00
|
|
|
* is set to TRUE.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-10 10:23:06 +02:00
|
|
|
* @param string $title
|
|
|
|
*
|
|
|
|
* @return ConfirmedPasswordField
|
2008-11-22 04:33:00 +01:00
|
|
|
*/
|
|
|
|
public function setShowOnClickTitle($title) {
|
|
|
|
$this->showOnClickTitle = $title;
|
2013-05-10 10:23:06 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-11-22 04:33:00 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
/**
|
2013-05-10 10:23:06 +02:00
|
|
|
* @return string $title
|
2008-11-22 04:33:00 +01:00
|
|
|
*/
|
|
|
|
public function getShowOnClickTitle() {
|
|
|
|
return $this->showOnClickTitle;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 10:23:06 +02:00
|
|
|
/**
|
|
|
|
* @param string $title
|
|
|
|
*
|
|
|
|
* @return ConfirmedPasswordField
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setRightTitle($title) {
|
2007-09-11 02:26:55 +02:00
|
|
|
foreach($this->children as $field) {
|
2016-05-13 06:23:38 +02:00
|
|
|
/** @var FormField $field */
|
2007-09-11 02:26:55 +02:00
|
|
|
$field->setRightTitle($title);
|
|
|
|
}
|
2013-05-10 10:23:06 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-23 09:47:45 +02:00
|
|
|
/**
|
2016-05-13 06:23:38 +02:00
|
|
|
* Set child field titles. Titles in order should be:
|
|
|
|
* - "Current Password" (if getRequireExistingPassword() is set)
|
|
|
|
* - "Password"
|
|
|
|
* - "Confirm Password"
|
2013-05-10 10:23:06 +02:00
|
|
|
*
|
2016-05-13 06:23:38 +02:00
|
|
|
* @param array $titles List of child titles
|
|
|
|
* @return $this
|
2009-06-23 09:47:45 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setChildrenTitles($titles) {
|
2016-05-13 06:23:38 +02:00
|
|
|
$expectedChildren = $this->getRequireExistingPassword() ? 3 : 2;
|
|
|
|
if(is_array($titles) && count($titles) == $expectedChildren) {
|
2009-06-23 09:46:12 +02:00
|
|
|
foreach($this->children as $field) {
|
2013-05-10 10:23:06 +02:00
|
|
|
if(isset($titles[0])) {
|
2016-05-13 06:23:38 +02:00
|
|
|
/** @var FormField $field */
|
2009-06-23 09:46:12 +02:00
|
|
|
$field->setTitle($titles[0]);
|
2013-05-10 10:23:06 +02:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
array_shift($titles);
|
2009-06-23 09:46:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-10 10:23:06 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2009-06-23 09:46:12 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Value is sometimes an array, and sometimes a single value, so we need
|
2013-05-10 10:23:06 +02:00
|
|
|
* to handle both cases.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
2016-05-13 06:23:38 +02:00
|
|
|
* @param mixed $data
|
|
|
|
* @return $this
|
2007-09-11 02:26:55 +02:00
|
|
|
*/
|
2013-06-20 04:08:46 +02:00
|
|
|
public function setValue($value, $data = null) {
|
|
|
|
// If $data is a DataObject, don't use the value, since it's a hashed value
|
|
|
|
if ($data && $data instanceof DataObject) $value = '';
|
|
|
|
|
2013-10-21 21:31:40 +02:00
|
|
|
//store this for later
|
|
|
|
$oldValue = $this->value;
|
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
if(is_array($value)) {
|
2014-01-07 05:16:58 +01:00
|
|
|
$this->value = $value['_Password'];
|
|
|
|
$this->confirmValue = $value['_ConfirmPassword'];
|
2016-05-13 06:23:38 +02:00
|
|
|
$this->currentPasswordValue = ($this->getRequireExistingPassword() && isset($value['_CurrentPassword']))
|
|
|
|
? $value['_CurrentPassword']
|
|
|
|
: null;
|
2013-05-10 10:23:06 +02:00
|
|
|
|
|
|
|
if($this->showOnClick && isset($value['_PasswordFieldVisible'])) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->children->fieldByName($this->getName() . '[_PasswordFieldVisible]')
|
|
|
|
->setValue($value['_PasswordFieldVisible']);
|
2008-05-24 03:22:27 +02:00
|
|
|
}
|
2007-09-11 02:26:55 +02:00
|
|
|
} else {
|
2011-03-18 23:10:34 +01:00
|
|
|
if($value || (!$value && $this->canBeEmpty)) {
|
2007-09-11 02:26:55 +02:00
|
|
|
$this->value = $value;
|
2015-11-18 14:11:31 +01:00
|
|
|
$this->confirmValue = $value;
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-10 10:23:06 +02:00
|
|
|
|
2013-10-21 21:31:40 +02:00
|
|
|
//looking up field by name is expensive, so lets check it needs to change
|
|
|
|
if ($oldValue != $this->value) {
|
|
|
|
$this->children->fieldByName($this->getName() . '[_Password]')
|
|
|
|
->setValue($this->value);
|
2013-05-10 10:23:06 +02:00
|
|
|
|
2013-10-21 21:31:40 +02:00
|
|
|
$this->children->fieldByName($this->getName() . '[_ConfirmPassword]')
|
2015-11-18 14:11:31 +01:00
|
|
|
->setValue($this->confirmValue);
|
2013-10-21 21:31:40 +02:00
|
|
|
}
|
2012-02-17 13:35:26 +01:00
|
|
|
|
|
|
|
return $this;
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
|
|
|
|
2014-01-07 05:16:58 +01:00
|
|
|
/**
|
|
|
|
* Update the names of the child fields when updating name of field.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-01-07 05:16:58 +01:00
|
|
|
* @param string $name new name to give to the field.
|
2016-05-13 06:23:38 +02:00
|
|
|
* @return $this
|
2014-01-07 05:16:58 +01:00
|
|
|
*/
|
|
|
|
public function setName($name) {
|
|
|
|
$this->children->fieldByName($this->getName() . '[_Password]')
|
|
|
|
->setName($name . '[_Password]');
|
|
|
|
$this->children->fieldByName($this->getName() . '[_ConfirmPassword]')
|
|
|
|
->setName($name . '[_ConfirmPassword]');
|
|
|
|
|
|
|
|
return parent::setName($name);
|
|
|
|
}
|
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Determines if the field was actually shown on the client side - if not,
|
2007-12-19 02:45:22 +01:00
|
|
|
* we don't validate or save it.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-10 10:23:06 +02:00
|
|
|
* @return boolean
|
2007-12-19 02:45:22 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isSaveable() {
|
2011-10-29 06:01:52 +02:00
|
|
|
$isVisible = $this->children->fieldByName($this->getName() . '[_PasswordFieldVisible]');
|
2013-05-10 10:23:06 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
return (!$this->showOnClick || ($this->showOnClick && $isVisible && $isVisible->Value()));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 10:23:06 +02:00
|
|
|
/**
|
2014-11-12 03:19:12 +01:00
|
|
|
* Validate this field
|
2013-05-10 10:23:06 +02:00
|
|
|
*
|
2014-11-12 03:19:12 +01:00
|
|
|
* @param Validator $validator
|
|
|
|
* @return bool
|
2013-05-10 10:23:06 +02:00
|
|
|
*/
|
2015-05-11 02:32:00 +02:00
|
|
|
public function validate($validator) {
|
2007-09-11 02:26:55 +02:00
|
|
|
$name = $this->name;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
// if field isn't visible, don't validate
|
2013-05-10 10:23:06 +02:00
|
|
|
if(!$this->isSaveable()) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
$passwordField = $this->children->fieldByName($name.'[_Password]');
|
|
|
|
$passwordConfirmField = $this->children->fieldByName($name.'[_ConfirmPassword]');
|
2014-01-07 05:16:58 +01:00
|
|
|
$passwordField->setValue($this->value);
|
|
|
|
$passwordConfirmField->setValue($this->confirmValue);
|
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
$value = $passwordField->Value();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
// both password-fields should be the same
|
2007-12-19 02:45:22 +01:00
|
|
|
if($value != $passwordConfirmField->Value()) {
|
2013-05-10 10:23:06 +02:00
|
|
|
$validator->validationError(
|
2014-08-15 08:53:05 +02:00
|
|
|
$name,
|
2013-05-10 10:23:06 +02:00
|
|
|
_t('Form.VALIDATIONPASSWORDSDONTMATCH',"Passwords don't match"),
|
2016-05-13 06:23:38 +02:00
|
|
|
"validation"
|
2013-05-10 10:23:06 +02:00
|
|
|
);
|
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$this->canBeEmpty) {
|
|
|
|
// both password-fields shouldn't be empty
|
2007-12-19 02:52:11 +01:00
|
|
|
if(!$value || !$passwordConfirmField->Value()) {
|
2013-05-10 10:23:06 +02:00
|
|
|
$validator->validationError(
|
2014-08-15 08:53:05 +02:00
|
|
|
$name,
|
2013-05-10 10:23:06 +02:00
|
|
|
_t('Form.VALIDATIONPASSWORDSNOTEMPTY', "Passwords can't be empty"),
|
2016-05-13 06:23:38 +02:00
|
|
|
"validation"
|
2013-05-10 10:23:06 +02:00
|
|
|
);
|
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
// lengths
|
2007-12-19 02:45:22 +01:00
|
|
|
if(($this->minLength || $this->maxLength)) {
|
2016-05-13 06:23:38 +02:00
|
|
|
$errorMsg = null;
|
|
|
|
$limit = null;
|
2007-09-11 02:26:55 +02:00
|
|
|
if($this->minLength && $this->maxLength) {
|
2008-09-12 07:40:55 +02:00
|
|
|
$limit = "{{$this->minLength},{$this->maxLength}}";
|
2012-05-01 21:44:54 +02:00
|
|
|
$errorMsg = _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'ConfirmedPasswordField.BETWEEN',
|
|
|
|
'Passwords must be {min} to {max} characters long.',
|
2012-05-01 21:44:54 +02:00
|
|
|
array('min' => $this->minLength, 'max' => $this->maxLength)
|
|
|
|
);
|
2007-09-11 02:26:55 +02:00
|
|
|
} elseif($this->minLength) {
|
2008-09-12 07:40:55 +02:00
|
|
|
$limit = "{{$this->minLength}}.*";
|
2012-05-01 21:44:54 +02:00
|
|
|
$errorMsg = _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'ConfirmedPasswordField.ATLEAST',
|
|
|
|
'Passwords must be at least {min} characters long.',
|
2012-05-01 21:44:54 +02:00
|
|
|
array('min' => $this->minLength)
|
|
|
|
);
|
2007-09-11 02:26:55 +02:00
|
|
|
} elseif($this->maxLength) {
|
2008-09-12 07:40:55 +02:00
|
|
|
$limit = "{0,{$this->maxLength}}";
|
2012-05-01 21:44:54 +02:00
|
|
|
$errorMsg = _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'ConfirmedPasswordField.MAXIMUM',
|
|
|
|
'Passwords must be at most {max} characters long.',
|
2012-05-01 21:44:54 +02:00
|
|
|
array('max' => $this->maxLength)
|
|
|
|
);
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
2008-09-12 07:40:55 +02:00
|
|
|
$limitRegex = '/^.' . $limit . '$/';
|
2007-12-19 02:45:22 +01:00
|
|
|
if(!empty($value) && !preg_match($limitRegex,$value)) {
|
2013-10-21 21:31:40 +02:00
|
|
|
$validator->validationError(
|
|
|
|
$name,
|
|
|
|
$errorMsg,
|
2016-05-13 06:23:38 +02:00
|
|
|
"validation"
|
2007-09-11 02:26:55 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
if($this->requireStrongPassword) {
|
2007-12-19 02:45:22 +01:00
|
|
|
if(!preg_match('/^(([a-zA-Z]+\d+)|(\d+[a-zA-Z]+))[a-zA-Z0-9]*$/',$value)) {
|
2007-09-11 02:26:55 +02:00
|
|
|
$validator->validationError(
|
2013-10-21 21:31:40 +02:00
|
|
|
$name,
|
2012-09-26 23:34:00 +02:00
|
|
|
_t('Form.VALIDATIONSTRONGPASSWORD',
|
2014-08-15 08:53:05 +02:00
|
|
|
"Passwords must have at least one digit and one alphanumeric character"),
|
2016-05-13 06:23:38 +02:00
|
|
|
"validation"
|
2007-09-11 02:26:55 +02:00
|
|
|
);
|
2013-05-10 10:23:06 +02:00
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-05-10 10:23:06 +02:00
|
|
|
|
2016-05-13 06:23:38 +02:00
|
|
|
// Check if current password is valid
|
|
|
|
if(!empty($value) && $this->getRequireExistingPassword()) {
|
|
|
|
if(!$this->currentPasswordValue) {
|
|
|
|
$validator->validationError(
|
|
|
|
$name,
|
|
|
|
_t(
|
|
|
|
'ConfirmedPasswordField.CURRENT_PASSWORD_MISSING',
|
|
|
|
"You must enter your current password."
|
|
|
|
),
|
|
|
|
"validation"
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check this password is valid for the current user
|
|
|
|
$member = Member::currentUser();
|
|
|
|
if(!$member) {
|
|
|
|
$validator->validationError(
|
|
|
|
$name,
|
|
|
|
_t(
|
|
|
|
'ConfirmedPasswordField.LOGGED_IN_ERROR',
|
|
|
|
"You must be logged in to change your password."
|
|
|
|
),
|
|
|
|
"validation"
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// With a valid user and password, check the password is correct
|
|
|
|
$checkResult = $member->checkPassword($this->currentPasswordValue);
|
|
|
|
if(!$checkResult->valid()) {
|
|
|
|
$validator->validationError(
|
|
|
|
$name,
|
|
|
|
_t(
|
|
|
|
'ConfirmedPasswordField.CURRENT_PASSWORD_ERROR',
|
|
|
|
"The current password you have entered is not correct."
|
|
|
|
),
|
|
|
|
"validation"
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
2013-05-10 10:23:06 +02:00
|
|
|
* Only save if field was shown on the client, and is not empty.
|
|
|
|
*
|
|
|
|
* @param DataObjectInterface $record
|
2007-12-19 02:45:22 +01:00
|
|
|
*
|
2013-05-10 10:23:06 +02:00
|
|
|
* @return boolean
|
2007-12-19 02:45:22 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function saveInto(DataObjectInterface $record) {
|
2013-05-10 10:23:06 +02:00
|
|
|
if(!$this->isSaveable()) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-11-12 03:58:27 +01:00
|
|
|
if(!($this->canBeEmpty && !$this->value)) {
|
|
|
|
parent::saveInto($record);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-11-12 03:58:27 +01:00
|
|
|
/**
|
2013-05-10 10:23:06 +02:00
|
|
|
* Makes a read only field with some stars in it to replace the password
|
|
|
|
*
|
|
|
|
* @return ReadonlyField
|
2007-11-12 03:58:27 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2012-12-13 13:51:28 +01:00
|
|
|
$field = $this->castedCopy('ReadonlyField')
|
|
|
|
->setTitle($this->title ? $this->title : _t('Member.PASSWORD'))
|
|
|
|
->setValue('*****');
|
2007-11-12 03:58:27 +01:00
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
2016-05-13 06:23:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if existing password is required
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getRequireExistingPassword() {
|
|
|
|
return $this->requireExistingPassword;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set if the existing password should be required
|
|
|
|
*
|
|
|
|
* @param bool $show Flag to show or hide this field
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setRequireExistingPassword($show) {
|
|
|
|
// Don't modify if already added / removed
|
|
|
|
if((bool)$show === $this->requireExistingPassword) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
$this->requireExistingPassword = $show;
|
|
|
|
$name = $this->getName();
|
|
|
|
$currentName = "{$name}[_CurrentPassword]";
|
|
|
|
if ($show) {
|
|
|
|
$confirmField = PasswordField::create($currentName, _t('Member.CURRENT_PASSWORD', 'Current Password'));
|
|
|
|
$this->children->unshift($confirmField);
|
|
|
|
} else {
|
|
|
|
$this->children->removeByName($currentName, true);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
2007-11-12 03:58:27 +01:00
|
|
|
}
|