diff --git a/ldapcherry/__init__.py b/ldapcherry/__init__.py index e8cd9c2..409281b 100644 --- a/ldapcherry/__init__.py +++ b/ldapcherry/__init__.py @@ -43,7 +43,7 @@ ): if traceback and msg == '': - msg = 'python exception' + msg = 'Python Exception:' if context == '': cherrypy.log.error_log.log(severity, msg) else: @@ -51,8 +51,8 @@ severity, ' '.join((context, msg)) ) - import traceback if traceback: + import traceback try: exc = sys.exc_info() if exc == (None, None, None): @@ -65,6 +65,28 @@ del exc +def exception_decorator(func): + def ret(self, *args, **kwargs): + try: + return func(self, *args, **kwargs) + except cherrypy.HTTPRedirect as e: + raise e + except cherrypy.HTTPError as e: + raise e + except Exception as e: + self._handle_exception(e) + username = self._check_session() + if not username: + return self.temp_service_unavailable.render() + is_admin = self._check_admin() + return self.temp_error.render( + is_admin=is_admin, + alert='danger', + message="An error occured, please check logs for details" + ) + return ret + + class LdapCherry(object): def _handle_exception(self, e): @@ -75,7 +97,7 @@ ) else: cherrypy.log.error( - msg="unkwon exception: '%(e)s'" % {'e': str(e)}, + msg="uncatched exception: [%(e)s]" % {'e': str(e)}, severity=logging.ERROR ) # log the traceback as 'debug' @@ -407,6 +429,8 @@ self.temp_lookup.get_template('selfmodify.tmpl') self.temp_modify = \ self.temp_lookup.get_template('modify.tmpl') + self.temp_service_unavailable = \ + self.temp_lookup.get_template('service_unavailable.tmpl') def reload(self, config=None): """ load/reload configuration @@ -543,6 +567,11 @@ return True return cherrypy.session['isadmin'] + def _check_session(self): + if self.auth_mode == 'none': + return 'anonymous' + username = cherrypy.session.get(SESSION_KEY) + def _check_auth(self, must_admin): """ check if a user is autheticated and, optionnaly an administrator if user not authentifaced -> redirection to login page (with base64 @@ -554,7 +583,7 @@ """ if self.auth_mode == 'none': return 'anonymous' - username = cherrypy.session.get(SESSION_KEY) + username = self._check_session() if cherrypy.request.query_string == '': qs = '' @@ -812,12 +841,14 @@ return self.ppolicy.check(password) @cherrypy.expose + @exception_decorator def signin(self, url=None): """simple signin page """ return self.temp_login.render(url=url) @cherrypy.expose + @exception_decorator def login(self, login, password, url=None): """login page """ @@ -861,6 +892,7 @@ raise cherrypy.HTTPRedirect("/signin" + qs) @cherrypy.expose + @exception_decorator def logout(self): """ logout page """ @@ -877,6 +909,7 @@ raise cherrypy.HTTPRedirect("/signin") @cherrypy.expose + @exception_decorator def index(self): """main page rendering """ @@ -885,6 +918,7 @@ return self.temp_index.render(is_admin=is_admin) @cherrypy.expose + @exception_decorator def searchuser(self, searchstring=None): """ search user page """ self._check_auth(must_admin=False) @@ -901,6 +935,7 @@ ) @cherrypy.expose + @exception_decorator def checkppolicy(self, **params): """ search user page """ keys = params.keys() @@ -918,6 +953,7 @@ return json.dumps(ret, separators=(',', ':')) @cherrypy.expose + @exception_decorator def searchadmin(self, searchstring=None): """ search user page """ self._check_auth(must_admin=True) @@ -934,6 +970,7 @@ ) @cherrypy.expose + @exception_decorator def adduser(self, **params): """ add user page """ self._check_auth(must_admin=True) @@ -979,6 +1016,7 @@ ) @cherrypy.expose + @exception_decorator def delete(self, user): """ remove user page """ self._check_auth(must_admin=True) @@ -988,6 +1026,7 @@ raise cherrypy.HTTPRedirect(referer) @cherrypy.expose + @exception_decorator def modify(self, user=None, **params): """ modify user page """ self._check_auth(must_admin=True) @@ -1042,6 +1081,7 @@ ) @cherrypy.expose + @exception_decorator def selfmodify(self, **params): """ self modify user page """ self._check_auth(must_admin=False) diff --git a/ldapcherry/exceptions.py b/ldapcherry/exceptions.py index d91d0fc..c249d76 100644 --- a/ldapcherry/exceptions.py +++ b/ldapcherry/exceptions.py @@ -184,3 +184,23 @@ self.attr = attr self.log = \ "attribute '" + attr + "' is not defined in configuration" + + +class UserDoesntExist(Exception): + def __init__(self, user, backend): + self.user = user + self.bakend = backend + self.log = \ + "user '" + user + "'" \ + " does not exist" \ + " in backend '" + backend + "'" + + +class GroupDoesntExist(Exception): + def __init__(self, group, backend): + self.group = group + self.bakend = backend + self.log = \ + "group '" + group + "'" \ + " does not exist" \ + " in backend '" + backend + "'" diff --git a/resources/templates/service_unavailable.tmpl b/resources/templates/service_unavailable.tmpl new file mode 100644 index 0000000..7d706d2 --- /dev/null +++ b/resources/templates/service_unavailable.tmpl @@ -0,0 +1,16 @@ +## -*- coding: utf-8 -*- +<%inherit file="base.tmpl"/> +<%block name="core"> +