summaryrefslogtreecommitdiffstats
path: root/pykolab/auth/ldap/cache.py
blob: 23b4e07e6af81b69605c9717bf0b6d3a01d4088d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# 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, either version 3 of the License, 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 General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

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

from uuid import UUID

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 = {}

##
## Classes
##

class Entry(object):
    def __init__(self, uniqueid, result_attr, last_change):
        self.uniqueid = uniqueid
        self.result_attribute = result_attr

        modifytimestamp_format = conf.get_raw(
            'ldap',
            'modifytimestamp_format',
            default="%Y%m%d%H%M%SZ"
        ).replace('%%', '%')

        self.last_change = datetime.datetime.strptime(
                last_change,
            modifytimestamp_format
        )

##
## Tables
##

entry_table = Table(
        'entry', metadata,
        Column('id', Integer, primary_key=True),
        Column('uniqueid', String(128), nullable=False),
        Column('result_attribute', String(128), nullable=False),
        Column('last_change', DateTime),
    )

##
## Table <-> Class Mappers
##

mapper(Entry, entry_table)

##
## Functions
##

def delete_entry(domain, entry):
    result_attribute = conf.get_raw('cyrus-sasl', 'result_attribute')

    db = init_db(domain)
    _entry = db.query(Entry).filter_by(uniqueid=entry['id']).first()

    if not _entry == None:
        db.delete(_entry)
        db.commit()

def get_entry(domain, entry, update=True):
    result_attribute = conf.get_raw('cyrus-sasl', 'result_attribute')

    _entry = None

    db = init_db(domain)

    try:
        _uniqueid = str(UUID(bytes_le=entry['id']))
        log.debug(_("Entry uniqueid was converted from binary form to string: %s") % _uniqueid, level=8)
    except ValueError:
        _uniqueid = entry['id']

    try:
        _entry = db.query(Entry).filter_by(uniqueid=_uniqueid).first()
    except sqlalchemy.exc.OperationalError, errmsg:
        db = init_db(domain,reinit=True)
    except sqlalchemy.exc.InvalidRequestError, errmsg:
        db = init_db(domain,reinit=True)
    finally:
        _entry = db.query(Entry).filter_by(uniqueid=_uniqueid).first()

    if not update:
        return _entry

    if _entry == None:
        log.debug(_("Inserting cache entry %r") % (_uniqueid), level=8)

        if not entry.has_key(result_attribute):
            entry[result_attribute] = ''

        db.add(
                Entry(
                        _uniqueid,
                        entry[result_attribute],
                        entry['modifytimestamp']
                    )
            )

        db.commit()
        _entry = db.query(Entry).filter_by(uniqueid=_uniqueid).first()
    else:
        modifytimestamp_format = conf.get_raw(
            'ldap',
            'modifytimestamp_format',
            default="%Y%m%d%H%M%SZ"
        ).replace('%%', '%')

        if not _entry.last_change.strftime(modifytimestamp_format) == entry['modifytimestamp']:
            log.debug(_("Updating timestamp for cache entry %r") % (_uniqueid), level=8)
            last_change = datetime.datetime.strptime(entry['modifytimestamp'], modifytimestamp_format)
            _entry.last_change = last_change
            db.commit()
            _entry = db.query(Entry).filter_by(uniqueid=_uniqueid).first()

        if entry.has_key(result_attribute):
            if not _entry.result_attribute == entry[result_attribute]:
                log.debug(_("Updating result_attribute for cache entry %r") % (_uniqueid), level=8)
                _entry.result_attribute = entry[result_attribute]
                db.commit()
                _entry = db.query(Entry).filter_by(uniqueid=_uniqueid).first()

    return _entry

def init_db(domain,reinit=False):
    """
        Returns a SQLAlchemy Session() instance.
    """
    global db

    if domain in db and not reinit:
        return db[domain]

    if reinit:
        import os
        if os.path.isfile('sqlite:///%s/%s.db' % (KOLAB_LIB_PATH, domain)):
            os.unlink('sqlite:///%s/%s.db' % (KOLAB_LIB_PATH, domain))

    db_uri = 'sqlite:///%s/%s.db' % (KOLAB_LIB_PATH, domain)
    echo = conf.debuglevel > 8

    try:
        engine = create_engine(db_uri, echo=echo)
        metadata.create_all(engine)
    except:
        engine = create_engine('sqlite://')
        metadata.create_all(engine)

    Session = sessionmaker(bind=engine)
    db[domain] = Session()

    return db[domain]

def last_modify_timestamp(domain):
    modifytimestamp_format = conf.get_raw(
        'ldap',
        'modifytimestamp_format',
        "%Y%m%d%H%M%SZ"
    ).replace('%%', '%')

    try:
        db = init_db(domain)
        last_change = db.query(Entry).order_by(desc(Entry.last_change)).first()

        if not last_change == None:
            return last_change.last_change.strftime(modifytimestamp_format)
        else:
            return datetime.datetime(1900, 01, 01, 00, 00, 00).strftime(modifytimestamp_format)
    except:
        return datetime.datetime(1900, 01, 01, 00, 00, 00).strftime(modifytimestamp_format)