2 * Copyright 2011-2013 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.dom.client.NativeEvent;
38 import com.google.gwt.event.dom.client.ClickEvent;
39 import com.google.gwt.event.dom.client.ClickHandler;
40 import com.google.gwt.event.dom.client.KeyCodes;
41 import com.google.gwt.regexp.shared.RegExp;
42 import com.google.gwt.user.client.Event.NativePreviewEvent;
43 import com.google.gwt.user.client.ui.*;
44 import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
45 import gr.grnet.pithos.web.client.catalog.UserCatalogs;
46 import gr.grnet.pithos.web.client.grouptree.Group;
48 import java.util.List;
50 public class PermissionsAddDialog extends DialogBox {
51 final static RegExp EmailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i");
53 private TextBox userBox = new TextBox();
55 private ListBox groupBox = new ListBox();
57 private RadioButton read = new RadioButton("permissions");
59 private RadioButton write = new RadioButton("permissions");
61 private final PermissionsList permList;
63 private final boolean isUser;
65 private final Pithos app;
67 public PermissionsAddDialog(Pithos app, List<Group> groups, PermissionsList permList, boolean isUser) {
70 this.permList = permList;
72 Anchor close = new Anchor("close");
73 close.addStyleName("close");
74 close.addClickHandler(new ClickHandler() {
76 public void onClick(ClickEvent event) {
80 final String dialogText = isUser ? Const.TXT_ADD_USER : Const.TXT_ADD_GROUP;
82 setStyleName("pithos-DialogBox");
84 final VerticalPanel panel = new VerticalPanel();
87 VerticalPanel inner = new VerticalPanel();
88 inner.addStyleName("inner");
90 final FlexTable permTable = new FlexTable();
91 permTable.setText(0, 0, isUser ? Const.TXT_USER : Const.TXT_USER);
92 permTable.setText(0, 1, "Read Only");
93 permTable.setText(0, 2, "Read/Write");
94 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
95 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
96 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
99 permTable.setWidget(1, 0, userBox);
102 for(Group group : groups) {
103 groupBox.addItem(group.getName(), group.getName());
105 permTable.setWidget(1, 0, groupBox);
109 permTable.setWidget(1, 1, read);
110 permTable.setWidget(1, 2, write);
112 permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
113 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
114 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
115 inner.add(permTable);
117 final Button ok = new Button("OK", new ClickHandler() {
119 public void onClick(ClickEvent event) {
125 ok.addStyleName("button");
129 panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
134 protected void addPermission() {
135 final boolean readValue = read.getValue();
136 final boolean writeValue = write.getValue();
138 String selected = null;
140 final String userDisplayName = userBox.getText().trim();
141 addUserPermission(userDisplayName, readValue, writeValue);
144 else if(groupBox.getSelectedIndex() > -1) {
145 String groupName = groupBox.getValue(groupBox.getSelectedIndex());
146 selected = app.getUserID() + ":" + groupName;
148 if(permList.getPermissions().get(selected) != null) {
151 if(selected == null || selected.length() == 0 || selected.equals(app.getUserID() + ":")) {
152 app.displayWarning("You have to select a username or group");
156 permList.addPermission(selected, readValue, writeValue);
159 private boolean alreadyHasPermission(String selected) {
160 return permList.getPermissions().get(selected) != null;
163 private void addUserPermission(final String userDisplayName, final boolean readValue, final boolean writeValue) {
164 if(!EmailValidator.test(userDisplayName)) {
165 app.displayWarning("Username must be a valid email address");
169 // Now get the userID
170 final String userID = app.getIDForUserDisplayName(userDisplayName);
172 // Check if already have the permission
173 if(!alreadyHasPermission(userID)) {
174 permList.addPermission(userID, readValue, writeValue);
178 // Must call server to obtain userID
179 new UpdateUserCatalogs(app, null, Helpers.toList(userDisplayName)) {
181 public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
182 final String userID = updatedUserCatalogs.getID(userDisplayName);
184 Pithos.LOG("PermissionsDialog::addUserPermission(), UpdateUserCatalogs() => Unknown user ", userDisplayName);
185 app.displayWarning("Unknown user " + userDisplayName);
187 else if(!alreadyHasPermission(userID)) {
188 permList.addPermission(userID, readValue, writeValue);
191 }.scheduleDeferred();
196 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
197 super.onPreviewNativeEvent(preview);
199 NativeEvent evt = preview.getNativeEvent();
200 if(evt.getType().equals("keydown"))
201 // Use the popup's key preview hooks to close the dialog when either
202 // enter or escape is pressed.
204 switch(evt.getKeyCode()) {
205 case KeyCodes.KEY_ENTER:
209 case KeyCodes.KEY_ESCAPE:
218 public void center() {
221 userBox.setFocus(true);