]> err.no Git - scalable-opengroupware.org/blob - UI/Common/UIxAclEditor.m
Install libs to /usr/lib
[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 <NGObjWeb/SoSecurityManager.h>
29 #import <NGCards/iCalPerson.h>
30 #import <SoObjects/SOGo/LDAPUserManager.h>
31 #import <SoObjects/SOGo/SOGoContentObject.h>
32 #import <SoObjects/SOGo/SOGoPermissions.h>
33 #import <SoObjects/SOGo/NSArray+Utilities.h>
34 #import <SoObjects/SOGo/SOGoUser.h>
35
36 #import "UIxAclEditor.h"
37
38 @implementation UIxAclEditor
39
40 - (id) init
41 {
42   if ((self = [super init]))
43     {
44       aclUsers = nil;
45       prepared = NO;
46       publishInFreeBusy = NO;
47       users = [NSMutableArray new];
48       currentUser = nil;
49       defaultUserID = nil;
50       savedUIDs = nil;
51     }
52
53   return self;
54 }
55
56 - (void) dealloc
57 {
58   [savedUIDs release];
59   [users release];
60   [currentUser release];
61   [defaultUserID release];
62   [super dealloc];
63 }
64
65 - (NSArray *) aclsForObject
66 {
67   if (!aclUsers)
68     aclUsers = [[self clientObject] aclUsers];
69
70   return aclUsers;
71 }
72
73 - (NSString *) _displayNameForUID: (NSString *) uid
74 {
75   LDAPUserManager *um;
76   
77   um = [LDAPUserManager sharedUserManager];
78
79   return [NSString stringWithFormat: @"%@ <%@>",
80                    [um getCNForUID: uid], [um getEmailForUID: uid]];
81 }
82
83 - (NSString *) ownerName
84 {
85   NSString *ownerLogin;
86
87   ownerLogin = [[self clientObject] ownerInContext: context];
88
89   return [self _displayNameForUID: ownerLogin];
90 }
91
92 - (BOOL) hasOwner
93 {
94   NSString *ownerLogin;
95
96   ownerLogin = [[self clientObject] ownerInContext: context];
97
98   return (![ownerLogin isEqualToString: @"nobody"]);
99 }
100
101 - (NSString *) defaultUserID
102 {
103   if (!defaultUserID)
104     ASSIGN (defaultUserID, [[self clientObject] defaultUserID]);
105
106   return defaultUserID;
107 }
108
109 - (void) _prepareUsers
110 {
111   NSEnumerator *aclsEnum;
112   NSString *currentUID, *ownerLogin;
113
114   ownerLogin = [[self clientObject] ownerInContext: context];
115   if (!defaultUserID)
116     ASSIGN (defaultUserID, [[self clientObject] defaultUserID]);
117
118   aclsEnum = [[self aclsForObject] objectEnumerator];
119   currentUID = [aclsEnum nextObject];
120   while (currentUID)
121     {
122       if (!([currentUID isEqualToString: ownerLogin]
123             || [currentUID isEqualToString: defaultUserID]))
124         [users addObjectUniquely: currentUID];
125       currentUID = [aclsEnum nextObject];
126     }
127
128   prepared = YES;
129 }
130
131 - (NSArray *) usersForObject
132 {
133   if (!prepared)
134     [self _prepareUsers];
135
136   return users;
137 }
138
139 - (void) setCurrentUser: (NSString *) newCurrentUser
140 {
141   ASSIGN (currentUser, newCurrentUser);
142 }
143
144 - (NSString *) currentUser
145 {
146   return currentUser;
147 }
148
149 - (NSString *) currentUserDisplayName
150 {
151   return [self _displayNameForUID: currentUser];
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) canModifyAcls
191 {
192   SoSecurityManager *mgr;
193
194   mgr = [SoSecurityManager sharedSecurityManager];
195
196   return (![mgr validatePermission: SOGoPerm_SaveAcls
197                 onObject: [self clientObject]
198                 inContext: context]);
199 }
200
201 // - (id <WOActionResults>) addUserInAcls
202 // {
203 //   SOGoObject *clientObject;
204 //   NSString *uid;
205
206 //   uid = [self queryParameterForKey: @"uid"];
207
208 //   clientObject = [self clientObject];
209 // }
210
211 @end