Added "webLogoutHook" so we can, on authenticators, do some corrections

to response from authenticators
This commit is contained in:
Adolfo Gómez García 2019-01-21 11:48:10 +01:00
parent 26af865483
commit 5353573ca3
5 changed files with 47 additions and 45 deletions

View File

@ -29,8 +29,6 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from __future__ import unicode_literals
from django import http
from django.views.generic.base import View
from django.views.decorators.csrf import csrf_exempt
@ -42,8 +40,6 @@ from uds.REST.handlers import Handler, HandlerError, AccessDenied, NotFound, Req
import time
import logging
import six
logger = logging.getLogger(__name__)
__all__ = [str(v) for v in ['Handler', 'Dispatcher']]
@ -142,20 +138,20 @@ class Dispatcher(View):
response[k] = val
return response
except RequestError as e:
return http.HttpResponseBadRequest(six.text_type(e), content_type="text/plain")
return http.HttpResponseBadRequest(str(e), content_type="text/plain")
except ResponseError as e:
return http.HttpResponseServerError(six.text_type(e), content_type="text/plain")
return http.HttpResponseServerError(str(e), content_type="text/plain")
except NotSupportedError as e:
return http.HttpResponseBadRequest(six.text_type(e), content_type="text/plain")
return http.HttpResponseBadRequest(str(e), content_type="text/plain")
except AccessDenied as e:
return http.HttpResponseForbidden(six.text_type(e), content_type="text/plain")
return http.HttpResponseForbidden(str(e), content_type="text/plain")
except NotFound as e:
return http.HttpResponseNotFound(six.text_type(e), content_type="text/plain")
return http.HttpResponseNotFound(str(e), content_type="text/plain")
except HandlerError as e:
return http.HttpResponseBadRequest(six.text_type(e), content_type="text/plain")
return http.HttpResponseBadRequest(str(e), content_type="text/plain")
except Exception as e:
logger.exception('Error processing request')
return http.HttpResponseServerError(six.text_type(e), content_type="text/plain")
return http.HttpResponseServerError(str(e), content_type="text/plain")
@staticmethod
def registerSubclasses(classes):

View File

@ -31,18 +31,7 @@
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from django.utils.translation import ugettext, ugettext_lazy as _
from uds.models import MetaPool, ServicePool, OSManager, Service, Image, ServicesPoolGroup, Account
from uds.models.CalendarAction import (
CALENDAR_ACTION_INITIAL,
CALENDAR_ACTION_MAX,
CALENDAR_ACTION_CACHE_L1,
CALENDAR_ACTION_CACHE_L2,
CALENDAR_ACTION_PUBLISH,
CALENDAR_ACTION_ADD_TRANSPORT,
CALENDAR_ACTION_DEL_TRANSPORT,
CALENDAR_ACTION_ADD_GROUP,
CALENDAR_ACTION_DEL_GROUP
)
from uds.models import MetaPool, Image, ServicesPoolGroup
from uds.core.ui.images import DEFAULT_THUMB_BASE64
from uds.core.util.State import State
from uds.core.util.model import processUuid
@ -55,7 +44,6 @@ from .user_services import Groups
from uds.REST.methods.op_calendars import AccessCalendars
from .meta_service_pools import MetaServicesPool
import six
import logging
logger = logging.getLogger(__name__)
@ -87,7 +75,7 @@ class MetaPools(ModelHandler):
{'tags': {'title': _('tags'), 'visible': False}},
]
custom_methods = [('setFallbackAccess', True), ]
custom_methods = [('setFallbackAccess', True), ('getFallbackAccess', True)]
def item_as_dict(self, item: MetaPool):
# if item does not have an associated service, hide it (the case, for example, for a removed service)
@ -219,18 +207,6 @@ class MetaPools(ModelHandler):
item.save()
return ''
# Returns the action list based on current element, for calendar
def actionsList(self, item):
validActions = ()
itemInfo = item.service.getType()
if itemInfo.usesCache is True:
validActions += (CALENDAR_ACTION_INITIAL, CALENDAR_ACTION_CACHE_L1, CALENDAR_ACTION_MAX)
if itemInfo.usesCache_L2 is True:
validActions += (CALENDAR_ACTION_CACHE_L2,)
def getFallbackAccess(self, item):
return item.fallbackAccess
if itemInfo.publicationType is not None:
validActions += (CALENDAR_ACTION_PUBLISH,)
# Transport & groups actions
validActions += (CALENDAR_ACTION_ADD_TRANSPORT, CALENDAR_ACTION_DEL_TRANSPORT, CALENDAR_ACTION_ADD_GROUP, CALENDAR_ACTION_DEL_GROUP)
return validActions

View File

@ -93,8 +93,13 @@ class AccessCalendars(DetailHandler):
# If already exists
uuid = processUuid(item) if item is not None else None
calendar = Calendar.objects.get(uuid=processUuid(self._params['calendarId']))
access = self._params['access'].upper()
try:
calendar = Calendar.objects.get(uuid=processUuid(self._params['calendarId']))
access = self._params['access'].upper()
if access not in ('ALLOW', 'DENY'):
raise Exception()
except Exception:
self.invalidRequestException(_('Invalid parameters on request'))
priority = int(self._params['priority'])
if uuid is not None:

View File

@ -39,7 +39,7 @@ from uds.core.auths.GroupsManager import GroupsManager
from uds.core.auths.Exceptions import InvalidUserException
import logging
__updated__ = '2018-09-12'
__updated__ = '2019-01-21'
logger = logging.getLogger(__name__)
@ -388,6 +388,24 @@ class Authenticator(Module):
"""
return None
def webLogoutHook(self, username, request, response):
'''
Invoked on web logout of an user
Args:
username: Name of the user being logged out of the web
request: Django request
response: Django response
Returns:
Nothing
:note: This method will be invoked whenever the webLogout is requested. It receibes request & response so auth cna
make changes (for example, on cookies) to it.
'''
return
def getForAuth(self, username):
"""
Process the username for this authenticator and returns it.

View File

@ -54,7 +54,7 @@ from uds.models import User
import logging
import six
__updated__ = '2018-12-21'
__updated__ = '2019-01-21'
logger = logging.getLogger(__name__)
authLogger = logging.getLogger('authLog')
@ -319,9 +319,13 @@ def webLogout(request, exit_url=None):
Helper function to clear user related data from session. If this method is not used, the session we be cleaned anyway
by django in regular basis.
"""
# Invoke exit for authenticator
authenticator = request.user and request.user.manager.getInstance() or None
username = request.user.name
exit_url = authenticator.logout(username) or exit_url
if request.user is not None and request.user.id != ROOT_ID:
# Try yo invoke logout of auth
events.addEvent(request.user.manager, events.ET_LOGOUT, username=request.user.name, srcip=request.ip)
request.session.clear()
@ -331,7 +335,10 @@ def webLogout(request, exit_url=None):
exit_url = exit_url.replace('http://', 'https://')
# Try to delete session
return HttpResponseRedirect(request.build_absolute_uri(exit_url))
response = HttpResponseRedirect(request.build_absolute_uri(exit_url))
if authenticator:
authenticator.webLogoutHook(username, request, response)
return response
def authLogLogin(request, authenticator, userName, logStr=''):