Replace "operator/operators.php" with a controller

This commit is contained in:
Dmitriy Simushev 2014-05-21 10:45:09 +00:00
parent 55dff20bc1
commit b674060ddf
6 changed files with 250 additions and 148 deletions

View File

@ -0,0 +1,209 @@
<?php
/*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Mibew\Controller;
use Mibew\Database;
use Mibew\Http\Exception\NotFoundException;
use Symfony\Component\HttpFoundation\Request;
/**
* Contains all actions which are related with operators.
*/
class OperatorController extends AbstractController
{
/**
* Generates list of all operators in the system.
*
* @param Request $request
* @return string Rendered page content
*/
public function indexAction(Request $request)
{
set_csrf_token();
setlocale(LC_TIME, getstring('time.locale'));
$operator = $request->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'));
}
}

View File

@ -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

View File

@ -1,142 +0,0 @@
<?php
/*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Import namespaces and classes of the core
use Mibew\Database;
use Mibew\Style\PageStyle;
// Initialize libraries
require_once(dirname(dirname(__FILE__)) . '/libs/init.php');
$operator = check_login();
force_password($operator);
csrf_check_token();
$page = array(
'errors' => 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);

View File

@ -29,7 +29,7 @@
<li{{#ifEqual menuid "canned"}} class="active"{{/ifEqual}}><a href="{{mibewRoot}}/operator/canned-message">{{l10n "menu.canned"}}</a></li>
{{#if showadmin}}
<li{{#ifEqual menuid "getcode"}} class="active"{{/ifEqual}}><a href="{{mibewRoot}}/operator/getcode.php">{{l10n "leftMenu.client_gen_button"}}</a></li>
<li{{#ifEqual menuid "operators"}} class="active"{{/ifEqual}}><a href="{{mibewRoot}}/operator/operators.php">{{l10n "leftMenu.client_agents"}}</a></li>
<li{{#ifEqual menuid "operators"}} class="active"{{/ifEqual}}><a href="{{mibewRoot}}/operator/operator">{{l10n "leftMenu.client_agents"}}</a></li>
<li{{#ifEqual menuid "groups"}} class="active"{{/ifEqual}}><a href="{{mibewRoot}}/operator/group">{{l10n "menu.groups"}}</a></li>
<li{{#ifEqual menuid "settings"}} class="active"{{/ifEqual}}><a href="{{mibewRoot}}/operator/settings.php">{{l10n "leftMenu.client_settings"}}</a></li>
<li{{#ifEqual menuid "translate"}} class="active"{{/ifEqual}}><a href="{{mibewRoot}}/operator/translate.php">{{l10n "menu.translate"}}</a></li>

View File

@ -94,7 +94,7 @@
<div class="dashitem">
<div class="dashitem-content">
<img src="{{stylePath}}/images/dash/operators.gif" alt=""/>
<a href="{{mibewRoot}}/operator/operators.php">
<a href="{{mibewRoot}}/operator/operator">
{{l10n "leftMenu.client_agents"}}
</a>
{{l10n "admin.content.client_agents"}}

View File

@ -25,7 +25,7 @@
{{> _errors}}
<form name="agentsForm" method="get" action="{{mibewRoot}}/operator/operators.php">
<form name="agentsForm" method="get" action="{{mibewRoot}}/operator/operator">
<div class="mform">
<div class="formtop">
<div class="formtopi"></div>
@ -111,14 +111,14 @@
{{#if ../canmodify}}
<td>
{{#if isDisabled}}
<a href="{{../mibewRoot}}/operator/operators.php?act=enable&amp;id={{operatorid}}">{{l10n "page_agents.enable.agent"}}</a>
<a href="{{../mibewRoot}}/operator/operator/{{operatorid}}/enable?{{csrfTokenInUrl}}">{{l10n "page_agents.enable.agent"}}</a>
{{else}}
<a href="{{../mibewRoot}}/operator/operators.php?act=disable&amp;id={{operatorid}}">{{l10n "page_agents.disable.agent"}}</a>
<a href="{{../mibewRoot}}/operator/operator/{{operatorid}}/disable?{{csrfTokenInUrl}}">{{l10n "page_agents.disable.agent"}}</a>
{{/if}}
</td>
<td>
<a class="removelink" id="i{{operatorid}}" href="{{../mibewRoot}}/operator/operators.php?act=del&amp;id={{operatorid}}{{csrfTokenInUrl}}">
<a class="removelink" id="i{{operatorid}}" href="{{../mibewRoot}}/operator/operator/{{operatorid}}/delete?{{csrfTokenInUrl}}">
{{l10n "remove.item"}}
</a>
</td>