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