diff --git a/src/mibew/libs/classes/Mibew/Controller/OperatorController.php b/src/mibew/libs/classes/Mibew/Controller/OperatorController.php new file mode 100644 index 00000000..724f03b2 --- /dev/null +++ b/src/mibew/libs/classes/Mibew/Controller/OperatorController.php @@ -0,0 +1,209 @@ +attributes->get('_operator'); + $page = array( + // Use errors list stored in the request. We need to do so to have + // an ability to pass the request from the "submitMembersForm" action. + 'errors' => $request->attributes->get('errors', array()), + ); + + $sort['by'] = $request->query->get('sortby'); + if (!in_array($sort['by'], array('login', 'commonname', 'localename', 'lastseen'))) { + $sort['by'] = 'login'; + } + + $sort['desc'] = ($request->query->get('sortdirection', 'desc') == 'desc'); + + $page['formsortby'] = $sort['by']; + $page['formsortdirection'] = $sort['desc'] ? 'desc' : 'asc'; + $list_options['sort'] = $sort; + if (in_isolation($operator)) { + $list_options['isolated_operator_id'] = $operator['operatorid']; + } + + $operators_list = get_operators_list($list_options); + + // Prepare operator to render in template + foreach ($operators_list as &$item) { + $item['vclogin'] = $item['vclogin']; + $item['vclocalename'] = $item['vclocalename']; + $item['vccommonname'] = $item['vccommonname']; + $item['isAvailable'] = operator_is_available($item); + $item['isAway'] = operator_is_away($item); + $item['lastTimeOnline'] = time() - $item['time']; + $item['isDisabled'] = operator_is_disabled($item); + } + unset($item); + + $page['allowedAgents'] = $operators_list; + $page['canmodify'] = is_capable(CAN_ADMINISTRATE, $operator); + $page['availableOrders'] = array( + array('id' => 'login', 'name' => getlocal('page_agents.login')), + array('id' => 'localename', 'name' => getlocal('page_agents.agent_name')), + array('id' => 'commonname', 'name' => getlocal('page_agents.commonname')), + array('id' => 'lastseen', 'name' => getlocal('page_agents.status')), + ); + $page['availableDirections'] = array( + array('id' => 'desc', 'name' => getlocal('page_agents.sortdirection.desc')), + array('id' => 'asc', 'name' => getlocal('page_agents.sortdirection.asc')), + ); + + $page['title'] = getlocal('page_agents.title'); + $page['menuid'] = 'operators'; + $page = array_merge($page, prepare_menu($operator)); + + return $this->render('operators', $page); + } + + /** + * Removes an operator from the database. + * + * @param Request $request + * @return string Rendered page content + * @throws NotFoundException If the operator with specified ID is not found + * in the system. + */ + public function deleteAction(Request $request) + { + csrf_check_token($request); + + $current_operator = $request->attributes->get('_operator'); + $operator_id = $request->attributes->getInt('operator_id'); + $errors = array(); + + if ($operator_id == $current_operator['operatorid']) { + $errors[] = getlocal('page_agents.error.cannot_remove_self'); + } else { + $operator = operator_by_id($operator_id); + if (!$operator) { + throw new NotFoundException('The operator is not found.'); + } elseif ($operator['vclogin'] == 'admin') { + $errors[] = getlocal("page_agents.error.cannot_remove_admin"); + } + } + + if (count($errors) != 0) { + $request->attributes->set('errors', $errors); + + // The operator cannot be removed by some reasons. Just rebuild + // index page and show errors there. + return $this->indexAction($request); + } + + // Remove the operator and redirect the current operator. + delete_operator($operator_id); + + return $this->redirect($this->generateUrl('operators')); + } + + /** + * Disables an operator. + * + * @param Request $request + * @return string Rendered page content + * @throws NotFoundException If the operator with specified ID is not found + * in the system. + */ + public function disableAction(Request $request) + { + csrf_check_token($request); + + $current_operator = $request->attributes->get('_operator'); + $operator_id = $request->attributes->getInt('operator_id'); + $errors = array(); + + if ($operator_id == $current_operator['operatorid']) { + $errors[] = getlocal('page_agents.cannot.disable.self'); + } else { + $operator = operator_by_id($operator_id); + if (!$operator) { + throw new NotFoundException('The operator is not found.'); + } elseif ($operator['vclogin'] == 'admin') { + $errors[] = getlocal('page_agents.cannot.disable.admin'); + } + } + + if (count($errors) != 0) { + $request->attributes->set('errors', $errors); + + // The operator cannot be removed by some reasons. Just rebuild + // index page and show errors there. + return $this->indexAction($request); + } + + // Disable the operator + $db = Database::getInstance(); + $db->query( + "update {chatoperator} set idisabled = ? where operatorid = ?", + array('1', $operator_id) + ); + + // Redirect the current operator to the page with operators list + return $this->redirect($this->generateUrl('operators')); + } + + /** + * Enables an operator. + * + * @param Request $request + * @return string Rendered page content + * @throws NotFoundException If the operator with specified ID is not found + * in the system. + */ + public function enableAction(Request $request) + { + csrf_check_token($request); + + $operator_id = $request->attributes->getInt('operator_id'); + + if (!operator_by_id($operator_id)) { + throw new NotFoundException('The operator is not found.'); + } + + $db = Database::getInstance(); + $db->query( + "update {chatoperator} set idisabled = ? where operatorid = ?", + array('0', $operator_id) + ); + + // Redirect the current operator to the page with operators list + return $this->redirect($this->generateUrl('operators')); + } +} diff --git a/src/mibew/libs/routing.yml b/src/mibew/libs/routing.yml index 24a1a450..031ac4bc 100644 --- a/src/mibew/libs/routing.yml +++ b/src/mibew/libs/routing.yml @@ -169,6 +169,41 @@ invite: _controller: Mibew\Controller\InvitationController::inviteAction _access_check: Mibew\AccessControl\Check\LoggedInCheck +## Operators +operator_enable: + path: /operator/operator/{operator_id}/enable + defaults: + _controller: Mibew\Controller\OperatorController::enableAction + _access_check: Mibew\AccessControl\Check\PermissionsCheck + _access_permissions: [CAN_ADMINISTRATE] + requirements: + operator_id: \d{1,10} + +operator_delete: + path: /operator/operator/{operator_id}/delete + defaults: + _controller: Mibew\Controller\OperatorController::deleteAction + _access_check: Mibew\AccessControl\Check\PermissionsCheck + _access_permissions: [CAN_ADMINISTRATE] + requirements: + operator_id: \d{1,10} + +operator_disable: + path: /operator/operator/{operator_id}/disable + defaults: + _controller: Mibew\Controller\OperatorController::disableAction + _access_check: Mibew\AccessControl\Check\PermissionsCheck + _access_permissions: [CAN_ADMINISTRATE] + requirements: + operator_id: \d{1,10} + +operators: + path: /operator/operator + defaults: + _controller: Mibew\Controller\OperatorController::indexAction + _access_check: Mibew\AccessControl\Check\PermissionsCheck + _access_permissions: [CAN_ADMINISTRATE] + ## Password recovery password_recovery: path: /operator/password-recovery diff --git a/src/mibew/operator/operators.php b/src/mibew/operator/operators.php deleted file mode 100644 index dcb627e9..00000000 --- a/src/mibew/operator/operators.php +++ /dev/null @@ -1,142 +0,0 @@ - array(), -); - -if (isset($_GET['act'])) { - - $operator_id = isset($_GET['id']) ? $_GET['id'] : ""; - if (!preg_match("/^\d+$/", $operator_id)) { - $page['errors'][] = getlocal("no_such_operator"); - } - - if ($_GET['act'] == 'del') { - if (!is_capable(CAN_ADMINISTRATE, $operator)) { - $page['errors'][] = getlocal("page_agents.error.forbidden_remove"); - } - - if ($operator_id == $operator['operatorid']) { - $page['errors'][] = getlocal("page_agents.error.cannot_remove_self"); - } - - if (count($page['errors']) == 0) { - $op = operator_by_id($operator_id); - if (!$op) { - $page['errors'][] = getlocal("no_such_operator"); - } elseif ($op['vclogin'] == 'admin') { - $page['errors'][] = getlocal("page_agents.error.cannot_remove_admin"); - } - } - - if (count($page['errors']) == 0) { - delete_operator($operator_id); - header("Location: " . MIBEW_WEB_ROOT . "/operator/operators.php"); - exit; - } - } - if ($_GET['act'] == 'disable' || $_GET['act'] == 'enable') { - $act_disable = ($_GET['act'] == 'disable'); - if (!is_capable(CAN_ADMINISTRATE, $operator)) { - $page['errors'][] = $act_disable - ? getlocal('page_agents.disable.not.allowed') - : getlocal('page_agents.enable.not.allowed'); - } - - if ($operator_id == $operator['operatorid'] && $act_disable) { - $page['errors'][] = getlocal('page_agents.cannot.disable.self'); - } - - if (count($page['errors']) == 0) { - $op = operator_by_id($operator_id); - if (!$op) { - $page['errors'][] = getlocal("no_such_operator"); - } elseif ($op['vclogin'] == 'admin' && $act_disable) { - $page['errors'][] = getlocal('page_agents.cannot.disable.admin'); - } - } - - if (count($page['errors']) == 0) { - $db = Database::getInstance(); - $db->query( - "update {chatoperator} set idisabled = ? where operatorid = ?", - array(($act_disable ? '1' : '0'), $operator_id) - ); - - header("Location: " . MIBEW_WEB_ROOT . "/operator/operators.php"); - exit; - } - } -} - -$sort['by'] = verify_param("sortby", "/^(login|commonname|localename|lastseen)$/", "login"); -$sort['desc'] = (verify_param("sortdirection", "/^(desc|asc)$/", "desc") == "desc"); -$page['formsortby'] = $sort['by']; -$page['formsortdirection'] = $sort['desc'] ? 'desc' : 'asc'; -$list_options['sort'] = $sort; -if (in_isolation($operator)) { - $list_options['isolated_operator_id'] = $operator['operatorid']; -} - -$operators_list = get_operators_list($list_options); - -// Prepare operator to render in template -foreach ($operators_list as &$item) { - $item['vclogin'] = $item['vclogin']; - $item['vclocalename'] = $item['vclocalename']; - $item['vccommonname'] = $item['vccommonname']; - $item['isAvailable'] = operator_is_available($item); - $item['isAway'] = operator_is_away($item); - $item['lastTimeOnline'] = time() - $item['time']; - $item['isDisabled'] = operator_is_disabled($item); -} -unset($item); - -$page['allowedAgents'] = $operators_list; -$page['canmodify'] = is_capable(CAN_ADMINISTRATE, $operator); -$page['availableOrders'] = array( - array('id' => 'login', 'name' => getlocal('page_agents.login')), - array('id' => 'localename', 'name' => getlocal('page_agents.agent_name')), - array('id' => 'commonname', 'name' => getlocal('page_agents.commonname')), - array('id' => 'lastseen', 'name' => getlocal('page_agents.status')), -); -$page['availableDirections'] = array( - array('id' => 'desc', 'name' => getlocal('page_agents.sortdirection.desc')), - array('id' => 'asc', 'name' => getlocal('page_agents.sortdirection.asc')), -); - -$page['title'] = getlocal("page_agents.title"); -$page['menuid'] = "operators"; - -setlocale(LC_TIME, getstring("time.locale")); - -$page = array_merge($page, prepare_menu($operator)); - -$page_style = new PageStyle(PageStyle::getCurrentStyle()); -$page_style->render('operators', $page); diff --git a/src/mibew/styles/pages/default/templates_src/server_side/_menu.handlebars b/src/mibew/styles/pages/default/templates_src/server_side/_menu.handlebars index 3b0f861a..65a170fa 100644 --- a/src/mibew/styles/pages/default/templates_src/server_side/_menu.handlebars +++ b/src/mibew/styles/pages/default/templates_src/server_side/_menu.handlebars @@ -29,7 +29,7 @@