Initial commit for role managers

This commit is contained in:
Andrew 2022-08-20 08:32:39 -04:00
parent f75286e112
commit bd64f0d2ae
7 changed files with 191 additions and 88 deletions

View File

@ -64,8 +64,12 @@ class RolesController:
HelperRoles.update_role(role_id, up_data)
@staticmethod
def add_role(role_name):
return HelperRoles.add_role(role_name)
def add_role(role_name, manager):
return HelperRoles.add_role(role_name, manager)
@staticmethod
def get_managed_roles(exec_user_id):
return HelperRoles.get_managed_roles(exec_user_id)
class RoleServerJsonType(t.TypedDict):
server_id: t.Union[str, int]

View File

@ -6,6 +6,7 @@ from peewee import (
DoesNotExist,
AutoField,
DateTimeField,
IntegerField,
)
from playhouse.shortcuts import model_to_dict
@ -22,6 +23,7 @@ class Roles(BaseModel):
created = DateTimeField(default=datetime.datetime.now)
last_update = DateTimeField(default=datetime.datetime.now)
role_name = CharField(default="", unique=True, index=True)
manager = IntegerField(null=True)
class Meta:
table_name = "roles"
@ -38,6 +40,10 @@ class HelperRoles:
def get_all_roles():
return Roles.select()
@staticmethod
def get_managed_roles(exec_user_id):
return Roles.select().where(Roles.manager == exec_user_id)
@staticmethod
def get_all_role_ids() -> t.List[int]:
return [role.role_id for role in Roles.select(Roles.role_id).execute()]
@ -71,11 +77,12 @@ class HelperRoles:
)
@staticmethod
def add_role(role_name):
def add_role(role_name, manager):
role_id = Roles.insert(
{
Roles.role_name: role_name.lower(),
Roles.created: Helpers.get_time_as_string(),
Roles.manager: manager,
}
).execute()
return role_id

View File

@ -865,6 +865,13 @@ class PanelHandler(BaseHandler):
page_data["managed_users"] = self.controller.users.get_managed_users(
exec_user["user_id"]
)
page_data["assigned_roles"] = []
for item in page_data["roles"]:
page_data["assigned_roles"].append(item.role_id)
page_data["managed_roles"] = self.controller.roles.get_managed_roles(
exec_user["user_id"]
)
template = "panel/panel_config.html"
@ -1245,6 +1252,11 @@ class PanelHandler(BaseHandler):
defined_servers = self.controller.servers.get_authorized_servers(
exec_user["user_id"]
)
page_data["role_manager"] = {
"user_id": -100,
"username": "None",
}
page_servers = []
for server in defined_servers:
if server not in page_servers:
@ -1285,6 +1297,16 @@ class PanelHandler(BaseHandler):
page_data["user-roles"] = user_roles
page_data["users"] = self.controller.users.get_all_users()
if page_data["role"]["manager"] is not None:
page_data["role_manager"] = self.controller.users.get_user_by_id(
page_data["role"]["manager"]
)
else:
page_data["role_manager"] = {
"user_id": -100,
"username": "None",
}
if EnumPermissionsCrafty.ROLES_CONFIG not in exec_user_crafty_permissions:
self.redirect(
"/panel/error?error=Unauthorized access: not a role editor"

View File

@ -283,7 +283,8 @@ class ServerHandler(BaseHandler):
new_server_id
).get("server_uuid")
role_id = self.controller.roles.add_role(
f"Creator of Server with uuid={new_server_uuid}"
f"Creator of Server with uuid={new_server_uuid}",
exec_user["user_id"],
)
self.controller.server_perms.add_role_server(
new_server_id, role_id, "11111111"
@ -420,7 +421,8 @@ class ServerHandler(BaseHandler):
new_server_id
).get("server_uuid")
role_id = self.controller.roles.add_role(
f"Creator of Server with uuid={new_server_uuid}"
f"Creator of Server with uuid={new_server_uuid}",
exec_user["user_id"],
)
self.controller.server_perms.add_role_server(
new_server_id, role_id, "11111111"
@ -545,7 +547,8 @@ class ServerHandler(BaseHandler):
new_server_id
).get("server_uuid")
role_id = self.controller.roles.add_role(
f"Creator of Server with uuid={new_server_uuid}"
f"Creator of Server with uuid={new_server_uuid}",
exec_user["user_id"],
)
self.controller.server_perms.add_role_server(
new_server_id, role_id, "11111111"

View File

@ -177,7 +177,32 @@
<td><a href="/panel/edit_role?id={{role.role_id}}"><i class="fas fa-pencil-alt"></i></a></td>
</tr>
{% end %}
{% for role in data['managed_roles'] %}
{% if role.role_id not in data['assigned_roles'] %}
<tr>
<td>{{ role.role_name }}</td>
<td id="role_list_{{role.role_id}}">
<ul id="{{role.role_id}}">
{% for item in data['role-servers'][role.role_id] %}
<li>{{item}}</li>
{% end %}
</ul>
</td>
<td>
<ul>
{% for user in data['users'] %}
{% for ruser in data['user-roles'][user.user_id] %}
{% if ruser == role.role_name %}
<li>{{ user.username }}</li>
{% end %}
{% end %}
{% end %}
</ul>
</td>
<td><a href="/panel/edit_role?id={{role.role_id}}"><i class="fas fa-pencil-alt"></i></a></td>
</tr>
{& end %}
{% end %}
</tbody>
</table>
</div>

View File

@ -39,7 +39,8 @@
<div class="card-body pt-0">
<ul class="nav nav-tabs col-md-12 tab-simple-styled " role="tablist">
<li class="nav-item">
<a class="nav-link active" href="/panel/edit_role?id={{ data['role']['role_id'] }}&subpage=config" role="tab" aria-selected="true">
<a class="nav-link active" href="/panel/edit_role?id={{ data['role']['role_id'] }}&subpage=config"
role="tab" aria-selected="true">
<i class="fas fa-cogs"></i>{{ translate('rolesConfig', 'config', data['lang']) }}</a>
</li>
<!-- <li class="nav-item">
@ -49,26 +50,33 @@
</ul>
<div class="">
<div class="">
<form class="forms-sample" method="post" action="{{ '/panel/add_role' if data['new_role'] else '/panel/edit_role' }}">
<form class="forms-sample" method="post"
action="{{ '/panel/add_role' if data['new_role'] else '/panel/edit_role' }}">
{% raw xsrf_form_html() %}
<input type="hidden" name="id" value="{{ data['role']['role_id'] }}">
<input type="hidden" name="subpage" value="config">
<div class="card">
<div class="card-header header-sm d-flex justify-content-between align-items-center">
<h4 class="card-title"><i class="fas fa-user-tag"></i> {{ translate('rolesConfig', 'roleTitle', data['lang']) }}</h4>
<h4 class="card-title"><i class="fas fa-user-tag"></i> {{ translate('rolesConfig', 'roleTitle',
data['lang']) }}</h4>
</div>
<div class="card-body">
<div class="form-group">
<label for="role_name">{{ translate('rolesConfig', 'roleName', data['lang']) }} <small class="text-muted ml-1"> - {{ translate('rolesConfig', 'roleDesc', data['lang']) }}</small> </label>
<input type="text" class="form-control" name="role_name" id="role_name" value="{{ data['role']['role_name'] }}" placeholder="Role Name" >
<label for="role_name">{{ translate('rolesConfig', 'roleName', data['lang']) }} <small
class="text-muted ml-1"> - {{ translate('rolesConfig', 'roleDesc', data['lang']) }}</small>
</label>
<input type="text" class="form-control" name="role_name" id="role_name"
value="{{ data['role']['role_name'] }}" placeholder="Role Name">
</div>
</div>
</div>
<div class="card">
<div class="card-header header-sm d-flex justify-content-between align-items-center">
<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>
<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>
</div>
<div class="card-body">
<div class="form-group">
@ -86,6 +94,7 @@
--table-border-width: 1px;
border-collapse: collapse;
}
th.rotate-column-header {
/* Something you can count on */
height: 140px;
@ -100,14 +109,17 @@
rotate(315deg);
width: 30px;
}
th.rotate-column-header>div>span {
border-bottom: 1px solid #ccc;
padding: 5px 10px;
}
th.rotate {
white-space: nowrap;
position: relative;
}
th.rotate>div {
/* place div at bottom left of the th parent */
position: absolute;
@ -118,12 +130,12 @@
/* 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:
translate(calc(100% - var(--table-border-width) / 2), var(--table-border-width))
rotate(-45deg);
translate(calc(100% - var(--table-border-width) / 2), var(--table-border-width)) rotate(-45deg);
transform-origin: 0% calc(100% - var(--table-border-width));
transition: transform 500ms;
width: 100%;
}
th.rotate>div>span {
/* make sure the bottom of the span is matched up with the bottom of the parent div */
position: absolute;
@ -134,6 +146,7 @@
padding-bottom: 5px;
user-select: none;
}
table.rotate-table>tbody td {
border-right: var(--table-border-width) solid #383e5d;
/* 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*/
@ -147,6 +160,7 @@
th.rotate>div {
transform: translate(15px, 0px) rotate(0deg);
}
th.rotate>div>span {
border-bottom-color: transparent;
}
@ -154,9 +168,13 @@
</style>
<tr class="rounded">
<th>{{ translate('rolesConfig', 'serverName', data['lang']) }}</th>
<th class="rotate"><div><span>{{ translate('rolesConfig', 'serverAccess', data['lang']) }}</span></div></th>
<th class="rotate">
<div><span>{{ translate('rolesConfig', 'serverAccess', data['lang']) }}</span></div>
</th>
{% for permission in data['permissions_all'] %}
<th class="rotate"><div><span>{{ permission.name }}</span></div></th>
<th class="rotate">
<div><span>{{ permission.name }}</span></div>
</th>
{% end %}
</tr>
</thead>
@ -165,27 +183,26 @@
<tr>
<td>{{ server['server_name'] }}</td>
<td>
<input type="checkbox" class="" onclick="enable_disable(event)" data-id="{{server['server_id']}}"
id="server_{{ server['server_id'] }}_access"
name="server_{{ server['server_id'] }}_access"
{{ 'checked' if server['server_id'] in data['role']['servers'] else '' }}
autocomplete="off" value="1">
<input type="checkbox" class="" onclick="enable_disable(event)"
data-id="{{server['server_id']}}" id="server_{{ server['server_id'] }}_access"
name="server_{{ server['server_id'] }}_access" {{ 'checked' if server['server_id'] in
data['role']['servers'] else '' }} autocomplete="off" value="1">
</td>
{% for permission in data['permissions_all'] %}
{% if server['server_id'] in data['role']['servers'] %}
<td>
<input type="checkbox" class="{{server['server_id']}}_perms"
id="permission_{{ server['server_id'] }}_{{ permission.name }}"
name="permission_{{ server['server_id'] }}_{{ permission.name }}"
{{ 'checked' if permission in data['permissions_dict'].get(server['server_id'], []) else '' }}
name="permission_{{ server['server_id'] }}_{{ permission.name }}" {{ 'checked' if
permission in data['permissions_dict'].get(server['server_id'], []) else '' }}
autocomplete="off" value="1">
</td>
{% else %}
<td>
<input type="checkbox" class="{{server['server_id']}}_perms"
id="permission_{{ server['server_id'] }}_{{ permission.name }}"
name="permission_{{ server['server_id'] }}_{{ permission.name }}"
autocomplete="off" value="1" disabled>
name="permission_{{ server['server_id'] }}_{{ permission.name }}" autocomplete="off"
value="1" disabled>
</td>
{% end %}
{% end %}
@ -201,18 +218,22 @@
<div class="card">
<div class="card-header header-sm d-flex justify-content-between align-items-center">
<h4 class="card-title"><i class="fas fa-settings"></i> {{ translate('panelConfig', 'save', data['lang']) }}</h4>
<h4 class="card-title"><i class="fas fa-settings"></i> {{ translate('panelConfig', 'save',
data['lang']) }}</h4>
</div>
<div class="card-body">
<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>
<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>
</div>
</div>
</form>
<div class="card">
<div class="card-header header-sm d-flex justify-content-between align-items-center">
<h4 class="card-title"><i class="fas fa-users"></i> {{ translate('rolesConfig', 'roleUsers', data['lang']) }}</h4>
<h4 class="card-title"><i class="fas fa-users"></i> {{ translate('rolesConfig', 'roleUsers',
data['lang']) }}</h4>
</div>
<div class="card-body">
<div class="table-responsive">
@ -250,16 +271,21 @@
<p class="mb-0">
{{ translate('rolesConfig', 'created', data['lang']) }} {{ str(data['role']['created']) }}
<br />
{{ translate('rolesConfig', 'configUpdate', data['lang']) }} {{ str(data['role']['last_update']) }}
{{ translate('rolesConfig', 'configUpdate', data['lang']) }} {{ str(data['role']['last_update'])
}}
<br />
Manager: {{ data['role_manager']['username'] }}
<br />
</p>
</blockquote>
<div class="text-center">
{% if data['new_role'] %}
<a class="btn btn-sm btn-danger disabled"><i class="fas fa-trash"></i>{{ translate('rolesConfig', 'delRole', data['lang']) }}</a><br />
<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>
{% else %}
<a href="/panel/remove_role?id={{ data['role']['role_id'] }}" class="btn btn-sm btn-danger"><i class="fas fa-trash"></i>{{ translate('rolesConfig', 'delRole', data['lang']) }}</a>
<a href="/panel/remove_role?id={{ data['role']['role_id'] }}" class="btn btn-sm btn-danger"><i
class="fas fa-trash"></i>{{ translate('rolesConfig', 'delRole', data['lang']) }}</a>
{% end %}
</div>
</div>

View File

@ -0,0 +1,16 @@
# Generated by database migrator
import peewee
def migrate(migrator, database, **kwargs):
migrator.add_columns("roles", role_manager=peewee.IntegerField(null=True))
"""
Write your migrations here.
"""
def rollback(migrator, database, **kwargs):
migrator.drop_columns("roles", ["role_manager"])
"""
Write your rollback migrations here.
"""