Correct discourse login for non-superusers.

Correct TS3 error handling.
Closes #554
This commit is contained in:
Adarnof 2016-10-25 21:12:23 +00:00
parent 4ff21b25c3
commit 5ee65706cb
2 changed files with 46 additions and 29 deletions

View File

@ -40,10 +40,15 @@ class Teamspeak3Manager:
def _get_userid(uid):
logger.debug("Looking for uid %s on TS3 server." % uid)
server = Teamspeak3Manager.__get_created_server()
try:
ret = server.send_command('customsearch', {'ident': 'sso_uid', 'pattern': uid})
if ret and 'keys' in ret and 'cldbid' in ret['keys']:
logger.debug("Got userid %s for uid %s" % (ret['keys']['cldbid'], uid))
return ret['keys']['cldbid']
except TeamspeakError as e:
if not e.code == '1281':
raise e
return None
@staticmethod
def _group_id_by_name(groupname):
@ -85,7 +90,13 @@ class Teamspeak3Manager:
def _user_group_list(cldbid):
logger.debug("Retrieving group list for user with id %s" % cldbid)
server = Teamspeak3Manager.__get_created_server()
try:
groups = server.send_command('servergroupsbyclientid', {'cldbid': cldbid})
except TeamspeakError as e:
if e.code == '1281': # no groups
groups = []
else:
raise e
logger.debug("Retrieved group list: %s" % groups)
outlist = {}
@ -231,7 +242,10 @@ class Teamspeak3Manager:
user = Teamspeak3Manager._get_userid(uid)
logger.debug("Deleting user %s with id %s from TS3 server." % (user, uid))
if user:
for client in server.send_command('clientlist'):
clients = server.send_command('clientlist')
logger.debug(clients['keys'])
if clients['keys']:
for client in clients:
try:
if client['keys']['client_database_id'] == user:
logger.debug("Found user %s on TS3 server - issuing deletion command." % user)
@ -253,6 +267,9 @@ class Teamspeak3Manager:
else:
logger.exception("Failed to delete user id %s from TS3 - received response %s" % (uid, ret))
return False
else:
logger.warning('Received no clients from TS3 server. Assuming user %s already deleted.' % uid)
return True
else:
logger.warn("User with id %s not found on TS3 server. Assuming succesful deletion." % uid)
return True

View File

@ -36,7 +36,7 @@ from services.forms import FleetFormatterForm
from services.forms import ServicePasswordForm
from services.forms import TeamspeakJoinForm
from authentication.decorators import members_and_blues
from authentication.states import MEMBER_STATE, BLUE_STATE
from authentication.states import MEMBER_STATE, BLUE_STATE, NONE_STATE
import base64
import hmac
@ -1132,13 +1132,13 @@ def discourse_sso(request):
auth, c = AuthServicesInfo.objects.get_or_create(user=request.user)
if not request.user.is_superuser:
if auth.state == MEMBER_STATE and not settings.ENABLE_AUTH_DISCOURSE:
messages.error(request, 'You are not authorized to access Discourse.')
if not settings.ENABLE_AUTH_DISCOURSE and auth.state == MEMBER_STATE:
messages.error(request, 'Members are not authorized to access Discourse.')
return redirect('auth_dashboard')
elif auth.state == BLUE_STATE and not settings.ENABLE_BLUE_DISCOURSE:
messages.error(request, 'You are not authorized to access Discourse.')
elif not settings.ENABLE_BLUE_DISCOURSE and auth.state == BLUE_STATE:
messages.error(request, 'Blues are not authorized to access Discourse.')
return redirect('auth_dashboard')
else:
elif auth.state == NONE_STATE:
messages.error(request, 'You are not authorized to access Discourse.')
return redirect('auth_dashboard')