1/* UIxUserRightsEditor.m - this file is part of SOGo
2 *
3 * Copyright (C) 2007-2015 Inverse inc.
4 *
5 * This file is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This file is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; see the file COPYING.  If not, write to
17 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
20
21#import <Foundation/NSDictionary.h>
22
23#import <NGObjWeb/NSException+HTTP.h>
24#import <NGObjWeb/WOApplication.h>
25#import <NGObjWeb/WOResponse.h>
26#import <NGObjWeb/WORequest.h>
27#import <SOGo/SOGoDomainDefaults.h>
28#import <SOGo/SOGoGroup.h>
29#import <SOGo/SOGoObject.h>
30#import <SOGo/SOGoPermissions.h>
31#import <SOGo/SOGoUser.h>
32#import <SOGo/SOGoUserDefaults.h>
33#import <SOGo/SOGoUserManager.h>
34#import <UI/SOGoUI/SOGoACLAdvisory.h>
35
36#import "UIxUserRightsEditor.h"
37
38@implementation UIxUserRightsEditor
39
40- (id) init
41{
42  if ((self = [super init]))
43    {
44      uid = nil;
45      userRights = [NSMutableArray new];
46      defaultUserID = nil;
47    }
48
49  return self;
50}
51
52- (void) dealloc
53{
54  [uid release];
55  [userRights release];
56  [defaultUserID release];
57  [super dealloc];
58}
59
60- (NSString *) uid
61{
62  return uid;
63}
64
65- (NSString *) folderName
66{
67  id folder;
68
69  folder = [context clientObject];
70
71  return [folder displayName];
72}
73
74- (BOOL) userIsDefaultUser
75{
76  if (!defaultUserID)
77    ASSIGN (defaultUserID, [[self clientObject] defaultUserID]);
78
79  return [uid isEqualToString: defaultUserID];
80}
81
82- (BOOL) userIsAnonymousUser
83{
84  return [uid isEqualToString: @"anonymous"];
85}
86
87- (NSString *) userDisplayName
88{
89  NSDictionary *infos;
90  SOGoUserManager *um;
91
92  if ([self userIsAnonymousUser])
93    {
94      return [self labelForKey: @"Public Access"];
95    }
96  else if ([self userIsDefaultUser])
97    {
98      return [self labelForKey: @"Any Authenticated User"];
99    }
100  else
101    {
102      um = [SOGoUserManager sharedUserManager];
103      infos = [um contactInfosForUserWithUIDorEmail: uid inDomain: [[context activeUser] domain]];
104      if (infos)
105        {
106          return [NSString stringWithFormat: @"%@ <%@>",
107                        [infos objectForKey: @"cn"],
108                        [infos objectForKey: @"c_email"]];
109        }
110      else
111        return uid;
112    }
113}
114
115- (BOOL) _initRights
116{
117  BOOL response;
118  NSString *newUID, *domain;
119  SOGoUserManager *um;
120  SOGoObject *clientObject;
121  SOGoGroup *group;
122
123  response = NO;
124
125  newUID = [[context request] formValueForKey: @"uid"];
126  if ([newUID length] > 0)
127    {
128      if (!defaultUserID)
129	ASSIGN (defaultUserID, [[self clientObject] defaultUserID]);
130
131      um = [SOGoUserManager sharedUserManager];
132      if ([newUID isEqualToString: defaultUserID]
133	  || [newUID isEqualToString: @"anonymous"]
134	  || [[um getEmailForUID: newUID] length] > 0)
135	{
136	  if (![newUID hasPrefix: @"@"])
137	    {
138              domain = [[context activeUser] domain];
139	      group = [SOGoGroup groupWithIdentifier: newUID inDomain: domain];
140	      if (group)
141		newUID = [NSString stringWithFormat: @"@%@", newUID];
142	    }
143
144	  ASSIGN (uid, newUID);
145	  clientObject = [self clientObject];
146	  [userRights addObjectsFromArray: [clientObject aclsForUser: uid]];
147
148	  response = YES;
149	}
150    }
151
152  return response;
153}
154
155- (id <WOActionResults>) defaultAction
156{
157  id <WOActionResults> response;
158
159  if (![self _initRights])
160    response = [NSException exceptionWithHTTPStatus: 403
161			    reason: @"No such user."];
162  else
163    {
164      [self prepareRightsForm];
165      response = self;
166    }
167
168  return response;
169}
170
171- (void) sendACLAdvisoryTemplateForObject: (id) theObject
172{
173  NSString *language, *pageName;
174  SOGoUserDefaults *ud;
175  SOGoACLAdvisory *page;
176  WOApplication *app;
177
178  if (!([self userIsDefaultUser] || [self userIsAnonymousUser]))
179    {
180      ud = [[SOGoUser userWithLogin: uid roles: nil] userDefaults];
181      language = [ud language];
182      pageName = [NSString stringWithFormat: @"SOGoACL%@ModificationAdvisory",
183                           language];
184
185      app = [WOApplication application];
186      page = [app pageWithName: pageName inContext: context];
187      [page setACLObject: theObject];
188      [page setRecipientUID: uid];
189      [page send];
190    }
191}
192
193- (id <WOActionResults>) saveUserRightsAction
194{
195  id <WOActionResults> response;
196  SOGoDomainDefaults *dd;
197
198  if (![self _initRights])
199    response = [NSException exceptionWithHTTPStatus: 403
200			    reason: @"No such user."];
201  else
202    {
203      NSArray *o;
204
205      o = [NSArray arrayWithArray: userRights];
206
207      [self updateRights];
208      [[self clientObject] setRoles: userRights forUser: uid];
209
210      dd = [[context activeUser] domainDefaults];
211      if (![o isEqualToArray: userRights] && [dd aclSendEMailNotifications])
212        [self sendACLAdvisoryTemplateForObject: [self clientObject]];
213
214      response = [self jsCloseWithRefreshMethod: nil];
215    }
216
217  return response;
218}
219
220- (void) appendRight: (NSString *) newRight
221{
222  if (![userRights containsObject: newRight])
223    [userRights addObject: newRight];
224}
225
226- (void) removeRight: (NSString *) right
227{
228  if ([userRights containsObject: right])
229    [userRights removeObject: right];
230}
231
232- (void) appendExclusiveRight: (NSString *) newRight
233		     fromList: (NSArray *) list
234{
235  [userRights removeObjectsInArray: list];
236  [self appendRight: newRight];
237}
238
239- (void) removeAllRightsFromList: (NSArray *) list
240{
241  [userRights removeObjectsInArray: list];
242}
243
244- (void) prepareRightsForm
245{
246}
247
248- (void) updateRights
249{
250  [self subclassResponsibility: _cmd];
251}
252
253@end
254