Revision 8d09767f src/gr/grnet/pithos/web/client/PermissionsAddDialog.java
b/src/gr/grnet/pithos/web/client/PermissionsAddDialog.java | ||
---|---|---|
34 | 34 |
*/ |
35 | 35 |
package gr.grnet.pithos.web.client; |
36 | 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.UserCatalogs; |
|
37 | 42 |
import gr.grnet.pithos.web.client.grouptree.Group; |
38 | 43 |
|
39 | 44 |
import java.util.List; |
... | ... | |
55 | 60 |
import com.google.gwt.user.client.ui.VerticalPanel; |
56 | 61 |
|
57 | 62 |
public class PermissionsAddDialog extends DialogBox { |
63 |
final static RegExp EmailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i"); |
|
58 | 64 |
|
59 | 65 |
private TextBox userBox = new TextBox(); |
60 | 66 |
|
... | ... | |
105 | 111 |
permTable.setWidget(1, 0, userBox); |
106 | 112 |
} |
107 | 113 |
else { |
108 |
for (Group group : _groups) |
|
114 |
for (Group group : _groups) {
|
|
109 | 115 |
groupBox.addItem(group.getName(), group.getName()); |
116 |
} |
|
110 | 117 |
permTable.setWidget(1, 0, groupBox); |
111 | 118 |
} |
112 | 119 |
|
... | ... | |
137 | 144 |
} |
138 | 145 |
|
139 | 146 |
protected void addPermission() { |
147 |
final boolean readValue = read.getValue(); |
|
148 |
final boolean writeValue = write.getValue(); |
|
149 |
|
|
140 | 150 |
String selected = null; |
141 | 151 |
if (userAdd) { |
142 |
selected = userBox.getText().trim(); |
|
143 |
RegExp emailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i"); |
|
144 |
if (!emailValidator.test(selected)) { |
|
145 |
app.displayWarning("Username must be a valid email address"); |
|
146 |
return; |
|
147 |
} |
|
152 |
final String userDisplayName = userBox.getText().trim(); |
|
153 |
addUserPermission(userDisplayName, readValue, writeValue); |
|
154 |
return; |
|
148 | 155 |
} else if (groupBox.getSelectedIndex() > -1) { |
149 | 156 |
String groupName = groupBox.getValue(groupBox.getSelectedIndex()); |
150 | 157 |
selected = app.getUserID() + ":" + groupName; |
... | ... | |
157 | 164 |
return; |
158 | 165 |
} |
159 | 166 |
|
160 |
boolean readValue = read.getValue(); |
|
161 |
boolean writeValue = write.getValue(); |
|
162 |
|
|
163 | 167 |
permList.addPermission(selected, readValue, writeValue); |
164 | 168 |
} |
165 | 169 |
|
170 |
private boolean alreadyHasPermission(String selected) { |
|
171 |
return permList.getPermissions().get(selected) != null; |
|
172 |
} |
|
173 |
|
|
174 |
private void addUserPermission(final String userDisplayName, final boolean readValue, final boolean writeValue) { |
|
175 |
if (!EmailValidator.test(userDisplayName)) { |
|
176 |
app.displayWarning("Username must be a valid email address"); |
|
177 |
return; |
|
178 |
} |
|
179 |
|
|
180 |
// Now get the userID |
|
181 |
final String userID = app.getUserIDForDisplayName(userDisplayName); |
|
182 |
if(userID != null) { |
|
183 |
// Check if already have the permission |
|
184 |
if(!alreadyHasPermission(userID)) { |
|
185 |
permList.addPermission(userID, readValue, writeValue); |
|
186 |
} |
|
187 |
} |
|
188 |
else { |
|
189 |
// Must call server to obtain userID |
|
190 |
new GetUserCatalogs(app, null, Helpers.toList(userDisplayName)) { |
|
191 |
@Override |
|
192 |
public void onSuccess(Request request, Response response, JSONObject result, UserCatalogs userCatalogs) { |
|
193 |
app.getUserCatalogs().updateFrom(userCatalogs); |
|
194 |
final String userID = app.getUserIDForDisplayName(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 |
|
|
166 | 206 |
@Override |
167 | 207 |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
168 | 208 |
super.onPreviewNativeEvent(preview); |
Also available in: Unified diff