mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2026-02-07 23:56:23 +01:00
Redone correctly , structure, its pretty
This commit is contained in:
0
services/__init__.py
Normal file
0
services/__init__.py
Normal file
3
services/admin.py
Normal file
3
services/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
0
services/managers/__init__.py
Normal file
0
services/managers/__init__.py
Normal file
36
services/managers/eve_api_manager.py
Normal file
36
services/managers/eve_api_manager.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import evelink.api
|
||||
import evelink.char
|
||||
import evelink.eve
|
||||
|
||||
|
||||
class EveApiManager():
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def get_characters_from_api(api_id, api_key):
|
||||
chars = []
|
||||
try:
|
||||
api = evelink.api.API(api_key=(api_id, api_key))
|
||||
# Should get characters
|
||||
account = evelink.account.Account(api=api)
|
||||
chars = account.characters()
|
||||
except evelink.api.APIError as error:
|
||||
print error
|
||||
|
||||
return chars
|
||||
|
||||
@staticmethod
|
||||
def get_corporation_ticker_from_id(corp_id):
|
||||
ticker = ""
|
||||
try:
|
||||
api = evelink.api.API()
|
||||
corp = evelink.corp.Corp(api)
|
||||
response = corp.corporation_sheet(corp_id)
|
||||
ticker = response[0]['ticker']
|
||||
except evelink.api.APIError as error:
|
||||
print error
|
||||
|
||||
return ticker
|
||||
|
||||
131
services/managers/forum_manager.py
Normal file
131
services/managers/forum_manager.py
Normal file
@@ -0,0 +1,131 @@
|
||||
import os
|
||||
from passlib.apps import phpbb3_context
|
||||
from django.db import connections
|
||||
|
||||
|
||||
class ForumManager:
|
||||
|
||||
SQL_ADD_USER = r"INSERT INTO phpbb_users (username, username_clean, " \
|
||||
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_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"
|
||||
|
||||
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_ADD_GROUP = r"INSERT INTO phpbb_groups (group_name) VALUES (%s)"
|
||||
|
||||
SQL_UPDATE_USER_PASSWORD = r"UPDATE phpbb_users SET user_password = %s WHERE username = %s"
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def __generate_random_pass():
|
||||
return os.urandom(8).encode('hex')
|
||||
|
||||
@staticmethod
|
||||
def __gen_hash(password):
|
||||
return phpbb3_context.encrypt(password)
|
||||
|
||||
@staticmethod
|
||||
def __santatize_username(username):
|
||||
sanatized = username.replace(" ", "_")
|
||||
return sanatized.lower()
|
||||
|
||||
@staticmethod
|
||||
def add_user(username, email, groups):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
|
||||
username_clean = ForumManager.__santatize_username(username)
|
||||
password = ForumManager.__generate_random_pass()
|
||||
pwhash = ForumManager.__gen_hash(password)
|
||||
|
||||
# check if the username was simply revoked
|
||||
if ForumManager.check_user(username_clean):
|
||||
ForumManager.__update_user_info(username_clean, email, pwhash)
|
||||
else:
|
||||
try:
|
||||
cursor.execute(ForumManager.SQL_ADD_USER, [username_clean, username_clean, pwhash,
|
||||
email, 2, "", "", "", ""])
|
||||
ForumManager.update_groups(username_clean, groups)
|
||||
except:
|
||||
pass
|
||||
|
||||
return username_clean, password
|
||||
|
||||
@staticmethod
|
||||
def disable_user(username):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
|
||||
password = ForumManager.__gen_hash(ForumManager.__generate_random_pass())
|
||||
revoke_email = "revoked@the99eve.com"
|
||||
try:
|
||||
pwhash = ForumManager.__gen_hash(password)
|
||||
cursor.execute(ForumManager.SQL_DIS_USER, [revoke_email, pwhash, username])
|
||||
return True
|
||||
except TypeError as e:
|
||||
print e
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def delete_user(username):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
|
||||
if ForumManager.check_user(username):
|
||||
cursor.execute(ForumManager.SQL_DEL_USER, [username])
|
||||
return True
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def update_groups(username, groups):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
|
||||
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()
|
||||
|
||||
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)])
|
||||
row = cursor.fetchone()
|
||||
if row:
|
||||
return True
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def update_user_password(username):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
password = ForumManager.__generate_random_pass()
|
||||
if ForumManager.check_user(username):
|
||||
pwhash = ForumManager.__gen_hash(password)
|
||||
cursor.execute(ForumManager.SQL_UPDATE_USER_PASSWORD, [pwhash, username])
|
||||
return password
|
||||
|
||||
return ""
|
||||
|
||||
@staticmethod
|
||||
def __update_user_info(username, email, password):
|
||||
cursor = connections['phpbb3'].cursor()
|
||||
try:
|
||||
cursor.execute(ForumManager.SQL_DIS_USER, [email, password, username])
|
||||
except:
|
||||
pass
|
||||
70
services/managers/jabber_manager.py
Normal file
70
services/managers/jabber_manager.py
Normal file
@@ -0,0 +1,70 @@
|
||||
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
|
||||
|
||||
@staticmethod
|
||||
def __add_address_to_username(username):
|
||||
address = urlparse(settings.OPENFIRE_ADDRESS).netloc.split(":")[0]
|
||||
completed_username = username + "@" + address
|
||||
return completed_username
|
||||
|
||||
@staticmethod
|
||||
def __santatize_username(username):
|
||||
sanatized = username.replace(" ", "_")
|
||||
return sanatized.lower()
|
||||
|
||||
@staticmethod
|
||||
def __generate_random_pass():
|
||||
return os.urandom(8).encode('hex')
|
||||
|
||||
@staticmethod
|
||||
def add_user(username):
|
||||
|
||||
try:
|
||||
sanatized_username = JabberManager.__santatize_username(username)
|
||||
password = JabberManager.__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
|
||||
|
||||
@staticmethod
|
||||
def delete_user(username):
|
||||
try:
|
||||
api = UserService(settings.OPENFIRE_ADDRESS, settings.OPENFIRE_SECRET_KEY)
|
||||
api.delete_user(username)
|
||||
return True
|
||||
except exception.UserNotFoundException:
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def lock_user(username):
|
||||
api = UserService(settings.OPENFIRE_ADDRESS, settings.OPENFIRE_SECRET_KEY)
|
||||
api.lock_user(username)
|
||||
|
||||
@staticmethod
|
||||
def unlock_user(username):
|
||||
api = UserService(settings.OPENFIRE_ADDRESS, settings.OPENFIRE_SECRET_KEY)
|
||||
api.unlock_user(username)
|
||||
|
||||
@staticmethod
|
||||
def update_user_pass(username):
|
||||
try:
|
||||
password = JabberManager.__generate_random_pass()
|
||||
api = UserService(settings.OPENFIRE_ADDRESS, settings.OPENFIRE_SECRET_KEY)
|
||||
api.update_user(username, password)
|
||||
return password
|
||||
except exception.UserNotFoundException:
|
||||
return ""
|
||||
103
services/managers/mumble_manager.py
Normal file
103
services/managers/mumble_manager.py
Normal file
@@ -0,0 +1,103 @@
|
||||
import os
|
||||
import hashlib
|
||||
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):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def __santatize_username(username):
|
||||
sanatized = username.replace(" ", "_")
|
||||
return sanatized
|
||||
|
||||
@staticmethod
|
||||
def __generate_random_pass():
|
||||
return os.urandom(8).encode('hex')
|
||||
|
||||
@staticmethod
|
||||
def __generate_username(username, corp_ticker):
|
||||
return "["+corp_ticker+"]"+username
|
||||
|
||||
@staticmethod
|
||||
def _gen_pwhash(password):
|
||||
return hashlib.sha1(password).hexdigest()
|
||||
|
||||
@staticmethod
|
||||
def get_user_id_by_name(name):
|
||||
dbcursor = connections['mumble'].cursor()
|
||||
dbcursor.execute(MumbleManager.SQL_SELECT_GET_USER_ID_BY_NAME, [name, settings.MUMBLE_SERVER_ID])
|
||||
row = dbcursor.fetchone()
|
||||
if row:
|
||||
return row[0]
|
||||
|
||||
@staticmethod
|
||||
def create_user(corp_ticker, username):
|
||||
dbcursor = connections['mumble'].cursor()
|
||||
username_clean = MumbleManager.__generate_username(MumbleManager.__santatize_username(username), corp_ticker)
|
||||
password = MumbleManager.__generate_random_pass()
|
||||
pwhash = MumbleManager._gen_pwhash(password)
|
||||
|
||||
try:
|
||||
dbcursor.execute(MumbleManager.SQL_SELECT_MAX_ID)
|
||||
user_id = dbcursor.fetchone()[0]
|
||||
|
||||
dbcursor.execute(MumbleManager.SQL_CREATE_USER,
|
||||
[settings.MUMBLE_SERVER_ID, user_id, username_clean, pwhash])
|
||||
|
||||
return username_clean, password
|
||||
except:
|
||||
|
||||
return "", ""
|
||||
|
||||
@staticmethod
|
||||
def check_user_exist(username):
|
||||
dbcursor = connections['mumble'].cursor()
|
||||
dbcursor.execute(MumbleManager.SQL_CHECK_USER_EXIST,
|
||||
[username, settings.MUMBLE_SERVER_ID])
|
||||
|
||||
row = dbcursor.fetchone()
|
||||
if row and row[0].lower() == username.lower():
|
||||
return True
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def delete_user(username):
|
||||
dbcursor = connections['mumble'].cursor()
|
||||
if MumbleManager.check_user_exist(username):
|
||||
try:
|
||||
|
||||
dbcursor.execute(MumbleManager.SQL_DELETE_USER,
|
||||
[username, settings.MUMBLE_SERVER_ID])
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def update_user_password(username):
|
||||
dbcursor = connections['mumble'].cursor()
|
||||
password = MumbleManager.__generate_random_pass()
|
||||
pwhash = MumbleManager._gen_pwhash(password)
|
||||
|
||||
if MumbleManager.check_user_exist(username):
|
||||
try:
|
||||
|
||||
dbcursor.execute(MumbleManager.SQL_UPDATE_USER_PASSWORD,
|
||||
[pwhash, username, settings.MUMBLE_SERVER_ID])
|
||||
return password
|
||||
except:
|
||||
return ""
|
||||
|
||||
return ""
|
||||
3
services/models.py
Normal file
3
services/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
3
services/tests.py
Normal file
3
services/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
131
services/views.py
Normal file
131
services/views.py
Normal file
@@ -0,0 +1,131 @@
|
||||
from django.template import RequestContext
|
||||
from django.shortcuts import HttpResponseRedirect
|
||||
from django.shortcuts import render_to_response
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.decorators import permission_required
|
||||
|
||||
from managers.jabber_manager import JabberManager
|
||||
from managers.forum_manager import ForumManager
|
||||
from managers.mumble_manager import MumbleManager
|
||||
|
||||
from authentication.managers import AuthServicesInfoManager
|
||||
from eveonline.managers import EveManager
|
||||
|
||||
|
||||
@login_required
|
||||
def services_view(request):
|
||||
authinfo = AuthServicesInfoManager.get_auth_service_info(request.user)
|
||||
|
||||
return render_to_response('registered/services.html', {'authinfo': authinfo}, context_instance=RequestContext(request))
|
||||
|
||||
@login_required
|
||||
@permission_required('auth.alliance_member')
|
||||
def activate_forum(request):
|
||||
authinfo = AuthServicesInfoManager.get_auth_service_info(request.user)
|
||||
# Valid now we get the main characters
|
||||
character = EveManager.get_character_by_id(authinfo.main_char_id)
|
||||
result = ForumManager.add_user(character.character_name, request.user.email, ['REGISTERED'])
|
||||
# if empty we failed
|
||||
if result[0] != "":
|
||||
AuthServicesInfoManager.update_user_forum_info(result[0], result[1], request.user)
|
||||
return HttpResponseRedirect("/services/")
|
||||
return HttpResponseRedirect("/dashboard")
|
||||
|
||||
|
||||
@login_required
|
||||
@permission_required('auth.alliance_member')
|
||||
def deactivate_forum(request):
|
||||
authinfo = AuthServicesInfoManager.get_auth_service_info(request.user)
|
||||
result = ForumManager.disable_user(authinfo.forum_username)
|
||||
# false we failed
|
||||
if result:
|
||||
AuthServicesInfoManager.update_user_forum_info("", "", request.user)
|
||||
return HttpResponseRedirect("/services/")
|
||||
return HttpResponseRedirect("/dashboard")
|
||||
|
||||
|
||||
@login_required
|
||||
@permission_required('auth.alliance_member')
|
||||
def reset_forum_password(request):
|
||||
authinfo = AuthServicesInfoManager.get_auth_service_info(request.user)
|
||||
result = ForumManager.update_user_password(authinfo.forum_username)
|
||||
# false we failed
|
||||
if result != "":
|
||||
AuthServicesInfoManager.update_user_forum_info(authinfo.forum_username, result, request.user)
|
||||
return HttpResponseRedirect("/services/")
|
||||
return HttpResponseRedirect("/dashboard")
|
||||
|
||||
|
||||
@login_required
|
||||
@permission_required('auth.alliance_member')
|
||||
def activate_jabber(request):
|
||||
authinfo = AuthServicesInfoManager.get_auth_service_info(request.user)
|
||||
character = EveManager.get_character_by_id(authinfo.main_char_id)
|
||||
info = JabberManager.add_user(character.character_name)
|
||||
# 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)
|
||||
return HttpResponseRedirect("/services/")
|
||||
return HttpResponseRedirect("/dashboard")
|
||||
|
||||
|
||||
@login_required
|
||||
@permission_required('auth.alliance_member')
|
||||
def deactivate_jabber(request):
|
||||
authinfo = AuthServicesInfoManager.get_auth_service_info(request.user)
|
||||
result = JabberManager.delete_user(authinfo.jabber_username)
|
||||
# If our username is blank means we failed
|
||||
if result:
|
||||
AuthServicesInfoManager.update_user_jabber_info("", "", request.user)
|
||||
return HttpResponseRedirect("/services/")
|
||||
return HttpResponseRedirect("/dashboard")
|
||||
|
||||
|
||||
@login_required
|
||||
@permission_required('auth.alliance_member')
|
||||
def reset_jabber_password(request):
|
||||
authinfo = AuthServicesInfoManager.get_auth_service_info(request.user)
|
||||
result = JabberManager.update_user_pass(authinfo.jabber_username)
|
||||
# If our username is blank means we failed
|
||||
if result != "":
|
||||
AuthServicesInfoManager.update_user_jabber_info(authinfo.jabber_username, result, request.user)
|
||||
return HttpResponseRedirect("/services/")
|
||||
return HttpResponseRedirect("/dashboard")
|
||||
|
||||
|
||||
@login_required
|
||||
@permission_required('auth.alliance_member')
|
||||
def activate_mumble(request):
|
||||
authinfo = AuthServicesInfoManager.get_auth_service_info(request.user)
|
||||
character = EveManager.get_character_by_id(authinfo.main_char_id)
|
||||
result = MumbleManager.create_user(character.corporation_ticker, character.character_name)
|
||||
# if its empty we failed
|
||||
if result[0] is not "":
|
||||
AuthServicesInfoManager.update_user_mumble_info(result[0], result[1], request.user)
|
||||
return HttpResponseRedirect("/services/")
|
||||
return HttpResponseRedirect("/")
|
||||
|
||||
|
||||
@login_required
|
||||
@permission_required('auth.alliance_member')
|
||||
def deactivate_mumble(request):
|
||||
authinfo = AuthServicesInfoManager.get_auth_service_info(request.user)
|
||||
result = MumbleManager.delete_user(authinfo.mumble_username)
|
||||
# if false we failed
|
||||
if result:
|
||||
AuthServicesInfoManager.update_user_mumble_info("", "", request.user)
|
||||
return HttpResponseRedirect("/services/")
|
||||
return HttpResponseRedirect("/")
|
||||
|
||||
|
||||
@login_required
|
||||
@permission_required('auth.alliance_member')
|
||||
def reset_mumble_password(request):
|
||||
authinfo = AuthServicesInfoManager.get_auth_service_info(request.user)
|
||||
result = MumbleManager.update_user_password(authinfo.mumble_username)
|
||||
# if blank we failed
|
||||
if result != "":
|
||||
AuthServicesInfoManager.update_user_mumble_info(authinfo.mumble_username, result, request.user)
|
||||
return HttpResponseRedirect("/services/")
|
||||
return HttpResponseRedirect("/")
|
||||
Reference in New Issue
Block a user