2008-01-16 19:47:20 +01:00
|
|
|
/* SOGoCache.m - this file is part of SOGo
|
|
|
|
*
|
2013-06-11 15:41:17 +02:00
|
|
|
* Copyright (C) 2008-2013 Inverse inc.
|
2008-01-16 19:47:20 +01:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
2008-11-11 01:17:35 +01:00
|
|
|
* Ludovic Marcotte <lmarcotte@inverse.ca>
|
2008-01-16 19:47:20 +01:00
|
|
|
*
|
|
|
|
* This file 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 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file 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; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2009-02-24 02:54:59 +01:00
|
|
|
/*
|
2009-08-03 17:46:02 +02:00
|
|
|
* [ Cache Structure ]
|
2009-02-24 02:54:59 +01:00
|
|
|
*
|
2013-06-11 17:21:58 +02:00
|
|
|
* users value = instances of SOGoUser > flushed after the completion of every SOGo requests
|
|
|
|
* groups value = instances of SOGoGroup > flushed after the completion of every SOGo requests
|
|
|
|
* imap4Connections value =
|
|
|
|
* localCache value = any value of what's in memcached - this is used to NOT query memcached within the same sogod instance
|
2010-10-15 20:30:08 +02:00
|
|
|
*
|
|
|
|
* [ Distributed (using memcached) cache structure ]
|
|
|
|
*
|
2013-06-11 17:21:58 +02:00
|
|
|
* <uid>+defaults value = NSDictionary instance > user's defaults
|
|
|
|
* <uid>+settings value = NSDictionary instance > user's settings
|
|
|
|
* <uid>+attributes value = NSMutableDictionary instance > user's LDAP attributes
|
|
|
|
* <object path>+acl value = NSDictionary instance > ACLs on an object at specified path
|
|
|
|
* <groupname>+<domain> value = NSString instance (array components separated by ",") or group member logins for a specific group in domain
|
|
|
|
* cas-id:< > value =
|
|
|
|
* cas-ticket:< > value =
|
|
|
|
* cas-pgtiou:< > value =
|
|
|
|
* session:< > value =
|
|
|
|
* <uid>+failedlogins value = NSDictionary instance holding the failed count and the date of the first failed authentication
|
|
|
|
* <uid>+messagesubmissions value = NSDictionary instance holding the number of messages sent, and number of recipients
|
2009-02-24 02:54:59 +01:00
|
|
|
*/
|
|
|
|
|
2010-12-28 18:42:50 +01:00
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
#import <Foundation/NSArray.h>
|
2009-11-19 18:08:47 +01:00
|
|
|
#import <Foundation/NSData.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2008-12-12 19:55:12 +01:00
|
|
|
#import <Foundation/NSLock.h>
|
2013-06-11 15:41:17 +02:00
|
|
|
#import <Foundation/NSValue.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSString.h>
|
2008-11-11 01:17:35 +01:00
|
|
|
#import <Foundation/NSTimer.h>
|
2008-01-16 19:47:20 +01:00
|
|
|
|
|
|
|
#import <NGObjWeb/SoObject.h>
|
2008-11-11 01:17:35 +01:00
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2008-01-16 19:47:20 +01:00
|
|
|
|
2010-11-02 14:16:05 +01:00
|
|
|
#import "NSDictionary+Utilities.h"
|
2010-10-26 20:20:46 +02:00
|
|
|
#import "NSString+Utilities.h"
|
2008-07-29 18:36:16 +02:00
|
|
|
#import "SOGoObject.h"
|
2009-11-29 05:19:32 +01:00
|
|
|
#import "SOGoSystemDefaults.h"
|
2008-01-16 19:47:20 +01:00
|
|
|
#import "SOGoUser.h"
|
2009-11-29 05:19:32 +01:00
|
|
|
#import "SOGoUserProfile.h"
|
2008-07-29 18:36:16 +02:00
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
#import "SOGoCache.h"
|
|
|
|
|
2010-02-18 20:14:37 +01:00
|
|
|
/* Those used to be ivars but were converted to static globals to work around
|
|
|
|
a bug in GCC that prevent SOGo from using libmemcached > 0.37:
|
|
|
|
|
|
|
|
"SOGoCache.m:409: internal compiler error: in encode_gnu_bitfield, at
|
|
|
|
objc/objc-act.c:8218
|
|
|
|
Please submit a full bug report,
|
|
|
|
with preprocessed source if appropriate.
|
|
|
|
See <file:///usr/share/doc/gcc-4.4/README.Bugs> for instructions." */
|
|
|
|
|
|
|
|
static memcached_server_st *servers = NULL;
|
|
|
|
static memcached_st *handle = NULL;
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
@implementation SOGoCache
|
|
|
|
|
|
|
|
+ (SOGoCache *) sharedCache
|
|
|
|
{
|
2009-11-16 16:02:14 +01:00
|
|
|
static SOGoCache *sharedCache = nil;
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
if (!sharedCache)
|
2009-11-19 18:08:47 +01:00
|
|
|
sharedCache = [self new];
|
2008-01-16 19:47:20 +01:00
|
|
|
|
|
|
|
return sharedCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
SOGoSystemDefaults *sd;
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2010-09-24 17:24:24 +02:00
|
|
|
cache = [[NSMutableDictionary alloc] init];
|
2011-07-15 22:30:39 +02:00
|
|
|
requestsCacheEnabled = YES;
|
2010-09-24 17:24:24 +02:00
|
|
|
users = [[NSMutableDictionary alloc] init];
|
|
|
|
groups = [[NSMutableDictionary alloc] init];
|
2010-10-13 15:21:16 +02:00
|
|
|
imap4Connections = [[NSMutableDictionary alloc] init];
|
2009-08-03 17:46:02 +02:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
// localCache is used to avoid going all the time to the memcached
|
|
|
|
// server during each request. We'll cache the value we got from
|
|
|
|
// memcached for the duration of the current request - which is good
|
|
|
|
// enough for pretty much all cases. We surely don't want to get new
|
|
|
|
// defaults/settings during the _same_ requests, it could produce
|
|
|
|
// relatively strange behaviors
|
|
|
|
localCache = [NSMutableDictionary new];
|
2009-08-03 17:46:02 +02:00
|
|
|
|
2010-02-18 20:14:37 +01:00
|
|
|
if (!handle)
|
|
|
|
{
|
|
|
|
handle = memcached_create(NULL);
|
|
|
|
if (handle)
|
|
|
|
{
|
2014-02-03 15:53:47 +01:00
|
|
|
memcached_behavior_set(handle, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
|
|
|
|
|
2010-02-18 20:14:37 +01:00
|
|
|
sd = [SOGoSystemDefaults sharedSystemDefaults];
|
2010-03-24 16:22:59 +01:00
|
|
|
|
|
|
|
// We define the default value for cleaning up cached users'
|
2010-02-18 20:14:37 +01:00
|
|
|
// preferences. This value should be relatively high to avoid
|
|
|
|
// useless database calls.
|
|
|
|
cleanupInterval = [sd cacheCleanupInterval];
|
2010-03-24 16:22:59 +01:00
|
|
|
ASSIGN(memcachedServerName, [sd memcachedHost]);
|
2010-02-18 20:14:37 +01:00
|
|
|
|
|
|
|
[self logWithFormat: @"Cache cleanup interval set every %f seconds",
|
|
|
|
cleanupInterval];
|
2010-03-24 16:22:59 +01:00
|
|
|
[self logWithFormat: @"Using host(s) '%@' as server(s)",
|
|
|
|
memcachedServerName];
|
2010-02-18 20:14:37 +01:00
|
|
|
if (!servers)
|
|
|
|
servers
|
2010-03-24 16:22:59 +01:00
|
|
|
= memcached_servers_parse([memcachedServerName UTF8String]);
|
|
|
|
|
2011-12-30 13:20:57 +01:00
|
|
|
if ([memcachedServerName hasPrefix:@"/"])
|
|
|
|
memcached_server_add_unix_socket(handle, [memcachedServerName UTF8String]);
|
|
|
|
else
|
|
|
|
memcached_server_push(handle, servers);
|
2010-02-18 20:14:37 +01:00
|
|
|
}
|
|
|
|
}
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2010-02-18 20:14:37 +01:00
|
|
|
/* Commented out because of a bug in GCC:
|
|
|
|
memcached_server_free (servers);
|
|
|
|
memcached_free (handle); */
|
2009-11-29 05:19:32 +01:00
|
|
|
[memcachedServerName release];
|
2008-01-16 19:47:20 +01:00
|
|
|
[cache release];
|
|
|
|
[users release];
|
2010-09-24 17:24:24 +02:00
|
|
|
[groups release];
|
2010-10-13 15:21:16 +02:00
|
|
|
[imap4Connections release];
|
2009-10-13 22:17:29 +02:00
|
|
|
[localCache release];
|
2008-01-16 19:47:20 +01:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2011-07-15 22:30:39 +02:00
|
|
|
- (void) disableRequestsCache
|
|
|
|
{
|
|
|
|
requestsCacheEnabled = NO;
|
|
|
|
}
|
|
|
|
|
2011-11-18 16:14:31 +01:00
|
|
|
- (void) disableLocalCache
|
|
|
|
{
|
|
|
|
[localCache release];
|
|
|
|
localCache = nil;
|
|
|
|
}
|
|
|
|
|
2011-07-15 22:30:39 +02:00
|
|
|
- (void) killCache
|
|
|
|
{
|
|
|
|
[cache removeAllObjects];
|
|
|
|
[imap4Connections removeAllObjects];
|
|
|
|
|
|
|
|
// This is essential for refetching the cached values in case something has changed
|
|
|
|
// accross various sogod processes
|
|
|
|
[users removeAllObjects];
|
|
|
|
[groups removeAllObjects];
|
|
|
|
[localCache removeAllObjects];
|
|
|
|
}
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
- (NSString *) _pathFromObject: (SOGoObject *) container
|
2009-02-24 02:54:59 +01:00
|
|
|
withName: (NSString *) name
|
2008-01-16 19:47:20 +01:00
|
|
|
{
|
|
|
|
NSString *fullPath, *nameInContainer;
|
|
|
|
NSMutableArray *names;
|
|
|
|
id currentObject;
|
|
|
|
|
2009-06-04 20:56:06 +02:00
|
|
|
if ([name length])
|
2008-01-16 19:47:20 +01:00
|
|
|
{
|
2009-06-04 20:56:06 +02:00
|
|
|
names = [NSMutableArray array];
|
2008-01-16 19:47:20 +01:00
|
|
|
|
2009-06-04 20:56:06 +02:00
|
|
|
[names addObject: name];
|
|
|
|
currentObject = container;
|
|
|
|
while ((nameInContainer = [currentObject nameInContainer]))
|
|
|
|
{
|
|
|
|
[names addObject: nameInContainer];
|
|
|
|
currentObject = [currentObject container];
|
|
|
|
}
|
|
|
|
|
|
|
|
fullPath = [names componentsJoinedByString: @"/"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fullPath = nil;
|
2008-01-16 19:47:20 +01:00
|
|
|
|
|
|
|
return fullPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) registerObject: (id) object
|
|
|
|
withName: (NSString *) name
|
|
|
|
inContainer: (SOGoObject *) container
|
|
|
|
{
|
|
|
|
NSString *fullPath;
|
|
|
|
|
2011-07-15 22:30:39 +02:00
|
|
|
if (requestsCacheEnabled && object && name)
|
2008-01-16 19:47:20 +01:00
|
|
|
{
|
2008-01-16 20:52:43 +01:00
|
|
|
[self registerObject: container
|
|
|
|
withName: [container nameInContainer]
|
|
|
|
inContainer: [container container]];
|
2008-01-16 19:47:20 +01:00
|
|
|
fullPath = [self _pathFromObject: container
|
|
|
|
withName: name];
|
2012-02-14 16:51:23 +01:00
|
|
|
if (fullPath && ![cache objectForKey: fullPath])
|
2008-01-16 20:52:43 +01:00
|
|
|
{
|
|
|
|
[cache setObject: object forKey: fullPath];
|
|
|
|
}
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 19:58:14 +01:00
|
|
|
- (void) unregisterObjectWithName: (NSString *) name
|
|
|
|
inContainer: (SOGoObject *) container
|
|
|
|
{
|
|
|
|
NSString *fullPath;
|
|
|
|
|
2011-07-15 22:30:39 +02:00
|
|
|
if (requestsCacheEnabled && name)
|
2011-03-07 19:58:14 +01:00
|
|
|
{
|
|
|
|
fullPath = [self _pathFromObject: container
|
|
|
|
withName: name];
|
|
|
|
[cache removeObjectForKey: fullPath];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
- (id) objectNamed: (NSString *) name
|
|
|
|
inContainer: (SOGoObject *) container
|
|
|
|
{
|
|
|
|
NSString *fullPath;
|
2011-07-15 22:30:39 +02:00
|
|
|
id cacheObject;
|
2008-01-16 19:47:20 +01:00
|
|
|
|
2011-07-15 22:30:39 +02:00
|
|
|
if (requestsCacheEnabled)
|
|
|
|
{
|
|
|
|
fullPath = [self _pathFromObject: container
|
|
|
|
withName: name];
|
|
|
|
cacheObject = [cache objectForKey: fullPath];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cacheObject = nil;
|
2008-01-16 19:47:20 +01:00
|
|
|
|
2011-07-15 22:30:39 +02:00
|
|
|
return cacheObject;
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) registerUser: (SOGoUser *) user
|
2009-09-11 16:38:43 +02:00
|
|
|
withName: (NSString *) userName
|
2008-11-11 02:52:56 +01:00
|
|
|
{
|
2009-09-11 16:38:43 +02:00
|
|
|
[users setObject: user forKey: userName];
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) userNamed: (NSString *) name
|
|
|
|
{
|
2009-08-03 17:46:02 +02:00
|
|
|
return [users objectForKey: name];
|
2008-11-12 04:30:13 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 17:24:24 +02:00
|
|
|
- (void) registerGroup: (SOGoGroup *) group
|
|
|
|
withName: (NSString *) groupName
|
|
|
|
inDomain: (NSString *) domainName
|
|
|
|
|
|
|
|
{
|
|
|
|
if (group)
|
|
|
|
[groups setObject: group forKey: [NSString stringWithFormat: @"%@+%@", groupName, domainName]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) groupNamed: (NSString *) groupName
|
|
|
|
inDomain: (NSString *) domainName
|
|
|
|
|
|
|
|
{
|
|
|
|
return [groups objectForKey: [NSString stringWithFormat: @"%@+%@", groupName, domainName]];
|
|
|
|
}
|
|
|
|
|
2010-10-13 15:21:16 +02:00
|
|
|
- (void) registerIMAP4Connection: (NGImap4Connection *) connection
|
|
|
|
forKey: (NSString *) key
|
|
|
|
{
|
|
|
|
if (connection)
|
|
|
|
[imap4Connections setObject: connection forKey: key];
|
|
|
|
else
|
|
|
|
[imap4Connections removeObjectForKey: key];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NGImap4Connection *) imap4ConnectionForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
return [imap4Connections objectForKey: key];
|
|
|
|
}
|
2010-09-24 17:24:24 +02:00
|
|
|
|
2009-03-03 20:08:02 +01:00
|
|
|
//
|
2009-08-03 17:46:02 +02:00
|
|
|
// For non-blocking cache method, see memcached_behavior_set and MEMCACHED_BEHAVIOR_NO_BLOCK
|
|
|
|
// memcached is thread-safe so no need to lock here.
|
2009-03-03 20:08:02 +01:00
|
|
|
//
|
2010-01-28 22:42:03 +01:00
|
|
|
- (void) setValue: (NSString *) value
|
|
|
|
forKey: (NSString *) key
|
|
|
|
expire: (float) expiration
|
2009-03-03 20:08:02 +01:00
|
|
|
{
|
2010-01-28 22:42:03 +01:00
|
|
|
NSData *keyData, *valueData;
|
2009-08-03 17:46:02 +02:00
|
|
|
memcached_return error;
|
2009-02-24 02:54:59 +01:00
|
|
|
|
2010-11-16 19:44:17 +01:00
|
|
|
// [self logWithFormat: @"setValue: '%@' forKey: '%@'", value, key];
|
2009-11-19 18:08:47 +01:00
|
|
|
if (handle)
|
|
|
|
{
|
2010-01-28 22:42:03 +01:00
|
|
|
keyData = [key dataUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
valueData = [value dataUsingEncoding: NSUTF8StringEncoding];
|
2010-10-15 20:30:08 +02:00
|
|
|
error = memcached_set(handle,
|
|
|
|
[keyData bytes], [keyData length],
|
|
|
|
[valueData bytes], [valueData length],
|
|
|
|
expiration, 0);
|
2009-11-19 18:08:47 +01:00
|
|
|
if (error != MEMCACHED_SUCCESS)
|
2010-01-28 22:42:03 +01:00
|
|
|
[self logWithFormat:
|
2010-06-01 16:00:36 +02:00
|
|
|
@"an error occurred when caching value for key '%@':"
|
|
|
|
@" \"%s\"", key, memcached_strerror(handle, error)];
|
2009-11-19 18:08:47 +01:00
|
|
|
//else
|
|
|
|
//[self logWithFormat: @"memcached: cached values (%s) with subtype %@
|
|
|
|
//for user %@", value, theType, theLogin];
|
|
|
|
}
|
|
|
|
else
|
2010-01-28 22:42:03 +01:00
|
|
|
[self errorWithFormat: (@"attempting to cache value for key '%@' while"
|
|
|
|
" no handle exists"), key];
|
2009-02-24 02:54:59 +01:00
|
|
|
}
|
|
|
|
|
2013-06-11 15:41:17 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2010-01-28 22:42:03 +01:00
|
|
|
- (void) setValue: (NSString *) value
|
|
|
|
forKey: (NSString *) key
|
2009-02-24 02:54:59 +01:00
|
|
|
{
|
2010-01-28 22:42:03 +01:00
|
|
|
[self setValue: value forKey: key
|
|
|
|
expire: cleanupInterval];
|
|
|
|
}
|
|
|
|
|
2013-06-11 15:41:17 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2010-01-28 22:42:03 +01:00
|
|
|
- (NSString *) valueForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
NSString *valueString;
|
|
|
|
NSData *keyData;
|
2009-11-19 18:08:47 +01:00
|
|
|
char *value;
|
|
|
|
size_t vlen;
|
|
|
|
memcached_return rc;
|
|
|
|
unsigned int flags;
|
2010-01-28 22:42:03 +01:00
|
|
|
|
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
keyData = [key dataUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
value = memcached_get (handle, [keyData bytes], [keyData length],
|
|
|
|
&vlen, &flags, &rc);
|
|
|
|
if (rc == MEMCACHED_SUCCESS && value)
|
|
|
|
{
|
|
|
|
valueString
|
|
|
|
= [[NSString alloc] initWithBytesNoCopy: value
|
|
|
|
length: vlen
|
|
|
|
encoding: NSUTF8StringEncoding
|
|
|
|
freeWhenDone: YES];
|
|
|
|
[valueString autorelease];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
valueString = nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
valueString = nil;
|
|
|
|
[self errorWithFormat: @"attempting to retrieved cached value for key"
|
|
|
|
@" '%@' while no handle exists", key];
|
|
|
|
}
|
|
|
|
|
2010-11-16 19:44:17 +01:00
|
|
|
// [self logWithFormat: @"valueForKey: '%@' -> '%@'", key, valueString];
|
|
|
|
|
2010-01-28 22:42:03 +01:00
|
|
|
return valueString;
|
|
|
|
}
|
|
|
|
|
2013-06-11 15:41:17 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2010-01-28 22:42:03 +01:00
|
|
|
- (void) removeValueForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
NSData *keyData;
|
|
|
|
memcached_return rc;
|
|
|
|
|
2010-10-15 20:30:08 +02:00
|
|
|
[localCache removeObjectForKey: key];
|
|
|
|
|
2010-01-28 22:42:03 +01:00
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
keyData = [key dataUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
rc = memcached_delete (handle, [keyData bytes], [keyData length],
|
|
|
|
0);
|
|
|
|
if (rc != MEMCACHED_SUCCESS)
|
|
|
|
[self errorWithFormat: (@"failure deleting cached value for key"
|
|
|
|
@" '%@'"),
|
|
|
|
key];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self errorWithFormat: (@"attempting to delete cached value for key"
|
|
|
|
@" '%@' while no handle exists"),
|
|
|
|
key];
|
|
|
|
}
|
|
|
|
|
2010-10-15 20:30:08 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// This method is used to cache a specific value (represented as a
|
|
|
|
// string, usually JSON stuff) using the specified key and type-suffix
|
|
|
|
//
|
|
|
|
// The final key will aways be of the form: <key>+<type suffix>
|
|
|
|
//
|
|
|
|
// This method will insert the value in the localCache and also
|
|
|
|
// in memcached.
|
|
|
|
//
|
2010-01-28 22:42:03 +01:00
|
|
|
- (void) _cacheValues: (NSString *) theAttributes
|
|
|
|
ofType: (NSString *) theType
|
2010-10-15 20:30:08 +02:00
|
|
|
forKey: (NSString *) theKey
|
2010-01-28 22:42:03 +01:00
|
|
|
{
|
|
|
|
NSString *keyName;
|
|
|
|
|
2010-10-15 20:30:08 +02:00
|
|
|
keyName = [NSString stringWithFormat: @"%@+%@", theKey, theType];
|
|
|
|
|
|
|
|
if (theAttributes)
|
|
|
|
{
|
|
|
|
[self setValue: theAttributes forKey: keyName];
|
|
|
|
[localCache setObject: theAttributes forKey: keyName];
|
|
|
|
}
|
2010-01-28 22:42:03 +01:00
|
|
|
}
|
|
|
|
|
2013-06-11 15:41:17 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2010-01-28 22:42:03 +01:00
|
|
|
- (NSString *) _valuesOfType: (NSString *) theType
|
2010-10-15 20:30:08 +02:00
|
|
|
forKey: (NSString *) theKey
|
2010-01-28 22:42:03 +01:00
|
|
|
{
|
|
|
|
NSString *valueString, *keyName;
|
2009-03-03 20:08:02 +01:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
valueString = nil;
|
2009-03-24 21:36:49 +01:00
|
|
|
|
2010-10-15 20:30:08 +02:00
|
|
|
keyName = [NSString stringWithFormat: @"%@+%@", theKey, theType];
|
2010-01-28 22:42:03 +01:00
|
|
|
valueString = [localCache objectForKey: keyName];
|
|
|
|
if (!valueString)
|
2008-12-02 02:36:53 +01:00
|
|
|
{
|
2010-01-28 22:42:03 +01:00
|
|
|
valueString = [self valueForKey: keyName];
|
|
|
|
if (valueString)
|
|
|
|
[localCache setObject: valueString forKey: keyName];
|
2009-02-24 02:54:59 +01:00
|
|
|
}
|
2008-11-20 20:17:59 +01:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
return valueString;
|
2008-11-11 01:17:35 +01:00
|
|
|
}
|
|
|
|
|
2013-06-11 15:41:17 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2009-11-19 18:08:47 +01:00
|
|
|
- (void) setUserAttributes: (NSString *) theAttributes
|
|
|
|
forLogin: (NSString *) login
|
2008-11-11 01:17:35 +01:00
|
|
|
{
|
2009-11-19 18:08:47 +01:00
|
|
|
[self _cacheValues: theAttributes ofType: @"attributes"
|
2010-10-15 20:30:08 +02:00
|
|
|
forKey: login];
|
2009-11-19 18:08:47 +01:00
|
|
|
}
|
2008-11-11 01:17:35 +01:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
- (NSString *) userAttributesForLogin: (NSString *) theLogin
|
|
|
|
{
|
2010-10-15 20:30:08 +02:00
|
|
|
return [self _valuesOfType: @"attributes" forKey: theLogin];
|
2009-11-19 18:08:47 +01:00
|
|
|
}
|
2008-12-12 19:55:12 +01:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
- (void) setUserDefaults: (NSString *) theAttributes
|
|
|
|
forLogin: (NSString *) login
|
|
|
|
{
|
|
|
|
[self _cacheValues: theAttributes ofType: @"defaults"
|
2010-10-15 20:30:08 +02:00
|
|
|
forKey: login];
|
2009-11-19 18:08:47 +01:00
|
|
|
}
|
2008-11-11 02:52:56 +01:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
- (NSString *) userDefaultsForLogin: (NSString *) theLogin
|
|
|
|
{
|
2010-10-15 20:30:08 +02:00
|
|
|
return [self _valuesOfType: @"defaults" forKey: theLogin];
|
2009-08-03 17:46:02 +02:00
|
|
|
}
|
2008-12-12 19:55:12 +01:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
- (void) setUserSettings: (NSString *) theAttributes
|
|
|
|
forLogin: (NSString *) login
|
2009-08-03 17:46:02 +02:00
|
|
|
{
|
2009-11-19 18:08:47 +01:00
|
|
|
[self _cacheValues: theAttributes ofType: @"settings"
|
2010-10-15 20:30:08 +02:00
|
|
|
forKey: login];
|
2009-08-03 17:46:02 +02:00
|
|
|
}
|
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
- (NSString *) userSettingsForLogin: (NSString *) theLogin
|
2009-08-03 17:46:02 +02:00
|
|
|
{
|
2010-10-15 20:30:08 +02:00
|
|
|
return [self _valuesOfType: @"settings" forKey: theLogin];
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
2013-06-11 15:41:17 +02:00
|
|
|
//
|
|
|
|
// SOGo password failed counts
|
|
|
|
//
|
|
|
|
- (void) setFailedCount: (int) theCount
|
|
|
|
forLogin: (NSString *) theLogin
|
|
|
|
{
|
|
|
|
NSMutableDictionary *d;
|
|
|
|
NSNumber *count;
|
|
|
|
|
|
|
|
if (theCount)
|
|
|
|
{
|
|
|
|
count = [NSNumber numberWithInt: theCount];
|
|
|
|
|
|
|
|
d = [NSMutableDictionary dictionaryWithDictionary: [self failedCountForLogin: theLogin]];
|
|
|
|
|
|
|
|
if (![d objectForKey: @"InitialDate"])
|
|
|
|
{
|
|
|
|
[d setObject: [NSNumber numberWithUnsignedInt: [[NSCalendarDate date] timeIntervalSince1970]] forKey: @"InitialDate"];
|
|
|
|
}
|
|
|
|
|
|
|
|
[d setObject: count forKey: @"FailedCount"];
|
|
|
|
[self _cacheValues: [d jsonRepresentation]
|
|
|
|
ofType: @"failedlogins"
|
|
|
|
forKey: theLogin];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self removeValueForKey: [NSString stringWithFormat: @"%@+failedlogins", theLogin]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Returns a dictionary with two keys/values
|
|
|
|
//
|
|
|
|
// FailedCount ->
|
|
|
|
// InitialDate ->
|
|
|
|
//
|
|
|
|
- (NSDictionary *) failedCountForLogin: (NSString *) theLogin
|
|
|
|
{
|
|
|
|
NSDictionary *d;
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
s = [self _valuesOfType: @"failedlogins" forKey: theLogin];
|
|
|
|
d = nil;
|
|
|
|
|
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
d = [s objectFromJSONString];
|
|
|
|
}
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2013-06-11 17:21:58 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
- (void) setMessageSubmissionsCount: (int) theCount
|
|
|
|
recipientsCount: (int) theRecipientsCount
|
|
|
|
forLogin: (NSString *) theLogin
|
|
|
|
{
|
|
|
|
NSNumber *messages_count, *recipients_count;
|
|
|
|
NSMutableDictionary *d;
|
|
|
|
|
|
|
|
if (theCount)
|
|
|
|
{
|
|
|
|
messages_count = [NSNumber numberWithInt: theCount];
|
|
|
|
recipients_count = [NSNumber numberWithInt: theRecipientsCount];
|
|
|
|
|
|
|
|
d = [NSMutableDictionary dictionaryWithDictionary: [self messageSubmissionsCountForLogin: theLogin]];
|
|
|
|
|
|
|
|
if (![d objectForKey: @"InitialDate"])
|
|
|
|
{
|
|
|
|
[d setObject: [NSNumber numberWithUnsignedInt: [[NSCalendarDate date] timeIntervalSince1970]] forKey: @"InitialDate"];
|
|
|
|
}
|
|
|
|
|
|
|
|
[d setObject: messages_count forKey: @"MessagesCount"];
|
|
|
|
[d setObject: recipients_count forKey: @"RecipientsCount"];
|
|
|
|
|
|
|
|
[self _cacheValues: [d jsonRepresentation]
|
|
|
|
ofType: @"messagesubmissions"
|
|
|
|
forKey: theLogin];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self removeValueForKey: [NSString stringWithFormat: @"%@+messagesubmissions", theLogin]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// MessagesCount ->
|
|
|
|
// RecipientsCount ->
|
|
|
|
// InitialDate ->
|
|
|
|
//
|
|
|
|
- (NSDictionary *) messageSubmissionsCountForLogin: (NSString *) theLogin
|
|
|
|
{
|
|
|
|
NSDictionary *d;
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
s = [self _valuesOfType: @"messagesubmissions" forKey: theLogin];
|
|
|
|
d = nil;
|
|
|
|
|
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
d = [s objectFromJSONString];
|
|
|
|
}
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2013-06-11 15:41:17 +02:00
|
|
|
|
2010-12-28 18:42:50 +01:00
|
|
|
//
|
|
|
|
// CAS session support
|
|
|
|
//
|
2010-01-28 22:42:03 +01:00
|
|
|
- (NSString *) CASTicketFromIdentifier: (NSString *) identifier
|
|
|
|
{
|
|
|
|
return [self valueForKey: [NSString stringWithFormat: @"cas-id:%@",
|
|
|
|
identifier]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) CASSessionWithTicket: (NSString *) ticket
|
|
|
|
{
|
|
|
|
return [self valueForKey: [NSString stringWithFormat: @"cas-ticket:%@",
|
|
|
|
ticket]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setCASSession: (NSString *) casSession
|
|
|
|
withTicket: (NSString *) ticket
|
|
|
|
forIdentifier: (NSString *) identifier
|
|
|
|
{
|
|
|
|
[self setValue: ticket
|
|
|
|
forKey: [NSString stringWithFormat: @"cas-id:%@", identifier]];
|
|
|
|
[self setValue: casSession
|
|
|
|
forKey: [NSString stringWithFormat: @"cas-ticket:%@", ticket]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) CASPGTIdFromPGTIOU: (NSString *) pgtIou
|
|
|
|
{
|
|
|
|
NSString *casPgtId, *key;
|
|
|
|
|
|
|
|
key = [NSString stringWithFormat: @"cas-pgtiou:%@", pgtIou];
|
|
|
|
casPgtId = [self valueForKey: key];
|
|
|
|
/* we directly remove the value as it can only be used once anyway */
|
|
|
|
if (casPgtId)
|
|
|
|
[self removeValueForKey: key];
|
|
|
|
|
|
|
|
return casPgtId;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setCASPGTId: (NSString *) pgtId
|
|
|
|
forPGTIOU: (NSString *) pgtIou
|
|
|
|
{
|
|
|
|
[self setValue: pgtId
|
|
|
|
forKey: [NSString stringWithFormat: @"cas-pgtiou:%@", pgtIou]];
|
|
|
|
}
|
|
|
|
|
2013-06-20 20:32:14 +02:00
|
|
|
- (void) removeCASSessionWithTicket: (NSString *) ticket
|
|
|
|
{
|
|
|
|
NSString *key, *session;
|
|
|
|
if ((session = [self CASSessionWithTicket: ticket]))
|
|
|
|
{
|
|
|
|
key = [NSString stringWithFormat: @"cas-ticket:%@", ticket];
|
|
|
|
[self removeValueForKey: key];
|
|
|
|
[self debugWithFormat: @"Removed session: %@", session];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-02 20:29:02 +01:00
|
|
|
// SAML2 support
|
|
|
|
- (NSDictionary *) saml2LoginDumpsForIdentifier: (NSString *) identifier
|
|
|
|
{
|
|
|
|
NSString *key, *jsonString;
|
|
|
|
|
|
|
|
key = [NSString stringWithFormat: @"saml2-login:%@", identifier];
|
|
|
|
jsonString = [self valueForKey: key];
|
|
|
|
|
|
|
|
return [jsonString objectFromJSONString];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setSaml2LoginDumps: (NSDictionary *) dump
|
|
|
|
forIdentifier: (NSString *) identifier
|
|
|
|
{
|
|
|
|
NSString *key;
|
|
|
|
|
|
|
|
key = [NSString stringWithFormat: @"saml2-login:%@", identifier];
|
|
|
|
|
|
|
|
[self setValue: [dump jsonRepresentation] forKey: key];
|
|
|
|
}
|
|
|
|
|
2010-10-15 20:30:08 +02:00
|
|
|
//
|
2010-12-28 18:42:50 +01:00
|
|
|
// ACL caching code
|
2010-10-15 20:30:08 +02:00
|
|
|
//
|
|
|
|
- (void) setACLs: (NSDictionary *) theACLs
|
|
|
|
forPath: (NSString *) thePath
|
|
|
|
{
|
|
|
|
if (theACLs)
|
2010-11-02 14:16:05 +01:00
|
|
|
[self _cacheValues: [theACLs jsonRepresentation]
|
2010-10-15 20:30:08 +02:00
|
|
|
ofType: @"acl"
|
|
|
|
forKey: thePath];
|
|
|
|
else
|
|
|
|
[self removeValueForKey: [NSString stringWithFormat: @"%@+acl", thePath]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSMutableDictionary *) aclsForPath: (NSString *) thePath
|
|
|
|
{
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
s = [self _valuesOfType: @"acl" forKey: thePath];
|
|
|
|
|
|
|
|
if (s)
|
2010-10-26 20:20:46 +02:00
|
|
|
return [s objectFromJSONString];
|
2010-10-15 20:30:08 +02:00
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
@end
|