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