Reworking Functions After User_Servers Deletion

This commit is contained in:
Silversthorn 2021-08-22 18:50:30 +02:00
parent 0b29bdf855
commit 1acb7827ae
4 changed files with 15 additions and 103 deletions

View File

@ -136,7 +136,7 @@ class Controller:
server_list = []
for item in servers:
server_list.append(item)
role_servers = db_helper.get_authorized_servers_from_roles(userId)
role_servers = db_helper.get_authorized_servers(userId)
for item in role_servers:
server_list.append(item)
logger.debug("servers list = {}".format(servers))

View File

@ -287,7 +287,6 @@ class db_shortcuts:
def remove_server(server_id):
with database.atomic():
Role_Servers.delete().where(Role_Servers.server_id == server_id).execute()
User_Servers.delete().where(User_Servers.server_id == server_id).execute()
Servers.delete().where(Servers.server_id == server_id).execute()
@staticmethod
@ -305,60 +304,12 @@ class db_shortcuts:
@staticmethod
def get_authorized_servers(user_id):
user_servers = User_Servers.select().where(User_Servers.user_id == user_id)
server_data = []
for u in user_servers:
server_data.append(db_helper.get_server_data_by_id(u.server_id))
return server_data
@staticmethod
def get_all_authorized_servers(user_id):
user_servers = User_Servers.select().where(User_Servers.user_id == user_id)
user_roles = User_Roles.select().where(User_Roles.user_id == user_id)
roles_list = []
role_server = []
server_data = []
for u in user_servers:
server_data.append(db_helper.get_server_data_by_id(u.server_id))
for u in user_roles:
roles_list.append(db_helper.get_role(u.role_id))
for r in roles_list:
role_test = Role_Servers.select().where(Role_Servers.role_id == r.get('role_id'))
for t in role_test:
role_server.append(t)
for s in role_server:
found = False
for item in user_servers:
if s.server_id == item.server_id:
found = True
if not found:
server_data.append(db_helper.get_server_data_by_id(s.server_id))
return server_data
@staticmethod
def get_authorized_servers_from_roles(user_id):
user_roles = User_Roles.select().where(User_Roles.user_id == user_id)
roles_list = []
role_server = []
server_data = []
for u in user_roles:
roles_list.append(db_helper.get_role(u.role_id))
for r in roles_list:
role_test = Role_Servers.select().where(Role_Servers.role_id == r.get('role_id'))
for t in role_test:
role_server.append(t)
for s in role_server:
server_data.append(db_helper.get_server_data_by_id(s.server_id))
for us in user_roles:
role_servers = Role_Servers.select().where(Role_Servers.role_id == us.role_id)
for role in role_servers:
server_data.append(db_shortcuts.get_server_data_by_id(role.server_id))
return server_data
@ -374,31 +325,8 @@ class db_shortcuts:
@staticmethod
def get_authorized_servers_stats(user_id):
user_servers = User_Servers.select().where(User_Servers.user_id == user_id)
authorized_servers = []
server_data = []
user_roles = User_Roles.select().where(User_Roles.user_id == user_id)
roles_list = []
role_server = []
for u in user_servers:
authorized_servers.append(db_helper.get_server_data_by_id(u.server_id))
for u in user_roles:
roles_list.append(db_helper.get_role(u.role_id))
for r in roles_list:
role_test = Role_Servers.select().where(Role_Servers.role_id == r.get('role_id'))
for t in role_test:
role_server.append(t)
for s in role_server:
found = False
for item in user_servers:
if s.server_id == item.server_id:
found = True
if not found:
authorized_servers.append(db_helper.get_server_data_by_id(s.server_id))
authorized_servers = db_helper.get_authorized_servers(user_id)
for s in authorized_servers:
latest = Server_Stats.select().where(Server_Stats.server_id == s.get('server_id')).order_by(
@ -570,20 +498,15 @@ class db_shortcuts:
roles = set()
for r in roles_query:
roles.add(r.role_id.role_id)
servers_query = User_Servers.select().join(Servers, JOIN.INNER).where(User_Servers.user_id == user_id)
## TODO: this query needs to be narrower
servers = set()
for s in servers_query:
servers.add(s.server_id.server_id)
user['roles'] = roles
user['servers'] = servers
#logger.debug("user: ({}) {}".format(user_id, user))
return user
@staticmethod
def add_user_server(server_id, user_id, us_permissions):
servers = User_Servers.insert({User_Servers.server_id: server_id, User_Servers.user_id: user_id, User_Servers.permissions: us_permissions}).execute()
return servers
def add_user_crafty(user_id, uc_permissions):
user_crafty = User_Crafty.insert({User_Crafty.user_id: user_id, User_Crafty.permissions: uc_permissions}).execute()
return user_crafty
@staticmethod
def add_role_server(server_id, role_id, rs_permissions="00000000"):
@ -637,7 +560,6 @@ class db_shortcuts:
up_data = {}
added_roles = set()
removed_roles = set()
added_servers = set()
removed_servers = set()
for key in user_data:
if key == "user_id":
@ -645,9 +567,6 @@ class db_shortcuts:
elif key == "roles":
added_roles = user_data['roles'].difference(base_data['roles'])
removed_roles = base_data['roles'].difference(user_data['roles'])
elif key == "servers":
added_servers = user_data['servers'].difference(base_data['servers'])
removed_servers = base_data['servers'].difference(user_data['servers'])
elif key == "regen_api":
if user_data['regen_api']:
up_data['api_token'] = db_shortcuts.new_api_token()
@ -664,10 +583,6 @@ class db_shortcuts:
# TODO: This is horribly inefficient and we should be using bulk queries but im going for functionality at this point
User_Roles.delete().where(User_Roles.user_id == user_id).where(User_Roles.role_id.in_(removed_roles)).execute()
for server in added_servers:
User_Servers.get_or_create(user_id=user_id, server_id=server)
# # TODO: This is horribly inefficient and we should be using bulk queries but im going for functionality at this point
User_Servers.delete().where(User_Servers.user_id == user_id).where(User_Servers.server_id.in_(removed_servers)).execute()
if up_data:
Users.update(up_data).where(Users.user_id == user_id).execute()

View File

@ -42,7 +42,7 @@ class PanelHandler(BaseHandler):
for r in exec_user['roles']:
role = db_helper.get_role(r)
exec_user_role.add(role['role_name'])
defined_servers = db_helper.get_all_authorized_servers(exec_user_id)
defined_servers = db_helper.get_authorized_servers(exec_user_id)
page_data = {
# todo: make this actually pull and compare version data
@ -278,7 +278,7 @@ class PanelHandler(BaseHandler):
user_roles = {}
for user in db_helper.get_all_users():
user_roles_list = db_helper.get_user_roles_names(user.user_id)
user_servers = db_helper.get_all_authorized_servers(user.user_id)
user_servers = db_helper.get_authorized_servers(user.user_id)
servers = []
for server in user_servers:
servers.append(server['server_name'])
@ -338,7 +338,7 @@ class PanelHandler(BaseHandler):
elif page == "edit_user":
user_id = self.get_argument('id', None)
role_servers = db_helper.get_authorized_servers_from_roles(user_id)
role_servers = db_helper.get_authorized_servers(user_id)
page_role_servers = []
servers = set()
for server in role_servers:
@ -395,7 +395,7 @@ class PanelHandler(BaseHandler):
user_roles = {}
for user in db_helper.get_all_users():
user_roles_list = db_helper.get_user_roles_names(user.user_id)
user_servers = db_helper.get_all_authorized_servers(user.user_id)
user_servers = db_helper.get_authorized_servers(user.user_id)
data = {user.user_id: user_roles_list}
user_roles.update(data)
page_data['new_role'] = True
@ -423,7 +423,7 @@ class PanelHandler(BaseHandler):
user_roles = {}
for user in db_helper.get_all_users():
user_roles_list = db_helper.get_user_roles_names(user.user_id)
user_servers = db_helper.get_all_authorized_servers(user.user_id)
user_servers = db_helper.get_authorized_servers(user.user_id)
data = {user.user_id: user_roles_list}
user_roles.update(data)
page_data['new_role'] = False

View File

@ -187,9 +187,6 @@ class ServerHandler(BaseHandler):
new_server_id,
self.get_remote_ip())
#TODO: Remove the following line to remove User_Servers table
db_helper.add_user_server(new_server_id, exec_user_id, "11111111")
#These lines create a new Role for the Server with full permissions and add the user to it
role_id = db_helper.add_role("Creator of Server with id={}".format(new_server_id))
db_helper.add_role_server(new_server_id, role_id, "11111111")