Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / PermissionsAddDialog.java @ 63366925

History | View | Annotate | Download (9.9 kB)

1
/*
2
 * Copyright 2011 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.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;
43

    
44
import java.util.List;
45

    
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;
68

    
69
public class PermissionsAddDialog extends DialogBox {
70

    
71
        private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
72
        private SuggestBox suggestBox = new SuggestBox(oracle);
73

    
74
        private String selectedUser = null;
75

    
76
        private List<GroupResource> groups;
77

    
78
        private ListBox groupBox = new ListBox();
79

    
80
        private CheckBox read = new CheckBox();
81

    
82
        private CheckBox write = new CheckBox();
83

    
84
        private CheckBox modifyACL = new CheckBox();
85

    
86
        private final PermissionsList permList;
87

    
88
        boolean userAdd;
89

    
90
        public PermissionsAddDialog(List<GroupResource> _groups, PermissionsList _permList, boolean _userAdd) {
91
                groups = _groups;
92
                userAdd = _userAdd;
93
                permList = _permList;
94
                
95
                groupBox.getElement().setId("addPermission.dropDown");
96
                
97
                suggestBox.getElement().setId("addPermission.textBox");
98
                
99
                read.getElement().setId("addPermission.read");
100
                
101
                write.getElement().setId("addPermission.write");
102
                
103
                modifyACL.getElement().setId("addpermission.modify");
104
                
105
                for (GroupResource group : _groups)
106
                        groupBox.addItem(group.getName(), group.getName());
107
                final VerticalPanel panel = new VerticalPanel();
108
                final HorizontalPanel buttons = new HorizontalPanel();
109
                setWidget(panel);
110
                final FlexTable permTable = new FlexTable();
111
                permTable.setText(0, 0, "Users/Groups");
112
                permTable.setText(0, 1, "Read");
113
                permTable.setText(0, 2, "Write");
114
                permTable.setText(0, 3, "Modify Access");
115
                permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
116
                permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
117
                permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
118
                permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
119
                if (userAdd) {
120
                        suggestBox.getTextBox().addFocusHandler(new FocusHandler() {
121

    
122
                                @Override
123
                                public void onFocus(FocusEvent event) {
124
                                        if (selectedUser != null && selectedUser.endsWith("@"))
125
                                                updateSuggestions();
126

    
127
                                }
128
                        });
129

    
130
                        suggestBox.addKeyUpHandler(new KeyUpHandler() {
131

    
132
                                @Override
133
                                public void onKeyUp(KeyUpEvent event) {
134
                                        // Ignore the arrow keys.
135
                                        int keyCode = event.getNativeKeyCode();
136
                                        if (keyCode == KeyCodes.KEY_UP ||
137
                                                        keyCode == KeyCodes.KEY_DOWN ||
138
                                                        keyCode == KeyCodes.KEY_LEFT ||
139
                                                        keyCode == KeyCodes.KEY_RIGHT)
140
                                                return;
141
                                        if (keyCode==KeyCodes.KEY_ESCAPE) {
142
                                                suggestBox.hideSuggestionList();
143
                                                return;
144
                                        }
145
                                        String text = suggestBox.getText().trim();
146
                                        // Avoid useless queries for keystrokes that do not modify
147
                                        // the text.
148
                                        if (text.equals(selectedUser))
149
                                                return;
150
                                        selectedUser = text;
151
                                        // Go to the server only if the user typed the @ character.
152
                                        if (selectedUser.endsWith("@"))
153
                                                updateSuggestions();
154
                                }
155
                        });
156
                        permTable.setWidget(1, 0, suggestBox);
157
                } else
158
                        permTable.setWidget(1, 0, groupBox);
159
                permTable.setWidget(1, 1, read);
160
                permTable.setWidget(1, 2, write);
161
                permTable.setWidget(1, 3, modifyACL);
162

    
163
                permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
164
                permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
165
                permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
166
                permTable.getFlexCellFormatter().setHorizontalAlignment(1, 3, HasHorizontalAlignment.ALIGN_CENTER);
167
                panel.add(permTable);
168

    
169
                final Button ok = new Button("OK", new ClickHandler() {
170
                        @Override
171
                        public void onClick(ClickEvent event) {
172
                                addPermission();
173
                                hide();
174
                        }
175
                });
176
                ok.getElement().setId("addPermission.button.ok");
177
                buttons.add(ok);
178
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
179
                // Create the 'Cancel' button, along with a listener that hides the
180
                // dialog
181
                // when the button is clicked.
182
                final Button cancel = new Button("Cancel", new ClickHandler() {
183
                        @Override
184
                        public void onClick(ClickEvent event) {
185
                                hide();
186
                        }
187
                });
188
                cancel.getElement().setId("addPermission.button.cancel");
189
                buttons.add(cancel);
190
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
191
                buttons.setSpacing(8);
192
                buttons.addStyleName("pithos-TabPanelBottom");
193
                panel.add(buttons);
194
                panel.addStyleName("pithos-TabPanelBottom");
195
        }
196

    
197
        private void addPermission() {
198
                PermissionHolder perm = new PermissionHolder();
199
                if (userAdd) {
200
                        selectedUser = suggestBox.getText();
201
                        for(PermissionHolder p : permList.permissions)
202
                                if (selectedUser.equals(p.getUser())){
203
                                        GSS.get().displayError("User already has access to the resource");
204
                                        return;
205
                                }
206
                        perm.setUser(selectedUser);
207
                } else {
208
                        String groupId = groupBox.getValue(groupBox.getSelectedIndex());
209
                        GroupResource selected = null;
210
                        for (GroupResource g : groups)
211
                                if (g.getName().equals(groupId))
212
                                        selected = g;
213
                        if (selected == null)
214
                                return;
215
                        for(PermissionHolder p : permList.permissions)
216
                                if (selected.getName().equals(p.getGroup())){
217
                                        GSS.get().displayError("Group already has access to the resource");
218
                                        return;
219
                                }
220
                        perm.setGroup(selected.getName());
221
                }
222
                boolean readValue = read.getValue();
223
                boolean writeValue = write.getValue();
224
                boolean modifyValue = modifyACL.getValue();
225

    
226
                perm.setRead(readValue);
227
                perm.setWrite(writeValue);
228
                perm.setModifyACL(modifyValue);
229
                permList.addPermission(perm);
230
                permList.updateTable();
231
        }
232

    
233
        @Override
234
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
235
                super.onPreviewNativeEvent(preview);
236

    
237
                NativeEvent evt = preview.getNativeEvent();
238
                if (evt.getType().equals("keydown"))
239
                        // Use the popup's key preview hooks to close the dialog when either
240
                        // enter or escape is pressed.
241
                        switch (evt.getKeyCode()) {
242
                                case KeyCodes.KEY_ENTER:
243
                                        addPermission();
244
                                        hide();
245
                                        break;
246
                                case KeyCodes.KEY_ESCAPE:
247
                                        hide();
248
                                        break;
249
                        }
250
        }
251

    
252

    
253
        @Override
254
        public void center() {
255
                super.center();
256
                if (userAdd)
257
                        suggestBox.setFocus(true);
258
        }
259

    
260
        /**
261
         * Update the list of suggestions.
262
         */
263
        protected void updateSuggestions() {
264
                final GSS app = GSS.get();
265
                String query = selectedUser.substring(0, selectedUser.length()-1);
266
                GWT.log("Searching for " + query, null);
267

    
268
                GetCommand<UserSearchResource> eg = new GetCommand<UserSearchResource>(UserSearchResource.class,
269
                                        app.getApiPath() + "users/" + URL.encodeComponent(query), false, null) {
270

    
271
                        @Override
272
                        public void onComplete() {
273
                                suggestBox.hideSuggestionList();
274
                                oracle.clear();
275
                                UserSearchResource s = getResult();
276
                                for (UserResource user : s.getUsers()) {
277
                                        GWT.log("Found " + user.getUsername(), null);
278
                                        oracle.add(user.getUsername());
279
                                }
280
                                suggestBox.showSuggestionList();
281
                        }
282

    
283
                        @Override
284
                        public void onError(Throwable t) {
285
                                if(t instanceof RestException)
286
                                        app.displayError("Unable to perform search: "+((RestException)t).getHttpStatusText());
287
                                else
288
                                        app.displayError("System error while searching for users: "+t.getMessage());
289
                                GWT.log("", t);
290
                                DisplayHelper.log(t.getMessage());
291
                        }
292

    
293
                };
294
                DeferredCommand.addCommand(eg);
295
        }
296
}