2 * Copyright 2011-2012 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 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;
42 import gr.grnet.pithos.web.client.grouptree.Group;
44 import java.util.List;
46 import com.google.gwt.dom.client.NativeEvent;
47 import com.google.gwt.event.dom.client.ClickEvent;
48 import com.google.gwt.event.dom.client.ClickHandler;
49 import com.google.gwt.event.dom.client.KeyCodes;
50 import com.google.gwt.regexp.shared.RegExp;
51 import com.google.gwt.user.client.Event.NativePreviewEvent;
52 import com.google.gwt.user.client.ui.Anchor;
53 import com.google.gwt.user.client.ui.Button;
54 import com.google.gwt.user.client.ui.DialogBox;
55 import com.google.gwt.user.client.ui.FlexTable;
56 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
57 import com.google.gwt.user.client.ui.ListBox;
58 import com.google.gwt.user.client.ui.RadioButton;
59 import com.google.gwt.user.client.ui.TextBox;
60 import com.google.gwt.user.client.ui.VerticalPanel;
62 public class PermissionsAddDialog extends DialogBox {
63 final static RegExp EmailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i");
65 private TextBox userBox = new TextBox();
67 private ListBox groupBox = new ListBox();
69 private RadioButton read = new RadioButton("permissions");
71 private RadioButton write = new RadioButton("permissions");
73 private PermissionsList permList;
79 public PermissionsAddDialog(Pithos _app, List<Group> _groups, PermissionsList _permList, boolean _userAdd) {
84 Anchor close = new Anchor("close");
85 close.addStyleName("close");
86 close.addClickHandler(new ClickHandler() {
89 public void onClick(ClickEvent event) {
93 setText("Add permission");
94 setStyleName("pithos-DialogBox");
96 final VerticalPanel panel = new VerticalPanel();
99 VerticalPanel inner = new VerticalPanel();
100 inner.addStyleName("inner");
102 final FlexTable permTable = new FlexTable();
103 permTable.setText(0, 0, "Users/Groups");
104 permTable.setText(0, 1, "Read Only");
105 permTable.setText(0, 2, "Read/Write");
106 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
107 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
108 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
111 permTable.setWidget(1, 0, userBox);
114 for (Group group : _groups) {
115 groupBox.addItem(group.getName(), group.getName());
117 permTable.setWidget(1, 0, groupBox);
121 permTable.setWidget(1, 1, read);
122 permTable.setWidget(1, 2, write);
124 permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
125 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
126 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
127 inner.add(permTable);
129 final Button ok = new Button("OK", new ClickHandler() {
131 public void onClick(ClickEvent event) {
137 ok.addStyleName("button");
141 panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
146 protected void addPermission() {
147 final boolean readValue = read.getValue();
148 final boolean writeValue = write.getValue();
150 String selected = null;
152 final String userDisplayName = userBox.getText().trim();
153 addUserPermission(userDisplayName, readValue, writeValue);
155 } else if (groupBox.getSelectedIndex() > -1) {
156 String groupName = groupBox.getValue(groupBox.getSelectedIndex());
157 selected = app.getUserID() + ":" + groupName;
159 if (permList.getPermissions().get(selected) != null) {
162 if (selected == null || selected.length() == 0 || selected.equals(app.getUserID() + ":")) {
163 app.displayWarning("You have to select a username or group");
167 permList.addPermission(selected, readValue, writeValue);
170 private boolean alreadyHasPermission(String selected) {
171 return permList.getPermissions().get(selected) != null;
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");
180 // Now get the userID
181 final String userID = app.getUserIDForDisplayName(userDisplayName);
183 // Check if already have the permission
184 if(!alreadyHasPermission(userID)) {
185 permList.addPermission(userID, readValue, writeValue);
189 // Must call server to obtain userID
190 new GetUserCatalogs(app, null, Helpers.toList(userDisplayName)) {
192 public void onSuccess(Request request, Response response, JSONObject result, UserCatalogs userCatalogs) {
193 app.getUserCatalogs().updateFrom(userCatalogs);
194 final String userID = app.getUserIDForDisplayName(userDisplayName);
196 app.displayWarning("Unknown user " + userDisplayName);
198 else if(!alreadyHasPermission(userID)) {
199 permList.addPermission(userID, readValue, writeValue);
202 }.scheduleDeferred();
207 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
208 super.onPreviewNativeEvent(preview);
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:
219 case KeyCodes.KEY_ESCAPE:
227 public void center() {
230 userBox.setFocus(true);