]> err.no Git - scalable-opengroupware.org/blob - UI/Common/UIxAclEditor.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1162 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 - (NSString *) defaultUserID
91 {
92   if (!defaultUserID)
93     ASSIGN (defaultUserID, [[self clientObject] defaultUserID]);
94
95   return defaultUserID;
96 }
97
98 - (void) _prepareUsers
99 {
100   NSEnumerator *aclsEnum;
101   NSString *currentUID, *ownerLogin;
102
103   ownerLogin = [[self clientObject] ownerInContext: context];
104   if (!defaultUserID)
105     ASSIGN (defaultUserID, [[self clientObject] defaultUserID]);
106
107   aclsEnum = [[self aclsForObject] objectEnumerator];
108   currentUID = [aclsEnum nextObject];
109   while (currentUID)
110     {
111       if (!([currentUID isEqualToString: ownerLogin]
112             || [currentUID isEqualToString: defaultUserID]))
113         [users addObjectUniquely: currentUID];
114       currentUID = [aclsEnum nextObject];
115     }
116
117   prepared = YES;
118 }
119
120 - (NSArray *) usersForObject
121 {
122   if (!prepared)
123     [self _prepareUsers];
124
125   return users;
126 }
127
128 - (void) setCurrentUser: (NSString *) newCurrentUser
129 {
130   ASSIGN (currentUser, newCurrentUser);
131 }
132
133 - (NSString *) currentUser
134 {
135   return currentUser;
136 }
137
138 - (NSString *) currentUserDisplayName
139 {
140   return [self _displayNameForUID: currentUser];
141 }
142
143 - (NSString *) toolbar
144 {
145   NSString *currentLogin, *ownerLogin;
146
147   currentLogin = [[context activeUser] login];
148   ownerLogin = [[self clientObject] ownerInContext: context];
149
150   return (([ownerLogin isEqualToString: currentLogin])
151           ? @"SOGoAclOwner.toolbar" : @"SOGoAclAssistant.toolbar");
152 }
153
154 - (void) setUserUIDS: (NSString *) retainedUsers
155 {
156   if ([retainedUsers length] > 0)
157     savedUIDs = [retainedUsers componentsSeparatedByString: @","];
158   else
159     savedUIDs = [NSArray new];
160 }
161
162 - (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
163                            inContext: (WOContext *) context
164 {
165   return ([[request method] isEqualToString: @"POST"]);
166 }
167
168 - (id <WOActionResults>) saveAclsAction
169 {
170   NSEnumerator *aclsEnum;
171   SOGoObject *clientObject;
172   NSString *currentUID, *ownerLogin;
173
174   clientObject = [self clientObject];
175   ownerLogin = [clientObject ownerInContext: context];
176   aclsEnum = [[self aclsForObject] objectEnumerator];
177   currentUID = [[aclsEnum nextObject] objectForKey: @"c_uid"];
178   while (currentUID)
179     {
180       if ([currentUID isEqualToString: ownerLogin]
181           || [savedUIDs containsObject: currentUID])
182         [users removeObject: currentUID];
183       currentUID = [[aclsEnum nextObject] objectForKey: @"c_uid"];
184     }
185   [clientObject removeAclsForUsers: users];
186
187   return [self jsCloseWithRefreshMethod: nil];
188 }
189
190 - (BOOL) currentUserIsOwner
191 {
192   SOGoObject *clientObject;
193   NSString *currentUserLogin, *ownerLogin;
194
195   clientObject = [self clientObject];
196   ownerLogin = [clientObject ownerInContext: context];
197   currentUserLogin = [[context activeUser] login];
198
199   return [ownerLogin isEqualToString: currentUserLogin];
200 }
201
202 // - (id <WOActionResults>) addUserInAcls
203 // {
204 //   SOGoObject *clientObject;
205 //   NSString *uid;
206
207 //   uid = [self queryParameterForKey: @"uid"];
208
209 //   clientObject = [self clientObject];
210 // }
211
212 @end