root / src / gr / grnet / pithos / web / client / PermissionsAddDialog.java @ 0634d911
History | View | Annotate | Download (8.1 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2011-2012 GRNET S.A. All rights reserved.
|
3 |
*
|
4 |
* Redistribution and use in source and binary forms, with or
|
5 |
* without modification, are permitted provided that the following
|
6 |
* conditions are met:
|
7 |
*
|
8 |
* 1. Redistributions of source code must retain the above
|
9 |
* copyright notice, this list of conditions and the following
|
10 |
* disclaimer.
|
11 |
*
|
12 |
* 2. Redistributions in binary form must reproduce the above
|
13 |
* copyright notice, this list of conditions and the following
|
14 |
* disclaimer in the documentation and/or other materials
|
15 |
* provided with the distribution.
|
16 |
*
|
17 |
* THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 |
* POSSIBILITY OF SUCH DAMAGE.
|
29 |
*
|
30 |
* The views and conclusions contained in the software and
|
31 |
* documentation are those of the authors and should not be
|
32 |
* interpreted as representing official policies, either expressed
|
33 |
* or implied, of GRNET S.A.
|
34 |
*/
|
35 |
package gr.grnet.pithos.web.client; |
36 |
|
37 |
import com.google.gwt.http.client.Request; |
38 |
import com.google.gwt.http.client.Response; |
39 |
import com.google.gwt.json.client.JSONObject; |
40 |
import gr.grnet.pithos.web.client.catalog.GetUserCatalogs; |
41 |
import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs; |
42 |
import gr.grnet.pithos.web.client.catalog.UserCatalogs; |
43 |
import gr.grnet.pithos.web.client.grouptree.Group; |
44 |
|
45 |
import java.util.List; |
46 |
|
47 |
import com.google.gwt.dom.client.NativeEvent; |
48 |
import com.google.gwt.event.dom.client.ClickEvent; |
49 |
import com.google.gwt.event.dom.client.ClickHandler; |
50 |
import com.google.gwt.event.dom.client.KeyCodes; |
51 |
import com.google.gwt.regexp.shared.RegExp; |
52 |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
53 |
import com.google.gwt.user.client.ui.Anchor; |
54 |
import com.google.gwt.user.client.ui.Button; |
55 |
import com.google.gwt.user.client.ui.DialogBox; |
56 |
import com.google.gwt.user.client.ui.FlexTable; |
57 |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
58 |
import com.google.gwt.user.client.ui.ListBox; |
59 |
import com.google.gwt.user.client.ui.RadioButton; |
60 |
import com.google.gwt.user.client.ui.TextBox; |
61 |
import com.google.gwt.user.client.ui.VerticalPanel; |
62 |
|
63 |
public class PermissionsAddDialog extends DialogBox { |
64 |
final static RegExp EmailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i"); |
65 |
|
66 |
private TextBox userBox = new TextBox(); |
67 |
|
68 |
private ListBox groupBox = new ListBox(); |
69 |
|
70 |
private RadioButton read = new RadioButton("permissions"); |
71 |
|
72 |
private RadioButton write = new RadioButton("permissions"); |
73 |
|
74 |
private PermissionsList permList;
|
75 |
|
76 |
boolean userAdd;
|
77 |
|
78 |
private Pithos app;
|
79 |
|
80 |
public PermissionsAddDialog(Pithos _app, List<Group> _groups, PermissionsList _permList, boolean _userAdd) { |
81 |
app = _app; |
82 |
userAdd = _userAdd; |
83 |
permList = _permList; |
84 |
|
85 |
Anchor close = new Anchor("close"); |
86 |
close.addStyleName("close");
|
87 |
close.addClickHandler(new ClickHandler() {
|
88 |
|
89 |
@Override
|
90 |
public void onClick(ClickEvent event) { |
91 |
hide(); |
92 |
} |
93 |
}); |
94 |
setText("Add permission");
|
95 |
setStyleName("pithos-DialogBox");
|
96 |
|
97 |
final VerticalPanel panel = new VerticalPanel(); |
98 |
panel.add(close); |
99 |
|
100 |
VerticalPanel inner = new VerticalPanel();
|
101 |
inner.addStyleName("inner");
|
102 |
|
103 |
final FlexTable permTable = new FlexTable(); |
104 |
permTable.setText(0, 0, "Users/Groups"); |
105 |
permTable.setText(0, 1, "Read Only"); |
106 |
permTable.setText(0, 2, "Read/Write"); |
107 |
permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels"); |
108 |
permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels"); |
109 |
permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels"); |
110 |
|
111 |
if (userAdd) {
|
112 |
permTable.setWidget(1, 0, userBox); |
113 |
} |
114 |
else {
|
115 |
for (Group group : _groups) { |
116 |
groupBox.addItem(group.getName(), group.getName()); |
117 |
} |
118 |
permTable.setWidget(1, 0, groupBox); |
119 |
} |
120 |
|
121 |
read.setValue(true);
|
122 |
permTable.setWidget(1, 1, read); |
123 |
permTable.setWidget(1, 2, write); |
124 |
|
125 |
permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels"); |
126 |
permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER); |
127 |
permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER); |
128 |
inner.add(permTable); |
129 |
|
130 |
final Button ok = new Button("OK", new ClickHandler() { |
131 |
@Override
|
132 |
public void onClick(ClickEvent event) { |
133 |
addPermission(); |
134 |
hide(); |
135 |
} |
136 |
}); |
137 |
|
138 |
ok.addStyleName("button");
|
139 |
inner.add(ok); |
140 |
|
141 |
panel.add(inner); |
142 |
panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER); |
143 |
|
144 |
setWidget(panel); |
145 |
} |
146 |
|
147 |
protected void addPermission() { |
148 |
final boolean readValue = read.getValue(); |
149 |
final boolean writeValue = write.getValue(); |
150 |
|
151 |
String selected = null; |
152 |
if (userAdd) {
|
153 |
final String userDisplayName = userBox.getText().trim(); |
154 |
addUserPermission(userDisplayName, readValue, writeValue); |
155 |
return;
|
156 |
} else if (groupBox.getSelectedIndex() > -1) { |
157 |
String groupName = groupBox.getValue(groupBox.getSelectedIndex());
|
158 |
selected = app.getUserID() + ":" + groupName;
|
159 |
} |
160 |
if (permList.getPermissions().get(selected) != null) { |
161 |
return;
|
162 |
} |
163 |
if (selected == null || selected.length() == 0 || selected.equals(app.getUserID() + ":")) { |
164 |
app.displayWarning("You have to select a username or group");
|
165 |
return;
|
166 |
} |
167 |
|
168 |
permList.addPermission(selected, readValue, writeValue); |
169 |
} |
170 |
|
171 |
private boolean alreadyHasPermission(String selected) { |
172 |
return permList.getPermissions().get(selected) != null; |
173 |
} |
174 |
|
175 |
private void addUserPermission(final String userDisplayName, final boolean readValue, final boolean writeValue) { |
176 |
if (!EmailValidator.test(userDisplayName)) {
|
177 |
app.displayWarning("Username must be a valid email address");
|
178 |
return;
|
179 |
} |
180 |
|
181 |
// Now get the userID
|
182 |
final String userID = app.getUserIDForDisplayName(userDisplayName); |
183 |
if(userID != null) { |
184 |
// Check if already have the permission
|
185 |
if(!alreadyHasPermission(userID)) {
|
186 |
permList.addPermission(userID, readValue, writeValue); |
187 |
} |
188 |
} |
189 |
else {
|
190 |
// Must call server to obtain userID
|
191 |
new UpdateUserCatalogs(app, null, Helpers.toList(userDisplayName)) { |
192 |
@Override
|
193 |
public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) { |
194 |
final String userID = updatedUserCatalogs.getUserID(userDisplayName); |
195 |
if(userID == null) { |
196 |
app.displayWarning("Unknown user " + userDisplayName);
|
197 |
} |
198 |
else if(!alreadyHasPermission(userID)) { |
199 |
permList.addPermission(userID, readValue, writeValue); |
200 |
} |
201 |
} |
202 |
}.scheduleDeferred(); |
203 |
} |
204 |
} |
205 |
|
206 |
@Override
|
207 |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
208 |
super.onPreviewNativeEvent(preview);
|
209 |
|
210 |
NativeEvent evt = preview.getNativeEvent(); |
211 |
if (evt.getType().equals("keydown")) |
212 |
// Use the popup's key preview hooks to close the dialog when either
|
213 |
// enter or escape is pressed.
|
214 |
switch (evt.getKeyCode()) {
|
215 |
case KeyCodes.KEY_ENTER:
|
216 |
addPermission(); |
217 |
hide(); |
218 |
break;
|
219 |
case KeyCodes.KEY_ESCAPE:
|
220 |
hide(); |
221 |
break;
|
222 |
} |
223 |
} |
224 |
|
225 |
|
226 |
@Override
|
227 |
public void center() { |
228 |
super.center();
|
229 |
if (userAdd)
|
230 |
userBox.setFocus(true);
|
231 |
} |
232 |
} |