Add manager field to users

This commit is contained in:
amcmanu3 2022-08-19 13:09:59 -04:00
parent f3590ded0f
commit c1d16522d0
8 changed files with 143 additions and 2 deletions

View File

@ -132,6 +132,10 @@ class UsersController:
def set_support_path(user_id, support_path): def set_support_path(user_id, support_path):
HelperUsers.set_support_path(user_id, support_path) HelperUsers.set_support_path(user_id, support_path)
@staticmethod
def get_managed_users(exec_user_id):
return HelperUsers.get_managed_users(exec_user_id)
def update_user(self, user_id: str, user_data=None, user_crafty_data=None): def update_user(self, user_id: str, user_data=None, user_crafty_data=None):
if user_crafty_data is None: if user_crafty_data is None:
user_crafty_data = {} user_crafty_data = {}
@ -206,6 +210,7 @@ class UsersController:
def add_user( def add_user(
self, self,
username, username,
manager,
password, password,
email="default@example.com", email="default@example.com",
enabled: bool = True, enabled: bool = True,
@ -213,6 +218,7 @@ class UsersController:
): ):
return self.users_helper.add_user( return self.users_helper.add_user(
username, username,
manager,
password=password, password=password,
email=email, email=email,
enabled=enabled, enabled=enabled,
@ -236,6 +242,8 @@ class UsersController:
) )
def remove_user(self, user_id): def remove_user(self, user_id):
for user in self.get_managed_users(user_id):
self.update_user(user.user_id, {"manager": None})
return self.users_helper.remove_user(user_id) return self.users_helper.remove_user(user_id)
@staticmethod @staticmethod

View File

@ -6,6 +6,7 @@ from peewee import (
ForeignKeyField, ForeignKeyField,
CharField, CharField,
AutoField, AutoField,
IntegerField,
DateTimeField, DateTimeField,
BooleanField, BooleanField,
CompositeKey, CompositeKey,
@ -40,6 +41,7 @@ class Users(BaseModel):
server_order = CharField(default="") server_order = CharField(default="")
preparing = BooleanField(default=False) preparing = BooleanField(default=False)
hints = BooleanField(default=True) hints = BooleanField(default=True)
manager = IntegerField(default=None, null=True)
class Meta: class Meta:
table_name = "users" table_name = "users"
@ -138,6 +140,11 @@ class HelperUsers:
user_query = Users.select().where(Users.user_id == user_id) user_query = Users.select().where(Users.user_id == user_id)
return user_query return user_query
@staticmethod
def get_managed_users(exec_user_id):
user_query = Users.select().where(Users.manager == exec_user_id)
return user_query
@staticmethod @staticmethod
def get_user(user_id): def get_user(user_id):
if user_id == 0: if user_id == 0:
@ -192,6 +199,7 @@ class HelperUsers:
def add_user( def add_user(
self, self,
username: str, username: str,
manager: str,
password: str = None, password: str = None,
email: t.Optional[str] = None, email: t.Optional[str] = None,
enabled: bool = True, enabled: bool = True,
@ -209,6 +217,7 @@ class HelperUsers:
Users.enabled: enabled, Users.enabled: enabled,
Users.superuser: superuser, Users.superuser: superuser,
Users.created: Helpers.get_time_as_string(), Users.created: Helpers.get_time_as_string(),
Users.manager: manager,
} }
).execute() ).execute()
return user_id return user_id
@ -216,6 +225,7 @@ class HelperUsers:
@staticmethod @staticmethod
def add_rawpass_user( def add_rawpass_user(
username: str, username: str,
manager: str,
password: str = "", password: str = "",
email: t.Optional[str] = "default@example.com", email: t.Optional[str] = "default@example.com",
enabled: bool = True, enabled: bool = True,
@ -229,6 +239,7 @@ class HelperUsers:
Users.enabled: enabled, Users.enabled: enabled,
Users.superuser: superuser, Users.superuser: superuser,
Users.created: Helpers.get_time_as_string(), Users.created: Helpers.get_time_as_string(),
Users.manager: manager,
} }
).execute() ).execute()
return user_id return user_id

View File

@ -340,10 +340,11 @@ class CreateUser(ApiHandler):
new_username = self.get_argument("username").lower() new_username = self.get_argument("username").lower()
new_pass = self.get_argument("password") new_pass = self.get_argument("password")
manager = int(user_obj["user_id"])
if new_username: if new_username:
self.controller.users.add_user( self.controller.users.add_user(
new_username, new_pass, "default@example.com", True, False new_username, manager, new_pass, "default@example.com", True, False
) )
self.return_response( self.return_response(

View File

@ -860,6 +860,11 @@ class PanelHandler(BaseHandler):
page_data["users"] = self.controller.users.get_all_users() page_data["users"] = self.controller.users.get_all_users()
page_data["roles"] = self.controller.roles.get_all_roles() page_data["roles"] = self.controller.roles.get_all_roles()
page_data["auth-servers"][user.user_id] = super_auth_servers page_data["auth-servers"][user.user_id] = super_auth_servers
page_data["managed_users"] = []
else:
page_data["managed_users"] = self.controller.users.get_managed_users(
exec_user["user_id"]
)
template = "panel/panel_config.html" template = "panel/panel_config.html"
@ -904,8 +909,14 @@ class PanelHandler(BaseHandler):
) )
if superuser: if superuser:
page_data["super-disabled"] = "" page_data["super-disabled"] = ""
page_data["users"] = self.controller.users.get_all_users()
else: else:
page_data["super-disabled"] = "disabled" page_data["super-disabled"] = "disabled"
page_data["manager"] = {
"user_id": -100,
"username": "None",
}
for file in sorted( for file in sorted(
os.listdir(os.path.join(self.helper.root_dir, "app", "translations")) os.listdir(os.path.join(self.helper.root_dir, "app", "translations"))
): ):
@ -1077,6 +1088,17 @@ class PanelHandler(BaseHandler):
page_data["roles_all"] = self.controller.roles.get_all_roles() page_data["roles_all"] = self.controller.roles.get_all_roles()
page_data["servers_all"] = self.controller.servers.get_all_defined_servers() page_data["servers_all"] = self.controller.servers.get_all_defined_servers()
page_data["superuser"] = superuser page_data["superuser"] = superuser
if page_data["user"]["manager"] is not None:
page_data["manager"] = self.controller.users.get_user_by_id(
page_data["user"]["manager"]
)
else:
page_data["manager"] = {
"user_id": -100,
"username": "None",
}
if exec_user["superuser"]:
page_data["users"] = self.controller.users.get_all_users()
page_data[ page_data[
"permissions_all" "permissions_all"
] = self.controller.crafty_perms.list_defined_crafty_permissions() ] = self.controller.crafty_perms.list_defined_crafty_permissions()
@ -1115,6 +1137,13 @@ class PanelHandler(BaseHandler):
"/panel/error?error=Unauthorized access: not a user editor" "/panel/error?error=Unauthorized access: not a user editor"
) )
return return
if (
self.controller.users.get_user_by_id(user_id)["manager"]
!= exec_user["user_id"]
) and not exec_user["superuser"]:
self.redirect(
"/panel/error?error=Unauthorized access: you cannot edit this user"
)
page_data["servers"] = [] page_data["servers"] = []
page_data["role-servers"] = [] page_data["role-servers"] = []
@ -1962,6 +1991,13 @@ class PanelHandler(BaseHandler):
else: else:
superuser = 0 superuser = 0
if exec_user["superuser"]:
manager = self.get_argument("manager")
if manager == "":
manager = None
else:
manager = int(manager)
if not exec_user["superuser"]: if not exec_user["superuser"]:
if username is None or username == "": if username is None or username == "":
self.redirect("/panel/error?error=Invalid username") self.redirect("/panel/error?error=Invalid username")
@ -2013,6 +2049,7 @@ class PanelHandler(BaseHandler):
user_data = { user_data = {
"username": username, "username": username,
"manager": manager,
"password": password0, "password": password0,
"email": email, "email": email,
"enabled": enabled, "enabled": enabled,
@ -2158,6 +2195,15 @@ class PanelHandler(BaseHandler):
if username is None or username == "": if username is None or username == "":
self.redirect("/panel/error?error=Invalid username") self.redirect("/panel/error?error=Invalid username")
return return
if exec_user["superuser"]:
manager = self.get_argument("manager")
if manager == "":
manager = None
else:
manager = int(manager)
else:
manager = int(exec_user["user_id"])
# does this user id exist? # does this user id exist?
if self.controller.users.get_id_by_name(username) is not None: if self.controller.users.get_id_by_name(username) is not None:
self.redirect("/panel/error?error=User exists") self.redirect("/panel/error?error=User exists")
@ -2172,6 +2218,7 @@ class PanelHandler(BaseHandler):
user_id = self.controller.users.add_user( user_id = self.controller.users.add_user(
username, username,
manager=manager,
password=password0, password=password0,
email=email, email=email,
enabled=enabled, enabled=enabled,

View File

@ -96,6 +96,7 @@ class ApiUsersIndexHandler(BaseApiHandler):
username = data["username"] username = data["username"]
username = str(username).lower() username = str(username).lower()
manager = int(user["user_id"])
password = data["password"] password = data["password"]
email = data.get("email", "default@example.com") email = data.get("email", "default@example.com")
enabled = data.get("enabled", True) enabled = data.get("enabled", True)
@ -149,6 +150,7 @@ class ApiUsersIndexHandler(BaseApiHandler):
# TODO: do this in the most efficient way # TODO: do this in the most efficient way
user_id = self.controller.users.add_user( user_id = self.controller.users.add_user(
username, username,
manager,
password, password,
email, email,
enabled, enabled,

View File

@ -87,6 +87,38 @@
<td><a href="/panel/edit_user?id={{user.user_id}}"><i class="fas fa-pencil-alt"></i></a></td> <td><a href="/panel/edit_user?id={{user.user_id}}"><i class="fas fa-pencil-alt"></i></a></td>
</tr> </tr>
{% end %} {% end %}
{% for user in data['managed_users'] %}
<tr>
<td><i class="fas fa-user"></i> {{ user.username }}</td>
<td>
{% if user.enabled %}
<span class="text-success">
<i class="fas fa-check-square"></i> Yes
</span>
{% else %}
<span class="text-danger">
<i class="far fa-times-square"></i> No
</span>
{% end %}
</td>
<td id="server_list_{{user.user_id}}">
<ul id="{{user.user_id}}">
{% for item in data['auth-servers'][user.user_id] %}
<li>{{item}}</li>
{% end %}
</ul>
</td>
<td id="role_list_{{user.user_id}}">
<ul>
{% for item in data['user-roles'][user.user_id] %}
<li data-toggle="tooltip" title="{{ item }}">{{item}}</li>
{% end %}
</ul>
</td>
<td><a href="/panel/edit_user?id={{user.user_id}}"><i class="fas fa-pencil-alt"></i></a></td>
</tr>
{% end %}
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@ -121,6 +121,26 @@ data['lang']) }}{% end %}
{% end %} {% end %}
</select> </select>
</div> </div>
{% if data['superuser'] %}
<div class="form-group">
<label class="form-label" for="manager">Select The User's Manager
</label>
<select class="form-select form-control form-control-lg select-css" id="manager" name="manager"
form="user_form">
{% if data["manager"]["username"] != "None" %}
<option value='{{data["manager"]["user_id"]}}'>{{ data["manager"]["username"]
}}</option>
{% end %}
<option value="">None</option>
{% for user in data['users'] %}
{% if user.username != data['user']['username'] and user.user_id != data['manager']['user_id']
%}
<option value="{{user.user_id}}">{{user.username}}</option>
{% end %}
{% end %}
</select>
</div>
{% end %}
</div> </div>
</div> </div>
@ -284,7 +304,11 @@ data['lang']) }}{% end %}
{% else %} {% else %}
<button class="btn btn-sm btn-danger delete-user"><i class="fas fa-trash"></i> {{ <button class="btn btn-sm btn-danger delete-user"><i class="fas fa-trash"></i> {{
translate('userConfig', 'deleteUserB', data['lang']) }}</a> translate('userConfig', 'deleteUserB', data['lang']) }}</a>
{% end %} {% end %}</button>
<br>
<br>
<h4>Manager: {{data['manager']['username'] }}</h4>
</div> </div>
</div> </div>

View File

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