mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2026-02-06 15:16:20 +01:00
Added full services working, refresh, create, delete, updated look
This commit is contained in:
@@ -1,21 +1,48 @@
|
||||
import os
|
||||
from django.conf import settings
|
||||
from openfire import exception
|
||||
from openfire import UserService
|
||||
from urlparse import urlparse
|
||||
|
||||
|
||||
class JabberManager():
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def add_user(self, username, password):
|
||||
api = UserService(settings.OPENFIRE_ADDRESS, settings.OPENFIRE_SECRET_KEY)
|
||||
print str(username)
|
||||
print str(password)
|
||||
api.add_user(self.__santatize_username(username), str(password))
|
||||
|
||||
def __add_address_to_username(self, username):
|
||||
address = urlparse(settings.OPENFIRE_ADDRESS).netloc.split(":")[0]
|
||||
completed_username = username + "@" + address
|
||||
return completed_username
|
||||
|
||||
def __santatize_username(self, username):
|
||||
sanatized = username.replace(" ", "_")
|
||||
return sanatized.lower()
|
||||
|
||||
def __generate_random_pass(self):
|
||||
return os.urandom(8).encode('hex')
|
||||
|
||||
def add_user(self, username):
|
||||
|
||||
try:
|
||||
sanatized_username = self.__santatize_username(username)
|
||||
password = self.__generate_random_pass()
|
||||
api = UserService(settings.OPENFIRE_ADDRESS, settings.OPENFIRE_SECRET_KEY)
|
||||
api.add_user(sanatized_username, password)
|
||||
|
||||
except exception.UserAlreadyExistsException:
|
||||
# User exist
|
||||
return "", ""
|
||||
|
||||
return sanatized_username, password
|
||||
|
||||
def delete_user(self, username):
|
||||
api = UserService(settings.OPENFIRE_ADDRESS, settings.OPENFIRE_SECRET_KEY)
|
||||
api.delete_user(username)
|
||||
try:
|
||||
api = UserService(settings.OPENFIRE_ADDRESS, settings.OPENFIRE_SECRET_KEY)
|
||||
api.delete_user(username)
|
||||
return True
|
||||
except exception.UserNotFoundException:
|
||||
return False
|
||||
|
||||
def lock_user(self, username):
|
||||
api = UserService(settings.OPENFIRE_ADDRESS, settings.OPENFIRE_SECRET_KEY)
|
||||
@@ -25,10 +52,11 @@ class JabberManager():
|
||||
api = UserService(settings.OPENFIRE_ADDRESS, settings.OPENFIRE_SECRET_KEY)
|
||||
api.unlock_user(username)
|
||||
|
||||
def update_user_pass(self, username, password):
|
||||
api = UserService(settings.OPENFIRE_ADDRESS, settings.OPENFIRE_SECRET_KEY)
|
||||
api.update_user(username, password)
|
||||
|
||||
def __santatize_username(self, username):
|
||||
sanatized = username.replace(" ", "_")
|
||||
return sanatized.lower()
|
||||
def update_user_pass(self, username):
|
||||
try:
|
||||
password = self.__generate_random_pass()
|
||||
api = UserService(settings.OPENFIRE_ADDRESS, settings.OPENFIRE_SECRET_KEY)
|
||||
api.update_user(username, password)
|
||||
return password
|
||||
except exception.UserNotFoundException:
|
||||
return ""
|
||||
|
||||
@@ -1,38 +1,61 @@
|
||||
import uuid
|
||||
import os
|
||||
import hashlib
|
||||
import random
|
||||
from django.db import connections
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class MumbleManager:
|
||||
|
||||
SQL_SELECT_MAX_ID = r"SELECT max(user_id)+1 as next_id from murmur_users"
|
||||
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"
|
||||
|
||||
def __init__(self):
|
||||
self.dbcursor = connections['mumble'].cursor()
|
||||
|
||||
@staticmethod
|
||||
def _gen_pwhash(password):
|
||||
def __santatize_username(self, username):
|
||||
sanatized = username.replace(" ", "_")
|
||||
return sanatized
|
||||
|
||||
def __generate_random_pass(self):
|
||||
return os.urandom(8).encode('hex')
|
||||
|
||||
def __generate_username(self, username, corp_ticker):
|
||||
return "["+corp_ticker+"]"+username
|
||||
|
||||
def _gen_pwhash(self, password):
|
||||
return hashlib.sha1(password).hexdigest()
|
||||
|
||||
def get_user_id_by_name(self, name):
|
||||
self.dbcursor.execute(r"SELECT user_id from murmur_users WHERE name = %s AND server_id = %s",
|
||||
[name, settings.MUMBLE_SERVER_ID])
|
||||
self.dbcursor.execute(self.SQL_SELECT_GET_USER_ID_BY_NAME, [name, settings.MUMBLE_SERVER_ID])
|
||||
row = self.dbcursor.fetchone()
|
||||
if row:
|
||||
return row[0]
|
||||
|
||||
def create_user(self, username, password):
|
||||
""" Add a user """
|
||||
self.dbcursor.execute(r"SELECT max(user_id)+1 as next_id from murmur_users")
|
||||
user_id = self.dbcursor.fetchone()[0]
|
||||
def create_user(self, corp_ticker, username):
|
||||
|
||||
self.dbcursor.execute(r"INSERT INTO murmur_users (server_id, user_id, name, pw) VALUES (%s, %s, %s, %s)",
|
||||
[settings.MUMBLE_SERVER_ID, user_id, self.__santatize_username(username), self._gen_pwhash(password)])
|
||||
username_clean = self.__generate_username(self.__santatize_username(username), corp_ticker)
|
||||
password = self.__generate_random_pass()
|
||||
pwhash = self._gen_pwhash(password)
|
||||
|
||||
return {'username': username, 'password': password }
|
||||
try:
|
||||
self.dbcursor.execute(self.SQL_SELECT_MAX_ID)
|
||||
user_id = self.dbcursor.fetchone()[0]
|
||||
|
||||
self.dbcursor.execute(self.SQL_CREATE_USER,
|
||||
[settings.MUMBLE_SERVER_ID, user_id, username_clean,
|
||||
pwhash])
|
||||
|
||||
return username_clean, password
|
||||
except:
|
||||
return "", ""
|
||||
|
||||
def check_user_exist(self, username):
|
||||
""" Check if the username exists """
|
||||
self.dbcursor.execute(r"SELECT name from murmur_users WHERE name = %s AND server_id = %s",
|
||||
|
||||
self.dbcursor.execute(self.SQL_CHECK_USER_EXIST,
|
||||
[username, settings.MUMBLE_SERVER_ID])
|
||||
|
||||
row = self.dbcursor.fetchone()
|
||||
@@ -40,13 +63,31 @@ class MumbleManager:
|
||||
return True
|
||||
return False
|
||||
|
||||
def delete_user(self, uid):
|
||||
""" Delete a user """
|
||||
id = self.get_user_id_by_name(uid)
|
||||
self.dbcursor.execute(r"DELETE FROM murmur_users WHERE user_id = %s AND server_id = %s",
|
||||
[id, settings.MUMBLE_SERVER_ID])
|
||||
return True
|
||||
def delete_user(self, username):
|
||||
|
||||
def __santatize_username(self, username):
|
||||
sanatized = username.replace(" ","_")
|
||||
return sanatized.lower()
|
||||
if self.check_user_exist(username):
|
||||
try:
|
||||
|
||||
self.dbcursor.execute(self.SQL_DELETE_USER,
|
||||
[username, settings.MUMBLE_SERVER_ID])
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
return False
|
||||
|
||||
def update_user_password(self, username):
|
||||
|
||||
password = self.__generate_random_pass()
|
||||
pwhash = self._gen_pwhash(password)
|
||||
|
||||
if self.check_user_exist(username):
|
||||
try:
|
||||
|
||||
self.dbcursor.execute(self.SQL_UPDATE_USER_PASSWORD,
|
||||
[pwhash, username, settings.MUMBLE_SERVER_ID])
|
||||
return password
|
||||
except:
|
||||
return ""
|
||||
|
||||
return ""
|
||||
@@ -1,4 +1,5 @@
|
||||
import random
|
||||
import os
|
||||
import sys
|
||||
from passlib.apps import phpbb3_context
|
||||
from django.db import connections
|
||||
|
||||
@@ -9,8 +10,9 @@ class Phpbb3Manager():
|
||||
r"user_password, user_email, group_id , user_permissions, " \
|
||||
r"user_sig, user_occ, user_interests) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
|
||||
|
||||
SQL_DIS_USER = r"DELETE FROM phpbb_user_groups where user_id = " \
|
||||
r"(SELECT user_id FROM phpbb_users WHERE username = %s)"
|
||||
SQL_DEL_USER = r"DELETE FROM phpbb_users where username = %s"
|
||||
|
||||
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"
|
||||
|
||||
@@ -19,49 +21,91 @@ class Phpbb3Manager():
|
||||
SQL_GET_GROUP = r"SELECT group_id from phpbb_groups WHERE group_name = %s"
|
||||
|
||||
SQL_ADD_GROUP = r"INSERT INTO phpbb_groups (group_name) VALUES (%s)"
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def _gen_salt(self):
|
||||
return "%x" % random.randint(0, 2147483647)
|
||||
|
||||
def _gen_hash(self, password):
|
||||
SQL_UPDATE_USER_PASSWORD = r"UPDATE phpbb_users SET user_password = %s WHERE username = %s"
|
||||
|
||||
def __init__(self):
|
||||
self.cursor = connections['phpbb3'].cursor()
|
||||
|
||||
def __generate_random_pass(self):
|
||||
return os.urandom(8).encode('hex')
|
||||
|
||||
def __gen_hash(self, password):
|
||||
return phpbb3_context.encrypt(password)
|
||||
|
||||
def add_user(self, username, password, email, groups):
|
||||
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
|
||||
""" Add a user """
|
||||
username_clean = username.lower()
|
||||
pwhash = self._gen_hash(password)
|
||||
cursor.execute(self.SQL_ADD_USER, [username, username_clean, pwhash, email, 2, "", "","", ""])
|
||||
self.update_groups(username,groups)
|
||||
return { 'username': username, 'password': password }
|
||||
|
||||
def __santatize_username(self, username):
|
||||
sanatized = username.replace(" ", "_")
|
||||
return sanatized.lower()
|
||||
|
||||
def add_user(self, username, email, groups):
|
||||
username_clean = self.__santatize_username(username)
|
||||
password = self.__generate_random_pass()
|
||||
pwhash = self.__gen_hash(password)
|
||||
|
||||
# check if the username was simply revoked
|
||||
if self.check_user(username_clean):
|
||||
self.__update_user_info(username_clean, email, pwhash)
|
||||
else:
|
||||
try:
|
||||
self.cursor.execute(self.SQL_ADD_USER, [username_clean, username_clean, pwhash, email, 2, "", "", "", ""])
|
||||
self.update_groups(username_clean, groups)
|
||||
except:
|
||||
pass
|
||||
|
||||
return username_clean, password
|
||||
|
||||
def disable_user(self, username):
|
||||
password = self.__gen_hash(self.__generate_random_pass())
|
||||
revoke_email = "revoked@the99eve.com"
|
||||
try:
|
||||
pwhash = self.__gen_hash(password)
|
||||
self.cursor.execute(self.SQL_DIS_USER, [revoke_email, pwhash, username])
|
||||
return True
|
||||
except TypeError as e:
|
||||
print e
|
||||
return False
|
||||
|
||||
def delete_user(self, username):
|
||||
if self.check_user(username):
|
||||
self.cursor.execute(self.SQL_DEL_USER, [username])
|
||||
return True
|
||||
return False
|
||||
|
||||
def update_groups(self, username, groups):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
|
||||
cursor.execute(self.SQL_CHECK_USER, [username])
|
||||
row = cursor.fetchone()
|
||||
self.cursor.execute(self.SQL_CHECK_USER, [username])
|
||||
row = self.cursor.fetchone()
|
||||
userid = row[0]
|
||||
for group in groups:
|
||||
cursor.execute(self.SQL_GET_GROUP, [group])
|
||||
row = cursor.fetchone()
|
||||
self.cursor.execute(self.SQL_GET_GROUP, [group])
|
||||
row = self.cursor.fetchone()
|
||||
print row
|
||||
if not row:
|
||||
cursor.execute(self.SQL_ADD_GROUP, [group])
|
||||
cursor.execute(self.SQL_GET_GROUP, [group])
|
||||
row = cursor.fetchone()
|
||||
self.cursor.execute(self.SQL_ADD_GROUP, [group])
|
||||
self.cursor.execute(self.SQL_GET_GROUP, [group])
|
||||
row = self.cursor.fetchone()
|
||||
|
||||
cursor.execute(self.SQL_ADD_USER_GROUP, [row[0], userid,0])
|
||||
self.cursor.execute(self.SQL_ADD_USER_GROUP, [row[0], userid,0])
|
||||
|
||||
def check_user(self, username):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
""" Check if the username exists """
|
||||
cursor.execute(self.SQL_CHECK_USER, [username])
|
||||
cursor.execute(self.SQL_CHECK_USER, [self.__santatize_username(username)])
|
||||
row = cursor.fetchone()
|
||||
if row:
|
||||
return True
|
||||
return False
|
||||
return False
|
||||
|
||||
def update_user_password(self, username):
|
||||
password = self.__generate_random_pass()
|
||||
if self.check_user(username):
|
||||
pwhash = self.__gen_hash(password)
|
||||
self.cursor.execute(self.SQL_UPDATE_USER_PASSWORD, [pwhash, username])
|
||||
return password
|
||||
|
||||
return ""
|
||||
|
||||
def __update_user_info(self, username, email, password):
|
||||
try:
|
||||
self.cursor.execute(self.SQL_DIS_USER, [email, password, username])
|
||||
except:
|
||||
pass
|
||||
Reference in New Issue
Block a user