mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2026-02-09 16:46:20 +01:00
Update to rule all updates. group support and clean up
This commit is contained in:
@@ -13,16 +13,23 @@ class ForumManager:
|
||||
|
||||
SQL_DIS_USER = r"UPDATE phpbb_users SET user_email= %s, user_password=%s WHERE username = %s"
|
||||
|
||||
SQL_CHECK_USER = r"SELECT user_id from phpbb_users WHERE username = %s"
|
||||
SQL_USER_ID_FROM_USERNAME = r"SELECT user_id from phpbb_users WHERE username = %s"
|
||||
|
||||
SQL_ADD_USER_GROUP = r"INSERT INTO phpbb_user_group (group_id, user_id, user_pending) VALUES (%s, %s, %s)"
|
||||
|
||||
SQL_GET_GROUP = r"SELECT group_id from phpbb_groups WHERE group_name = %s"
|
||||
SQL_GET_GROUP_ID = r"SELECT group_id from phpbb_groups WHERE group_name = %s"
|
||||
|
||||
SQL_ADD_GROUP = r"INSERT INTO phpbb_groups (group_name) VALUES (%s)"
|
||||
SQL_ADD_GROUP = r"INSERT INTO phpbb_groups (group_name,group_desc) VALUES (%s,"")"
|
||||
|
||||
SQL_UPDATE_USER_PASSWORD = r"UPDATE phpbb_users SET user_password = %s WHERE username = %s"
|
||||
|
||||
SQL_REMOVE_USER_GROUP = r"DELETE FROM phpbb_user_group WHERE user_id=%s AND group_id=%s "
|
||||
|
||||
SQL_GET_ALL_GROUPS = r"SELECT group_id, group_name FROM phpbb_groups"
|
||||
|
||||
SQL_GET_USER_GROUPS = r"SELECT phpbb_groups.group_name FROM phpbb_groups , phpbb_user_group WHERE " \
|
||||
r"phpbb_user_group.group_id = phpbb_groups.group_id AND user_id=%s"
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@@ -39,6 +46,55 @@ class ForumManager:
|
||||
sanatized = username.replace(" ", "_")
|
||||
return sanatized.lower()
|
||||
|
||||
@staticmethod
|
||||
def __get_group_id(groupname):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
cursor.execute(ForumManager.SQL_GET_GROUP_ID, [groupname])
|
||||
row = cursor.fetchone()
|
||||
|
||||
return row[0]
|
||||
|
||||
@staticmethod
|
||||
def __get_user_id(username):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
cursor.execute(ForumManager.SQL_USER_ID_FROM_USERNAME, [username])
|
||||
row = cursor.fetchone()
|
||||
return row[0]
|
||||
|
||||
@staticmethod
|
||||
def __get_all_groups():
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
cursor.execute(ForumManager.SQL_GET_ALL_GROUPS)
|
||||
rows = cursor.fetchall()
|
||||
out = {}
|
||||
for row in rows:
|
||||
out[row[1]] = row[0]
|
||||
|
||||
return out
|
||||
|
||||
@staticmethod
|
||||
def __get_user_groups(userid):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
cursor.execute(ForumManager.SQL_GET_USER_GROUPS, [userid])
|
||||
return [row[0] for row in cursor.fetchall()]
|
||||
|
||||
@staticmethod
|
||||
def __create_group(groupname):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
cursor.execute(ForumManager.SQL_ADD_GROUP, [groupname])
|
||||
return ForumManager.__get_group_id(groupname)
|
||||
|
||||
@staticmethod
|
||||
def __add_user_to_group(userid, groupid):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
cursor.execute(ForumManager.SQL_ADD_USER_GROUP, [groupid, userid, 0])
|
||||
|
||||
@staticmethod
|
||||
def __remove_user_from_group(userid, groupid):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
cursor.execute(ForumManager.SQL_REMOVE_USER_GROUP, [userid, groupid])
|
||||
|
||||
|
||||
@staticmethod
|
||||
def add_user(username, email, groups):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
@@ -85,27 +141,39 @@ class ForumManager:
|
||||
|
||||
@staticmethod
|
||||
def update_groups(username, groups):
|
||||
userid = ForumManager.__get_user_id(username)
|
||||
forum_groups = ForumManager.__get_all_groups()
|
||||
user_groups = set(ForumManager.__get_user_groups(userid))
|
||||
act_groups = set([g.replace(' ', '-') for g in groups])
|
||||
addgroups = act_groups - user_groups
|
||||
remgroups = user_groups - act_groups
|
||||
print addgroups
|
||||
print remgroups
|
||||
print userid
|
||||
for g in addgroups:
|
||||
if not g in forum_groups:
|
||||
forum_groups[g] = ForumManager.__create_group(g)
|
||||
ForumManager.__add_user_to_group(userid, forum_groups[g])
|
||||
|
||||
for g in remgroups:
|
||||
ForumManager.__remove_user_from_group(userid, forum_groups[g])
|
||||
|
||||
@staticmethod
|
||||
def remove_group(username, group):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
userid = ForumManager.__get_user_id(username)
|
||||
groupid = ForumManager.__get_group_id(group)
|
||||
|
||||
cursor.execute(ForumManager.SQL_CHECK_USER, [username])
|
||||
row = cursor.fetchone()
|
||||
userid = row[0]
|
||||
for group in groups:
|
||||
cursor.execute(ForumManager.SQL_GET_GROUP, [group])
|
||||
row = cursor.fetchone()
|
||||
print row
|
||||
if not row:
|
||||
cursor.execute(ForumManager.SQL_ADD_GROUP, [group])
|
||||
cursor.execute(ForumManager.SQL_GET_GROUP, [group])
|
||||
row = cursor.fetchone()
|
||||
if userid:
|
||||
if groupid:
|
||||
cursor.execute(ForumManager.SQL_REMOVE_USER_GROUP, [userid, groupid])
|
||||
|
||||
cursor.execute(ForumManager.SQL_ADD_USER_GROUP, [row[0], userid,0])
|
||||
|
||||
@staticmethod
|
||||
def check_user(username):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
""" Check if the username exists """
|
||||
cursor.execute(ForumManager.SQL_CHECK_USER, [ForumManager.__santatize_username(username)])
|
||||
cursor.execute(ForumManager.SQL_USER_ID_FROM_USERNAME, [ForumManager.__santatize_username(username)])
|
||||
row = cursor.fetchone()
|
||||
if row:
|
||||
return True
|
||||
|
||||
@@ -67,4 +67,14 @@ class JabberManager:
|
||||
api.update_user(username, password)
|
||||
return password
|
||||
except exception.UserNotFoundException:
|
||||
return ""
|
||||
return ""
|
||||
|
||||
@staticmethod
|
||||
def update_user_groups(username, password, groups):
|
||||
api = UserService(settings.OPENFIRE_ADDRESS, settings.OPENFIRE_SECRET_KEY)
|
||||
api.update_user(username, password, "", "", groups)
|
||||
|
||||
@staticmethod
|
||||
def delete_user_groups(username, groups):
|
||||
api = UserService(settings.OPENFIRE_ADDRESS, settings.OPENFIRE_SECRET_KEY)
|
||||
api.delete_group(username,groups)
|
||||
@@ -6,13 +6,39 @@ from django.conf import settings
|
||||
|
||||
class MumbleManager:
|
||||
|
||||
SQL_SELECT_MAX_ID = r"SELECT max(user_id)+1 as next_id from murmur_users"
|
||||
SQL_SELECT_USER_MAX_ID = r"SELECT max(user_id)+1 as next_id from murmur_users"
|
||||
|
||||
SQL_SELECT_GROUP_MAX_ID = r"SELECT MAX(group_id)+1 FROM murmur_groups"
|
||||
|
||||
SQL_CREATE_USER = r"INSERT INTO murmur_users (server_id, user_id, name, pw) VALUES (%s, %s, %s, %s)"
|
||||
|
||||
SQL_SELECT_GET_USER_ID_BY_NAME = r"SELECT user_id from murmur_users WHERE name = %s AND server_id = %s"
|
||||
|
||||
SQL_CHECK_USER_EXIST = r"SELECT name from murmur_users WHERE name = %s AND server_id = %s"
|
||||
|
||||
SQL_DELETE_USER = r"DELETE FROM murmur_users WHERE name = %s AND server_id = %s"
|
||||
|
||||
SQL_UPDATE_USER_PASSWORD = r"UPDATE murmur_users SET pw = %s WHERE name = %s AND server_id = %s"
|
||||
|
||||
SQL_GET_GROUPS = r"SELECT group_id, name FROM murmur_groups WHERE server_id = %s AND channel_id = 0"
|
||||
|
||||
SQL_GET_GROUP_FROM_NAME = r"SELECT group_id, name FROM murmur_groups " \
|
||||
r"WHERE server_id = %s AND channel_id = 0 AND name = %s"
|
||||
|
||||
SQL_GET_USER_GROUPS = r"SELECT murmur_groups.name FROM murmur_groups, murmur_group_members WHERE " \
|
||||
r"murmur_group_members.group_id = murmur_groups.group_id AND " \
|
||||
r"murmur_group_members.server_id = murmur_groups.server_id AND " \
|
||||
r"murmur_group_members.user_id = %s"
|
||||
|
||||
SQL_ADD_GROUP = r"INSERT INTO murmur_groups (group_id, server_id, name, channel_id, inherit, inheritable) " \
|
||||
r"VALUES (%s, %s, %s, 0, 1, 1)"
|
||||
|
||||
SQL_ADD_USER_TO_GROUP = r"INSERT INTO murmur_group_members (group_id, server_id, user_id, addit) " \
|
||||
r"VALUES (%s, %s, %s, 1)"
|
||||
|
||||
SQL_DELETE_USER_FROM_GROUP = r"DELETE FROM murmur_group_members WHERE group_id = %s " \
|
||||
r"AND server_id = %s AND user_id = %s"
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@@ -33,6 +59,52 @@ class MumbleManager:
|
||||
def _gen_pwhash(password):
|
||||
return hashlib.sha1(password).hexdigest()
|
||||
|
||||
@staticmethod
|
||||
def _get_groups():
|
||||
dbcursor = connections['mumble'].cursor()
|
||||
dbcursor.execute(MumbleManager.SQL_GET_GROUPS, [settings.MUMBLE_SERVER_ID])
|
||||
rows = dbcursor.fetchall()
|
||||
|
||||
out = {}
|
||||
for row in rows:
|
||||
out[row[1]] = row[0]
|
||||
|
||||
return out
|
||||
|
||||
@staticmethod
|
||||
def _get_group(name):
|
||||
dbcursor = connections['mumble'].cursor()
|
||||
dbcursor.execute(MumbleManager.SQL_GET_GROUP_FROM_NAME, [settings.MUMBLE_SERVER_ID, name])
|
||||
row = dbcursor.fetchone()
|
||||
if row:
|
||||
return row[0]
|
||||
|
||||
@staticmethod
|
||||
def _get_user_groups(name):
|
||||
dbcursor = connections['mumble'].cursor()
|
||||
user_id = MumbleManager.get_user_id_by_name(name)
|
||||
dbcursor.execute(MumbleManager.SQL_GET_USER_GROUPS, [user_id])
|
||||
return [row[0] for row in dbcursor.fetchall()]
|
||||
|
||||
@staticmethod
|
||||
def _add_group(name):
|
||||
dbcursor = connections['mumble'].cursor()
|
||||
dbcursor.execute(MumbleManager.SQL_SELECT_GROUP_MAX_ID)
|
||||
row = dbcursor.fetchone()
|
||||
groupid = row[0]
|
||||
dbcursor.execute(MumbleManager.SQL_ADD_GROUP, [groupid, settings.MUMBLE_SERVER_ID, name])
|
||||
return groupid
|
||||
|
||||
@staticmethod
|
||||
def _add_user_to_group(userid, groupid):
|
||||
dbcursor = connections['mumble'].cursor()
|
||||
dbcursor.execute(MumbleManager.SQL_ADD_USER_TO_GROUP, [groupid, settings.MUMBLE_SERVER_ID, userid])
|
||||
|
||||
@staticmethod
|
||||
def _del_user_from_group(userid, groupid):
|
||||
dbcursor = connections['mumble'].cursor()
|
||||
dbcursor.execute(MumbleManager.SQL_DELETE_USER_FROM_GROUP, [groupid, settings.MUMBLE_SERVER_ID, userid])
|
||||
|
||||
@staticmethod
|
||||
def get_user_id_by_name(name):
|
||||
dbcursor = connections['mumble'].cursor()
|
||||
@@ -49,7 +121,7 @@ class MumbleManager:
|
||||
pwhash = MumbleManager._gen_pwhash(password)
|
||||
|
||||
try:
|
||||
dbcursor.execute(MumbleManager.SQL_SELECT_MAX_ID)
|
||||
dbcursor.execute(MumbleManager.SQL_SELECT_USER_MAX_ID)
|
||||
user_id = dbcursor.fetchone()[0]
|
||||
|
||||
dbcursor.execute(MumbleManager.SQL_CREATE_USER,
|
||||
@@ -100,4 +172,21 @@ class MumbleManager:
|
||||
except:
|
||||
return ""
|
||||
|
||||
return ""
|
||||
return ""
|
||||
|
||||
@staticmethod
|
||||
def update_groups(username, groups):
|
||||
userid = MumbleManager.get_user_id_by_name(username)
|
||||
mumble_groups = MumbleManager._get_groups()
|
||||
user_groups = set(MumbleManager._get_user_groups(username))
|
||||
act_groups = set([g.replace(' ', '-') for g in groups])
|
||||
addgroups = act_groups - user_groups
|
||||
remgroups = user_groups - act_groups
|
||||
|
||||
for g in addgroups:
|
||||
if not g in mumble_groups:
|
||||
mumble_groups[g] = MumbleManager._add_group(g)
|
||||
MumbleManager._add_user_to_group(userid, mumble_groups[g])
|
||||
|
||||
for g in remgroups:
|
||||
MumbleManager._del_user_from_group(userid, mumble_groups[g])
|
||||
|
||||
@@ -12,6 +12,8 @@ from managers.mumble_manager import MumbleManager
|
||||
from authentication.managers import AuthServicesInfoManager
|
||||
from eveonline.managers import EveManager
|
||||
|
||||
from celerytask.tasks import update_jabber_groups
|
||||
from celerytask.tasks import update_mumble_groups
|
||||
|
||||
@login_required
|
||||
def services_view(request):
|
||||
@@ -66,6 +68,7 @@ def activate_jabber(request):
|
||||
# If our username is blank means we already had a user
|
||||
if info[0] is not "":
|
||||
AuthServicesInfoManager.update_user_jabber_info(info[0], info[1], request.user)
|
||||
update_jabber_groups(request.user)
|
||||
return HttpResponseRedirect("/services/")
|
||||
return HttpResponseRedirect("/dashboard")
|
||||
|
||||
@@ -103,8 +106,9 @@ def activate_mumble(request):
|
||||
# if its empty we failed
|
||||
if result[0] is not "":
|
||||
AuthServicesInfoManager.update_user_mumble_info(result[0], result[1], request.user)
|
||||
update_mumble_groups(request.user)
|
||||
return HttpResponseRedirect("/services/")
|
||||
return HttpResponseRedirect("/")
|
||||
return HttpResponseRedirect("/dashboard")
|
||||
|
||||
|
||||
@login_required
|
||||
|
||||
Reference in New Issue
Block a user