Browse Source

Example plugins that uses UserContext

Yann Weber 9 years ago
parent
commit
97f1256615
2 changed files with 39 additions and 0 deletions
  1. 18
    0
      plugins/acl.py
  2. 21
    0
      plugins/dummy_auth.py

+ 18
- 0
plugins/acl.py View File

@@ -0,0 +1,18 @@
1
+#-*- conding: utf-8 -*-
2
+
3
+from Lodel.hooks import LodelHook
4
+from Lodel.user import UserContext
5
+
6
+class PermissionDenied(Exception): pass
7
+
8
+@LodelHook('leapi_get_pre')
9
+def check_anon(hook_name, caller, payload):
10
+    if not UserContext.identity().is_identified:
11
+        raise PermissionDenied("Anonymous user's are not allowed to get content")
12
+
13
+@LodelHook('leapi_update_pre')
14
+@LodelHook('leapi_delete_pre')
15
+@LodelHook('leapi_insert_pre')
16
+def check_auth(hook_name, caller, payload):
17
+    if not UserContext.identity().is_authenticated:
18
+        raise PermissionDenied("Only authenticated user's are allowed to do that !")

+ 21
- 0
plugins/dummy_auth.py View File

@@ -0,0 +1,21 @@
1
+#-*- coding: utf-8 -*-
2
+
3
+from Lodel.user import authentication_method, identification_method, UserIdentity
4
+
5
+@authentication_method
6
+def dummy_auth(identifier, proof):
7
+    print("Trying to authenticate user %s" % identifier)
8
+    if identifier == proof:
9
+        print("%s authenticated" % identifier)
10
+        return UserIdentity(identifier, identifier, "User %s" % identifier, authenticated = True)
11
+    return False
12
+
13
+@identification_method
14
+def dummy_identify(client_infos):
15
+    print("Trying to identify client with %s" % client_infos)
16
+    if 'ip' in client_infos:
17
+        ip = client_infos['ip']
18
+        if ip in ['localhost', '127.0.0.1', 'local']:
19
+            return UserIdentity(0, 'localuser', 'local user', identifier = True)
20
+    return False
21
+

Loading…
Cancel
Save