136 lines
4.7 KiB
Python
136 lines
4.7 KiB
Python
import os
|
|
import twill
|
|
|
|
from quixote import get_session, get_session_manager, get_request, get_publisher
|
|
from qommon.storage import fix_key
|
|
|
|
import authentic.identities
|
|
|
|
import collectivity
|
|
|
|
|
|
|
|
class MergedIdentityStore(authentic.identities.IdentitiesStoreStorage):
|
|
def get_identity_for_name_identifier(self, name_identifier):
|
|
x = authentic.identities.IdentitiesStoreStorage.get_identity_for_name_identifier(
|
|
self, name_identifier)
|
|
if x:
|
|
return x
|
|
|
|
for coll in collectivity.Collectivity.select():
|
|
store = get_collectivity_store(coll.id)
|
|
x = store.get_identity_for_name_identifier(name_identifier)
|
|
if x:
|
|
return x
|
|
|
|
return None
|
|
|
|
def get_identity_by_resource_id(self, resource_id):
|
|
for identity in self.values():
|
|
if identity.resource_id == resource_id:
|
|
return identity
|
|
|
|
for coll in collectivity.Collectivity.select():
|
|
store = get_collectivity_store(coll.id)
|
|
identity = store.get_identity_by_resource_id(resource_id)
|
|
if identity:
|
|
return identity
|
|
|
|
return None
|
|
|
|
|
|
|
|
class InheritingIdentityStore(authentic.identities.IdentitiesStoreStorage):
|
|
def get_identity_for_account(self, account):
|
|
t = authentic.identities.IdentitiesStoreStorage.get_identity_for_account(self, account)
|
|
if not t:
|
|
store = get_collectivity_store(None)
|
|
t = store.get_identity_for_account(account)
|
|
if t:
|
|
get_session().force_base_store = True
|
|
load_store()
|
|
return t
|
|
|
|
def get_identity_by_resource_id(self, resource_id):
|
|
for identity in self.values():
|
|
if identity.resource_id == resource_id:
|
|
return identity
|
|
return None
|
|
|
|
def get_filename(cls, filename, ignore_errors = False):
|
|
t = cls.get_filename(cls, filename, ignore_errors = ignore_errors)
|
|
if t:
|
|
t.__class__ = self.identity_class
|
|
return t
|
|
|
|
def add(self, identity):
|
|
# make sure identity class is the correct one, and get a new id
|
|
# appropriate for that class
|
|
identity.__class__ = self.identity_class
|
|
identity.id = identity.get_new_id()
|
|
authentic.identities.IdentitiesStoreStorage.add(self, identity)
|
|
|
|
|
|
|
|
class ParthenayIdentityStore(InheritingIdentityStore):
|
|
def get_identity_for_account(self, account):
|
|
t = None
|
|
if not self.identity_class.has_key('_' + account.username):
|
|
t = InheritingIdentityStore.get_identity_for_account(self, account)
|
|
if t:
|
|
return t
|
|
|
|
account.username = account.username.lower()
|
|
|
|
# some twill magic
|
|
twill.commands.reset_browser()
|
|
twill.commands.go(
|
|
"http://web.cc-parthenay.fr/_layouts/login.aspx?ReturnUrl=%2f_layouts%2fAuthenticate.aspx%3fSource%3d%252fPages%252f&Source=%2fPages%2f")
|
|
twill.commands.fv('aspnetForm', '2', account.username)
|
|
twill.commands.fv('aspnetForm', '3', account.password)
|
|
twill.commands.submit()
|
|
if twill.commands.browser.get_url() == 'http://web.cc-parthenay.fr/Pages/':
|
|
# authent ok
|
|
try:
|
|
t = self.identity_class.get('_' + account.username)
|
|
except KeyError:
|
|
t = self.identity_class(id = '_' + account.username)
|
|
t.store()
|
|
|
|
return t
|
|
|
|
|
|
def get_collectivity_identity_class(collectivity_id):
|
|
class CollectivityIdentity(authentic.identities.Identity):
|
|
_names = 'identities-' + str(collectivity_id)
|
|
def store(self):
|
|
objects_dir = self.get_objects_dir()
|
|
if not os.path.exists(objects_dir):
|
|
os.mkdir(objects_dir)
|
|
self._filename = os.path.join(self._names, fix_key(self.id))
|
|
old_class = self.__class__
|
|
self.__class__ = authentic.identities.Identity
|
|
authentic.identities.Identity.store(self)
|
|
self.__class__ = old_class
|
|
return CollectivityIdentity
|
|
|
|
|
|
def get_collectivity_store(collectivity_id):
|
|
if collectivity_id == '547263': # Parthenay
|
|
store = ParthenayIdentityStore(
|
|
identity_class = get_collectivity_identity_class(collectivity_id))
|
|
elif collectivity_id:
|
|
# Vandoeuvre (546555) and others
|
|
store = InheritingIdentityStore(
|
|
identity_class = get_collectivity_identity_class(collectivity_id))
|
|
else:
|
|
store = MergedIdentityStore()
|
|
return store
|
|
|
|
|
|
def load_store():
|
|
coll_id = get_request().get_header('X-Gdd-Account-Number')
|
|
if get_session().force_base_store:
|
|
coll_id = None
|
|
get_publisher().store = get_collectivity_store(coll_id)
|