2020-12-27 16:00:26 +00:00
{% extends ../base.html %}
{% block meta %}
{% end %}
2022-01-20 05:05:14 +00:00
{% block title %}Crafty Controller - {{ translate('rolesConfig', 'pageTitle', data['lang']) }}{% end %}
2020-12-27 16:00:26 +00:00
{% block content %}
< div class = "content-wrapper" >
2022-09-27 01:23:47 +00:00
<!-- Page Title Header Starts -->
2020-12-27 16:00:26 +00:00
< div class = "row page-title-header" >
< div class = "col-12" >
< div class = "page-header" >
{% if data['new_role'] %}
2022-09-27 01:23:47 +00:00
< h4 class = "page-title" >
{{ translate('rolesConfig', 'pageTitleNew', data['lang']) }}
< br / >
< small > RID: N/A< / small >
< / h4 >
2020-12-27 16:00:26 +00:00
{% else %}
2022-09-27 01:23:47 +00:00
< h4 class = "page-title" >
{{ translate('rolesConfig', 'pageTitle', data['lang']) }} - {{ data['role']['role_name'] }}
< br / >
< small > RID: {{ data['role']['role_id'] }}< / small >
< / h4 >
2020-12-27 16:00:26 +00:00
{% end %}
< / div >
< / div >
< / div >
<!-- Page Title Header Ends -->
< div class = "row" >
< div class = "col-sm-12 grid-margin" >
< div class = "card" >
< div class = "card-body pt-0" >
2022-05-17 22:09:58 +00:00
< ul class = "nav nav-tabs col-md-12 tab-simple-styled " role = "tablist" >
< li class = "nav-item" >
2022-09-07 17:48:18 +00:00
< a class = "nav-link active" href = "" role = "tab" aria-selected = "true" >
2022-05-17 22:09:58 +00:00
< i class = "fas fa-cogs" > < / i > {{ translate('rolesConfig', 'config', data['lang']) }}< / a >
< / li >
<!-- <li class="nav - item">
< a class = "nav-link" href = "/panel/edit_role?id={{ data['role']['role_id'] }}&subpage=other" role = "tab" aria-selected = "false" >
< i class = "fas fa-folder-tree" > < / i > Other< / a >
< / li > -->
< / ul >
< div class = "" >
< div class = "" >
2022-09-27 01:23:47 +00:00
< form id = "role_form" class = "forms-sample" method = "post" action = "{{ '/panel/add_role' if data['new_role'] else '/panel/edit_role' }}" >
2022-01-21 22:50:04 +00:00
2022-05-17 22:09:58 +00:00
< div class = "card" >
< div class = "card-header header-sm d-flex justify-content-between align-items-center" >
2022-09-27 01:23:47 +00:00
< h4 class = "card-title" > < i class = "fas fa-user-tag" > < / i > {{ translate('rolesConfig', 'roleTitle', data['lang']) }}< / h4 >
2022-05-17 22:09:58 +00:00
< / div >
< div class = "card-body" >
< div class = "form-group" >
2022-09-27 01:23:47 +00:00
< label for = "role_name" > {{ translate('rolesConfig', 'roleName', data['lang']) }} < small class = "text-muted ml-1" > - {{ translate('rolesConfig', 'roleDesc', data['lang']) }}< / small > < / label >
2023-08-16 17:34:52 +00:00
< input type = "text" class = "form-control" name = "name" id = "role_name" value = "{{ data['role']['role_name'] }}" placeholder = "Role Name" >
2022-05-17 22:09:58 +00:00
< / div >
2022-08-20 13:31:33 +00:00
< br / >
{% if data['superuser'] %}
< div class = "form-group" >
< label class = "form-label" for = "manager" > {{ translate('rolesConfig', 'selectManager',
data['lang']) }}
< / label >
< select class = "form-select form-control form-control-lg select-css" id = "manager" name = "manager"
form="role_form">
{% if data["role_manager"]["username"] != "None" %}
< option value = '{{data["role_manager"]["user_id"]}}' > {{ data["role_manager"]["username"]
}}< / option >
{% end %}
< option value = "" > None< / option >
{% for user in data['users'] %}
{% if user.user_id != data['role_manager']['user_id']
%}
< option value = "{{user.user_id}}" > {{user.username}}< / option >
{% end %}
{% end %}
< / select >
< / div >
{% end %}
2022-05-17 22:09:58 +00:00
< / div >
< / div >
2022-01-21 22:50:04 +00:00
2022-05-17 22:09:58 +00:00
< div class = "card" >
< div class = "card-header header-sm d-flex justify-content-between align-items-center" >
2022-09-27 01:23:47 +00:00
< h4 class = "card-title" > < i class = "fas fa-server" > < / i > {{ translate('rolesConfig', 'roleServers', data['lang']) }} < small class = "text-muted ml-1" > {{ translate('rolesConfig', 'serversDesc', data['lang']) }}< / small > < / h4 >
2022-05-17 22:09:58 +00:00
< / div >
< div class = "card-body" >
< div class = "form-group" >
< div class = "table-responsive rotate-table-parent" >
< table class = "table table-hover rotate-table" >
< thead >
< style >
.rotate-table-parent {
padding-top: 2.5rem;
padding-right: 4rem;
}
2021-08-19 22:18:50 +00:00
2022-05-17 22:09:58 +00:00
/* https://css-tricks.com/rotated-table-column-headers-now-with-fewer-magic-numbers/ */
table.rotate-table {
--table-border-width: 1px;
border-collapse: collapse;
}
th.rotate-column-header {
/* Something you can count on */
height: 140px;
white-space: nowrap;
}
2020-12-27 16:00:26 +00:00
2022-09-27 01:23:47 +00:00
th.rotate-column-header > div {
2022-05-17 22:09:58 +00:00
transform:
/* Magic Numbers */
translate(0px, 51px)
/* 315 is 360 - 45 */
rotate(315deg);
width: 30px;
}
2022-09-27 01:23:47 +00:00
th.rotate-column-header > div > span {
2022-05-17 22:09:58 +00:00
border-bottom: 1px solid #ccc;
padding: 5px 10px;
}
th.rotate {
white-space: nowrap;
position: relative;
}
2022-09-27 01:23:47 +00:00
th.rotate > div {
2022-05-17 22:09:58 +00:00
/* place div at bottom left of the th parent */
position: absolute;
bottom: 0;
left: 0;
/* Make sure short labels still meet the corner of the parent otherwise you'll get a gap */
text-align: left;
/* Move the top left corner of the span's bottom-border to line up with the top left corner of the td's border-right border so that the border corners are matched
* Rotate 315 (-45) degrees about matched border corners */
transform:
2022-09-27 01:23:47 +00:00
translate(calc(100% - var(--table-border-width) / 2), var(--table-border-width))
rotate(-45deg);
2022-05-17 22:09:58 +00:00
transform-origin: 0% calc(100% - var(--table-border-width));
transition: transform 500ms;
width: 100%;
}
2022-09-27 01:23:47 +00:00
th.rotate > div > span {
2022-05-17 22:09:58 +00:00
/* make sure the bottom of the span is matched up with the bottom of the parent div */
position: absolute;
bottom: 0;
left: 0;
2022-09-25 21:52:01 +00:00
border-bottom: var(--table-border-width) solid var(--outline);
2022-05-17 22:09:58 +00:00
transition: border-bottom-color 500ms;
padding-bottom: 5px;
user-select: none;
}
2022-09-27 01:23:47 +00:00
table.rotate-table > tbody td {
2022-09-25 21:52:01 +00:00
border-right: var(--table-border-width) solid var(--outline);
2022-05-17 22:09:58 +00:00
/* make sure this is at least as wide as sqrt(2) * height of the tallest letter in your font or the headers will overlap each other*/
min-width: 30px;
padding-top: 2px;
padding-left: 5px;
text-align: right;
}
@media screen and (min-width: 1650px) {
2022-09-27 01:23:47 +00:00
th.rotate > div {
2022-05-17 22:09:58 +00:00
transform: translate(15px, 0px) rotate(0deg);
}
2022-09-27 01:23:47 +00:00
th.rotate > div > span {
2022-05-17 22:09:58 +00:00
border-bottom-color: transparent;
}
}
< / style >
2021-08-21 23:41:25 +00:00
< tr class = "rounded" >
2022-05-17 22:09:58 +00:00
< th > {{ translate('rolesConfig', 'serverName', data['lang']) }}< / th >
2022-09-27 01:23:47 +00:00
< th class = "rotate" > < div > < span > {{ translate('rolesConfig', 'serverAccess', data['lang']) }}< / span > < / div > < / th >
2022-05-17 22:09:58 +00:00
{% for permission in data['permissions_all'] %}
2022-09-27 01:23:47 +00:00
< th class = "rotate" > < div > < span > {{ permission.name }}< / span > < / div > < / th >
2022-05-17 22:09:58 +00:00
{% end %}
2021-08-21 23:41:25 +00:00
< / tr >
< / thead >
< tbody >
2022-09-27 01:23:47 +00:00
{% for server in data['servers_all'] %}
2022-05-17 22:09:58 +00:00
< tr >
< td > {{ server['server_name'] }}< / td >
< td >
2023-08-16 17:34:52 +00:00
< input type = "checkbox" class = "access" onclick = "enable_disable(event)" data-id = "{{server['server_id']}}"
2022-09-27 01:23:47 +00:00
id="server_{{ server['server_id'] }}_access"
name="server_{{ server['server_id'] }}_access"
{{ 'checked' if server['server_id'] in data['role']['servers'] else '' }}
2023-08-16 17:34:52 +00:00
autocomplete="off" value="1" form="dummy">
2022-05-17 22:09:58 +00:00
< / td >
{% for permission in data['permissions_all'] %}
2022-05-18 20:37:38 +00:00
{% if server['server_id'] in data['role']['servers'] %}
2022-09-27 01:23:47 +00:00
< td >
< input type = "checkbox" class = "{{server['server_id']}}_perms"
2022-05-17 22:09:58 +00:00
id="permission_{{ server['server_id'] }}_{{ permission.name }}"
2022-09-27 01:23:47 +00:00
name="permission_{{ server['server_id'] }}_{{ permission.name }}"
{{ 'checked' if permission in data['permissions_dict'].get(server['server_id'], []) else '' }}
2023-08-16 17:34:52 +00:00
autocomplete="off" value="1" form="dummy">
2022-09-27 01:23:47 +00:00
< / td >
2022-05-18 20:37:38 +00:00
{% else %}
< td >
< input type = "checkbox" class = "{{server['server_id']}}_perms"
2022-09-27 01:23:47 +00:00
id="permission_{{ server['server_id'] }}_{{ permission.name }}"
name="permission_{{ server['server_id'] }}_{{ permission.name }}"
2023-08-16 17:34:52 +00:00
autocomplete="off" value="1" disabled form="dummy">
2022-05-18 20:37:38 +00:00
< / td >
{% end %}
2021-08-09 21:11:45 +00:00
{% end %}
2022-05-17 22:09:58 +00:00
< / tr >
2022-09-27 01:23:47 +00:00
{% end %}
2022-05-17 22:09:58 +00:00
2021-08-21 23:41:25 +00:00
< / tbody >
< / table >
2020-12-27 16:00:26 +00:00
< / div >
< / div >
2021-08-21 23:41:25 +00:00
< / div >
2022-05-17 22:09:58 +00:00
< / div >
< div class = "card" >
< div class = "card-header header-sm d-flex justify-content-between align-items-center" >
2022-09-27 01:23:47 +00:00
< h4 class = "card-title" > < i class = "fas fa-settings" > < / i > {{ translate('panelConfig', 'save', data['lang']) }}< / h4 >
2022-05-17 22:09:58 +00:00
< / div >
< div class = "card-body" >
2022-09-27 01:23:47 +00:00
< button type = "submit" class = "btn btn-success mr-2" > < i class = "fas fa-save" > < / i > {{ translate('panelConfig', 'save', data['lang']) }}< / button >
< button type = "reset" onclick = "location.href='/panel/panel_config'" class = "btn btn-light" > < i class = "fas fa-undo-alt" > < / i > {{ translate('panelConfig', 'cancel', data['lang']) }}< / button >
2022-05-17 22:09:58 +00:00
< / div >
< / div >
< / form >
< div class = "card" >
< div class = "card-header header-sm d-flex justify-content-between align-items-center" >
2022-09-27 01:23:47 +00:00
< h4 class = "card-title" > < i class = "fas fa-users" > < / i > {{ translate('rolesConfig', 'roleUsers', data['lang']) }}< / h4 >
2022-05-17 22:09:58 +00:00
< / div >
< div class = "card-body" >
< div class = "table-responsive" >
< table class = "table table-hover" >
< thead >
< tr class = "rounded" >
< th > {{ translate('rolesConfig', 'roleUserName', data['lang']) }}< / th >
< th > < / th >
< / tr >
< / thead >
< tbody >
2022-09-27 01:23:47 +00:00
{% for user in data['users'] %}
2022-05-17 22:09:58 +00:00
{% for ruser in data['user-roles'][user.user_id] %}
2022-09-27 01:23:47 +00:00
{% if ruser == data['role']['role_name'] %}
< tr >
< td > {{ user.username }}< / td >
< td >
< a href = "/panel/edit_user?id={{user.user_id}}" > < i class = "fas fa-user-edit" > < / i > < / a >
< / td >
< / tr >
{% end %}
2020-12-27 16:00:26 +00:00
{% end %}
2022-09-27 01:23:47 +00:00
{% end %}
2022-05-17 22:09:58 +00:00
< / tbody >
< / table >
2021-08-21 23:41:25 +00:00
< / div >
2022-05-17 22:09:58 +00:00
< / div >
< / div >
< div class = "card" >
< div class = "card-body" >
< h4 class = "card-title" > {{ translate('rolesConfig', 'roleConfigArea', data['lang']) }}< / h4 >
< p class = "card-description" > {{ translate('rolesConfig', 'configDesc', data['lang']) }}< / p >
< blockquote class = "blockquote" >
< p class = "mb-0" >
{{ translate('rolesConfig', 'created', data['lang']) }} {{ str(data['role']['created']) }}
2022-09-27 01:23:47 +00:00
< br / >
{{ translate('rolesConfig', 'configUpdate', data['lang']) }} {{ str(data['role']['last_update']) }}
< br / >
{{ translate('userConfig', 'manager', data['lang']) }}: {{ data['role_manager']['username'] }}
< br / >
2022-05-17 22:09:58 +00:00
< / p >
< / blockquote >
< div class = "text-center" >
{% if data['new_role'] %}
2022-09-27 01:23:47 +00:00
< a class = "btn btn-sm btn-danger disabled" > < i class = "fas fa-trash" > < / i > {{ translate('rolesConfig', 'delRole', data['lang']) }}< / a > < br / >
< small > {{ translate('rolesConfig', 'doesNotExist', data['lang']) }}< / small >
2022-05-17 22:09:58 +00:00
{% else %}
2023-08-16 17:34:52 +00:00
< button onclick = "del_role()" class = "btn btn-sm btn-danger" > < i class = "fas fa-trash" > < / i > {{ translate('rolesConfig', 'delRole', data['lang']) }}< / button >
2022-05-17 22:09:58 +00:00
{% end %}
< / div >
2021-08-21 23:41:25 +00:00
< / div >
< / div >
2022-05-17 22:09:58 +00:00
< / div >
2020-12-27 16:00:26 +00:00
< / div >
< / div >
< / div >
< / div >
2022-09-27 01:23:47 +00:00
< / div >
<!-- content - wrapper ends -->
2020-12-27 16:00:26 +00:00
2022-09-27 01:23:47 +00:00
{% end %}
2020-12-27 16:00:26 +00:00
2022-09-27 01:23:47 +00:00
{% block js %}
< script >
2020-12-27 16:00:26 +00:00
2022-09-27 01:23:47 +00:00
function enable_disable(event) {
2022-05-18 20:37:38 +00:00
let server_id = event.target.getAttribute('data-id');
console.log(server_id);
if (document.getElementById("server_" + server_id + "_access").checked) {
2022-09-27 01:23:47 +00:00
$('.'+server_id+'_perms').attr('disabled', false);
$('.'+server_id+'_perms').attr('enabled', true);
}else{
$('.'+server_id+'_perms').prop('checked', false);
$('.'+server_id+'_perms').attr('disabled', true);
$('.'+server_id+'_perms').attr('enabled', false);
2022-05-18 20:37:38 +00:00
}
2020-12-27 16:00:26 +00:00
2022-05-18 20:37:38 +00:00
}
2020-12-27 16:00:26 +00:00
//used to get cookies from browser - this is part of tornados xsrf protection - it's for extra security
function getCookie(name) {
2022-09-27 01:23:47 +00:00
var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
return r ? r[1] : undefined;
2020-12-27 16:00:26 +00:00
}
2023-03-13 19:54:44 +00:00
function gather_server_json() {
servers = [];
for (s = 0; s < page_servers.length ; s + + ) {
mask = ""
2023-03-15 15:17:39 +00:00
for (i = 0; i < permissions.length ; i + + ) {
if ($(`#permission_${page_servers[s].id}_${permissions[i]}`).prop('checked')){
mask += "1"
}else{
mask += "0"
}
2023-03-13 19:54:44 +00:00
}
2023-03-15 15:17:39 +00:00
servers.push(JSON.stringify({"id": page_servers[s].id, "permissions": mask}));
2023-03-13 19:54:44 +00:00
}
2023-03-15 15:17:39 +00:00
return servers;
2023-03-13 19:54:44 +00:00
}
2022-09-27 01:23:47 +00:00
$( document ).ready(function() {
console.log( "ready!" );
2020-12-27 16:00:26 +00:00
});
2023-03-13 19:54:44 +00:00
const roleId = new URLSearchParams(document.location.search).get('id');
2023-08-16 17:34:52 +00:00
function replacer(key, value) {
if (key === "permissions"){
return value;
}
if (key === "servers" & & value.length === 0){
return value;
}
if (typeof value == "boolean") {
console.log(value);
return value
} else {
return (isNaN(value) ? value : +value);
}
}
2023-03-13 19:54:44 +00:00
2023-08-16 17:34:52 +00:00
async function del_role(){
var token = getCookie("_xsrf")
let res = await fetch(`/api/v2/roles/${roleId}`, {
method: "DELETE",
headers: {
'X-XSRFToken': token
},
});
let responseData = await res.json();
if (responseData.status === "ok") {
window.location.href = "/panel/panel_config";
} else {
bootbox.alert({
title: responseData.error,
message: responseData.error_data
});
}
}
$("#role_form").on("submit", async function (e) {
2023-03-13 19:54:44 +00:00
e.preventDefault();
var token = getCookie("_xsrf")
2023-08-16 17:34:52 +00:00
let roleForm = document.getElementById("role_form");
let server_ids = $('.access').map(function() {
if ($(this).is(':checked')){
return $(this).data('id');
}
}).get();
let servers = []
for(i=0; i < server_ids.length ; i + + ) {
arrchecked = $(`.${server_ids[i]}_perms`).map(function() {
if(this.checked){
return "1";
}else{
return "0"
}
}).get();
servers.push({"server_id": server_ids[i], "permissions": arrchecked.join("")});
}
console.log(servers)
2023-03-13 19:54:44 +00:00
2023-08-16 17:34:52 +00:00
let formData = new FormData(roleForm);
2023-03-13 19:54:44 +00:00
//Create an object from the form data entries
let formDataObject = Object.fromEntries(formData.entries());
2023-08-16 17:34:52 +00:00
formDataObject.servers = servers;
console.log(formDataObject);
//We need to make sure these are sent regardless of whether or not they're checked
2023-03-13 19:54:44 +00:00
// Format the plain form data as JSON
let formDataJsonString = JSON.stringify(formDataObject, replacer);
2023-08-16 17:34:52 +00:00
console.log(formDataJsonString);
if (roleId){
url = `/api/v2/roles/${roleId}`
method = 'PATCH'
}else{
url = `/api/v2/roles/`
method = 'POST'
}
let res = await fetch(url, {
method: method,
2023-03-13 19:54:44 +00:00
headers: {
'X-XSRFToken': token
},
body: formDataJsonString,
});
let responseData = await res.json();
if (responseData.status === "ok") {
2023-08-16 17:34:52 +00:00
window.location.href = "/panel/panel_config";
2023-03-13 19:54:44 +00:00
} else {
bootbox.alert({
title: responseData.error,
message: responseData.error_data
2023-08-16 17:34:52 +00:00
});
}
2023-03-13 19:54:44 +00:00
});
2020-12-27 16:00:26 +00:00
2022-09-27 01:23:47 +00:00
< / script >
2020-12-27 16:00:26 +00:00
2022-09-27 01:23:47 +00:00
{% end %}