]> err.no Git - scalable-opengroupware.org/blob - UI/Common/UIxAclEditor.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1034 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Common / UIxAclEditor.m
1 /* UIxAclEditor.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/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/AgenorUserManager.h>
30 #import <SoObjects/SOGo/SOGoAclsFolder.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       delegates = [NSMutableArray new];
46       assistants = [NSMutableArray new];
47       ownerLogin = nil;
48       currentUser = nil;
49     }
50
51   return self;
52 }
53
54 - (void) dealloc
55 {
56   [users release];
57   [delegates release];
58   [assistants release];
59   [ownerLogin release];
60   [currentUser release];
61   [super dealloc];
62 }
63
64 - (NSArray *) aclsForFolder
65 {
66   SOGoAclsFolder *folder;
67
68   if (!acls)
69     {
70       folder = [SOGoAclsFolder aclsFolder];
71       acls = [folder aclsForObject: [self clientObject]];
72     }
73
74   return acls;
75 }
76
77 - (NSString *) ownerLogin
78 {
79   if (!ownerLogin)
80     {
81       ownerLogin = [[self clientObject] ownerInContext: context];
82       [ownerLogin retain];
83     }
84
85   return ownerLogin;
86 }
87
88 - (NSString *) _displayNameForUID: (NSString *) uid
89 {
90   AgenorUserManager *um;
91   
92   um = [AgenorUserManager sharedUserManager];
93
94   return [NSString stringWithFormat: @"%@ <%@>",
95                    [um getCNForUID: uid],
96                    [um getEmailForUID: uid]];
97 }
98
99 - (NSString *) ownerName
100 {
101   return [self _displayNameForUID: [self ownerLogin]];
102 }
103
104 - (void) _prepareUsers
105 {
106   NSEnumerator *aclsEnum;
107   AgenorUserManager *um;
108   NSDictionary *currentAcl;
109   NSString *currentUID;
110
111   aclsEnum = [[self aclsForFolder] objectEnumerator];
112   um = [AgenorUserManager sharedUserManager];
113   currentAcl = [aclsEnum nextObject];
114   while (currentAcl)
115     {
116       currentUID = [currentAcl objectForKey: @"c_uid"];
117       if ([currentUID isEqualToString: @"freebusy"])
118         publishInFreeBusy = YES;
119       else
120         {
121           if (![[um getCNForUID: currentUID]
122                  isEqualToString: [self ownerLogin]])
123             {
124               if ([[currentAcl objectForKey: @"c_role"]
125                     isEqualToString: SOGoRole_Delegate])
126                 [delegates addObject: currentUID];
127               else
128                 [assistants addObject: currentUID];
129               [users addObject: currentUID];
130             }
131         }
132       currentAcl = [aclsEnum nextObject];
133
134       prepared = YES;
135     }
136 }
137
138 - (NSArray *) usersForFolder
139 {
140   if (!prepared)
141     [self _prepareUsers];
142
143   return users;
144 }
145
146 - (void) setCurrentUser: (NSString *) newCurrentUser
147 {
148   ASSIGN (currentUser, newCurrentUser);
149 }
150
151 - (NSString *) currentUser
152 {
153   return currentUser;
154 }
155
156 - (NSString *) currentUserDisplayName
157 {
158   return [self _displayNameForUID: currentUser];
159 }
160
161 - (NSArray *) delegates
162 {
163   if (!prepared)
164     [self _prepareUsers];
165
166   return delegates;
167 }
168
169 - (NSString *) assistantsValue
170 {
171   if (!prepared)
172     [self _prepareUsers];
173
174   return [assistants componentsJoinedByString: @","];
175 }
176
177 - (NSString *) delegatesValue
178 {
179   if (!prepared)
180     [self _prepareUsers];
181
182   return [delegates componentsJoinedByString: @","];
183 }
184
185 - (BOOL) publishInFreeBusy
186 {
187   if (!prepared)
188     [self _prepareUsers];
189
190   return publishInFreeBusy;
191 }
192
193 - (NSString *) toolbar
194 {
195   NSString *currentLogin;
196
197   currentLogin = [[context activeUser] login];
198
199   return (([[self ownerLogin] isEqualToString: currentLogin])
200           ? @"SOGoAclOwner.toolbar" : @"SOGoAclAssistant.toolbar");
201 }
202
203 - (BOOL) clientIsCalendar
204 {
205   return [NSStringFromClass ([[self clientObject] class])
206                             isEqualToString: @"SOGoAppointmentFolder"];
207 }
208
209 - (id) saveAclsAction
210 {
211   NSString *uids;
212   NSArray *fbUsers;
213   WORequest *request;
214   SOGoAclsFolder *folder;
215   SOGoObject *clientObject;
216
217   folder = [SOGoAclsFolder aclsFolder];
218   request = [context request];
219   clientObject = [self clientObject];
220   uids = [request formValueForKey: @"delegates"];
221   [folder setRoleForObject: clientObject
222           forUsers: [uids componentsSeparatedByString: @","]
223           to: SOGoRole_Delegate];
224   uids = [request formValueForKey: @"assistants"];
225   [folder setRoleForObject: clientObject
226           forUsers: [uids componentsSeparatedByString: @","]
227           to: SOGoRole_Assistant];
228   if ([self clientIsCalendar]) {
229     if ([[request formValueForKey: @"freebusy"] intValue])
230       fbUsers = [NSArray arrayWithObject: @"freebusy"];
231     else
232       fbUsers = nil;
233     [folder setRoleForObject: clientObject
234             forUsers: fbUsers
235             to: SOGoRole_FreeBusy];
236   }
237
238   return [self jsCloseWithRefreshMethod: nil];
239 }
240
241 @end