Change copyright notice
[pithos-web-client] / src / gr / grnet / pithos / web / client / PermissionsAddDialog.java
1 /*
2  * Copyright 2011-2013 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 gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
38 import gr.grnet.pithos.web.client.catalog.UserCatalogs;
39 import gr.grnet.pithos.web.client.grouptree.Group;
40
41 import java.util.List;
42
43 import com.google.gwt.dom.client.NativeEvent;
44 import com.google.gwt.event.dom.client.ClickEvent;
45 import com.google.gwt.event.dom.client.ClickHandler;
46 import com.google.gwt.event.dom.client.KeyCodes;
47 import com.google.gwt.regexp.shared.RegExp;
48 import com.google.gwt.user.client.Event.NativePreviewEvent;
49 import com.google.gwt.user.client.ui.Anchor;
50 import com.google.gwt.user.client.ui.Button;
51 import com.google.gwt.user.client.ui.DialogBox;
52 import com.google.gwt.user.client.ui.FlexTable;
53 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
54 import com.google.gwt.user.client.ui.ListBox;
55 import com.google.gwt.user.client.ui.RadioButton;
56 import com.google.gwt.user.client.ui.TextBox;
57 import com.google.gwt.user.client.ui.VerticalPanel;
58
59 public class PermissionsAddDialog extends DialogBox {
60     final static RegExp EmailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i");
61
62         private TextBox userBox = new TextBox();
63
64         private ListBox groupBox = new ListBox();
65
66         private RadioButton read = new RadioButton("permissions");
67
68         private RadioButton write = new RadioButton("permissions");
69
70         private PermissionsList permList;
71
72         boolean userAdd;
73
74     private Pithos app;
75
76         public PermissionsAddDialog(Pithos _app, List<Group> _groups, PermissionsList _permList, boolean _userAdd) {
77         app = _app;
78                 userAdd = _userAdd;
79                 permList = _permList;
80
81                 Anchor close = new Anchor("close");
82                 close.addStyleName("close");
83                 close.addClickHandler(new ClickHandler() {
84                         
85                         @Override
86                         public void onClick(ClickEvent event) {
87                                 hide();
88                         }
89                 });
90                 setText("Add permission");
91                 setStyleName("pithos-DialogBox");
92
93         final VerticalPanel panel = new VerticalPanel();
94         panel.add(close);
95
96         VerticalPanel inner = new VerticalPanel();
97                 inner.addStyleName("inner");
98
99         final FlexTable permTable = new FlexTable();
100         permTable.setText(0, 0, "Users/Groups");
101         permTable.setText(0, 1, "Read Only");
102         permTable.setText(0, 2, "Read/Write");
103         permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
104         permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
105         permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
106
107         if (userAdd) {
108             permTable.setWidget(1, 0, userBox);
109         }
110         else {
111             for (Group group : _groups) {
112                 groupBox.addItem(group.getName(), group.getName());
113             }
114             permTable.setWidget(1, 0, groupBox);
115         }
116                 
117         read.setValue(true);
118         permTable.setWidget(1, 1, read);
119         permTable.setWidget(1, 2, write);
120
121         permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
122         permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
123         permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
124         inner.add(permTable);
125
126         final Button ok = new Button("OK", new ClickHandler() {
127             @Override
128             public void onClick(ClickEvent event) {
129                 addPermission();
130                 hide();
131             }
132         });
133
134         ok.addStyleName("button");
135         inner.add(ok);
136
137         panel.add(inner);
138         panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
139         
140         setWidget(panel);
141         }
142
143         protected void addPermission() {
144         final boolean readValue = read.getValue();
145         final boolean writeValue = write.getValue();
146
147         String selected = null;
148                 if (userAdd) {
149                         final String userDisplayName = userBox.getText().trim();
150                         addUserPermission(userDisplayName, readValue, writeValue);
151             return;
152                 } else if (groupBox.getSelectedIndex() > -1) {
153                         String groupName = groupBox.getValue(groupBox.getSelectedIndex());
154                         selected = app.getUserID() + ":" + groupName;
155                 }
156         if (permList.getPermissions().get(selected) != null) {
157             return;
158         }
159         if (selected == null || selected.length() == 0 || selected.equals(app.getUserID() + ":")) {
160                 app.displayWarning("You have to select a username or group");
161                 return;
162         }
163
164                 permList.addPermission(selected, readValue, writeValue);
165         }
166
167     private boolean alreadyHasPermission(String selected) {
168         return permList.getPermissions().get(selected) != null;
169     }
170
171     private void addUserPermission(final String userDisplayName, final boolean readValue, final boolean writeValue) {
172         if (!EmailValidator.test(userDisplayName)) {
173             app.displayWarning("Username must be a valid email address");
174             return;
175         }
176
177         // Now get the userID
178         final String userID = app.getIDForUserDisplayName(userDisplayName);
179         if(userID != null) {
180             // Check if already have the permission
181             if(!alreadyHasPermission(userID)) {
182                 permList.addPermission(userID, readValue, writeValue);
183             }
184         }
185         else {
186             // Must call server to obtain userID
187             new UpdateUserCatalogs(app, null, Helpers.toList(userDisplayName)) {
188                 @Override
189                 public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
190                     final String userID = updatedUserCatalogs.getID(userDisplayName);
191                     if(userID == null) {
192                         app.displayWarning("Unknown user " + userDisplayName);
193                     }
194                     else if(!alreadyHasPermission(userID)) {
195                         permList.addPermission(userID, readValue, writeValue);
196                     }
197                 }
198             }.scheduleDeferred();
199         }
200     }
201
202         @Override
203         protected void onPreviewNativeEvent(NativePreviewEvent preview) {
204                 super.onPreviewNativeEvent(preview);
205
206                 NativeEvent evt = preview.getNativeEvent();
207                 if (evt.getType().equals("keydown"))
208                         // Use the popup's key preview hooks to close the dialog when either
209                         // enter or escape is pressed.
210                         switch (evt.getKeyCode()) {
211                                 case KeyCodes.KEY_ENTER:
212                                         addPermission();
213                                         hide();
214                                         break;
215                                 case KeyCodes.KEY_ESCAPE:
216                                         hide();
217                                         break;
218                         }
219         }
220
221
222         @Override
223         public void center() {
224                 super.center();
225                 if (userAdd)
226                         userBox.setFocus(true);
227         }
228 }