mirror of
https://github.com/Mibew/mibew.git
synced 2025-01-31 13:24:41 +03:00
Refactor update_operator function
This commit is contained in:
parent
614f02a108
commit
05f5a606d1
@ -204,18 +204,29 @@ class ProfileController extends AbstractController
|
|||||||
return $this->redirect($redirect_to);
|
return $this->redirect($redirect_to);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update existing operator
|
// Mix old operator's fields with updated values
|
||||||
update_operator($op_id, $login, $email, $password, $local_name, $common_name, $code);
|
$target_operator = array(
|
||||||
|
'vclogin' => $login,
|
||||||
|
'vcemail' => $email,
|
||||||
|
'vclocalename' => $local_name,
|
||||||
|
'vccommonname' => $common_name,
|
||||||
|
'code' => $code,
|
||||||
|
) + operator_by_id($op_id);
|
||||||
|
// Set the password only if it's not an empty string.
|
||||||
|
if ($password !== '') {
|
||||||
|
$target_operator['vcpassword'] = calculate_password_hash($login, $password);
|
||||||
|
}
|
||||||
|
// Update operator's fields in the database.
|
||||||
|
update_operator($target_operator);
|
||||||
|
|
||||||
// Operator data are cached in the authentication manager, thus we need
|
// Operator's data are cached in the authentication manager, thus we need
|
||||||
// to manually update them.
|
// to manually update them.
|
||||||
if (!empty($password) && $op_id == $operator['operatorid']) {
|
if ($target_operator['operatorid'] == $operator['operatorid']) {
|
||||||
// Check if the admin has set his password for the first time.
|
// Check if the admin has set his password for the first time.
|
||||||
$to_dashboard = check_password_hash($login, '', $operator['vcpassword']) && $password != '';
|
$to_dashboard = check_password_hash($login, '', $operator['vcpassword']) && $password != '';
|
||||||
|
|
||||||
// Update operator's password.
|
// Update operator's fields.
|
||||||
$operator['vcpassword'] = calculate_password_hash($login, $password);
|
$this->getAuthenticationManager()->setOperator($target_operator);
|
||||||
$this->getAuthenticationManager()->setOperator($operator);
|
|
||||||
|
|
||||||
// Redirect the admin to the home page if needed.
|
// Redirect the admin to the home page if needed.
|
||||||
if ($to_dashboard) {
|
if ($to_dashboard) {
|
||||||
|
@ -277,45 +277,55 @@ function operator_is_disabled($operator)
|
|||||||
/**
|
/**
|
||||||
* Update existing operator's info.
|
* Update existing operator's info.
|
||||||
*
|
*
|
||||||
* If $password argument is empty operators password will not be changed.
|
* @param array $operator Associative array of operator's fields. This array
|
||||||
|
* must contain the following keys:
|
||||||
|
* - operatorid,
|
||||||
|
* - vclogin,
|
||||||
|
* - vcpassword,
|
||||||
|
* - vclocalename,
|
||||||
|
* - vccommonname,
|
||||||
|
* - vcemail,
|
||||||
|
* - dtmlastvisited,
|
||||||
|
* - istatus,
|
||||||
|
* - idisabled,
|
||||||
|
* - vcavatar,
|
||||||
|
* - iperm,
|
||||||
|
* - dtmrestore,
|
||||||
|
* - vcrestoretoken,
|
||||||
|
* - code
|
||||||
*
|
*
|
||||||
* @param int $operator_id ID of operator to update
|
* @throws \InvalidArgumentException if not all operator's fields are in place.
|
||||||
* @param string $login Operator's login
|
|
||||||
* @param string $email Operator's
|
|
||||||
* @param string $password Operator's password
|
|
||||||
* @param string $locale_name Operator's local name
|
|
||||||
* @param string $common_name Operator's international name
|
|
||||||
* @param string $code Operator's code which use to start chat with specified
|
|
||||||
* operator
|
|
||||||
*/
|
*/
|
||||||
function update_operator(
|
function update_operator($operator)
|
||||||
$operator_id,
|
{
|
||||||
$login,
|
if (!check_operator_fields($operator)) {
|
||||||
$email,
|
throw new \InvalidArgumentException('Not all operator fields are specified');
|
||||||
$password,
|
|
||||||
$locale_name,
|
|
||||||
$common_name,
|
|
||||||
$code
|
|
||||||
) {
|
|
||||||
$db = Database::getInstance();
|
|
||||||
$values = array(
|
|
||||||
':login' => $login,
|
|
||||||
':localname' => $locale_name,
|
|
||||||
':commonname' => $common_name,
|
|
||||||
':email' => $email,
|
|
||||||
':operatorid' => $operator_id,
|
|
||||||
':code' => $code,
|
|
||||||
);
|
|
||||||
if ($password) {
|
|
||||||
$values[':password'] = calculate_password_hash($login, $password);
|
|
||||||
}
|
}
|
||||||
$db->query(
|
|
||||||
("UPDATE {operator} SET vclogin = :login, "
|
Database::getInstance()->query(
|
||||||
. ($password ? " vcpassword=:password, " : "")
|
('UPDATE {operator} SET vclogin = :login, vcpassword=:password, '
|
||||||
. "vclocalename = :localname, vccommonname = :commonname, "
|
. 'vclocalename = :local_name, vccommonname = :common_name, '
|
||||||
. "vcemail = :email, code = :code "
|
. 'vcemail = :email, dtmlastvisited = :last_visited, '
|
||||||
. "WHERE operatorid = :operatorid"),
|
. 'istatus = :status, idisabled = :disabled, vcavatar = :avatar, '
|
||||||
$values
|
. 'iperm = :permissions, dtmrestore = :restore_time, '
|
||||||
|
. 'vcrestoretoken = :restore_token, code = :code '
|
||||||
|
. 'WHERE operatorid = :id'),
|
||||||
|
array(
|
||||||
|
':id' => $operator['operatorid'],
|
||||||
|
':login' => $operator['vclogin'],
|
||||||
|
':password' => $operator['vcpassword'],
|
||||||
|
':local_name' => $operator['vclocalename'],
|
||||||
|
':common_name' => $operator['vccommonname'],
|
||||||
|
':email' => $operator['vcemail'],
|
||||||
|
':last_visited' => $operator['dtmlastvisited'],
|
||||||
|
':status' => $operator['istatus'],
|
||||||
|
':disabled' => $operator['idisabled'],
|
||||||
|
':avatar' => $operator['vcavatar'],
|
||||||
|
':permissions' => $operator['iperm'],
|
||||||
|
':restore_time' => $operator['dtmrestore'],
|
||||||
|
':restore_token' => $operator['vcrestoretoken'],
|
||||||
|
':code' => $operator['code'],
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -792,3 +802,31 @@ function enable_operator($operator_id)
|
|||||||
array('0', $operator_id)
|
array('0', $operator_id)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks that operator array has all needed fields.
|
||||||
|
*
|
||||||
|
* @param array $operator Associative operator's array.
|
||||||
|
* @return bool Boolean true if all the fields are in place and false otherwise.
|
||||||
|
*/
|
||||||
|
function check_operator_fields($operator)
|
||||||
|
{
|
||||||
|
$obligatory_fields = array(
|
||||||
|
'operatorid',
|
||||||
|
'vclogin',
|
||||||
|
'vcpassword',
|
||||||
|
'vclocalename',
|
||||||
|
'vccommonname',
|
||||||
|
'vcemail',
|
||||||
|
'dtmlastvisited',
|
||||||
|
'istatus',
|
||||||
|
'idisabled',
|
||||||
|
'vcavatar',
|
||||||
|
'iperm',
|
||||||
|
'dtmrestore',
|
||||||
|
'vcrestoretoken',
|
||||||
|
'code',
|
||||||
|
);
|
||||||
|
|
||||||
|
return (count(array_diff($obligatory_fields, array_keys($operator))) == 0);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user