1 /* SOGoAclsFolder.m - this file is part of SOGo
3 * Copyright (C) 2006 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSKeyValueCoding.h>
25 #import <Foundation/NSString.h>
27 #import <NGExtensions/NSNull+misc.h>
28 #import <NGObjWeb/SoObject.h>
29 #import <EOControl/EOQualifier.h>
30 #import <GDLAccess/EOAdaptorChannel.h>
31 #import <GDLContentStore/GCSFolder.h>
32 #import <GDLContentStore/GCSFolderManager.h>
34 #import "SOGoFolder.h"
35 #import "SOGoAclsFolder.h"
37 @implementation SOGoAclsFolder
43 aclsFolder = [self new];
44 [aclsFolder autorelease];
51 if ((self = [super init]))
69 - (void) setOCSPath: (NSString *) newOCSPath
78 - (GCSFolderManager *)folderManager {
79 return [GCSFolderManager defaultFolderManager];
82 - (GCSFolder *)ocsFolderForPath:(NSString *)_path {
83 return [[self folderManager] folderAtPath:_path];
86 - (GCSFolder *) ocsFolder {
90 ocsFolder = [[self ocsFolderForPath: ocsPath] retain];
92 if ([ocsFolder isNotNull])
100 - (NSString *) _ocsPathForObject: (SOGoObject *) object
102 NSString *pathForObject;
107 currentObject = object;
109 while (currentObject && !done)
110 if ([currentObject isKindOfClass: [SOGoFolder class]])
112 pathForObject = [(SOGoFolder *) currentObject ocsPath];
114 // if (!pathForObject)
115 // currentObject = [currentObject container];
118 currentObject = [currentObject container];
120 return pathForObject;
123 - (NSArray *) aclsForObject: (SOGoObject *) object
125 EOQualifier *qualifier;
126 NSString *objectPath;
128 [self setOCSPath: [self _ocsPathForObject: object]];
131 = [NSString stringWithFormat: @"/%@",
132 [[object pathArrayToSoObject] componentsJoinedByString: @"/"]];
134 = [EOQualifier qualifierWithQualifierFormat: @"c_object = %@", objectPath];
136 return [[self ocsFolder] fetchAclMatchingQualifier: qualifier];
139 - (NSArray *) aclsForObject: (SOGoObject *) object
140 forUser: (NSString *) uid
142 EOQualifier *qualifier;
143 NSString *objectPath;
146 [self setOCSPath: [self _ocsPathForObject: object]];
149 = [NSString stringWithFormat: @"/%@",
150 [[object pathArrayToSoObject] componentsJoinedByString: @"/"]];
151 qualifier = [EOQualifier
152 qualifierWithQualifierFormat: @"(c_object = %@) AND (c_uid = %@)",
155 records = [[self ocsFolder] fetchAclMatchingQualifier: qualifier];
157 return [records valueForKey: @"c_role"];
160 - (void) removeUsersWithRole: (NSString *) role
161 forObjectAtPath: (NSString *) objectPath
162 inFolder: (GCSFolder *) folder
165 EOAdaptorChannel *channel;
167 channel = [folder acquireAclChannel];
169 deleteSQL = [NSString stringWithFormat: @"DELETE FROM %@"
170 @" WHERE c_object = '%@'"
171 @" AND c_role = '%@'",
172 [folder aclTableName], objectPath, role];
173 [channel evaluateExpressionX: deleteSQL];
176 - (void) setRoleForObjectAtPath: (NSString *) objectPath
177 forUser: (NSString *) uid
178 to: (NSString *) role
179 inFolder: (GCSFolder *) folder
182 EOAdaptorChannel *channel;
184 channel = [folder acquireAclChannel];
186 SQL = [NSString stringWithFormat: @"DELETE FROM %@"
187 @" WHERE c_object = '%@'"
188 @" AND c_uid = '%@'",
189 [folder aclTableName], objectPath, uid];
190 [channel evaluateExpressionX: SQL];
191 SQL = [NSString stringWithFormat: @"INSERT INTO %@"
192 @" (c_object, c_uid, c_role)"
193 @" VALUES ('%@', '%@', '%@')", [folder aclTableName],
194 objectPath, uid, role];
195 [channel evaluateExpressionX: SQL];
198 /* FIXME: part of this code should be moved to sope-gdl/GCSFolder.m */
199 - (void) setRoleForObject: (SOGoObject *) object
200 forUsers: (NSArray *) uids
201 to: (NSString *) role
203 GCSFolder *aclsFolder;
204 NSString *objectPath, *currentUID;
205 NSEnumerator *userUIDs;
207 [self setOCSPath: [self _ocsPathForObject: object]];
208 aclsFolder = [self ocsFolder];
211 = [NSString stringWithFormat: @"/%@",
212 [[object pathArrayToSoObject] componentsJoinedByString: @"/"]];
213 [self removeUsersWithRole: role
214 forObjectAtPath: objectPath
215 inFolder: aclsFolder];
217 userUIDs = [uids objectEnumerator];
218 currentUID = [userUIDs nextObject];
221 if ([currentUID length] > 0)
222 [self setRoleForObjectAtPath: objectPath
225 inFolder: aclsFolder];
226 currentUID = [userUIDs nextObject];