Remove permission related global vars

This commit is contained in:
Dmitriy Simushev 2013-04-26 09:19:52 +00:00
parent 935ad85fd5
commit a31cdfce24
10 changed files with 68 additions and 49 deletions

View File

@ -156,8 +156,6 @@ class UsersProcessor extends ClientSideProcessor {
* Return updated threads list. API function * Return updated threads list. API function
* *
* @global string $session_prefix Session vars prefix * @global string $session_prefix Session vars prefix
* @global int $can_viewthreads View threads permission code
* @global int $can_takeover Take threads over permission code
* @param array $args Associative array of arguments. It must contains * @param array $args Associative array of arguments. It must contains
* following keys: * following keys:
* - 'agentId': Id of the agent related to users window * - 'agentId': Id of the agent related to users window
@ -166,7 +164,7 @@ class UsersProcessor extends ClientSideProcessor {
* - 'threads': array of threads changes * - 'threads': array of threads changes
*/ */
protected function apiUpdateThreads($args) { protected function apiUpdateThreads($args) {
global $session_prefix, $can_viewthreads, $can_takeover; global $session_prefix;
$operator = self::checkOperator($args['agentId']); $operator = self::checkOperator($args['agentId']);
@ -221,11 +219,11 @@ class UsersProcessor extends ClientSideProcessor {
// Calculate agent permissions // Calculate agent permissions
$can_open = !($thread->state == Thread::STATE_CHATTING $can_open = !($thread->state == Thread::STATE_CHATTING
&& $thread->agentId != $operator['operatorid'] && $thread->agentId != $operator['operatorid']
&& !is_capable($can_takeover, $operator)); && !is_capable(CAN_TAKEOVER, $operator));
$can_view = ($thread->agentId != $operator['operatorid'] $can_view = ($thread->agentId != $operator['operatorid']
&& $thread->nextAgent != $operator['operatorid'] && $thread->nextAgent != $operator['operatorid']
&& is_capable($can_viewthreads, $operator)); && is_capable(CAN_VIEWTHREADS, $operator));
$can_ban = (Settings::get('enableban') == "1"); $can_ban = (Settings::get('enableban') == "1");

View File

@ -15,19 +15,43 @@
* limitations under the License. * limitations under the License.
*/ */
$can_administrate = 0; /** Permissions constants */
$can_takeover = 1;
$can_viewthreads = 2;
$can_modifyprofile = 3;
$can_count = 4; /**
* Operator can administer Mibew instalation
*/
define('CAN_ADMINISTRATE', 0);
$permission_ids = array( /**
$can_administrate => "admin", * Operator can take over threads
$can_takeover => "takeover", */
$can_viewthreads => "viewthreads", define('CAN_TAKEOVER', 1);
$can_modifyprofile => "modifyprofile"
); /**
* Operator can view threads of other operators
*/
define('CAN_VIEWTHREADS', 2);
/**
* Operator can modify own profile
*/
define('CAN_MODIFYPROFILE', 3);
/** End of permissions constants */
/**
* Map numerical permissions ids onto string names.
* @return array Associativa array whose keys are numerical permission ids and
* values are string permission names.
*/
function permission_ids() {
return array(
CAN_ADMINISTRATE => "admin",
CAN_TAKEOVER => "takeover",
CAN_VIEWTHREADS => "viewthreads",
CAN_MODIFYPROFILE => "modifyprofile"
);
}
function operator_by_login($login) function operator_by_login($login)
{ {
@ -555,13 +579,11 @@ function setup_redirect_links($threadid, $operator, $token)
$page['redirectToGroup'] = $group_list; $page['redirectToGroup'] = $group_list;
} }
$permission_list = array();
function get_permission_list() function get_permission_list()
{ {
global $permission_list, $permission_ids; static $permission_list = array();
if (count($permission_list) == 0) { if (count($permission_list) == 0) {
foreach ($permission_ids as $permid) { foreach (permission_ids() as $permid) {
$permission_list[] = array( $permission_list[] = array(
'id' => $permid, 'id' => $permid,
'descr' => getlocal("permission.$permid") 'descr' => getlocal("permission.$permid")
@ -579,18 +601,17 @@ function is_capable($perm, $operator)
function in_isolation($operator) function in_isolation($operator)
{ {
global $can_administrate; return (!is_capable(CAN_ADMINISTRATE, $operator) && Settings::get('enablegroups') && Settings::get('enablegroupsisolation'));
return (!is_capable($can_administrate, $operator) && Settings::get('enablegroups') && Settings::get('enablegroupsisolation'));
} }
function prepare_menu($operator, $hasright = true) function prepare_menu($operator, $hasright = true)
{ {
global $page, $can_administrate; global $page;
$page['operator'] = topage(get_operator_name($operator)); $page['operator'] = topage(get_operator_name($operator));
if ($hasright) { if ($hasright) {
$page['showban'] = Settings::get('enableban') == "1"; $page['showban'] = Settings::get('enableban') == "1";
$page['showstat'] = Settings::get('enablestatistics') == "1"; $page['showstat'] = Settings::get('enablestatistics') == "1";
$page['showadmin'] = is_capable($can_administrate, $operator); $page['showadmin'] = is_capable(CAN_ADMINISTRATE, $operator);
$page['currentopid'] = $operator['operatorid']; $page['currentopid'] = $operator['operatorid'];
} }
} }

View File

@ -64,7 +64,7 @@ if (!isset($_GET['token'])) {
$forcetake = verifyparam("force", "/^true$/", false); $forcetake = verifyparam("force", "/^true$/", false);
if (!$viewonly && $thread->state == Thread::STATE_CHATTING && $operator['operatorid'] != $thread->agentId) { if (!$viewonly && $thread->state == Thread::STATE_CHATTING && $operator['operatorid'] != $thread->agentId) {
if (!is_capable($can_takeover, $operator)) { if (!is_capable(CAN_TAKEOVER, $operator)) {
$errors = array(getlocal("thread.error.cannot_take_over")); $errors = array(getlocal("thread.error.cannot_take_over"));
start_html_output(); start_html_output();
expand("../styles/dialogs", getchatstyle(), "error.tpl"); expand("../styles/dialogs", getchatstyle(), "error.tpl");
@ -90,7 +90,7 @@ if (!isset($_GET['token'])) {
expand("../styles/dialogs", getchatstyle(), "error.tpl"); expand("../styles/dialogs", getchatstyle(), "error.tpl");
exit; exit;
} }
} else if (!is_capable($can_viewthreads, $operator)) { } else if (!is_capable(CAN_VIEWTHREADS, $operator)) {
$errors = array(getlocal("thread.error.cannot_view")); $errors = array(getlocal("thread.error.cannot_view"));
start_html_output(); start_html_output();
expand("../styles/dialogs", getchatstyle(), "error.tpl"); expand("../styles/dialogs", getchatstyle(), "error.tpl");
@ -109,7 +109,7 @@ if (!$thread) {
die("wrong thread"); die("wrong thread");
} }
if ($thread->agentId != $operator['operatorid'] && !is_capable($can_viewthreads, $operator)) { if ($thread->agentId != $operator['operatorid'] && !is_capable(CAN_VIEWTHREADS, $operator)) {
$errors = array("Cannot view threads"); $errors = array("Cannot view threads");
start_html_output(); start_html_output();
expand("../styles/dialogs", getchatstyle(), "error.tpl"); expand("../styles/dialogs", getchatstyle(), "error.tpl");

View File

@ -26,8 +26,8 @@ $opId = verifyparam("op", "/^\d{1,9}$/");
$page = array('opid' => $opId, 'avatar' => ''); $page = array('opid' => $opId, 'avatar' => '');
$errors = array(); $errors = array();
$canmodify = ($opId == $operator['operatorid'] && is_capable($can_modifyprofile, $operator)) $canmodify = ($opId == $operator['operatorid'] && is_capable(CAN_MODIFYPROFILE, $operator))
|| is_capable($can_administrate, $operator); || is_capable(CAN_ADMINISTRATE, $operator);
$op = operator_by_id($opId); $op = operator_by_id($opId);

View File

@ -44,7 +44,7 @@ foreach ($options as $opt) {
} }
if (isset($_POST['sent'])) { if (isset($_POST['sent'])) {
if (is_capable($can_administrate, $operator)) { if (is_capable(CAN_ADMINISTRATE, $operator)) {
foreach ($options as $opt) { foreach ($options as $opt) {
Settings::set($opt,(verifyparam($opt, "/^on$/", "") == "on" ? "1" : "0")); Settings::set($opt,(verifyparam($opt, "/^on$/", "") == "on" ? "1" : "0"));
} }
@ -56,7 +56,7 @@ if (isset($_POST['sent'])) {
} }
} }
$page['canmodify'] = is_capable($can_administrate, $operator); $page['canmodify'] = is_capable(CAN_ADMINISTRATE, $operator);
$page['stored'] = isset($_GET['stored']); $page['stored'] = isset($_GET['stored']);
foreach ($options as $opt) { foreach ($options as $opt) {
$page["form$opt"] = $params[$opt] == "1"; $page["form$opt"] = $params[$opt] == "1";

View File

@ -28,7 +28,7 @@ if (isset($_GET['act']) && $_GET['act'] == 'del') {
$errors[] = getlocal("page.groups.error.cannot_delete"); $errors[] = getlocal("page.groups.error.cannot_delete");
} }
if (!is_capable($can_administrate, $operator)) { if (!is_capable(CAN_ADMINISTRATE, $operator)) {
$errors[] = getlocal("page.groups.error.forbidden_remove"); $errors[] = getlocal("page.groups.error.forbidden_remove");
} }
@ -59,7 +59,7 @@ $sort['desc'] = (verifyparam("sortdirection", "/^(desc|asc)$/", "desc") == "desc
$page['groups'] = get_sorted_groups($sort); $page['groups'] = get_sorted_groups($sort);
$page['formsortby'] = $sort['by']; $page['formsortby'] = $sort['by'];
$page['formsortdirection'] = $sort['desc']?'desc':'asc'; $page['formsortdirection'] = $sort['desc']?'desc':'asc';
$page['canmodify'] = is_capable($can_administrate, $operator); $page['canmodify'] = is_capable(CAN_ADMINISTRATE, $operator);
$page['availableOrders'] = array( $page['availableOrders'] = array(
array('id' => 'name', 'name' => getlocal('form.field.groupname')), array('id' => 'name', 'name' => getlocal('form.field.groupname')),
array('id' => 'lastseen', 'name' => getlocal('page_agents.status')), array('id' => 'lastseen', 'name' => getlocal('page_agents.status')),

View File

@ -26,9 +26,9 @@ $page = array('opid' => '');
$errors = array(); $errors = array();
$opId = ''; $opId = '';
if ((isset($_POST['login']) || !is_capable($can_administrate, $operator)) && isset($_POST['password'])) { if ((isset($_POST['login']) || !is_capable(CAN_ADMINISTRATE, $operator)) && isset($_POST['password'])) {
$opId = verifyparam("opid", "/^(\d{1,9})?$/", ""); $opId = verifyparam("opid", "/^(\d{1,9})?$/", "");
if (is_capable($can_administrate, $operator)) { if (is_capable(CAN_ADMINISTRATE, $operator)) {
$login = getparam('login'); $login = getparam('login');
} else { } else {
$login = $operator['vclogin']; $login = $operator['vclogin'];
@ -65,8 +65,8 @@ if ((isset($_POST['login']) || !is_capable($can_administrate, $operator)) && iss
($opId && $existing_operator && $opId != $existing_operator['operatorid'])) ($opId && $existing_operator && $opId != $existing_operator['operatorid']))
$errors[] = getlocal("page_agent.error.duplicate_login"); $errors[] = getlocal("page_agent.error.duplicate_login");
$canmodify = ($opId == $operator['operatorid'] && is_capable($can_modifyprofile, $operator)) $canmodify = ($opId == $operator['operatorid'] && is_capable(CAN_MODIFYPROFILE, $operator))
|| is_capable($can_administrate, $operator); || is_capable(CAN_ADMINISTRATE, $operator);
if (!$canmodify) { if (!$canmodify) {
$errors[] = getlocal('page_agent.cannot_modify'); $errors[] = getlocal('page_agent.cannot_modify');
} }
@ -120,16 +120,16 @@ if ((isset($_POST['login']) || !is_capable($can_administrate, $operator)) && iss
} }
} }
if (!$opId && !is_capable($can_administrate, $operator)) { if (!$opId && !is_capable(CAN_ADMINISTRATE, $operator)) {
$errors[] = getlocal("page_agent.error.forbidden_create"); $errors[] = getlocal("page_agent.error.forbidden_create");
} }
$canmodify = ($opId == $operator['operatorid'] && is_capable($can_modifyprofile, $operator)) $canmodify = ($opId == $operator['operatorid'] && is_capable(CAN_MODIFYPROFILE, $operator))
|| is_capable($can_administrate, $operator); || is_capable(CAN_ADMINISTRATE, $operator);
$page['stored'] = isset($_GET['stored']); $page['stored'] = isset($_GET['stored']);
$page['canmodify'] = $canmodify ? "1" : ""; $page['canmodify'] = $canmodify ? "1" : "";
$page['canchangelogin'] = is_capable($can_administrate, $operator); $page['canchangelogin'] = is_capable(CAN_ADMINISTRATE, $operator);
$page['needChangePassword'] = $operator['vcpassword'] == md5(''); $page['needChangePassword'] = $operator['vcpassword'] == md5('');
prepare_menu($operator); prepare_menu($operator);

View File

@ -30,7 +30,7 @@ if (isset($_GET['act'])) {
} }
if ($_GET['act'] == 'del') { if ($_GET['act'] == 'del') {
if (!is_capable($can_administrate, $operator)) { if (!is_capable(CAN_ADMINISTRATE, $operator)) {
$errors[] = getlocal("page_agents.error.forbidden_remove"); $errors[] = getlocal("page_agents.error.forbidden_remove");
} }
@ -55,7 +55,7 @@ if (isset($_GET['act'])) {
} }
if ($_GET['act'] == 'disable' || $_GET['act'] == 'enable') { if ($_GET['act'] == 'disable' || $_GET['act'] == 'enable') {
$act_disable = ($_GET['act'] == 'disable'); $act_disable = ($_GET['act'] == 'disable');
if (!is_capable($can_administrate, $operator)) { if (!is_capable(CAN_ADMINISTRATE, $operator)) {
$errors[] = $act_disable?getlocal('page_agents.disable.not.allowed'):getlocal('page_agents.enable.not.allowed'); $errors[] = $act_disable?getlocal('page_agents.disable.not.allowed'):getlocal('page_agents.enable.not.allowed');
} }
@ -95,7 +95,7 @@ if (in_isolation($operator)) {
$list_options['isolated_operator_id'] = $operator['operatorid']; $list_options['isolated_operator_id'] = $operator['operatorid'];
} }
$page['allowedAgents'] = get_operators_list($list_options); $page['allowedAgents'] = get_operators_list($list_options);
$page['canmodify'] = is_capable($can_administrate, $operator); $page['canmodify'] = is_capable(CAN_ADMINISTRATE, $operator);
$page['availableOrders'] = array( $page['availableOrders'] = array(
array('id' => 'login', 'name' => getlocal('page_agents.login')), array('id' => 'login', 'name' => getlocal('page_agents.login')),
array('id' => 'localename', 'name' => getlocal('page_agents.agent_name')), array('id' => 'localename', 'name' => getlocal('page_agents.agent_name')),

View File

@ -44,7 +44,7 @@ $page = array('opid' => $opId);
$page['groups'] = $operator_in_isolation?get_all_groups_for_operator($operator):get_all_groups(); $page['groups'] = $operator_in_isolation?get_all_groups_for_operator($operator):get_all_groups();
$errors = array(); $errors = array();
$canmodify = is_capable($can_administrate, $operator); $canmodify = is_capable(CAN_ADMINISTRATE, $operator);
$op = operator_by_id($opId); $op = operator_by_id($opId);

View File

@ -32,7 +32,7 @@ function update_operator_permissions($operatorid, $newvalue)
} }
$opId = verifyparam("op", "/^\d{1,9}$/"); $opId = verifyparam("op", "/^\d{1,9}$/");
$page = array('opid' => $opId, 'canmodify' => is_capable($can_administrate, $operator) ? "1" : ""); $page = array('opid' => $opId, 'canmodify' => is_capable(CAN_ADMINISTRATE, $operator) ? "1" : "");
$errors = array(); $errors = array();
$op = operator_by_id($opId); $op = operator_by_id($opId);
@ -42,13 +42,13 @@ if (!$op) {
} else if (isset($_POST['op'])) { } else if (isset($_POST['op'])) {
if (!is_capable($can_administrate, $operator)) { if (!is_capable(CAN_ADMINISTRATE, $operator)) {
$errors[] = getlocal('page_agent.cannot_modify'); $errors[] = getlocal('page_agent.cannot_modify');
} }
$new_permissions = isset($op['iperm']) ? $op['iperm'] : 0; $new_permissions = isset($op['iperm']) ? $op['iperm'] : 0;
foreach ($permission_ids as $perm => $id) { foreach (permission_ids() as $perm => $id) {
if (verifyparam("permissions$id", "/^on$/", "") == "on") { if (verifyparam("permissions$id", "/^on$/", "") == "on") {
$new_permissions |= (1 << $perm); $new_permissions |= (1 << $perm);
} else { } else {
@ -73,7 +73,7 @@ $page['formpermissions'] = array("");
$page['currentop'] = $op ? topage(get_operator_name($op)) . " (" . $op['vclogin'] . ")" : "-not found-"; $page['currentop'] = $op ? topage(get_operator_name($op)) . " (" . $op['vclogin'] . ")" : "-not found-";
if ($op) { if ($op) {
foreach ($permission_ids as $perm => $id) { foreach (permission_ids() as $perm => $id) {
if (is_capable($perm, $op)) { if (is_capable($perm, $op)) {
$page['formpermissions'][] = $id; $page['formpermissions'][] = $id;
} }