]> err.no Git - scalable-opengroupware.org/blob - UI/Common/UIxUserRightsEditor.m
Install libs to /usr/lib
[scalable-opengroupware.org] / UI / Common / UIxUserRightsEditor.m
1 /* UIxUserRightsEditor.m - this file is part of SOGo
2  *
3  * Copyright (C) 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 <NGObjWeb/NSException+HTTP.h>
24 #import <NGObjWeb/WOApplication.h>
25 #import <NGObjWeb/WOResponse.h>
26 #import <NGObjWeb/WORequest.h>
27 #import <SoObjects/SOGo/LDAPUserManager.h>
28 #import <SoObjects/SOGo/SOGoPermissions.h>
29 #import <SoObjects/SOGo/SOGoObject.h>
30 #import <SoObjects/SOGo/SOGoUser.h>
31 #import <UI/SOGoUI/SOGoACLAdvisory.h>
32 #import <Foundation/NSUserDefaults.h>
33
34 #import "UIxUserRightsEditor.h"
35
36 static BOOL sendACLAdvisories = NO;
37
38 @implementation UIxUserRightsEditor
39
40 + (void) initialize
41 {
42   NSUserDefaults *ud;
43
44   ud = [NSUserDefaults standardUserDefaults];
45   sendACLAdvisories = [ud boolForKey: @"SOGoACLsSendEMailNotifications"];
46 }
47
48 - (id) init
49 {
50   if ((self = [super init]))
51     {
52       uid = nil;
53       userRights = [NSMutableArray new];
54       defaultUserID = nil;
55     }
56
57   return self;
58 }
59
60 - (void) dealloc
61 {
62   [uid release];
63   [userRights release];
64   [defaultUserID release];
65   [super dealloc];
66 }
67
68 - (NSString *) uid
69 {
70   return uid;
71 }
72
73 - (BOOL) userIsDefaultUser
74 {
75   if (!defaultUserID)
76     ASSIGN (defaultUserID, [[self clientObject] defaultUserID]);
77
78   return [uid isEqualToString: defaultUserID];
79 }
80
81 - (NSString *) userDisplayName
82 {
83   LDAPUserManager *um;
84
85   um = [LDAPUserManager sharedUserManager];
86
87   return [NSString stringWithFormat: @"%@ <%@>",
88                    [um getCNForUID: uid],
89                    [um getEmailForUID: uid]];
90 }
91
92 - (BOOL) _initRights
93 {
94   BOOL response;
95   NSString *newUID;
96   LDAPUserManager *um;
97   SOGoObject *clientObject;
98
99   response = NO;
100
101   newUID = [[context request] formValueForKey: @"uid"];
102   if ([newUID length] > 0)
103     {
104       if (!defaultUserID)
105         ASSIGN (defaultUserID, [[self clientObject] defaultUserID]);
106
107       um = [LDAPUserManager sharedUserManager];
108       if ([newUID isEqualToString: defaultUserID]
109           || [[um getEmailForUID: newUID] length] > 0)
110         {
111           ASSIGN (uid, newUID);
112           clientObject = [self clientObject];
113           [userRights addObjectsFromArray: [clientObject aclsForUser: uid]];
114
115           response = YES;
116         }
117     }
118
119   return response;
120 }
121
122 - (id <WOActionResults>) defaultAction
123 {
124   id <WOActionResults> response;
125
126   if (![self _initRights])
127     response = [NSException exceptionWithHTTPStatus: 403
128                             reason: @"No such user."];
129   else
130     {
131       [self prepareRightsForm];
132       response = self;
133     }
134
135   return response;
136 }
137
138 - (void) sendACLAdvisoryTemplateForObject: (id) theObject
139 {
140   NSString *language, *pageName;
141   SOGoUser *user;
142   SOGoACLAdvisory *page;
143   WOApplication *app;
144
145   user = [SOGoUser userWithLogin: uid roles: nil];
146   language = [user language];
147   pageName = [NSString stringWithFormat: @"SOGoACL%@ModificationAdvisory",
148                        language];
149
150   app = [WOApplication application];
151   page = [app pageWithName: pageName inContext: context];
152   [page setACLObject: theObject];
153   [page setRecipientUID: uid];
154   [page send];
155 }
156
157 - (id <WOActionResults>) saveUserRightsAction
158 {
159   id <WOActionResults> response;
160
161   if (![self _initRights])
162     response = [NSException exceptionWithHTTPStatus: 403
163                             reason: @"No such user."];
164   else
165     {
166       NSArray *o;
167
168       o = [NSArray arrayWithArray: userRights];
169
170       [self updateRights];
171       [[self clientObject] setRoles: userRights forUser: uid];
172
173       if (![o isEqualToArray: userRights] && sendACLAdvisories)
174         {
175           [self sendACLAdvisoryTemplateForObject: [self clientObject]];
176         } 
177       
178       response = [self jsCloseWithRefreshMethod: nil];
179     }
180
181   return response;
182 }
183
184 - (void) appendRight: (NSString *) newRight
185 {
186   if (![userRights containsObject: newRight])
187     [userRights addObject: newRight];
188 }
189
190 - (void) removeRight: (NSString *) right
191 {
192   if ([userRights containsObject: right])
193     [userRights removeObject: right];
194 }
195
196 - (void) appendExclusiveRight: (NSString *) newRight
197                      fromList: (NSArray *) list
198 {
199   [userRights removeObjectsInArray: list];
200   [self appendRight: newRight];
201 }
202
203 - (void) removeAllRightsFromList: (NSArray *) list
204 {
205   [userRights removeObjectsInArray: list];
206 }
207
208 - (void) prepareRightsForm
209 {
210 }
211
212 - (void) updateRights
213 {
214   [self subclassResponsibility: _cmd];
215 }
216
217 @end