summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2013-08-22 14:24:31 (GMT)
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2013-08-22 14:24:31 (GMT)
commit7efeedbf1b5ac25fdc897cf5d931cb043da7c173 (patch)
tree8a935c9b1cc81b569a4a46d1b8b5b91a927136ee
parent2acc8266336a0bdbe9844e0209d5c779ad6f6113 (diff)
downloadpykolab-7efeedbf1b5ac25fdc897cf5d931cb043da7c173.tar.gz
Add an authentication cache
-rw-r--r--pykolab/auth/ldap/auth_cache.py138
1 files changed, 138 insertions, 0 deletions
diff --git a/pykolab/auth/ldap/auth_cache.py b/pykolab/auth/ldap/auth_cache.py
new file mode 100644
index 0000000..12f362c
--- /dev/null
+++ b/pykolab/auth/ldap/auth_cache.py
@@ -0,0 +1,138 @@
+# Copyright 2010-2013 Kolab Systems AG (http://www.kolabsys.com)
+#
+# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 3 or, at your option, any later version
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Library General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+
+import datetime
+
+import sqlalchemy
+
+from sqlalchemy import Column
+from sqlalchemy import DateTime
+from sqlalchemy import Integer
+from sqlalchemy import MetaData
+from sqlalchemy import String
+from sqlalchemy import Table
+
+from sqlalchemy import desc
+from sqlalchemy import create_engine
+from sqlalchemy.orm import mapper
+
+try:
+ from sqlalchemy.orm import relationship
+except:
+ from sqlalchemy.orm import relation as relationship
+
+try:
+ from sqlalchemy.orm import sessionmaker
+except:
+ from sqlalchemy.orm import create_session
+
+import pykolab
+
+from pykolab import utils
+from pykolab.constants import KOLAB_LIB_PATH
+from pykolab.translate import _
+
+conf = pykolab.getConf()
+log = pykolab.getLogger('pykolab.auth_cache')
+
+metadata = MetaData()
+
+db = None
+
+##
+## Classes
+##
+
+class Entry(object):
+ def __init__(self, key, value):
+ self.key = key
+ self.value = value
+
+##
+## Tables
+##
+
+entry_table = Table(
+ 'entries', metadata,
+ Column('id', Integer, primary_key=True),
+ Column('domain', String(128), index=True, nullable=True),
+ Column('key', String(128), index=True, nullable=False),
+ Column('value', String(128), nullable=False),
+ Column('last_change', DateTime, nullable=False, default=datetime.datetime.now())
+ )
+
+##
+## Table <-> Class Mappers
+##
+
+mapper(Entry, entry_table)
+
+##
+## Functions
+##
+
+def get_entry(key):
+ db = init_db()
+ _entries = db.query(Entry).filter_by(key=key).all()
+
+ if len(_entries) == 0:
+ return None
+ if len(_entries) > 1:
+ return None
+
+ log.debug("Entry found: %r" % (_entries[0].__dict__))
+ log.debug("Returning: %r" % (_entries[0].value))
+
+ return _entries[0].value
+
+def set_entry(key, value):
+ db = init_db()
+ _entries = db.query(Entry).filter_by(key=key).all()
+
+ if len(_entries) == 0:
+ db.add(
+ Entry(
+ key,
+ value
+ )
+ )
+
+ db.commit()
+
+#def purge_entries():
+ #db = init_db()
+ #db.query(Entry).filter(Entry.last_change <= datetime.datetime.now()).delete()
+
+def init_db():
+ """
+ Returns a SQLAlchemy Session() instance.
+ """
+ global db
+
+ if not db == None:
+ return db
+
+ db_uri = 'sqlite:///%s/auth_cache.db' % (KOLAB_LIB_PATH)
+ echo = conf.debuglevel > 8
+ engine = create_engine(db_uri, echo=echo)
+ metadata.create_all(engine)
+
+ Session = sessionmaker(bind=engine)
+ db = Session()
+
+ return db