Adds checkbox option for adding roles at server creation.

Fixes bug on Panel Config page where duplicate servers would be listed.
Changes the way "roles" are served to the page. Dictionary instead of model object.
This commit is contained in:
Andrew 2021-11-17 13:08:41 -05:00
parent ce25a8fa4d
commit 327122348d
3 changed files with 99 additions and 36 deletions

View File

@ -315,7 +315,8 @@ class PanelHandler(BaseHandler):
user_servers = self.controller.servers.get_authorized_servers(user.user_id)
servers = []
for server in user_servers:
servers.append(server['server_name'])
if server['server_name'] not in servers:
servers.append(server['server_name'])
new_item = {user.user_id: servers}
auth_servers.update(new_item)
data = {user.user_id: user_roles_list}

View File

@ -38,12 +38,17 @@ class ServerHandler(BaseHandler):
defined_servers = self.controller.list_defined_servers()
exec_user_role.add("Super User")
exec_user_crafty_permissions = self.controller.crafty_perms.list_defined_crafty_permissions()
list_roles = []
for role in self.controller.roles.get_all_roles():
list_roles.append(self.controller.roles.get_role(role.role_id))
else:
exec_user_crafty_permissions = self.controller.crafty_perms.get_crafty_permissions_list(exec_user_id)
defined_servers = self.controller.servers.get_authorized_servers(exec_user_id)
list_roles = []
for r in exec_user['roles']:
role = self.controller.roles.get_role(r)
exec_user_role.add(role['role_name'])
list_roles.append(self.controller.roles.get_role(role['role_id']))
template = "public/404.html"
@ -51,7 +56,7 @@ class ServerHandler(BaseHandler):
'version_data': helper.get_version_string(),
'user_data': exec_user_data,
'user_role' : exec_user_role,
'roles' : exec_user_role,
'roles' : list_roles,
'user_crafty_permissions' : exec_user_crafty_permissions,
'crafty_permissions': {
'Server_Creation': Enum_Permissions_Crafty.Server_Creation,
@ -69,7 +74,7 @@ class ServerHandler(BaseHandler):
'lang': self.controller.users.get_user_lang_by_id(exec_user_id)
}
if exec_user['superuser'] == 1:
page_data['roles'] = self.controller.roles.get_all_roles()
page_data['roles'] = list_roles
if page == "step1":
if not exec_user['superuser'] and not self.controller.crafty_perms.can_create_server(exec_user_id):
@ -150,6 +155,10 @@ class ServerHandler(BaseHandler):
if page == "step1":
if not exec_user['superuser']:
user_roles = self.controller.roles.get_all_roles()
else:
user_roles = self.controller.roles.get_all_roles()
server = bleach.clean(self.get_argument('server', ''))
server_name = bleach.clean(self.get_argument('server_name', ''))
min_mem = bleach.clean(self.get_argument('min_memory', ''))
@ -159,8 +168,13 @@ class ServerHandler(BaseHandler):
import_server_path = bleach.clean(self.get_argument('server_path', ''))
import_server_jar = bleach.clean(self.get_argument('server_jar', ''))
server_parts = server.split("|")
roles = bleach.clean(self.get_argument('roles', ''))
print(roles)
captured_roles = []
for role in user_roles:
try:
if bleach.clean(self.get_argument(str(role), '')) == "on":
captured_roles.append(role)
except:
pass
if not server_name:
self.redirect("/panel/error?error=Server name cannot be empty!")
@ -207,7 +221,7 @@ class ServerHandler(BaseHandler):
self.get_remote_ip())
# These lines create a new Role for the Server with full permissions and add the user to it if he's not a superuser
if roles == -1:
if len(captured_roles) == 0:
if not exec_user['superuser']:
new_server_uuid = self.controller.servers.get_server_data_by_id(new_server_id).get("server_uuid")
role_id = self.controller.roles.add_role("Creator of Server with uuid={}".format(new_server_uuid))
@ -216,8 +230,9 @@ class ServerHandler(BaseHandler):
self.controller.crafty_perms.add_server_creation(exec_user_id)
else:
role_id = roles
self.controller.server_perms.add_role_server(new_server_id, role_id, "11111111")
for role in captured_roles:
role_id = role
self.controller.server_perms.add_role_server(new_server_id, role_id, "11111111")
self.controller.stats.record_stats()
self.redirect("/panel/dashboard")

View File

@ -3,30 +3,9 @@
{% block title %}Crafty Controller - {{ translate('serverWizard', 'newServer', data['lang']) }}{% end %}
{% block content %}
<head>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="css/bootstrap-multiselect/bootstrap-multiselect.css" type="text/css">
<script type="text/javascript" src="js/bootstrap-multiselect/bootstrap-multiselect.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function() {
$('#roles').multiselect();
});
</script>
<div class="content-wrapper">
<div class="d-none" id="overlay" onclick="hide(event)"></div>
<div class="row">
<div class="col-sm-6 grid-margin stretch-card">
<div class="card">
@ -93,16 +72,19 @@
</div>
</div>
<div class="col-sm-12">
<h4 class="card-title">Add Roles <small style="text-transform: none;"> - If none are selected Crafty will make one!</small></h4>
<div class="form-group">
<label for="roles">Add your server to your roles</label>
<select class="form-control form-control-lg select-css" id="roles" name="roles" multiple="multiple">
<option value='-1'>None (Crafty Will Create A New Role)</option>
<button onclick="dropDown(event);" class="menu-btn form-control form-control-lg select-css" type="button">
Select a role! &#9013;
</button>
<div class="d-none shadow rounded menu">
{% for r in data['roles'] %}
<option value="{{ r }}">{{ r.role_name.capitalize() }}</option>
<span class="d-block menu-option"><label><input name="{{ r['role_id'] }}" type="checkbox">&nbsp;
{{ r['role_name'].capitalize() }}</label></span>
{% end %}
</select>
</div>
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary mr-2" onclick="wait_msg()">{{ translate('serverWizard', 'buildServer', data['lang']) }}</button>
@ -176,6 +158,20 @@
<input type="number" class="form-control" id="port2" name="port" value="25565" step="1" min="1">
</div>
</div>
<div class="col-sm-12">
<h4 class="card-title">Add Roles <small style="text-transform: none;"> - If none are selected Crafty will make one!</small></h4>
<div class="form-group">
<button onclick="dropDown(event);" class="menu-btn form-control form-control-lg select-css" type="button">
Select a role! &#9013;
</button>
<div class="d-none shadow rounded menu">
{% for r in data['roles'] %}
<span class="d-block menu-option"><label><input name="{{ r['role_id'] }}" type="checkbox">&nbsp;
{{ r['role_name'].capitalize() }}</label></span>
{% end %}
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary mr-2" onclick="wait_msg(true)">{{ translate('serverWizard', 'importServerButton', data['lang']) }}</button>
@ -251,6 +247,21 @@
</div>
</div>
<div class="col-sm-12">
<h4 class="card-title">Add Roles <small style="text-transform: none;"> - If none are selected Crafty will make one!</small></h4>
<div class="form-group">
<button onclick="dropDown(event);" class="menu-btn form-control form-control-lg select-css" type="button">
Select a role! &#9013;
</button>
<div class="d-none shadow rounded menu">
{% for r in data['roles'] %}
<span class="d-block menu-option"><label><input name="{{ r['role_id'] }}" type="checkbox">&nbsp;
{{ r['role_name'].capitalize() }}</label></span>
{% end %}
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary mr-2" onclick="wait_msg(true)">{{ translate('serverWizard', 'importServerButton', data['lang']) }}</button>
<button type="reset" class="btn btn-danger mr-2">{{ translate('serverWizard', 'resetForm', data['lang']) }}</button>
@ -262,12 +273,48 @@
</div>
</div>
</div>
<style>
.menu-btn {
font-size: 0.9em;
padding: 2px 10px;
}
.menu {
padding-top: 10px;
z-index: 200;
margin-top: 4px;
position: absolute;
background-color: #2a2c44;
}
.menu-option {
padding: 6px 20px 6px;
color: white;
}
#overlay {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
z-index: 100;
}
</style>
{% end %}
{% block js%}
<script>
function dropDown(event) {
event.target.parentElement.children[1].classList.remove("d-none");
document.getElementById("overlay").classList.remove("d-none");
}
function hide(event) {
var items = document.getElementsByClassName('menu');
for (let i = 0; i < items.length; i++) {
items[i].classList.add("d-none");
}
document.getElementById("overlay").classList.add("d-none");
}
$( document ).ready(function() {
console.log('ready');
var forms = $('form.server-wizard');