]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxCalUserRightsEditor.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1236 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Scheduler / UIxCalUserRightsEditor.m
1 /* UIxCalUserRightsEditor.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 <Foundation/NSArray.h>
24 #import <Foundation/NSEnumerator.h>
25 #import <NGObjWeb/WORequest.h>
26 #import <SoObjects/SOGo/SOGoPermissions.h>
27
28 #import "UIxCalUserRightsEditor.h"
29
30 @implementation UIxCalUserRightsEditor
31
32 - (id) init
33 {
34   if ((self = [super init]))
35     {
36       currentRight = nil;
37       currentRightType = nil;
38       rights = [NSMutableDictionary new];
39       [rights setObject: @"None" forKey: @"Public"];
40       [rights setObject: @"None" forKey: @"Private"];
41       [rights setObject: @"None" forKey: @"Confidential"];
42     }
43
44   return self;
45 }
46
47 - (void) dealloc
48 {
49   [currentRight release];
50   [currentRightType release];
51   [rights release];
52   [super dealloc];
53 }
54
55 - (void) prepareRightsForm
56 {
57   NSEnumerator *roles, *types;
58   NSString *role, *type;
59   unsigned int length;
60
61   roles = [userRights objectEnumerator];
62   role = [roles nextObject];
63   while (role)
64     {
65       types = [[self rightTypes] objectEnumerator];
66       type = [types nextObject];
67       while (type)
68         {
69           if ([role hasPrefix: type])
70             {
71               length = [type length];
72               [rights setObject: [role substringFromIndex: length]
73                       forKey: type];
74             }
75           type = [types nextObject];
76         }
77       role = [roles nextObject];
78     }
79 }
80
81 - (NSArray *) _rightsForType: (NSString *) type
82 {
83   NSMutableArray *rightsForType;
84   NSEnumerator *commonRights;
85   NSString *currentCommonRight;
86
87   rightsForType = [NSMutableArray new];
88   [rightsForType autorelease];
89   commonRights = [[self objectRights] objectEnumerator];
90   currentCommonRight = [commonRights nextObject];
91   while (currentCommonRight)
92     {
93       [rightsForType addObject: [NSString stringWithFormat: @"%@%@",
94                                           type, currentCommonRight]];
95       currentCommonRight = [commonRights nextObject];
96     }
97
98   return rightsForType;
99 }
100
101 - (void) updateRights
102 {
103   NSEnumerator *types;
104   NSString *currentType, *currentValue;
105   NSArray *rightsForType;
106   WORequest *request;
107
108   request = [context request];
109   types = [[self rightTypes] objectEnumerator];
110   currentType = [types nextObject];
111   while (currentType)
112     {
113       rightsForType = [self _rightsForType: currentType];
114       currentValue
115         = [request formValueForKey:
116                      [NSString stringWithFormat: @"%@Right", currentType]];
117       if ([currentValue isEqualToString: @"None"])
118         [self removeAllRightsFromList: rightsForType];
119       else
120         [self appendExclusiveRight: [NSString stringWithFormat: @"%@%@",
121                                               currentType, currentValue]
122               fromList: rightsForType];
123       currentType = [types nextObject];
124     }
125
126   if ([[request formValueForKey: @"ObjectCreator"] length] > 0)
127     [self appendRight: SOGoRole_ObjectCreator];
128   else
129     [self removeRight: SOGoRole_ObjectCreator];
130
131   if ([[request formValueForKey: @"ObjectEraser"] length] > 0)
132     [self appendRight: SOGoRole_ObjectEraser];
133   else
134     [self removeRight: SOGoRole_ObjectEraser];
135 }
136
137 - (NSArray *) objectRights
138 {
139   return
140     [NSArray arrayWithObjects:
141                @"Viewer", @"DAndTViewer", @"Modifier", @"Responder", @"None", nil];
142 }
143
144 - (void) setCurrentRight: (NSString *) newCurrentRight
145 {
146   ASSIGN (currentRight, newCurrentRight);
147 }
148
149 - (NSString *) currentRight
150 {
151   return currentRight;
152 }
153
154 - (NSArray *) rightTypes
155 {
156   return
157     [NSArray arrayWithObjects: @"Public", @"Confidential", @"Private", nil];
158 }
159
160 - (void) setCurrentRightType: (NSString *) newCurrentRightType
161 {
162   ASSIGN (currentRightType, newCurrentRightType);
163 }
164
165 - (NSString *) currentRightType
166 {
167   return currentRightType;
168 }
169
170 - (NSString *) currentRightTypeLabel
171 {
172   return [self labelForKey:
173                  [NSString stringWithFormat: @"label_%@", currentRightType]];
174 }
175
176 - (NSString *) currentRightTypeName
177 {
178   return [NSString stringWithFormat: @"%@Right", currentRightType];
179 }
180
181 - (NSString *) currentRightSelection
182 {
183   return [rights objectForKey: currentRightType];
184 }
185
186 - (void) setUserCanCreateObjects: (BOOL) userCanCreateObjects
187 {
188   [self appendRight: SOGoRole_ObjectCreator];
189 }
190
191 - (BOOL) userCanCreateObjects
192 {
193   return [userRights containsObject: SOGoRole_ObjectCreator];
194 }
195
196 - (void) setUserCanEraseObjects: (BOOL) userCanEraseObjects
197 {
198   [self appendRight: SOGoRole_ObjectEraser];
199 }
200
201 - (BOOL) userCanEraseObjects
202 {
203   return [userRights containsObject: SOGoRole_ObjectEraser];
204 }
205
206 @end