]> err.no Git - scalable-opengroupware.org/blob - UI/Common/UIxUserRightsEditor.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1108 d1b88da0-ebda-0310...
[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/WOResponse.h>
25 #import <NGObjWeb/WORequest.h>
26 #import <SoObjects/SOGo/LDAPUserManager.h>
27 #import <SoObjects/SOGo/SOGoPermissions.h>
28 #import <SoObjects/SOGo/SOGoObject.h>
29
30 #import "UIxUserRightsEditor.h"
31
32 @implementation UIxUserRightsEditor
33
34 - (id) init
35 {
36   if ((self = [super init]))
37     {
38       uid = nil;
39       userRights = [NSMutableArray new];
40       defaultUserID = nil;
41     }
42
43   return self;
44 }
45
46 - (void) dealloc
47 {
48   [uid release];
49   [userRights release];
50   [defaultUserID release];
51   [super dealloc];
52 }
53
54 - (NSString *) uid
55 {
56   return uid;
57 }
58
59 - (BOOL) userIsDefaultUser
60 {
61   if (!defaultUserID)
62     ASSIGN (defaultUserID, [[self clientObject] defaultUserID]);
63
64   return [uid isEqualToString: defaultUserID];
65 }
66
67 - (NSString *) userDisplayName
68 {
69   LDAPUserManager *um;
70
71   um = [LDAPUserManager sharedUserManager];
72
73   return [NSString stringWithFormat: @"%@ <%@>",
74                    [um getCNForUID: uid],
75                    [um getEmailForUID: uid]];
76 }
77
78 - (BOOL) _initRights
79 {
80   BOOL response;
81   NSString *newUID;
82   LDAPUserManager *um;
83   SOGoObject *clientObject;
84
85   response = NO;
86
87   newUID = [[context request] formValueForKey: @"uid"];
88   if ([newUID length] > 0)
89     {
90       if (!defaultUserID)
91         ASSIGN (defaultUserID, [[self clientObject] defaultUserID]);
92
93       um = [LDAPUserManager sharedUserManager];
94       if ([newUID isEqualToString: defaultUserID]
95           || [[um getEmailForUID: newUID] length] > 0)
96         {
97           ASSIGN (uid, newUID);
98           clientObject = [self clientObject];
99           [userRights addObjectsFromArray: [clientObject aclsForUser: uid]];
100
101           response = YES;
102         }
103     }
104
105   return response;
106 }
107
108 - (id <WOActionResults>) defaultAction
109 {
110   id <WOActionResults> response;
111
112   if (![self _initRights])
113     response = [NSException exceptionWithHTTPStatus: 403
114                             reason: @"No such user."];
115   else
116     {
117       [self prepareRightsForm];
118       response = self;
119     }
120
121   return response;
122 }
123
124 - (id <WOActionResults>) saveUserRightsAction
125 {
126   id <WOActionResults> response;
127
128   if (![self _initRights])
129     response = [NSException exceptionWithHTTPStatus: 403
130                             reason: @"No such user."];
131   else
132     {
133       [self updateRights];
134       [[self clientObject] setRoles: userRights forUser: uid];
135       response = [self jsCloseWithRefreshMethod: nil];
136     }
137
138   return response;
139 }
140
141 - (void) appendRight: (NSString *) newRight
142 {
143   if (![userRights containsObject: newRight])
144     [userRights addObject: newRight];
145 }
146
147 - (void) removeRight: (NSString *) right
148 {
149   if ([userRights containsObject: right])
150     [userRights removeObject: right];
151 }
152
153 - (void) appendExclusiveRight: (NSString *) newRight
154                      fromList: (NSArray *) list
155 {
156   [userRights removeObjectsInArray: list];
157   [self appendRight: newRight];
158 }
159
160 - (void) removeAllRightsFromList: (NSArray *) list
161 {
162   [userRights removeObjectsInArray: list];
163 }
164
165 - (void) prepareRightsForm
166 {
167 }
168
169 - (void) updateRights
170 {
171   [self subclassResponsibility: _cmd];
172 }
173
174 @end