mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-12 14:00:17 +02:00
commit
57aca60cdf
@ -202,12 +202,14 @@ DEFAULT_BLUE_GROUP = os.environ.get('AA_DEFAULT_BLUE_GROUP', 'Blue')
|
|||||||
# ENABLE_AUTH_JABBER - Enable jabber support in the auth for auth'd members
|
# ENABLE_AUTH_JABBER - Enable jabber support in the auth for auth'd members
|
||||||
# ENABLE_AUTH_MUMBLE - Enable mumble support in the auth for auth'd members
|
# ENABLE_AUTH_MUMBLE - Enable mumble support in the auth for auth'd members
|
||||||
# ENABLE_AUTH_IPBOARD - Enable IPBoard forum support in the auth for auth'd members
|
# ENABLE_AUTH_IPBOARD - Enable IPBoard forum support in the auth for auth'd members
|
||||||
|
# ENABLE_AUTH_DISCORD - Enable Discord support in the auth for auth'd members
|
||||||
#########################
|
#########################
|
||||||
ENABLE_AUTH_FORUM = 'True' == os.environ.get('AA_ENABLE_AUTH_FORUM', 'True')
|
ENABLE_AUTH_FORUM = 'True' == os.environ.get('AA_ENABLE_AUTH_FORUM', 'False')
|
||||||
ENABLE_AUTH_JABBER = 'True' == os.environ.get('AA_ENABLE_AUTH_JABBER', 'True')
|
ENABLE_AUTH_JABBER = 'True' == os.environ.get('AA_ENABLE_AUTH_JABBER', 'False')
|
||||||
ENABLE_AUTH_MUMBLE = 'True' == os.environ.get('AA_ENABLE_AUTH_MUMBLE', 'True')
|
ENABLE_AUTH_MUMBLE = 'True' == os.environ.get('AA_ENABLE_AUTH_MUMBLE', 'False')
|
||||||
ENABLE_AUTH_IPBOARD = 'True' == os.environ.get('AA_ENABLE_AUTH_IPBOARD', 'False')
|
ENABLE_AUTH_IPBOARD = 'True' == os.environ.get('AA_ENABLE_AUTH_IPBOARD', 'False')
|
||||||
ENABLE_AUTH_TEAMSPEAK3 = 'True' == os.environ.get('AA_ENABLE_AUTH_TEAMSPEAK3', 'False')
|
ENABLE_AUTH_TEAMSPEAK3 = 'True' == os.environ.get('AA_ENABLE_AUTH_TEAMSPEAK3', 'False')
|
||||||
|
ENABLE_AUTH_DISCORD = 'True' == os.environ.get('AA_ENABLE_AUTH_DISCORD', 'False')
|
||||||
|
|
||||||
#####################
|
#####################
|
||||||
# Blue service Setup
|
# Blue service Setup
|
||||||
@ -217,13 +219,15 @@ ENABLE_AUTH_TEAMSPEAK3 = 'True' == os.environ.get('AA_ENABLE_AUTH_TEAMSPEAK3', '
|
|||||||
# ENABLE_BLUE_JABBER - Enable jabber support in the auth for blues
|
# ENABLE_BLUE_JABBER - Enable jabber support in the auth for blues
|
||||||
# ENABLE_BLUE_MUMBLE - Enable mumble support in the auth for blues
|
# ENABLE_BLUE_MUMBLE - Enable mumble support in the auth for blues
|
||||||
# ENABLE_BLUE_IPBOARD - Enable IPBoard forum support in the auth for blues
|
# ENABLE_BLUE_IPBOARD - Enable IPBoard forum support in the auth for blues
|
||||||
|
# ENABLE_BLUE_DISCORD - Enable Discord support in the auth for blues
|
||||||
#####################
|
#####################
|
||||||
BLUE_STANDING = float(os.environ.get('AA_BLUE_STANDING', '5.0'))
|
BLUE_STANDING = float(os.environ.get('AA_BLUE_STANDING', '5.0'))
|
||||||
ENABLE_BLUE_FORUM = 'True' == os.environ.get('AA_ENABLE_BLUE_FORUM', 'False')
|
ENABLE_BLUE_FORUM = 'True' == os.environ.get('AA_ENABLE_BLUE_FORUM', 'False')
|
||||||
ENABLE_BLUE_JABBER = 'True' == os.environ.get('AA_ENABLE_BLUE_JABBER', 'False')
|
ENABLE_BLUE_JABBER = 'True' == os.environ.get('AA_ENABLE_BLUE_JABBER', 'False')
|
||||||
ENABLE_BLUE_MUMBLE = 'True' == os.environ.get('AA_ENABLE_BLUE_MUMBLE', 'True')
|
ENABLE_BLUE_MUMBLE = 'True' == os.environ.get('AA_ENABLE_BLUE_MUMBLE', 'False')
|
||||||
ENABLE_BLUE_IPBOARD = 'True' == os.environ.get('AA_ENABLE_BLUE_IPBOARD', 'False')
|
ENABLE_BLUE_IPBOARD = 'True' == os.environ.get('AA_ENABLE_BLUE_IPBOARD', 'False')
|
||||||
ENABLE_BLUE_TEAMSPEAK3 = 'True' == os.environ.get('AA_ENABLE_BLUE_TEAMSPEAK3', 'False')
|
ENABLE_BLUE_TEAMSPEAK3 = 'True' == os.environ.get('AA_ENABLE_BLUE_TEAMSPEAK3', 'False')
|
||||||
|
ENABLE_BLUE_DISCORD = 'True' == os.environ.get('AA_ENABLE_BLUE_DISCORD', 'False')
|
||||||
|
|
||||||
#########################
|
#########################
|
||||||
# Corp Configuration
|
# Corp Configuration
|
||||||
@ -330,3 +334,14 @@ TEAMSPEAK3_SERVERQUERY_USER = os.environ.get('AA_TEAMSPEAK3_SERVERQUERY_USER', '
|
|||||||
TEAMSPEAK3_SERVERQUERY_PASSWORD = os.environ.get('AA_TEAMSPEAK3_SERVERQUERY_PASSWORD', 'passwordhere')
|
TEAMSPEAK3_SERVERQUERY_PASSWORD = os.environ.get('AA_TEAMSPEAK3_SERVERQUERY_PASSWORD', 'passwordhere')
|
||||||
TEAMSPEAK3_VIRTUAL_SERVER = int(os.environ.get('AA_TEAMSPEAK3_VIRTUAL_SERVER', '1'))
|
TEAMSPEAK3_VIRTUAL_SERVER = int(os.environ.get('AA_TEAMSPEAK3_VIRTUAL_SERVER', '1'))
|
||||||
TEAMSPEAK3_PUBLIC_URL = os.environ.get('AA_TEAMSPEAK3_PUBLIC_URL', 'yourdomain.com')
|
TEAMSPEAK3_PUBLIC_URL = os.environ.get('AA_TEAMSPEAK3_PUBLIC_URL', 'yourdomain.com')
|
||||||
|
|
||||||
|
######################################
|
||||||
|
# Discord Configuration
|
||||||
|
######################################
|
||||||
|
# DISCORD_SERVER_ID - ID of the server to manage
|
||||||
|
# DISCORD_USER_EMAIL - email of the server management user
|
||||||
|
# DISCORD_USER_PASSWORD - password of the server management user
|
||||||
|
######################################
|
||||||
|
DISCORD_SERVER_ID = os.environ.get('AA_DISCORD_SERVER_ID', '')
|
||||||
|
DISCORD_USER_EMAIL = os.environ.get('AA_DISCORD_USER_EMAIL', '')
|
||||||
|
DISCORD_USER_PASSWORD = os.environ.get('AA_DISCORD_USER_PASSWORD', '')
|
||||||
|
@ -125,6 +125,11 @@ urlpatterns = patterns('',
|
|||||||
url(r'reset_teamspeak3_perm/$', 'services.views.reset_teamspeak3_perm',
|
url(r'reset_teamspeak3_perm/$', 'services.views.reset_teamspeak3_perm',
|
||||||
name='auth_reset_teamspeak3_perm'),
|
name='auth_reset_teamspeak3_perm'),
|
||||||
|
|
||||||
|
# Discord Service Control
|
||||||
|
url(r'^activate_discord/$', 'services.views.activate_discord', name='auth_activate_discord'),
|
||||||
|
url(r'^deactivate_discord/$', 'services.views.deactivate_discord', name='auth_deactivate_discord'),
|
||||||
|
url(r'^reset_discord/$', 'services.views.reset_discord', name='auth_reset_discord'),
|
||||||
|
|
||||||
# Tools
|
# Tools
|
||||||
url(r'^tool/fleet_formatter_tool/$', 'services.views.fleet_formatter_view',
|
url(r'^tool/fleet_formatter_tool/$', 'services.views.fleet_formatter_view',
|
||||||
name='auth_fleet_format_tool_view'),
|
name='auth_fleet_format_tool_view'),
|
||||||
|
@ -80,3 +80,9 @@ class AuthServicesInfoManager:
|
|||||||
authserviceinfo.is_blue = is_blue
|
authserviceinfo.is_blue = is_blue
|
||||||
authserviceinfo.save(update_fields=['is_blue'])
|
authserviceinfo.save(update_fields=['is_blue'])
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def update_user_discord_info(user_id, user):
|
||||||
|
if User.objects.filter(username=user.username).exists():
|
||||||
|
authserviceinfo = AuthServicesInfoManager.__get_or_create(user)
|
||||||
|
authserviceinfo.discord_uid = user_id
|
||||||
|
authserviceinfo.save(update_fields=['discord_uid'])
|
||||||
|
@ -13,6 +13,7 @@ class AuthServicesInfo(models.Model):
|
|||||||
mumble_password = models.CharField(max_length=254, default="")
|
mumble_password = models.CharField(max_length=254, default="")
|
||||||
teamspeak3_uid = models.CharField(max_length=254, default="")
|
teamspeak3_uid = models.CharField(max_length=254, default="")
|
||||||
teamspeak3_perm_key = models.CharField(max_length=254, default="")
|
teamspeak3_perm_key = models.CharField(max_length=254, default="")
|
||||||
|
discord_uid = models.CharField(max_length=254, default="")
|
||||||
main_char_id = models.CharField(max_length=64, default="")
|
main_char_id = models.CharField(max_length=64, default="")
|
||||||
is_blue = models.BooleanField(default=False)
|
is_blue = models.BooleanField(default=False)
|
||||||
user = models.ForeignKey(User)
|
user = models.ForeignKey(User)
|
||||||
|
@ -9,6 +9,7 @@ from services.managers.mumble_manager import MumbleManager
|
|||||||
from services.managers.phpbb3_manager import Phpbb3Manager
|
from services.managers.phpbb3_manager import Phpbb3Manager
|
||||||
from services.managers.ipboard_manager import IPBoardManager
|
from services.managers.ipboard_manager import IPBoardManager
|
||||||
from services.managers.teamspeak3_manager import Teamspeak3Manager
|
from services.managers.teamspeak3_manager import Teamspeak3Manager
|
||||||
|
from services.managers.discord_manager import DiscordManager
|
||||||
from services.models import AuthTS
|
from services.models import AuthTS
|
||||||
from services.models import TSgroup
|
from services.models import TSgroup
|
||||||
from authentication.models import AuthServicesInfo
|
from authentication.models import AuthServicesInfo
|
||||||
@ -105,6 +106,17 @@ def update_teamspeak3_groups(user):
|
|||||||
|
|
||||||
Teamspeak3Manager.update_groups(authserviceinfo.teamspeak3_uid, groups)
|
Teamspeak3Manager.update_groups(authserviceinfo.teamspeak3_uid, groups)
|
||||||
|
|
||||||
|
def update_discord_groups(user):
|
||||||
|
syncgroups = SyncGroupCache.objects.filter(user=user)
|
||||||
|
authserviceinfo = AuthServicesInfo.objects.get(user=user)
|
||||||
|
groups = []
|
||||||
|
for syncgroup in syncgroups:
|
||||||
|
groups.append(str(syncgroup.groupname))
|
||||||
|
|
||||||
|
if len(groups) == 0:
|
||||||
|
groups.append('empty')
|
||||||
|
|
||||||
|
DiscordManager.update_groups(authserviceinfo.discord_uid, groups)
|
||||||
|
|
||||||
def create_syncgroup_for_user(user, groupname, servicename):
|
def create_syncgroup_for_user(user, groupname, servicename):
|
||||||
synccache = SyncGroupCache()
|
synccache = SyncGroupCache()
|
||||||
@ -151,6 +163,10 @@ def add_to_databases(user, groups, syncgroups):
|
|||||||
if syncgroups.filter(groupname=group.name).filter(servicename="ipboard").exists() is not True:
|
if syncgroups.filter(groupname=group.name).filter(servicename="ipboard").exists() is not True:
|
||||||
create_syncgroup_for_user(user, group.name, "ipboard")
|
create_syncgroup_for_user(user, group.name, "ipboard")
|
||||||
update_ipboard_groups(user)
|
update_ipboard_groups(user)
|
||||||
|
if authserviceinfo.discord_uid and authserviceinfo.discord_uid != "":
|
||||||
|
if syncgroups.filter(groupname=group.name).filter(servicename="discord").exists() is not True:
|
||||||
|
create_syncgroup_for_user(user, group.name, "discord")
|
||||||
|
update_discord_groups(user)
|
||||||
|
|
||||||
|
|
||||||
def remove_from_databases(user, groups, syncgroups):
|
def remove_from_databases(user, groups, syncgroups):
|
||||||
@ -180,6 +196,8 @@ def remove_from_databases(user, groups, syncgroups):
|
|||||||
update_ipboard_groups(user)
|
update_ipboard_groups(user)
|
||||||
if authserviceinfo.teamspeak3_uid and authserviceinfo.teamspeak3_uid != "":
|
if authserviceinfo.teamspeak3_uid and authserviceinfo.teamspeak3_uid != "":
|
||||||
update_teamspeak3_groups(user)
|
update_teamspeak3_groups(user)
|
||||||
|
if authserviceinfo.discord_uid and authserviceinfo.discord_uid != "":
|
||||||
|
update_discord_groups(user)
|
||||||
|
|
||||||
|
|
||||||
# Run every minute
|
# Run every minute
|
||||||
|
@ -24,3 +24,7 @@ class FleetFormatterForm(forms.Form):
|
|||||||
reimbursable = forms.ChoiceField(label='Reimbursable?*', choices=[('Yes', 'Yes'), ('No', 'No')], required=True)
|
reimbursable = forms.ChoiceField(label='Reimbursable?*', choices=[('Yes', 'Yes'), ('No', 'No')], required=True)
|
||||||
important = forms.ChoiceField(label='Important?*', choices=[('Yes', 'Yes'), ('No', 'No')], required=True)
|
important = forms.ChoiceField(label='Important?*', choices=[('Yes', 'Yes'), ('No', 'No')], required=True)
|
||||||
comments = forms.CharField(widget=forms.Textarea, required=False)
|
comments = forms.CharField(widget=forms.Textarea, required=False)
|
||||||
|
|
||||||
|
class DiscordForm(forms.Form):
|
||||||
|
email = forms.CharField(label="Email Address", required=True)
|
||||||
|
password = forms.CharField(label="Password", required=True, widget=forms.PasswordInput)
|
||||||
|
356
services/managers/discord_manager.py
Normal file
356
services/managers/discord_manager.py
Normal file
@ -0,0 +1,356 @@
|
|||||||
|
import requests
|
||||||
|
import json
|
||||||
|
from django.conf import settings
|
||||||
|
import re
|
||||||
|
import os
|
||||||
|
|
||||||
|
DISCORD_URL = "https://discordapp.com/api"
|
||||||
|
|
||||||
|
class DiscordAPIManager:
|
||||||
|
|
||||||
|
def __init__(self, server_id, email, password):
|
||||||
|
data = {
|
||||||
|
"email" : email,
|
||||||
|
"password": password,
|
||||||
|
}
|
||||||
|
custom_headers = {'content-type':'application/json'}
|
||||||
|
path = DISCORD_URL + "/auth/login"
|
||||||
|
r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
||||||
|
r.raise_for_status()
|
||||||
|
self.token = r.json()['token']
|
||||||
|
self.email = email
|
||||||
|
self.password = password
|
||||||
|
self.server_id = server_id
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
if hasattr(self, 'token'):
|
||||||
|
if self.token and self.token != "":
|
||||||
|
data = {'token': self.token}
|
||||||
|
path = DISCORD_URL + "/auth/logout"
|
||||||
|
custom_headers = {'content-type':'application/json'}
|
||||||
|
r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_auth_token():
|
||||||
|
data = {
|
||||||
|
"email" : settings.DISCORD_USER_EMAIL,
|
||||||
|
"password": settings.DISCORD_USER_PASSWORD,
|
||||||
|
}
|
||||||
|
custom_headers = {'content-type':'application/json'}
|
||||||
|
path = DISCORD_URL + "/auth/login"
|
||||||
|
r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()['token']
|
||||||
|
|
||||||
|
def add_server(self, name):
|
||||||
|
data = {"name": name}
|
||||||
|
custom_headers = {'content-type':'application/json', 'authorization': self.token}
|
||||||
|
path = DISCORD_URL + "/guilds"
|
||||||
|
r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def rename_server(self, name):
|
||||||
|
data = {"name": name}
|
||||||
|
custom_headers = {'content-type':'application/json', 'authorization': self.token}
|
||||||
|
path = DISCORD_URL + "/guilds/" + str(self.server_id)
|
||||||
|
r = requests.patch(path, headers=custom_headers, data=json.dumps(data))
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def delete_server(self):
|
||||||
|
custom_headers = {'content-type':'application/json', 'authorization': self.token}
|
||||||
|
path = DISCORD_URL + "/guilds/" + str(self.server_id)
|
||||||
|
r = requests.delete(path, headers=custom_headers)
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
def get_members(self):
|
||||||
|
custom_headers = {'accept':'application/json', 'authorization': self.token}
|
||||||
|
path = DISCORD_URL + "/guilds/" + str(self.server_id) + "/members"
|
||||||
|
r = requests.get(path, headers=custom_headers)
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def get_bans(self):
|
||||||
|
custom_headers = {'accept':'application/json', 'authorization': self.token}
|
||||||
|
path = DISCORD_URL + "/guilds/" + str(self.server_id) + "/bans"
|
||||||
|
r = requests.get(path, headers=custom_headers)
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def ban_user(self, user_id, delete_message_age=0):
|
||||||
|
custom_headers = {'authorization': self.token}
|
||||||
|
path = DISCORD_URL + "/guilds/" + str(self.server_id) + "/bans/" + str(user_id) + "?delete-message-days=" + str(delete_message_age)
|
||||||
|
r = requests.put(path, headers=custom_headers)
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
def unban_user(self, user_id):
|
||||||
|
custom_headers = {'authorization': self.token}
|
||||||
|
path = DISCORD_URL + "/guilds/" + str(self.server_id) + "/bans/" + str(user_id)
|
||||||
|
r = requests.delete(path, headers=custom_headers)
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
def generate_role(self):
|
||||||
|
custom_headers = {'accept':'application/json', 'authorization': self.token}
|
||||||
|
path = DISCORD_URL + "/guilds/" + str(self.server_id) + "/roles"
|
||||||
|
r = requests.post(path, headers=custom_headers)
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def edit_role(self, role_id, name, color=0, hoist=True, permissions=36785152):
|
||||||
|
custom_headers = {'content-type':'application/json', 'authorization': self.token}
|
||||||
|
data = {
|
||||||
|
'color': color,
|
||||||
|
'hoist': hoist,
|
||||||
|
'name': name,
|
||||||
|
'permissions': permissions,
|
||||||
|
}
|
||||||
|
path = DISCORD_URL + "/guilds/" + str(self.server_id) + "/roles/" + str(role_id)
|
||||||
|
r = requests.patch(path, headers=custom_headers, data=json.dumps(data))
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def delete_role(self, role_id):
|
||||||
|
custom_headers = {'authorization': self.token}
|
||||||
|
path = DISCORD_URL + "/guilds/" + str(self.server_id) + "/roles/" + str(role_id)
|
||||||
|
r = requests.delete(path, headers=custom_headers)
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_invite(invite_id):
|
||||||
|
custom_headers = {'accept': 'application/json'}
|
||||||
|
path = DISCORD_URL + "/invite/" + str(invite_id)
|
||||||
|
r = requests.get(path, headers=custom_headers)
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def accept_invite(invite_id, token):
|
||||||
|
custom_headers = {'accept': 'application/json', 'authorization': token}
|
||||||
|
path = DISCORD_URL + "/invite/" + str(invite_id)
|
||||||
|
r = requests.post(path, headers=custom_headers)
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def create_invite(self, max_age=600, max_uses=1, temporary=True, xkcdpass=False):
|
||||||
|
custom_headers = {'authorization': self.token}
|
||||||
|
path = DISCORD_URL + "/channels/" + str(self.server_id) + "/invites"
|
||||||
|
data = {
|
||||||
|
'max_age': max_age,
|
||||||
|
'max_uses': max_uses,
|
||||||
|
'temporary': temporary,
|
||||||
|
'xkcdpass': xkcdpass,
|
||||||
|
}
|
||||||
|
r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def delete_invite(self, invite_id):
|
||||||
|
custom_headers = {'authorization': self.token}
|
||||||
|
path = DISCORD_URL + "/invite/" + str(invite_id)
|
||||||
|
r = requests.delete(path, headers=custom_headers)
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
def set_roles(self, user_id, role_ids):
|
||||||
|
custom_headers = {'authorization': self.token, 'content-type':'application/json'}
|
||||||
|
path = DISCORD_URL + "/guilds/" + str(self.server_id) + "/members/" + str(user_id)
|
||||||
|
data = { 'roles': role_ids }
|
||||||
|
r = requests.patch(path, headers=custom_headers, data=json.dumps(data))
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def register_user(server_id, username, invite_code, password, email):
|
||||||
|
custom_headers = {'content-type': 'application/json'}
|
||||||
|
data = {
|
||||||
|
'fingerprint': None,
|
||||||
|
'username': username,
|
||||||
|
'invite': invite_code,
|
||||||
|
'password': password,
|
||||||
|
'email': email,
|
||||||
|
}
|
||||||
|
path = DISCORD_URL + "/auth/register"
|
||||||
|
r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
def kick_user(self, user_id):
|
||||||
|
custom_headers = {'authorization': self.token}
|
||||||
|
path = DISCORD_URL + "/guilds/" + str(self.server_id) + "/members/" + str(user_id)
|
||||||
|
r = requests.delete(path, headers=custom_headers)
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
def get_members(self):
|
||||||
|
custom_headers = {'authorization': self.token, 'accept':'application/json'}
|
||||||
|
path = DISCORD_URL + "/guilds/" + str(self.server_id) + "/members"
|
||||||
|
r = requests.get(path, headers=custom_headers)
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def get_user_id(self, username):
|
||||||
|
all_members = self.get_members()
|
||||||
|
for member in all_members:
|
||||||
|
if member['user']['username'] == username:
|
||||||
|
return member['user']['id']
|
||||||
|
raise KeyError('User not found on server: ' + username)
|
||||||
|
|
||||||
|
def get_roles(self):
|
||||||
|
custom_headers = {'authorization': self.token, 'accept':'application/json'}
|
||||||
|
path = DISCORD_URL + "/guilds/" + str(self.server_id) + "/roles"
|
||||||
|
r = requests.get(path, headers=custom_headers)
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def get_group_id(self, group_name):
|
||||||
|
all_roles = self.get_roles()
|
||||||
|
for role in all_roles:
|
||||||
|
if role['name'] == group_name:
|
||||||
|
return role['id']
|
||||||
|
raise KeyError('Group not found on server: ' + group_name)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_token_by_user(email, password):
|
||||||
|
data = {
|
||||||
|
"email" : email,
|
||||||
|
"password": password,
|
||||||
|
}
|
||||||
|
custom_headers = {'content-type':'application/json'}
|
||||||
|
path = DISCORD_URL + "/auth/login"
|
||||||
|
r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()['token']
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_user_profile(email, password):
|
||||||
|
token = DiscordAPIManager.get_token_by_user(email, password)
|
||||||
|
custom_headers = {'accept': 'application/json', 'authorization': token}
|
||||||
|
path = DISCORD_URL + "/users/@me"
|
||||||
|
r = requests.get(path, headers=custom_headers)
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def set_user_password(email, current_password, new_password):
|
||||||
|
profile = DiscordAPIManager.get_user_profile(email, current_password)
|
||||||
|
avatar = profile['avatar']
|
||||||
|
username = profile['username']
|
||||||
|
data = {
|
||||||
|
'avatar': avatar,
|
||||||
|
'username': username,
|
||||||
|
'password': current_password,
|
||||||
|
'new_password': new_password,
|
||||||
|
'email': email,
|
||||||
|
}
|
||||||
|
path = DISCORD_URL + "/users/@me"
|
||||||
|
custom_headers = {'content-type':'application/json', 'authorization': DiscordAPIManager.get_token_by_user(email, current_password)}
|
||||||
|
r = requests.patch(path, headers=custom_headers, data=json.dumps(data))
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def destroy_user(email, current_password):
|
||||||
|
data = {
|
||||||
|
'avatar': None,
|
||||||
|
'username': os.urandom(8).encode('hex'),
|
||||||
|
'password': current_password,
|
||||||
|
'email': os.urandom(8).encode('hex') + '@test.com',
|
||||||
|
}
|
||||||
|
path = DISCORD_URL + "/users/@me"
|
||||||
|
custom_headers = {'content-type':'application/json', 'authorization': DiscordAPIManager.get_token_by_user(email, current_password)}
|
||||||
|
r = requests.patch(path, headers=custom_headers, data=json.dumps(data))
|
||||||
|
r.raise_for_status
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def check_if_user_banned(self, user_id):
|
||||||
|
bans = self.get_bans()
|
||||||
|
for b in bans:
|
||||||
|
if b['user']['id'] == str(user_id):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
class DiscordManager:
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def __sanatize_username(username):
|
||||||
|
clean = re.sub(r'[^\w]','_', username)
|
||||||
|
return clean
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def __generate_random_pass():
|
||||||
|
return os.urandom(8).encode('hex')
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def update_groups(user_id, groups):
|
||||||
|
group_ids = []
|
||||||
|
api = DiscordAPIManager(settings.DISCORD_SERVER_ID, settings.DISCORD_USER_EMAIL, settings.DISCORD_USER_PASSWORD)
|
||||||
|
if len(groups) == 0:
|
||||||
|
group_ids = []
|
||||||
|
else:
|
||||||
|
for g in groups:
|
||||||
|
try:
|
||||||
|
group_id = api.get_group_id(g)
|
||||||
|
group_ids.append(group_id)
|
||||||
|
except:
|
||||||
|
# need to create role on server for group
|
||||||
|
group_ids.append(DiscordManager.create_group(g))
|
||||||
|
api.set_roles(user_id, group_ids)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def create_group(groupname):
|
||||||
|
api = DiscordAPIManager(settings.DISCORD_SERVER_ID, settings.DISCORD_USER_EMAIL, settings.DISCORD_USER_PASSWORD)
|
||||||
|
new_group = api.generate_role()
|
||||||
|
named_group = api.edit_role(new_group['id'], groupname)
|
||||||
|
return named_group['id']
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def lock_user(user_id):
|
||||||
|
try:
|
||||||
|
api = DiscordAPIManager(settings.DISCORD_SERVER_ID, settings.DISCORD_USER_EMAIL, settings.DISCORD_USER_PASSWORD)
|
||||||
|
api.ban_user(user_id)
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def unlock_user(user_id):
|
||||||
|
try:
|
||||||
|
api = DiscordAPIManager(settings.DISCORD_SERVER_ID, settings.DISCORD_USER_EMAIL, settings.DISCORD_USER_PASSWORD)
|
||||||
|
api.unban_user(user_id)
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def update_user_password(email, current_password):
|
||||||
|
new_password = DiscordManager.__generate_random_pass()
|
||||||
|
try:
|
||||||
|
profile = DiscordAPIManager.set_user_password(email, current_password, new_password)
|
||||||
|
return new_password
|
||||||
|
except:
|
||||||
|
return current_password
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def add_user(email, password):
|
||||||
|
try:
|
||||||
|
api = DiscordAPIManager(settings.DISCORD_SERVER_ID, settings.DISCORD_USER_EMAIL, settings.DISCORD_USER_PASSWORD)
|
||||||
|
profile = DiscordAPIManager.get_user_profile(email, password)
|
||||||
|
user_id = profile['id']
|
||||||
|
if api.check_if_user_banned(user_id):
|
||||||
|
api.unban_user(user_id)
|
||||||
|
invite_code = api.create_invite()['code']
|
||||||
|
token = DiscordAPIManager.get_token_by_user(email, password)
|
||||||
|
DiscordAPIManager.accept_invite(invite_code, token)
|
||||||
|
return user_id
|
||||||
|
except:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def delete_user(user_id):
|
||||||
|
try:
|
||||||
|
api = DiscordAPIManager(settings.DISCORD_SERVER_ID, settings.DISCORD_USER_EMAIL, settings.DISCORD_USER_PASSWORD)
|
||||||
|
DiscordManager.update_groups(user_id, [])
|
||||||
|
api.ban_user(user_id)
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
return False
|
@ -12,6 +12,7 @@ from managers.phpbb3_manager import Phpbb3Manager
|
|||||||
from managers.mumble_manager import MumbleManager
|
from managers.mumble_manager import MumbleManager
|
||||||
from managers.ipboard_manager import IPBoardManager
|
from managers.ipboard_manager import IPBoardManager
|
||||||
from managers.teamspeak3_manager import Teamspeak3Manager
|
from managers.teamspeak3_manager import Teamspeak3Manager
|
||||||
|
from managers.discord_manager import DiscordManager
|
||||||
from authentication.managers import AuthServicesInfoManager
|
from authentication.managers import AuthServicesInfoManager
|
||||||
from eveonline.managers import EveManager
|
from eveonline.managers import EveManager
|
||||||
from celerytask.tasks import remove_all_syncgroups_for_service
|
from celerytask.tasks import remove_all_syncgroups_for_service
|
||||||
@ -20,8 +21,10 @@ from celerytask.tasks import update_mumble_groups
|
|||||||
from celerytask.tasks import update_forum_groups
|
from celerytask.tasks import update_forum_groups
|
||||||
from celerytask.tasks import update_ipboard_groups
|
from celerytask.tasks import update_ipboard_groups
|
||||||
from celerytask.tasks import update_teamspeak3_groups
|
from celerytask.tasks import update_teamspeak3_groups
|
||||||
|
from celerytask.tasks import update_discord_groups
|
||||||
from forms import JabberBroadcastForm
|
from forms import JabberBroadcastForm
|
||||||
from forms import FleetFormatterForm
|
from forms import FleetFormatterForm
|
||||||
|
from forms import DiscordForm
|
||||||
from util import check_if_user_has_permission
|
from util import check_if_user_has_permission
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
@ -317,3 +320,47 @@ def fleet_fits(request):
|
|||||||
context = {}
|
context = {}
|
||||||
return render_to_response('registered/fleetfits.html', context,
|
return render_to_response('registered/fleetfits.html', context,
|
||||||
context_instance=RequestContext(request))
|
context_instance=RequestContext(request))
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
@user_passes_test(service_blue_alliance_test)
|
||||||
|
def deactivate_discord(request):
|
||||||
|
authinfo = AuthServicesInfoManager.get_auth_service_info(request.user)
|
||||||
|
result = DiscordManager.delete_user(authinfo.discord_uid)
|
||||||
|
if result:
|
||||||
|
remove_all_syncgroups_for_service(request.user, "discord")
|
||||||
|
AuthServicesInfoManager.update_user_discord_info("", request.user)
|
||||||
|
return HttpResponseRedirect("/services/")
|
||||||
|
return HttpResponseRedirect("/dashboard")
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
@user_passes_test(service_blue_alliance_test)
|
||||||
|
def reset_discord(request):
|
||||||
|
authinfo = AuthServicesInfoManager.get_auth_service_info(request.user)
|
||||||
|
result = DiscordManager.delete_user(authinfo.discord_uid)
|
||||||
|
if result:
|
||||||
|
AuthServicesInfoManager.update_user_discord_info("",request.user)
|
||||||
|
return HttpResponseRedirect("/activate_discord/")
|
||||||
|
return HttpResponseRedirect("/services/")
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
@user_passes_test(service_blue_alliance_test)
|
||||||
|
def activate_discord(request):
|
||||||
|
success = False
|
||||||
|
if request.method == 'POST':
|
||||||
|
form = DiscordForm(request.POST)
|
||||||
|
if form.is_valid():
|
||||||
|
email = form.cleaned_data['email']
|
||||||
|
password = form.cleaned_data['password']
|
||||||
|
try:
|
||||||
|
user_id = DiscordManager.add_user(email, password)
|
||||||
|
AuthServicesInfoManager.update_user_discord_info(user_id, request.user)
|
||||||
|
update_discord_groups(request.user)
|
||||||
|
success = True
|
||||||
|
return HttpResponseRedirect("/services/")
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
form = DiscordForm()
|
||||||
|
|
||||||
|
context = {'form': form, 'success': success}
|
||||||
|
return render_to_response('registered/discord.html', context, context_instance=RequestContext(request))
|
||||||
|
33
stock/templates/registered/discord.html
Normal file
33
stock/templates/registered/discord.html
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
{% extends "public/base.html" %}
|
||||||
|
{% load bootstrap %}
|
||||||
|
{% load staticfiles %}
|
||||||
|
|
||||||
|
{% block title %}Alliance Auth{% endblock %}
|
||||||
|
|
||||||
|
{% block page_title %}Jabber Broadcast{% endblock page_title %}
|
||||||
|
{% block extra_css %}{% endblock extra_css %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<div class="col-lg-12">
|
||||||
|
<h1 class="page-header text-center">Discord Connection</h1>
|
||||||
|
|
||||||
|
<div class="container-fluid">
|
||||||
|
<div class="col-md-4 col-md-offset-4">
|
||||||
|
<div class="row">
|
||||||
|
{% if success %}
|
||||||
|
<div class="alert alert-success" role="alert">Account Linked</div>
|
||||||
|
{% endif %}
|
||||||
|
<p>Enter your discord account credentials below. These are not stored: they are needed to determine your user ID and make you join the server.</p>
|
||||||
|
<form class="form-signin" role="form" action="" method="POST"
|
||||||
|
onsubmit="submitbutton.disabled = true; return true;">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{ form|bootstrap }}
|
||||||
|
<br/>
|
||||||
|
<button class="btn btn-lg btn-primary btn-block" name="submitbutton" type="submit">Connect</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% endblock content %}
|
@ -114,6 +114,31 @@
|
|||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% if ENABLE_BLUE_DISCORD %}
|
||||||
|
<tr>
|
||||||
|
<td class="text-center">Discord</td>
|
||||||
|
<td class="text-center"></td>
|
||||||
|
<td class="text-center"></td>
|
||||||
|
<td class="text-center"><a href="https://discordapp.com/channels/{{ DISCORD_SERVER_ID }}/{{ DISCORD_SERVER_ID}}">https://discordapp.com</a></td>
|
||||||
|
<td class="text-center">
|
||||||
|
{% ifequal authinfo.discord_uid "" %}
|
||||||
|
<a href="{% url 'auth_activate_discord' %}">
|
||||||
|
<button type="button" class="btn btn-warning"><span
|
||||||
|
class="glyphicon glyphicon-ok"></span></button>
|
||||||
|
</a>
|
||||||
|
{% else %}
|
||||||
|
<a href="{% url 'auth_reset_discord' %}">
|
||||||
|
<button type="button" class="btn btn-primary"><span
|
||||||
|
class="glyphicon glyphicon-refresh"></span></button>
|
||||||
|
</a>
|
||||||
|
<a href="{% url 'auth_deactivate_discord' %}">
|
||||||
|
<button type="button" class="btn btn-danger"><span
|
||||||
|
class="glyphicon glyphicon-remove"></span></button>
|
||||||
|
</a>
|
||||||
|
{% endifequal %}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% endif %}
|
||||||
{% if ENABLE_BLUE_TEAMSPEAK3 %}
|
{% if ENABLE_BLUE_TEAMSPEAK3 %}
|
||||||
<tr>
|
<tr>
|
||||||
<th class="text-center">Service</th>
|
<th class="text-center">Service</th>
|
||||||
@ -260,6 +285,31 @@
|
|||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% if ENABLE_AUTH_DISCORD %}
|
||||||
|
<tr>
|
||||||
|
<td class="text-center">Discord</td>
|
||||||
|
<td class="text-center"></td>
|
||||||
|
<td class="text-center"></td>
|
||||||
|
<td class="text-center"><a href="https://discordapp.com/channels/{{ DISCORD_SERVER_ID }}/{{ DISCORD_SERVER_ID}}">https://discordapp.com</a></td>
|
||||||
|
<td class="text-center">
|
||||||
|
{% ifequal authinfo.discord_uid "" %}
|
||||||
|
<a href="{% url 'auth_activate_discord' %}">
|
||||||
|
<button type="button" class="btn btn-warning"><span
|
||||||
|
class="glyphicon glyphicon-ok"></span></button>
|
||||||
|
</a>
|
||||||
|
{% else %}
|
||||||
|
<a href="{% url 'auth_reset_discord' %}">
|
||||||
|
<button type="button" class="btn btn-primary"><span
|
||||||
|
class="glyphicon glyphicon-refresh"></span></button>
|
||||||
|
</a>
|
||||||
|
<a href="{% url 'auth_deactivate_discord' %}">
|
||||||
|
<button type="button" class="btn btn-danger"><span
|
||||||
|
class="glyphicon glyphicon-remove"></span></button>
|
||||||
|
</a>
|
||||||
|
{% endifequal %}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% endif %}
|
||||||
{% if ENABLE_AUTH_TEAMSPEAK3 %}
|
{% if ENABLE_AUTH_TEAMSPEAK3 %}
|
||||||
<tr>
|
<tr>
|
||||||
<th class="text-center">Service</th>
|
<th class="text-center">Service</th>
|
||||||
|
@ -7,6 +7,7 @@ from services.managers.phpbb3_manager import Phpbb3Manager
|
|||||||
from services.managers.mumble_manager import MumbleManager
|
from services.managers.mumble_manager import MumbleManager
|
||||||
from services.managers.ipboard_manager import IPBoardManager
|
from services.managers.ipboard_manager import IPBoardManager
|
||||||
from services.managers.teamspeak3_manager import Teamspeak3Manager
|
from services.managers.teamspeak3_manager import Teamspeak3Manager
|
||||||
|
from services.managers.discord_manager import DiscordManager
|
||||||
|
|
||||||
|
|
||||||
def add_user_to_group(user, groupname):
|
def add_user_to_group(user, groupname):
|
||||||
@ -41,6 +42,9 @@ def deactivate_services(user):
|
|||||||
if authinfo.teamspeak3_uid and authinfo.teamspeak3_uid != "":
|
if authinfo.teamspeak3_uid and authinfo.teamspeak3_uid != "":
|
||||||
Teamspeak3Manager.delete_user(authinfo.teamspeak3_uid)
|
Teamspeak3Manager.delete_user(authinfo.teamspeak3_uid)
|
||||||
AuthServicesInfoManager.update_user_teamspeak3_info("", "", user)
|
AuthServicesInfoManager.update_user_teamspeak3_info("", "", user)
|
||||||
|
if authinfo.discord_uid and authinfo.discord_uid != "":
|
||||||
|
DiscordManager.delete_user(authinfo.discord_uid)
|
||||||
|
AuthServicesInfoManager.update_user_discord_info("", user)
|
||||||
|
|
||||||
|
|
||||||
def generate_corp_group_name(corpname):
|
def generate_corp_group_name(corpname):
|
||||||
|
@ -34,11 +34,14 @@ def domain_url(request):
|
|||||||
'ENABLE_AUTH_MUMBLE': settings.ENABLE_AUTH_MUMBLE,
|
'ENABLE_AUTH_MUMBLE': settings.ENABLE_AUTH_MUMBLE,
|
||||||
'ENABLE_AUTH_IPBOARD': settings.ENABLE_AUTH_IPBOARD,
|
'ENABLE_AUTH_IPBOARD': settings.ENABLE_AUTH_IPBOARD,
|
||||||
'ENABLE_AUTH_TEAMSPEAK3': settings.ENABLE_AUTH_TEAMSPEAK3,
|
'ENABLE_AUTH_TEAMSPEAK3': settings.ENABLE_AUTH_TEAMSPEAK3,
|
||||||
|
'ENABLE_AUTH_DISCORD': settings.ENABLE_AUTH_DISCORD,
|
||||||
'ENABLE_BLUE_JABBER': settings.ENABLE_BLUE_JABBER,
|
'ENABLE_BLUE_JABBER': settings.ENABLE_BLUE_JABBER,
|
||||||
'ENABLE_BLUE_FORUM': settings.ENABLE_BLUE_FORUM,
|
'ENABLE_BLUE_FORUM': settings.ENABLE_BLUE_FORUM,
|
||||||
'ENABLE_BLUE_MUMBLE': settings.ENABLE_BLUE_MUMBLE,
|
'ENABLE_BLUE_MUMBLE': settings.ENABLE_BLUE_MUMBLE,
|
||||||
'ENABLE_BLUE_IPBOARD': settings.ENABLE_BLUE_IPBOARD,
|
'ENABLE_BLUE_IPBOARD': settings.ENABLE_BLUE_IPBOARD,
|
||||||
'ENABLE_BLUE_TEAMSPEAK3': settings.ENABLE_BLUE_TEAMSPEAK3,
|
'ENABLE_BLUE_TEAMSPEAK3': settings.ENABLE_BLUE_TEAMSPEAK3,
|
||||||
|
'ENABLE_BLUE_DISCORD': settings.ENABLE_BLUE_DISCORD,
|
||||||
'TEAMSPEAK3_PUBLIC_URL': settings.TEAMSPEAK3_PUBLIC_URL,
|
'TEAMSPEAK3_PUBLIC_URL': settings.TEAMSPEAK3_PUBLIC_URL,
|
||||||
'JACK_KNIFE_URL': settings.JACK_KNIFE_URL,
|
'JACK_KNIFE_URL': settings.JACK_KNIFE_URL,
|
||||||
|
'DISCORD_SERVER_ID': settings.DISCORD_SERVER_ID,
|
||||||
'CURRENT_UTC_TIME': timezone.now()}
|
'CURRENT_UTC_TIME': timezone.now()}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user