From 05f5a606d1f2896028dad730f3026fb7ce3c75b5 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Tue, 28 Oct 2014 11:45:36 +0000 Subject: [PATCH] Refactor update_operator function --- .../Controller/Operator/ProfileController.php | 25 ++-- src/mibew/libs/operator.php | 110 ++++++++++++------ 2 files changed, 92 insertions(+), 43 deletions(-) diff --git a/src/mibew/libs/classes/Mibew/Controller/Operator/ProfileController.php b/src/mibew/libs/classes/Mibew/Controller/Operator/ProfileController.php index d2a25c76..f5a053d8 100644 --- a/src/mibew/libs/classes/Mibew/Controller/Operator/ProfileController.php +++ b/src/mibew/libs/classes/Mibew/Controller/Operator/ProfileController.php @@ -204,18 +204,29 @@ class ProfileController extends AbstractController return $this->redirect($redirect_to); } - // Update existing operator - update_operator($op_id, $login, $email, $password, $local_name, $common_name, $code); + // Mix old operator's fields with updated values + $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. - 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. $to_dashboard = check_password_hash($login, '', $operator['vcpassword']) && $password != ''; - // Update operator's password. - $operator['vcpassword'] = calculate_password_hash($login, $password); - $this->getAuthenticationManager()->setOperator($operator); + // Update operator's fields. + $this->getAuthenticationManager()->setOperator($target_operator); // Redirect the admin to the home page if needed. if ($to_dashboard) { diff --git a/src/mibew/libs/operator.php b/src/mibew/libs/operator.php index 3b136542..d6271698 100644 --- a/src/mibew/libs/operator.php +++ b/src/mibew/libs/operator.php @@ -277,45 +277,55 @@ function operator_is_disabled($operator) /** * 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 - * @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 + * @throws \InvalidArgumentException if not all operator's fields are in place. */ -function update_operator( - $operator_id, - $login, - $email, - $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); +function update_operator($operator) +{ + if (!check_operator_fields($operator)) { + throw new \InvalidArgumentException('Not all operator fields are specified'); } - $db->query( - ("UPDATE {operator} SET vclogin = :login, " - . ($password ? " vcpassword=:password, " : "") - . "vclocalename = :localname, vccommonname = :commonname, " - . "vcemail = :email, code = :code " - . "WHERE operatorid = :operatorid"), - $values + + Database::getInstance()->query( + ('UPDATE {operator} SET vclogin = :login, vcpassword=:password, ' + . 'vclocalename = :local_name, vccommonname = :common_name, ' + . 'vcemail = :email, dtmlastvisited = :last_visited, ' + . 'istatus = :status, idisabled = :disabled, vcavatar = :avatar, ' + . '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) ); } + +/** + * 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); +}