4913fbaab4daf295b1bc9a95a219ad512bbac649
[pithos-web-client] / src / gr / grnet / pithos / web / client / PermissionsAddDialog.java
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.UserCatalogs;
42 import gr.grnet.pithos.web.client.grouptree.Group;
43
44 import java.util.List;
45
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;
61
62 public class PermissionsAddDialog extends DialogBox {
63     final static RegExp EmailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i");
64
65         private TextBox userBox = new TextBox();
66
67         private ListBox groupBox = new ListBox();
68
69         private RadioButton read = new RadioButton("permissions");
70
71         private RadioButton write = new RadioButton("permissions");
72
73         private PermissionsList permList;
74
75         boolean userAdd;
76
77     private Pithos app;
78
79         public PermissionsAddDialog(Pithos _app, List<Group> _groups, PermissionsList _permList, boolean _userAdd) {
80         app = _app;
81                 userAdd = _userAdd;
82                 permList = _permList;
83
84                 Anchor close = new Anchor("close");
85                 close.addStyleName("close");
86                 close.addClickHandler(new ClickHandler() {
87                         
88                         @Override
89                         public void onClick(ClickEvent event) {
90                                 hide();
91                         }
92                 });
93                 setText("Add permission");
94                 setStyleName("pithos-DialogBox");
95
96         final VerticalPanel panel = new VerticalPanel();
97         panel.add(close);
98
99         VerticalPanel inner = new VerticalPanel();
100                 inner.addStyleName("inner");
101
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");
109
110         if (userAdd) {
111             permTable.setWidget(1, 0, userBox);
112         }
113         else {
114             for (Group group : _groups) {
115                 groupBox.addItem(group.getName(), group.getName());
116             }
117             permTable.setWidget(1, 0, groupBox);
118         }
119                 
120         read.setValue(true);
121         permTable.setWidget(1, 1, read);
122         permTable.setWidget(1, 2, write);
123
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);
128
129         final Button ok = new Button("OK", new ClickHandler() {
130             @Override
131             public void onClick(ClickEvent event) {
132                 addPermission();
133                 hide();
134             }
135         });
136
137         ok.addStyleName("button");
138         inner.add(ok);
139
140         panel.add(inner);
141         panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
142         
143         setWidget(panel);
144         }
145
146         protected void addPermission() {
147         final boolean readValue = read.getValue();
148         final boolean writeValue = write.getValue();
149
150         String selected = null;
151                 if (userAdd) {
152                         final String userDisplayName = userBox.getText().trim();
153                         addUserPermission(userDisplayName, readValue, writeValue);
154             return;
155                 } else if (groupBox.getSelectedIndex() > -1) {
156                         String groupName = groupBox.getValue(groupBox.getSelectedIndex());
157                         selected = app.getUserID() + ":" + groupName;
158                 }
159         if (permList.getPermissions().get(selected) != null) {
160             return;
161         }
162         if (selected == null || selected.length() == 0 || selected.equals(app.getUserID() + ":")) {
163                 app.displayWarning("You have to select a username or group");
164                 return;
165         }
166
167                 permList.addPermission(selected, readValue, writeValue);
168         }
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
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 }