mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Add manager field to users
This commit is contained in:
parent
f3590ded0f
commit
c1d16522d0
@ -132,6 +132,10 @@ class UsersController:
|
||||
def 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):
|
||||
if user_crafty_data is None:
|
||||
user_crafty_data = {}
|
||||
@ -206,6 +210,7 @@ class UsersController:
|
||||
def add_user(
|
||||
self,
|
||||
username,
|
||||
manager,
|
||||
password,
|
||||
email="default@example.com",
|
||||
enabled: bool = True,
|
||||
@ -213,6 +218,7 @@ class UsersController:
|
||||
):
|
||||
return self.users_helper.add_user(
|
||||
username,
|
||||
manager,
|
||||
password=password,
|
||||
email=email,
|
||||
enabled=enabled,
|
||||
@ -236,6 +242,8 @@ class UsersController:
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
@staticmethod
|
||||
|
@ -6,6 +6,7 @@ from peewee import (
|
||||
ForeignKeyField,
|
||||
CharField,
|
||||
AutoField,
|
||||
IntegerField,
|
||||
DateTimeField,
|
||||
BooleanField,
|
||||
CompositeKey,
|
||||
@ -40,6 +41,7 @@ class Users(BaseModel):
|
||||
server_order = CharField(default="")
|
||||
preparing = BooleanField(default=False)
|
||||
hints = BooleanField(default=True)
|
||||
manager = IntegerField(default=None, null=True)
|
||||
|
||||
class Meta:
|
||||
table_name = "users"
|
||||
@ -138,6 +140,11 @@ class HelperUsers:
|
||||
user_query = Users.select().where(Users.user_id == user_id)
|
||||
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
|
||||
def get_user(user_id):
|
||||
if user_id == 0:
|
||||
@ -192,6 +199,7 @@ class HelperUsers:
|
||||
def add_user(
|
||||
self,
|
||||
username: str,
|
||||
manager: str,
|
||||
password: str = None,
|
||||
email: t.Optional[str] = None,
|
||||
enabled: bool = True,
|
||||
@ -209,6 +217,7 @@ class HelperUsers:
|
||||
Users.enabled: enabled,
|
||||
Users.superuser: superuser,
|
||||
Users.created: Helpers.get_time_as_string(),
|
||||
Users.manager: manager,
|
||||
}
|
||||
).execute()
|
||||
return user_id
|
||||
@ -216,6 +225,7 @@ class HelperUsers:
|
||||
@staticmethod
|
||||
def add_rawpass_user(
|
||||
username: str,
|
||||
manager: str,
|
||||
password: str = "",
|
||||
email: t.Optional[str] = "default@example.com",
|
||||
enabled: bool = True,
|
||||
@ -229,6 +239,7 @@ class HelperUsers:
|
||||
Users.enabled: enabled,
|
||||
Users.superuser: superuser,
|
||||
Users.created: Helpers.get_time_as_string(),
|
||||
Users.manager: manager,
|
||||
}
|
||||
).execute()
|
||||
return user_id
|
||||
|
@ -340,10 +340,11 @@ class CreateUser(ApiHandler):
|
||||
|
||||
new_username = self.get_argument("username").lower()
|
||||
new_pass = self.get_argument("password")
|
||||
manager = int(user_obj["user_id"])
|
||||
|
||||
if new_username:
|
||||
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(
|
||||
|
@ -860,6 +860,11 @@ class PanelHandler(BaseHandler):
|
||||
page_data["users"] = self.controller.users.get_all_users()
|
||||
page_data["roles"] = self.controller.roles.get_all_roles()
|
||||
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"
|
||||
|
||||
@ -904,8 +909,14 @@ class PanelHandler(BaseHandler):
|
||||
)
|
||||
if superuser:
|
||||
page_data["super-disabled"] = ""
|
||||
page_data["users"] = self.controller.users.get_all_users()
|
||||
else:
|
||||
page_data["super-disabled"] = "disabled"
|
||||
|
||||
page_data["manager"] = {
|
||||
"user_id": -100,
|
||||
"username": "None",
|
||||
}
|
||||
for file in sorted(
|
||||
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["servers_all"] = self.controller.servers.get_all_defined_servers()
|
||||
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[
|
||||
"permissions_all"
|
||||
] = self.controller.crafty_perms.list_defined_crafty_permissions()
|
||||
@ -1115,6 +1137,13 @@ class PanelHandler(BaseHandler):
|
||||
"/panel/error?error=Unauthorized access: not a user editor"
|
||||
)
|
||||
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["role-servers"] = []
|
||||
@ -1962,6 +1991,13 @@ class PanelHandler(BaseHandler):
|
||||
else:
|
||||
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 username is None or username == "":
|
||||
self.redirect("/panel/error?error=Invalid username")
|
||||
@ -2013,6 +2049,7 @@ class PanelHandler(BaseHandler):
|
||||
|
||||
user_data = {
|
||||
"username": username,
|
||||
"manager": manager,
|
||||
"password": password0,
|
||||
"email": email,
|
||||
"enabled": enabled,
|
||||
@ -2158,6 +2195,15 @@ class PanelHandler(BaseHandler):
|
||||
if username is None or username == "":
|
||||
self.redirect("/panel/error?error=Invalid username")
|
||||
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?
|
||||
if self.controller.users.get_id_by_name(username) is not None:
|
||||
self.redirect("/panel/error?error=User exists")
|
||||
@ -2172,6 +2218,7 @@ class PanelHandler(BaseHandler):
|
||||
|
||||
user_id = self.controller.users.add_user(
|
||||
username,
|
||||
manager=manager,
|
||||
password=password0,
|
||||
email=email,
|
||||
enabled=enabled,
|
||||
|
@ -96,6 +96,7 @@ class ApiUsersIndexHandler(BaseApiHandler):
|
||||
|
||||
username = data["username"]
|
||||
username = str(username).lower()
|
||||
manager = int(user["user_id"])
|
||||
password = data["password"]
|
||||
email = data.get("email", "default@example.com")
|
||||
enabled = data.get("enabled", True)
|
||||
@ -149,6 +150,7 @@ class ApiUsersIndexHandler(BaseApiHandler):
|
||||
# TODO: do this in the most efficient way
|
||||
user_id = self.controller.users.add_user(
|
||||
username,
|
||||
manager,
|
||||
password,
|
||||
email,
|
||||
enabled,
|
||||
|
@ -87,6 +87,38 @@
|
||||
<td><a href="/panel/edit_user?id={{user.user_id}}"><i class="fas fa-pencil-alt"></i></a></td>
|
||||
</tr>
|
||||
{% 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>
|
||||
</table>
|
||||
</div>
|
||||
|
@ -121,6 +121,26 @@ data['lang']) }}{% end %}
|
||||
{% end %}
|
||||
</select>
|
||||
</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>
|
||||
|
||||
@ -284,7 +304,11 @@ data['lang']) }}{% end %}
|
||||
{% else %}
|
||||
<button class="btn btn-sm btn-danger delete-user"><i class="fas fa-trash"></i> {{
|
||||
translate('userConfig', 'deleteUserB', data['lang']) }}</a>
|
||||
{% end %}
|
||||
{% end %}</button>
|
||||
|
||||
<br>
|
||||
<br>
|
||||
<h4>Manager: {{data['manager']['username'] }}</h4>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
16
app/migrations/20220819_user_manager.py
Normal file
16
app/migrations/20220819_user_manager.py
Normal 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.
|
||||
"""
|
Loading…
Reference in New Issue
Block a user