]> err.no Git - scalable-opengroupware.org/blob - UI/Common/UIxAclEditor.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1188 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Common / UIxAclEditor.m
1 /* UIxAclEditor.m - this file is part of SOGo
2  *
3  * Copyright (C) 2006, 2007 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/NSDictionary.h>
25 #import <Foundation/NSKeyValueCoding.h>
26 #import <NGObjWeb/SoUser.h>
27 #import <NGObjWeb/WORequest.h>
28 #import <NGCards/iCalPerson.h>
29 #import <SoObjects/SOGo/LDAPUserManager.h>
30 #import <SoObjects/SOGo/SOGoContentObject.h>
31 #import <SoObjects/SOGo/SOGoPermissions.h>
32 #import <SoObjects/SOGo/NSArray+Utilities.h>
33
34 #import "UIxAclEditor.h"
35
36 @implementation UIxAclEditor
37
38 - (id) init
39 {
40   if ((self = [super init]))
41     {
42       aclUsers = nil;
43       prepared = NO;
44       publishInFreeBusy = NO;
45       users = [NSMutableArray new];
46       currentUser = nil;
47       defaultUserID = nil;
48       savedUIDs = nil;
49     }
50
51   return self;
52 }
53
54 - (void) dealloc
55 {
56   [savedUIDs release];
57   [users release];
58   [currentUser release];
59   [defaultUserID release];
60   [super dealloc];
61 }
62
63 - (NSArray *) aclsForObject
64 {
65   if (!aclUsers)
66     aclUsers = [[self clientObject] aclUsers];
67
68   return aclUsers;
69 }
70
71 - (NSString *) _displayNameForUID: (NSString *) uid
72 {
73   LDAPUserManager *um;
74   
75   um = [LDAPUserManager sharedUserManager];
76
77   return [NSString stringWithFormat: @"%@ <%@>",
78                    [um getCNForUID: uid], [um getEmailForUID: uid]];
79 }
80
81 - (NSString *) ownerName
82 {
83   NSString *ownerLogin;
84
85   ownerLogin = [[self clientObject] ownerInContext: context];
86
87   return [self _displayNameForUID: ownerLogin];
88 }
89
90 - (BOOL) hasOwner
91 {
92   NSString *ownerLogin;
93
94   ownerLogin = [[self clientObject] ownerInContext: context];
95
96   return (![ownerLogin isEqualToString: @"nobody"]);
97 }
98
99 - (NSString *) defaultUserID
100 {
101   if (!defaultUserID)
102     ASSIGN (defaultUserID, [[self clientObject] defaultUserID]);
103
104   return defaultUserID;
105 }
106
107 - (void) _prepareUsers
108 {
109   NSEnumerator *aclsEnum;
110   NSString *currentUID, *ownerLogin;
111
112   ownerLogin = [[self clientObject] ownerInContext: context];
113   if (!defaultUserID)
114     ASSIGN (defaultUserID, [[self clientObject] defaultUserID]);
115
116   aclsEnum = [[self aclsForObject] objectEnumerator];
117   currentUID = [aclsEnum nextObject];
118   while (currentUID)
119     {
120       if (!([currentUID isEqualToString: ownerLogin]
121             || [currentUID isEqualToString: defaultUserID]))
122         [users addObjectUniquely: currentUID];
123       currentUID = [aclsEnum nextObject];
124     }
125
126   prepared = YES;
127 }
128
129 - (NSArray *) usersForObject
130 {
131   if (!prepared)
132     [self _prepareUsers];
133
134   return users;
135 }
136
137 - (void) setCurrentUser: (NSString *) newCurrentUser
138 {
139   ASSIGN (currentUser, newCurrentUser);
140 }
141
142 - (NSString *) currentUser
143 {
144   return currentUser;
145 }
146
147 - (NSString *) currentUserDisplayName
148 {
149   return [self _displayNameForUID: currentUser];
150 }
151
152 - (NSString *) toolbar
153 {
154   NSString *currentLogin, *ownerLogin;
155
156   currentLogin = [[context activeUser] login];
157   ownerLogin = [[self clientObject] ownerInContext: context];
158
159   return (([ownerLogin isEqualToString: currentLogin])
160           ? @"SOGoAclOwner.toolbar" : @"SOGoAclAssistant.toolbar");
161 }
162
163 - (void) setUserUIDS: (NSString *) retainedUsers
164 {
165   if ([retainedUsers length] > 0)
166     savedUIDs = [retainedUsers componentsSeparatedByString: @","];
167   else
168     savedUIDs = [NSArray new];
169 }
170
171 - (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
172                            inContext: (WOContext *) context
173 {
174   return ([[request method] isEqualToString: @"POST"]);
175 }
176
177 - (id <WOActionResults>) saveAclsAction
178 {
179   NSEnumerator *aclsEnum;
180   SOGoObject *clientObject;
181   NSString *currentUID, *ownerLogin;
182
183   clientObject = [self clientObject];
184   ownerLogin = [clientObject ownerInContext: context];
185   aclsEnum = [[self aclsForObject] objectEnumerator];
186   currentUID = [[aclsEnum nextObject] objectForKey: @"c_uid"];
187   while (currentUID)
188     {
189       if ([currentUID isEqualToString: ownerLogin]
190           || [savedUIDs containsObject: currentUID])
191         [users removeObject: currentUID];
192       currentUID = [[aclsEnum nextObject] objectForKey: @"c_uid"];
193     }
194   [clientObject removeAclsForUsers: users];
195
196   return [self jsCloseWithRefreshMethod: nil];
197 }
198
199 - (BOOL) currentUserIsOwner
200 {
201   SOGoObject *clientObject;
202   NSString *currentUserLogin, *ownerLogin;
203
204   clientObject = [self clientObject];
205   ownerLogin = [clientObject ownerInContext: context];
206   currentUserLogin = [[context activeUser] login];
207
208   return [ownerLogin isEqualToString: currentUserLogin];
209 }
210
211 // - (id <WOActionResults>) addUserInAcls
212 // {
213 //   SOGoObject *clientObject;
214 //   NSString *uid;
215
216 //   uid = [self queryParameterForKey: @"uid"];
217
218 //   clientObject = [self clientObject];
219 // }
220
221 @end