2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
35 package gr.grnet.pithos.web.client;
37 import gr.grnet.pithos.web.client.rest.GetCommand;
38 import gr.grnet.pithos.web.client.rest.RestException;
39 import gr.grnet.pithos.web.client.rest.resource.GroupResource;
40 import gr.grnet.pithos.web.client.rest.resource.PermissionHolder;
41 import gr.grnet.pithos.web.client.rest.resource.UserResource;
42 import gr.grnet.pithos.web.client.rest.resource.UserSearchResource;
44 import java.util.List;
46 import com.google.gwt.core.client.GWT;
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.FocusEvent;
51 import com.google.gwt.event.dom.client.FocusHandler;
52 import com.google.gwt.event.dom.client.KeyCodes;
53 import com.google.gwt.event.dom.client.KeyUpEvent;
54 import com.google.gwt.event.dom.client.KeyUpHandler;
55 import com.google.gwt.http.client.URL;
56 import com.google.gwt.user.client.DeferredCommand;
57 import com.google.gwt.user.client.Event.NativePreviewEvent;
58 import com.google.gwt.user.client.ui.Button;
59 import com.google.gwt.user.client.ui.CheckBox;
60 import com.google.gwt.user.client.ui.DialogBox;
61 import com.google.gwt.user.client.ui.FlexTable;
62 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
63 import com.google.gwt.user.client.ui.HorizontalPanel;
64 import com.google.gwt.user.client.ui.ListBox;
65 import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
66 import com.google.gwt.user.client.ui.SuggestBox;
67 import com.google.gwt.user.client.ui.VerticalPanel;
69 public class PermissionsAddDialog extends DialogBox {
71 private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
72 private SuggestBox suggestBox = new SuggestBox(oracle);
74 private String selectedUser = null;
76 private List<GroupResource> groups;
78 private ListBox groupBox = new ListBox();
80 private CheckBox read = new CheckBox();
82 private CheckBox write = new CheckBox();
84 private CheckBox modifyACL = new CheckBox();
86 private final PermissionsList permList;
92 public PermissionsAddDialog(Pithos _app, List<GroupResource> _groups, PermissionsList _permList, boolean _userAdd) {
98 groupBox.getElement().setId("addPermission.dropDown");
100 suggestBox.getElement().setId("addPermission.textBox");
102 read.getElement().setId("addPermission.read");
104 write.getElement().setId("addPermission.write");
106 modifyACL.getElement().setId("addpermission.modify");
108 for (GroupResource group : _groups)
109 groupBox.addItem(group.getName(), group.getName());
110 final VerticalPanel panel = new VerticalPanel();
111 final HorizontalPanel buttons = new HorizontalPanel();
113 final FlexTable permTable = new FlexTable();
114 permTable.setText(0, 0, "Users/Groups");
115 permTable.setText(0, 1, "Read");
116 permTable.setText(0, 2, "Write");
117 permTable.setText(0, 3, "Modify Access");
118 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
119 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
120 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
121 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
123 suggestBox.getTextBox().addFocusHandler(new FocusHandler() {
126 public void onFocus(FocusEvent event) {
127 if (selectedUser != null && selectedUser.endsWith("@"))
133 suggestBox.addKeyUpHandler(new KeyUpHandler() {
136 public void onKeyUp(KeyUpEvent event) {
137 // Ignore the arrow keys.
138 int keyCode = event.getNativeKeyCode();
139 if (keyCode == KeyCodes.KEY_UP ||
140 keyCode == KeyCodes.KEY_DOWN ||
141 keyCode == KeyCodes.KEY_LEFT ||
142 keyCode == KeyCodes.KEY_RIGHT)
144 if (keyCode==KeyCodes.KEY_ESCAPE) {
145 suggestBox.hideSuggestionList();
148 String text = suggestBox.getText().trim();
149 // Avoid useless queries for keystrokes that do not modify
151 if (text.equals(selectedUser))
154 // Go to the server only if the user typed the @ character.
155 if (selectedUser.endsWith("@"))
159 permTable.setWidget(1, 0, suggestBox);
161 permTable.setWidget(1, 0, groupBox);
162 permTable.setWidget(1, 1, read);
163 permTable.setWidget(1, 2, write);
164 permTable.setWidget(1, 3, modifyACL);
166 permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
167 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
168 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
169 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 3, HasHorizontalAlignment.ALIGN_CENTER);
170 panel.add(permTable);
172 final Button ok = new Button("OK", new ClickHandler() {
174 public void onClick(ClickEvent event) {
179 ok.getElement().setId("addPermission.button.ok");
181 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
182 // Create the 'Cancel' button, along with a listener that hides the
184 // when the button is clicked.
185 final Button cancel = new Button("Cancel", new ClickHandler() {
187 public void onClick(ClickEvent event) {
191 cancel.getElement().setId("addPermission.button.cancel");
193 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
194 buttons.setSpacing(8);
195 buttons.addStyleName("pithos-TabPanelBottom");
197 panel.addStyleName("pithos-TabPanelBottom");
200 private void addPermission() {
201 PermissionHolder perm = new PermissionHolder();
203 selectedUser = suggestBox.getText();
204 for(PermissionHolder p : permList.permissions)
205 if (selectedUser.equals(p.getUser())){
206 app.displayError("User already has access to the resource");
209 perm.setUser(selectedUser);
211 String groupId = groupBox.getValue(groupBox.getSelectedIndex());
212 GroupResource selected = null;
213 for (GroupResource g : groups)
214 if (g.getName().equals(groupId))
216 if (selected == null)
218 for(PermissionHolder p : permList.permissions)
219 if (selected.getName().equals(p.getGroup())){
220 app.displayError("Group already has access to the resource");
223 perm.setGroup(selected.getName());
225 boolean readValue = read.getValue();
226 boolean writeValue = write.getValue();
227 boolean modifyValue = modifyACL.getValue();
229 perm.setRead(readValue);
230 perm.setWrite(writeValue);
231 perm.setModifyACL(modifyValue);
232 permList.addPermission(perm);
233 permList.updateTable();
237 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
238 super.onPreviewNativeEvent(preview);
240 NativeEvent evt = preview.getNativeEvent();
241 if (evt.getType().equals("keydown"))
242 // Use the popup's key preview hooks to close the dialog when either
243 // enter or escape is pressed.
244 switch (evt.getKeyCode()) {
245 case KeyCodes.KEY_ENTER:
249 case KeyCodes.KEY_ESCAPE:
257 public void center() {
260 suggestBox.setFocus(true);
264 * Update the list of suggestions.
266 protected void updateSuggestions() {
267 String query = selectedUser.substring(0, selectedUser.length()-1);
268 GWT.log("Searching for " + query, null);
270 GetCommand<UserSearchResource> eg = new GetCommand<UserSearchResource>(app, UserSearchResource.class,
271 app.getApiPath() + "users/" + URL.encodeComponent(query), false, null) {
274 public void onComplete() {
275 suggestBox.hideSuggestionList();
277 UserSearchResource s = getResult();
278 for (UserResource user : s.getUsers()) {
279 GWT.log("Found " + user.getUsername(), null);
280 oracle.add(user.getUsername());
282 suggestBox.showSuggestionList();
286 public void onError(Throwable t) {
287 if(t instanceof RestException)
288 app.displayError("Unable to perform search: "+((RestException)t).getHttpStatusText());
290 app.displayError("System error while searching for users: "+t.getMessage());
292 DisplayHelper.log(t.getMessage());
296 DeferredCommand.addCommand(eg);