]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoAclsFolder.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1015 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / SOGo / SOGoAclsFolder.m
1 /* SOGoAclsFolder.m - this file is part of SOGo
2  *
3  * Copyright (C) 2006 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSKeyValueCoding.h>
25 #import <Foundation/NSString.h>
26
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>
33
34 #import "SOGoFolder.h"
35 #import "SOGoAclsFolder.h"
36
37 @implementation SOGoAclsFolder
38
39 + (id) aclsFolder
40 {
41   id aclsFolder;
42
43   aclsFolder = [self new];
44   [aclsFolder autorelease];
45
46   return aclsFolder;
47 }
48
49 - (id) init
50 {
51   if ((self = [super init]))
52     {
53       ocsPath = nil;
54       ocsFolder = nil;
55     }
56
57   return self;
58 }
59
60 - (void) dealloc
61 {
62   if (ocsPath)
63     [ocsPath release];
64   if (ocsFolder)
65     [ocsFolder release];
66   [super dealloc];
67 }
68
69 - (void) setOCSPath: (NSString *) newOCSPath
70 {
71   if (ocsPath)
72     [ocsPath release];
73   ocsPath = newOCSPath;
74   if (ocsPath)
75     [ocsPath retain];
76 }
77
78 - (GCSFolderManager *)folderManager {
79   return [GCSFolderManager defaultFolderManager];
80 }
81
82 - (GCSFolder *)ocsFolderForPath:(NSString *)_path {
83   return [[self folderManager] folderAtPath:_path];
84 }
85
86 - (GCSFolder *) ocsFolder {
87   GCSFolder *folder;
88
89   if (!ocsFolder)
90     ocsFolder = [[self ocsFolderForPath: ocsPath] retain];
91
92   if ([ocsFolder isNotNull])
93     folder = ocsFolder;
94   else
95     folder = nil;
96
97   return folder;
98 }
99
100 - (NSString *) _ocsPathForObject: (SOGoObject *) object
101 {
102   NSString *pathForObject;
103   id currentObject;
104   BOOL done;
105
106   pathForObject = nil;
107   currentObject = object;
108   done = NO;
109   while (currentObject && !done)
110     if ([currentObject isKindOfClass: [SOGoFolder class]])
111       {
112         pathForObject = [(SOGoFolder *) currentObject ocsPath];
113         done = YES;
114 //         if (!pathForObject)
115 //           currentObject = [currentObject container];
116       }
117     else
118       currentObject = [currentObject container];
119
120   return pathForObject;
121 }
122
123 - (NSArray *) aclsForObject: (SOGoObject *) object
124 {
125   EOQualifier *qualifier;
126   NSString *objectPath;
127
128   [self setOCSPath: [self _ocsPathForObject: object]];
129
130   objectPath
131     = [NSString stringWithFormat: @"/%@",
132                 [[object pathArrayToSoObject] componentsJoinedByString: @"/"]];
133   qualifier
134     = [EOQualifier qualifierWithQualifierFormat: @"c_object = %@", objectPath];
135
136   return [[self ocsFolder] fetchAclMatchingQualifier: qualifier];
137 }
138
139 - (NSArray *) aclsForObject: (SOGoObject *) object
140                     forUser: (NSString *) uid
141 {
142   EOQualifier *qualifier;
143   NSString *objectPath;
144   NSArray *records;
145
146   [self setOCSPath: [self _ocsPathForObject: object]];
147
148   objectPath
149     = [NSString stringWithFormat: @"/%@",
150                 [[object pathArrayToSoObject] componentsJoinedByString: @"/"]];
151   qualifier = [EOQualifier
152                 qualifierWithQualifierFormat: @"(c_object = %@) AND (c_uid = %@)",
153                 objectPath, uid];
154
155   records = [[self ocsFolder] fetchAclMatchingQualifier: qualifier];
156
157   return [records valueForKey: @"c_role"];
158 }
159
160 - (void) removeUsersWithRole: (NSString *) role
161              forObjectAtPath: (NSString *) objectPath
162                     inFolder: (GCSFolder *) folder
163 {
164   NSString *deleteSQL;
165   EOAdaptorChannel *channel;
166
167   channel = [folder acquireAclChannel];
168
169   deleteSQL = [NSString stringWithFormat: @"DELETE FROM %@"
170                         @" WHERE c_object = '%@'"
171                         @" AND c_role = '%@'",
172                         [folder aclTableName], objectPath, role];
173   [channel evaluateExpressionX: deleteSQL];
174 }
175
176 - (void) setRoleForObjectAtPath: (NSString *) objectPath
177                         forUser: (NSString *) uid
178                              to: (NSString *) role
179                        inFolder: (GCSFolder *) folder
180 {
181   NSString *SQL;
182   EOAdaptorChannel *channel;
183
184   channel = [folder acquireAclChannel];
185
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];
196 }
197
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
202 {
203   GCSFolder *aclsFolder;
204   NSString *objectPath, *currentUID;
205   NSEnumerator *userUIDs;
206
207   [self setOCSPath: [self _ocsPathForObject: object]];
208   aclsFolder = [self ocsFolder];
209
210   objectPath
211     = [NSString stringWithFormat: @"/%@",
212                 [[object pathArrayToSoObject] componentsJoinedByString: @"/"]];
213   [self removeUsersWithRole: role
214         forObjectAtPath: objectPath
215         inFolder: aclsFolder];
216
217   userUIDs = [uids objectEnumerator];
218   currentUID = [userUIDs nextObject];
219   while (currentUID)
220     {
221       if ([currentUID length] > 0)
222         [self setRoleForObjectAtPath: objectPath
223               forUser: currentUID
224               to: role
225               inFolder: aclsFolder];
226       currentUID = [userUIDs nextObject];
227     }
228 }
229
230 @end