Ajout du decorateur has_group
authorCyril Robert <Cyril Robert cyrilrbt@gmail.com>
Wed, 28 Jul 2010 13:02:25 +0000 (09:02 -0400)
committerCyril Robert <Cyril Robert cyrilrbt@gmail.com>
Wed, 28 Jul 2010 13:02:25 +0000 (09:02 -0400)
auf_references_client/decorators.py [new file with mode: 0644]
auf_references_client/settings.py
auf_references_client/templates/references_403.html [new file with mode: 0644]

diff --git a/auf_references_client/decorators.py b/auf_references_client/decorators.py
new file mode 100644 (file)
index 0000000..d3ef466
--- /dev/null
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+
+from django.http import HttpResponseForbidden
+from django.contrib.auth import logout
+from django.template.loader import get_template
+from django.template import Context
+
+def has_group(group = ""):
+    def _dec(view_func):
+        def _view(request, *args, **kwargs):
+            allowed = False
+            if request.user.is_authenticated():
+                try:
+                    p = personne(request.user)
+                    g = Groupe.objects.get(nom = group)
+                    if g in p.groupes:
+                        allowed = True
+                except: pass
+            else:
+                allowed = True
+    
+            if allowed:
+                r = view_func(request, *args, **kwargs)
+            else:
+                logout(request)
+                t = get_template('references_403.html')
+                html = t.render(Context({}))
+                r = HttpResponseForbidden(html)
+
+            return r
+
+        _view.__name__ = view_func.__name__
+        _view.__dict__ = view_func.__dict__
+        _view.__doc__ = view_func.__doc__
+
+        return _view
+    return _dec
index 61d2272..83baeb7 100644 (file)
@@ -1,4 +1,10 @@
+import os
+
 # Django settings for app project.
+REFERENCES_TEMPLATE_DIRS = [
+    os.path.join(os.path.dirname(__file__), "templates"),
+]
+
 ROA_MODELS = True   # set to False if you'd like to develop/test locally
 
 ROA_FORMAT = 'django'
diff --git a/auf_references_client/templates/references_403.html b/auf_references_client/templates/references_403.html
new file mode 100644 (file)
index 0000000..e50eda4
--- /dev/null
@@ -0,0 +1,10 @@
+<html>
+  <head>
+    <title>Accès refusé</title>
+  </head>
+  <body>
+    <h1>Accès refusé</h1>
+    <h2>Vous n'avez pas le droit d'accéder à cette application</h2>
+    <a href="javascript:history.back();">Retour</a>
+  </body>
+</html>