Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW: TakePOS - manage permissions on terminals #24832

Open
wants to merge 28 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions htdocs/core/lib/takepos.lib.php
Expand Up @@ -73,3 +73,29 @@ function takepos_admin_prepare_head()

return $head;
}


/**
* Add permission on terminal
*
* @param Object $module Takepos module instance
* @return void
*/
function addTerminalPermissions($module)
{
global $conf, $langs;

$langs->load("cashdesk");

$r = array_key_last($module->rights);
$lastperm = 50190; // terminal permissions from 50191 to 50199
$numterminals = max(1, getDolGlobalInt("TAKEPOS_NUM_TERMINALS"));
for ($i = 1; $i <= $numterminals; $i++) {
$r++;
$module->rights[$r][0] = $lastperm + $i;
$module->rights[$r][1] = 'Allow access to ' . (getDolGlobalString("TAKEPOS_TERMINAL_NAME_".$i) != "" ? getDolGlobalString("TAKEPOS_TERMINAL_NAME_".$i) : $langs->trans("TerminalName", $i));
$module->rights[$r][2] = 'a';
$module->rights[$r][3] = 0;
$module->rights[$r][4] = 'access_takepos_' . $i;
}
}
5 changes: 5 additions & 0 deletions htdocs/core/modules/modTakePos.class.php
Expand Up @@ -206,6 +206,11 @@ public function __construct($db)
$this->rights[$r][3] = 0;
$this->rights[$r][4] = 'editorderedlines';

// add permissions on terminals (from 50191 to 50199)
if (getDolGlobalInt('TAKEPOS_USE_TERMINAL_PERMISSIONS')) {
require_once DOL_DOCUMENT_ROOT."/core/lib/takepos.lib.php";
addTerminalPermissions($this);
}

// Main menu entries
$this->menu = array(); // List of menus to add
Expand Down
1 change: 1 addition & 0 deletions htdocs/langs/en_US/cashdesk.lang
Expand Up @@ -158,3 +158,4 @@ InvoiceDiscount=Invoice discount
InvoiceDiscountShort=Invoice disc.
TestPrinterDesc=The server will send a simple test page to a ESC/POS printer
TestPrinterDesc2=The server will send an enhanced test page with image and barcode to a ESC/POS printer
CloseTerminal=Close terminal
9 changes: 9 additions & 0 deletions htdocs/takepos/admin/setup.php
Expand Up @@ -95,6 +95,15 @@
}

if (!$error) {
if (getDolGlobalInt('TAKEPOS_USE_TERMINAL_PERMISSIONS')) {
// add terminal permissions
require_once DOL_DOCUMENT_ROOT . '/core/modules/modTakePos.class.php';
$module = new modTakePos($db);
$module->delete_permissions(); // $module->rights already loaded in module _construct so we don't lose anything
addTerminalPermissions($module);
$module->insert_permissions(1);
}

$db->commit();
} else {
$db->rollback();
Expand Down
6 changes: 6 additions & 0 deletions htdocs/takepos/ajax/ajax.php
Expand Up @@ -64,6 +64,12 @@
/*
* View
*/
if (getDolGlobalInt('TAKEPOS_USE_TERMINAL_PERMISSIONS') && $action == 'closeTerminal') {
unset($_SESSION["takeposterminal"]);
unset($_COOKIE["takeposterminal"]);
setcookie("takeposterminal", "", time()-3600, '/', "", (empty($dolibarr_main_force_https) ? false : true), true);
exit;
}

$thirdparty = new Societe($db);

Expand Down
99 changes: 89 additions & 10 deletions htdocs/takepos/index.php
Expand Up @@ -56,11 +56,31 @@
$setcurrency = GETPOST('setcurrency', 'aZ09');

$hookmanager->initHooks(array('takeposfrontend'));
if (empty($_SESSION["takeposterminal"])) {
if (getDolGlobalInt('TAKEPOS_NUM_TERMINALS') == 1) {
$_SESSION["takeposterminal"] = 1; // Use terminal 1 if there is only 1 terminal
} elseif (!empty($_COOKIE["takeposterminal"])) {
$_SESSION["takeposterminal"] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $_COOKIE["takeposterminal"]); // Restore takeposterminal from previous session

if (getDolGlobalInt('TAKEPOS_USE_TERMINAL_PERMISSIONS')) {
// get user authorized terminals
$nb_auth_terms = 0;
$curterm = 0;
$numterminals = max(1, getDolGlobalInt("TAKEPOS_NUM_TERMINALS"));
for ($i = 1; $i <= $numterminals; $i++) {
if ($user->hasRights('takepos', 'access_takepos_' . $i)) {
$curterm = $i;
$nb_auth_terms++;
}
}

// TERMINAL SELECTION IF NOT SET
if (empty($_SESSION["takeposterminal"])) {
if (empty($nb_auth_terms)) {
accessforbidden();
} elseif ($nb_auth_terms > 1) {
if (!empty($_COOKIE["takeposterminal"]) && $user->hasRight('takepos', 'access_takepos_' . $_COOKIE["takeposterminal"])) {
$_SESSION["takeposterminal"] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $_COOKIE["takeposterminal"]); // Restore takeposterminal from previous session
}
} else {
$terminal_name = getDolGlobalString("TAKEPOS_TERMINAL_NAME_".$curterm) != "" ? getDolGlobalString("TAKEPOS_TERMINAL_NAME_".$curterm) : $langs->transnoentities("TerminalName", $curterm);
$_SESSION["takeposterminal"] = $curterm;
}
}
}

Expand Down Expand Up @@ -202,6 +222,10 @@
}
*/

function closeTerminal(modal) {
$.post("<?php echo DOL_URL_ROOT . '/takepos/ajax/ajax.php?action=closeTerminal&token=' . newToken(); ?>" , function(data) { if (modal) ModalBox('ModalTerminal'); });
}

function ClearSearch(clearSearchResults) {
console.log("ClearSearch");
$("#search").val('');
Expand Down Expand Up @@ -1035,6 +1059,30 @@
}

$( document ).ready(function() {
<?php
if (getDolGlobalInt('TAKEPOS_USE_TERMINAL_PERMISSIONS')) {
// TERMINAL SELECTION IF NOT SET
if ($_SESSION["takeposterminal"] == "") {
if (empty($nb_auth_terms)) {
accessforbidden();
} elseif ($nb_auth_terms > 1) {
print "ModalBox('ModalTerminal');";
} else {
$terminal_name = getDolGlobalString("TAKEPOS_TERMINAL_NAME_".$curterm) != "" ? getDolGlobalString("TAKEPOS_TERMINAL_NAME_".$curterm) : $langs->transnoentities("TerminalName", $curterm);

Check warning on line 1071 in htdocs/takepos/index.php

View workflow job for this annotation

GitHub Actions / phan / Run phan

index.php: PhanPossiblyUndeclaredGlobalVariable: Global variable $curterm is possibly undeclared
$_SESSION["takeposterminal"] = $curterm;

Check warning on line 1072 in htdocs/takepos/index.php

View workflow job for this annotation

GitHub Actions / phan / Run phan

index.php: PhanPossiblyUndeclaredGlobalVariable: Global variable $curterm is possibly undeclared
}
}

if (empty($_SESSION["takeposterminal"])) {
if (empty($nb_auth_terms)) {
accessforbidden();
} elseif ($nb_auth_terms > 1) {
print "ModalBox('ModalTerminal');";
}
}
}
?>

PrintCategories(0);
LoadProducts(0);
Refresh();
Expand Down Expand Up @@ -1136,7 +1184,14 @@
<div id="topnav" class="topnav">
<div id="topnav-left" class="topnav-left">
<div class="inline-block valignmiddle">
<a class="topnav-terminalhour" onclick="ModalBox('ModalTerminal')">
<?php
if (getDolGlobalInt('TAKEPOS_USE_TERMINAL_PERMISSIONS')) {
?>
<a href="#" onclick="closeTerminal(true);" title="<?php echo $langs->trans("CloseTerminal"); ?>" style="font-weight: bolder; font-size: 1.5em; margin: auto 0;">X</a>
<a class="topnav-terminalhour" <?php echo $nb_auth_terms > 1 ? "onclick=\"ModalBox('ModalTerminal');\"" : ""; ?>>

Check warning on line 1191 in htdocs/takepos/index.php

View workflow job for this annotation

GitHub Actions / phan / Run phan

index.php: PhanPossiblyUndeclaredGlobalVariable: Global variable $nb_auth_terms is possibly undeclared
<?php
}
?>
<span class="fa fa-cash-register"></span>
<span class="hideonsmartphone">
<?php
Expand Down Expand Up @@ -1216,11 +1271,22 @@
<h3><?php print $langs->trans("TerminalSelect"); ?></h3>
</div>
<div class="modal-body">
<button type="button" class="block" onclick="location.href='index.php?setterminal=1'"><?php print getDolGlobalString("TAKEPOS_TERMINAL_NAME_1", $langs->trans("TerminalName", 1)); ?></button>
<?php
$nbloop = getDolGlobalInt('TAKEPOS_NUM_TERMINALS');
for ($i = 2; $i <= $nbloop; $i++) {
print '<button type="button" class="block" onclick="location.href=\'index.php?setterminal='.$i.'\'">'.getDolGlobalString("TAKEPOS_TERMINAL_NAME_".$i, $langs->trans("TerminalName", $i)).'</button>';
if (getDolGlobalInt('TAKEPOS_USE_TERMINAL_PERMISSIONS')) {
for ($i = 1; $i <= $numterminals; $i++) {

Check warning on line 1276 in htdocs/takepos/index.php

View workflow job for this annotation

GitHub Actions / phan / Run phan

index.php: PhanPossiblyUndeclaredGlobalVariable: Global variable $numterminals is possibly undeclared
if ($user->hasRight('takepos', 'access_takepos_' . $i)) {
$terminal_name = getDolGlobalString("TAKEPOS_TERMINAL_NAME_".$i) != "" ? getDolGlobalString("TAKEPOS_TERMINAL_NAME_".$i) : $langs->trans("TerminalName", $i);
print '<button type="button" class="block" onclick="closeTerminal(true);location.href=\'index.php?setterminal='.$i.'\'">'. $terminal_name .'</button>';
}
}
} else {
?>
<button type="button" class="block" onclick="location.href='index.php?setterminal=1'"><?php print getDolGlobalString("TAKEPOS_TERMINAL_NAME_1", $langs->trans("TerminalName", 1)); ?></button>
<?php
$nbloop = getDolGlobalInt('TAKEPOS_NUM_TERMINALS');
for ($i = 2; $i <= $nbloop; $i++) {
print '<button type="button" class="block" onclick="location.href=\'index.php?setterminal='.$i.'\'">'.getDolGlobalString("TAKEPOS_TERMINAL_NAME_".$i, $langs->trans("TerminalName", $i)).'</button>';
}
}
?>
</div>
Expand Down Expand Up @@ -1598,6 +1664,19 @@
</div>
</div>
</div>
<?php
if (getDolGlobalInt('TAKEPOS_USE_TERMINAL_PERMISSIONS') && getDolGlobalInt("TAKEPOS_CLOSE_TERMINAL_ON_WINDOW_CLOSE")) {
?>
<script type="text/javascript">
// close terminal
$(window).on("beforeunload", function() {
closeTerminal(false);
return "ok";
});
</script>
<?php
}
?>
</body>
<?php

Expand Down