Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validations added for 'password' and 'email' fields for the 'employees' API. #286

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions classes/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class EmployeeCore extends ObjectModel

protected $webserviceParameters = array(
'fields' => array(
'email' => array('setter' => 'setWsEmail'),
'id_lang' => array('xlink_resource' => 'languages'),
'last_passwd_gen' => array('setter' => null),
'stats_date_from' => array('setter' => null),
Expand Down Expand Up @@ -344,15 +345,41 @@ public function isLastAdmin()
);
}

// validate and set email for the employee
public function setWsEmail($email)
{
if (Validate::isEmail($email)
&& Employee::employeeExists($email)
&& (!$this->email || ($employee = new Employee((int)$this->id)) && $employee->email != $email)
) {
WebserviceRequest::getInstance()->setError(400, 'An account already exists for this email address: '.$email, 134);

return false;
}
$this->email = $email;

return true;
}

// validate and set password for the employee
public function setWsPasswd($passwd)
{
if ($this->id != 0) {
if ($this->passwd != $passwd) {
$this->passwd = Tools::encrypt($passwd);
if (!Validate::isPasswd($passwd, Validate::ADMIN_PASSWORD_LENGTH)) {
WebserviceRequest::getInstance()->setError(400, 'The password must be at least '.Validate::ADMIN_PASSWORD_LENGTH.' characters long.', 134);
} else {
$this->passwd = Tools::encrypt($passwd);
}
}
} else {
$this->passwd = Tools::encrypt($passwd);
if (!Validate::isPasswd($passwd, Validate::ADMIN_PASSWORD_LENGTH)) {
WebserviceRequest::getInstance()->setError(400, 'The password must be at least '.Validate::ADMIN_PASSWORD_LENGTH.' characters long.', 134);
} else {
$this->passwd = Tools::encrypt($passwd);
}
}

return true;
}

Expand Down