summaryrefslogtreecommitdiff
path: root/src/meetingtools/ac
diff options
context:
space:
mode:
authorLeif Johansson <leifj@sunet.se>2011-05-02 14:43:23 +0200
committerLeif Johansson <leifj@sunet.se>2011-05-02 14:43:23 +0200
commit1c3423d452d0ca1e20dff36ce05c2e35d2894f29 (patch)
treedd52ff1e96b185f62bceef6e57f96d7e9bf6c9fe /src/meetingtools/ac
parentf936e1317c3ba08ab94a39ea99126c9d61337718 (diff)
new acl - first version
Diffstat (limited to 'src/meetingtools/ac')
-rw-r--r--src/meetingtools/ac/__init__.py40
-rw-r--r--src/meetingtools/ac/api.py41
2 files changed, 72 insertions, 9 deletions
diff --git a/src/meetingtools/ac/__init__.py b/src/meetingtools/ac/__init__.py
index 1250a5f..4fdc2f5 100644
--- a/src/meetingtools/ac/__init__.py
+++ b/src/meetingtools/ac/__init__.py
@@ -1,12 +1,46 @@
from meetingtools.ac.api import ACPClient
+import time
-def ac_api_client_cached(request,acc):
+def ac_api_client_cache(request,acc):
tag = 'ac_api_client_%s' % acc.name
if not request.session.has_key(tag):
- request.session[tag] = ACPClient(acc.api_url,acc.user,acc.password)
+ request.session[tag] = ACPClientWrapper(acc)
return request.session[tag]
-def ac_api_client(request,acc):
+def ac_api_client_nocache(request,acc):
+ return ACPClientWrapper(acc)
+
+ac_api_client = ac_api_client_nocache
+
+def ac_api(request,acc):
return ACPClient(acc.api_url,acc.user,acc.password)
+
+
+MAXCALLS = 10
+MAXIDLE = 10
+
+class ACPClientWrapper(object):
+
+ def __init__(self,acc):
+ self.acc = acc
+ self._delegate = None
+ self.ncalls = 0
+ self.lastcall = time.time()
+
+ def invalidate(self):
+ self._delegate = None
+
+ def client_factory(self):
+ now = time.time()
+ if self.ncalls > MAXCALLS or now - self.lastcall > MAXIDLE or not self._delegate:
+ self._delegate = ACPClient(self.acc.api_url,self.acc.user,self.acc.password)
+ self.ncalls = 0
+ self.ncalls += 1
+ self.lastcall = now
+ return self._delegate
+
+ def __getattr__(self,name):
+ client = self.client_factory()
+ return getattr(client,name)
\ No newline at end of file
diff --git a/src/meetingtools/ac/api.py b/src/meetingtools/ac/api.py
index c710298..5db5f2f 100644
--- a/src/meetingtools/ac/api.py
+++ b/src/meetingtools/ac/api.py
@@ -52,6 +52,15 @@ def _enc(v):
ev = ev.encode('iso-8859-1')
return ev
+def _getset(dict,key,value=None):
+ if value:
+ if dict.has_key(key):
+ return dict[key]
+ else:
+ return None
+ else:
+ dict[key] = value
+
class ACPClient():
def __init__(self,url,username=None,password=None):
@@ -59,6 +68,7 @@ class ACPClient():
self.session = None
if username and password:
self.login(username,password)
+ self._cache = {'login':{},'group':{}}
def request(self,method,p={},raise_error=False):
url = self.url+"?"+"action=%s" % method
@@ -68,7 +78,7 @@ class ACPClient():
u = []
for (k,v) in p.items():
if v:
- kv = "%s=%s" % (k,quote(str(v)))
+ kv = "%s=%s" % (k,quote(unicode(v)))
u.append(kv)
url = url + "&" + "&".join(u)
@@ -100,18 +110,34 @@ class ACPClient():
raise result.exception()
def find_or_create_principal(self,key,value,type,dict):
+ if not self._cache.has_key(type):
+ self._cache[type] = {}
+ cache = self._cache[type]
+
+ if not cache.has_key(key):
+ p = self._find_or_create_principal(key,value,type,dict)
+ cache[key] = p
+
+ return cache[key]
+
+ def find_principal(self,key,value,type):
+ return self.find_or_create_principal(key,value,type,None)
+
+ def _find_or_create_principal(self,key,value,type,dict):
result = self.request('principal-list',{'filter-%s' % key: value,'filter-type': type}, True)
principal = result.get_principal()
if result.is_error():
if result.status_code() != 'no_data':
result.exception()
- elif principal:
+ elif principal and dict:
dict['principal-id'] = principal.get('principal-id')
- update_result = self.request('principal-update',dict)
- rp = update_result.get_principal()
- if not rp:
- rp = principal
+ rp = principal
+ if dict:
+ update_result = self.request('principal-update',dict)
+ rp = update_result.get_principal()
+ if not rp:
+ rp = principal
return rp
def find_builtin(self,type):
@@ -122,6 +148,9 @@ class ACPClient():
result = self.request('principal-list',{'filter-name':name,'filter-type':'group'},True)
return result.get_principal()
+ def find_user(self,login):
+ return self.find_principal("login", login, "user")
+
def add_remove_member(self,principal_id,group_id,is_member):
m = "0"
if is_member: