Add permission editor

master
Skylar Ittner 7 years ago
parent b0f5721819
commit 937f44eb11

@ -105,6 +105,26 @@ switch ($VARS['action']) {
}
$database->delete('managers', ['AND' => ['managerid' => $VARS['mid'], 'employeeid' => $VARS['eid']]]);
returnToSender("relationship_deleted");
case "addpermission":
if (!$database->has('accounts', ['username' => $VARS['user']])) {
returnToSender("invalid_userid");
}
if (!$database->has('permissions', ['permcode' => $VARS['perm']])) {
returnToSender("permission_not_exists");
}
$uid = $database->select('accounts', 'uid', ['username' => $VARS['user']])[0];
$pid = $database->select('permissions', 'permid', ['permcode' => $VARS['perm']])[0];
$database->insert('assigned_permissions', ['uid' => $uid, 'permid' => $pid]);
returnToSender("permission_added");
case "delpermission":
if (!$database->has('accounts', ['uid' => $VARS['uid']])) {
returnToSender("invalid_userid");
}
if (!$database->has('permissions', ['permid' => $VARS['pid']])) {
returnToSender("permission_not_exists");
}
$database->delete('assigned_permissions', ['AND' => ['uid' => $VARS['uid'], 'permid' => $VARS['pid']]]);
returnToSender("permission_deleted");
case "autocomplete_user":
header("Content-Type: application/json");
if (is_empty($VARS['q']) || strlen($VARS['q']) < 3) {
@ -112,6 +132,13 @@ switch ($VARS['action']) {
}
$data = $database->select('accounts', ['uid', 'username', 'realname (name)'], ["OR" => ['username[~]' => $VARS['q'], 'realname[~]' => $VARS['q']], "LIMIT" => 10]);
exit(json_encode($data));
case "autocomplete_permission":
header("Content-Type: application/json");
if (is_empty($VARS['q'])) {
exit(json_encode([]));
}
$data = $database->select('permissions', ['permcode (name)', 'perminfo (info)'], ["OR" => ['permcode[~]' => $VARS['q'], 'perminfo[~]' => $VARS['q']], "LIMIT" => 10]);
exit(json_encode($data));
case "signout":
session_destroy();
header('Location: index.php');

@ -71,5 +71,15 @@ define("STRINGS", [
"relationship deleted" => "Relationship deleted.",
"edit relationship" => "Edit Relationship",
"adding relationship" => "Adding Relationship",
"relationship added" => "Relationship added."
"relationship added" => "Relationship added.",
"permissions" => "Permissions",
"permission" => "Permission",
"new permission" => "New Permission",
"delete permission" => "Delete Permission",
"adding permission" => "Adding Permission",
"user" => "User",
"permission does not exist" => "Permission does not exist.",
"really delete permission" => "Are you sure you want to revoke this permission?",
"permission added" => "Permission assigned.",
"permission deleted" => "Permission deleted."
]);

@ -40,5 +40,17 @@ define("MESSAGES", [
"relationship_deleted" => [
"string" => "relationship deleted",
"type" => "success"
]
],
"permission_not_exists" => [
"string" => "permission does not exist",
"type" => "danger"
],
"permission_added" => [
"string" => "permission added",
"type" => "success"
],
"permission_deleted" => [
"string" => "permission deleted",
"type" => "success"
],
]);

@ -0,0 +1,78 @@
<?php
require __DIR__ . '/../required.php';
dieifnotloggedin();
header("Content-Type: application/json");
$out = [];
$out['draw'] = intval($VARS['draw']);
$out['recordsTotal'] = $database->count('assigned_permissions');
$filter = false;
// sort
$order = null;
$sortby = "DESC";
if ($VARS['order'][0]['dir'] == 'asc') {
$sortby = "ASC";
}
switch ($VARS['order'][0]['column']) {
case 2:
$order = ["realname" => $sortby];
break;
case 3:
$order = ["permcode" => $sortby];
break;
}
// search
if (!is_empty($VARS['search']['value'])) {
$filter = true;
$wherenolimit = [
"OR" => [
"username[~]" => $VARS['search']['value'],
"realname[~]" => $VARS['search']['value'],
"permcode[~]" => $VARS['search']['value']
]
];
$where = $wherenolimit;
$where["LIMIT"] = [$VARS['start'], $VARS['length']];
} else {
$where = ["LIMIT" => [$VARS['start'], $VARS['length']]];
}
if (!is_null($order)) {
$where["ORDER"] = $order;
}
$data = $database->select('assigned_permissions', [
"[>]accounts" => ['uid' => 'uid'],
"[>]permissions" => ['permid' => 'permid']
], [
'username',
'realname',
'assigned_permissions.uid',
'permissions.permid',
'permcode'
], $where);
$out['status'] = "OK";
if ($filter) {
$recordsFiltered = $database->count('assigned_permissions', [
"[>]accounts" => ['uid' => 'uid'],
"[>]permissions" => ['permid' => 'permid']
], 'assigned_permissions.uid', $wherenolimit);
} else {
$recordsFiltered = $out['recordsTotal'];
}
$out['recordsFiltered'] = $recordsFiltered;
for ($i = 0; $i < count($data); $i++) {
$data[$i]["delbtn"] = '<a class="btn btn-danger btn-xs" href="app.php?page=delpermission&uid=' . $data[$i]['uid'] . '&pid=' . $data[$i]['permid'] . '"><i class="fa fa-trash"></i> ' . lang("delete", false) . '</a>';
}
$out['perms'] = $data;
echo json_encode($out);

@ -76,6 +76,34 @@ define("PAGES", [
"title" => "delete manager",
"navbar" => false
],
"permissions" => [
"title" => "permissions",
"navbar" => true,
"icon" => "key",
"styles" => [
"static/css/datatables.min.css",
"static/css/tables.css"
],
"scripts" => [
"static/js/datatables.min.js",
"static/js/permissions.js"
],
],
"addpermission" => [
"title" => "new permission",
"navbar" => false,
"styles" => [
"static/css/easy-autocomplete.min.css"
],
"scripts" => [
"static/js/jquery.easy-autocomplete.min.js",
"static/js/addpermission.js"
]
],
"delpermission" => [
"title" => "delete permission",
"navbar" => false
],
"404" => [
"title" => "404 error"
]

@ -0,0 +1,38 @@
<?php
require_once __DIR__ . '/../required.php';
redirectifnotloggedin();
?>
<form role="form" action="action.php" method="POST">
<div class="panel panel-blue">
<div class="panel-heading">
<h3 class="panel-title">
<i class="fa fa-plus"></i> <?php lang("adding permission"); ?>
</h3>
</div>
<div class="panel-body">
<div class="row">
<div class="col-xs-12 col-md-6">
<div class="form-group">
<label for="user"><i class="fa fa-id-card-o"></i> <?php lang("user"); ?></label>
<input type="text" class="form-control" name="user" id="user" required="required" />
</div>
</div>
<div class="col-xs-12 col-md-6">
<div class="form-group">
<label for="perm"><i class="fa fa-user"></i> <?php lang("permission"); ?></label>
<input type="text" class="form-control" name="perm" id="perm" required="required" />
</div>
</div>
</div>
</div>
<input type="hidden" name="action" value="addpermission" />
<input type="hidden" name="source" value="permissions" />
<div class="panel-footer">
<button type="submit" class="btn btn-success"><i class="fa fa-floppy-o"></i> <?php lang("save"); ?></button>
</div>
</div>
</form>

@ -0,0 +1,54 @@
<?php
require_once __DIR__ . "/../required.php";
redirectifnotloggedin();
if (is_empty($VARS['uid'])) {
header('Location: app.php?page=permissions&msg=user_not_exists');
die();
}
if (!$database->has('permissions', ['permid' => $VARS['pid']])) {
header('Location: app.php?page=permissions&msg=permission_not_exists');
die();
}
?>
<div class="row">
<div class="col-xs-12 col-sm-6 col-sm-offset-3">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">
<?php lang("delete permission") ?>
</h3>
</div>
<div class="panel-body">
<div style="text-align: center;">
<p><i class="fa fa-exclamation-triangle fa-5x"></i></p>
<h4><?php lang("really delete permission") ?></h4>
<?php
$data = $database->select('assigned_permissions', [
"[>]accounts" => ['uid' => 'uid'],
"[>]permissions" => ['permid' => 'permid']
], [
'username',
'realname',
'permcode',
'perminfo'
], ["AND" => ['assigned_permissions.permid' => $VARS['pid'], 'assigned_permissions.uid' => $VARS['uid']]])[0];
?>
<div class="list-group">
<div class="list-group-item">
<i class="fa fa-fw fa-user"></i> <?php echo $data['realname']; ?> (<?php echo $data['username']; ?>)
</div>
<div class="list-group-item">
<i class="fa fa-fw fa-key"></i> <?php echo $data['permcode']; ?> (<?php echo $data['perminfo']; ?>)
</div>
</div>
</div>
</div>
<div class="panel-footer">
<a href="action.php?action=delpermission&source=permissions&uid=<?php echo htmlspecialchars($VARS['uid']); ?>&pid=<?php echo htmlspecialchars($VARS['pid']); ?>" class="btn btn-danger"><i class="fa fa-times"></i> <?php lang('delete'); ?></a>
<a href="app.php?page=permissions" class="btn btn-primary pull-right"><i class="fa fa-arrow-left"></i> <?php lang('cancel'); ?></a>
</div>
</div>
</div>
</div>

@ -0,0 +1,27 @@
<?php
require_once __DIR__ . '/../required.php';
redirectifnotloggedin();
?>
<div class="btn-group" style="margin-bottom: 10px;">
<a href="app.php?page=addpermission" class="btn btn-success"><i class="fa fa-plus"></i> <?php lang("new permission"); ?></a>
</div>
<table id="permtable" class="table table-bordered table-striped">
<thead>
<tr>
<th data-priority="0"></th>
<th data-priority="1"><?php lang('actions'); ?></th>
<th data-priority="1"><i class="fa fa-fw fa-user"></i> <?php lang('user'); ?></th>
<th data-priority="1"><i class="fa fa-fw fa-key"></i> <?php lang('permission'); ?></th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<th data-priority="0"></th>
<th data-priority="1"><?php lang('actions'); ?></th>
<th data-priority="1"><i class="fa fa-fw fa-user"></i> <?php lang('user'); ?></th>
<th data-priority="1"><i class="fa fa-fw fa-key"></i> <?php lang('permission'); ?></th>
</tfoot>
</table>

@ -0,0 +1,47 @@
$("#user").easyAutocomplete({
url: "action.php",
ajaxSettings: {
dataType: "json",
method: "GET",
data: {
action: "autocomplete_user"
}
},
preparePostData: function (data) {
data.q = $("#user").val();
return data;
},
getValue: function (element) {
return element.username;
},
template: {
type: "custom",
method: function (value, item) {
return item.name + " <i class=\"small\">" + item.username + "</i>";
}
}
});
$("#perm").easyAutocomplete({
url: "action.php",
ajaxSettings: {
dataType: "json",
method: "GET",
data: {
action: "autocomplete_permission"
}
},
preparePostData: function (data) {
data.q = $("#perm").val();
return data;
},
getValue: function (element) {
return element.name;
},
template: {
type: "custom",
method: function (value, item) {
return item.name + " <i class=\"small\">" + item.info + "</i>";
}
}
});

@ -0,0 +1,47 @@
$('#permtable').DataTable({
responsive: {
details: {
display: $.fn.dataTable.Responsive.display.modal({
header: function (row) {
var data = row.data();
return "<i class=\"fa fa-key fa-fw\"></i> " + data[2] + " | " + data[3];
}
}),
renderer: $.fn.dataTable.Responsive.renderer.tableAll({
tableClass: 'table'
}),
type: "column"
}
},
columnDefs: [
{
targets: 0,
className: 'control',
orderable: false
},
{
targets: 1,
orderable: false
}
],
order: [
[2, 'asc']
],
serverSide: true,
ajax: {
url: "lib/getpermtable.php",
dataFilter: function (data) {
var json = jQuery.parseJSON(data);
json.data = [];
json.perms.forEach(function (row) {
json.data.push([
"",
row.delbtn,
row.realname + " (" + row.username + ")",
row.permcode
]);
});
return JSON.stringify(json);
}
}
});
Loading…
Cancel
Save