Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / PermissionsAddDialog.java @ 783db80b

History | View | Annotate | Download (8.6 kB)

1
/*
2
 * Copyright 2008, 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package gr.ebs.gss.client;
20

    
21
import gr.ebs.gss.client.rest.GetCommand;
22
import gr.ebs.gss.client.rest.RestException;
23
import gr.ebs.gss.client.rest.resource.GroupResource;
24
import gr.ebs.gss.client.rest.resource.PermissionHolder;
25
import gr.ebs.gss.client.rest.resource.UserResource;
26
import gr.ebs.gss.client.rest.resource.UserSearchResource;
27

    
28
import java.util.List;
29

    
30
import com.google.gwt.core.client.GWT;
31
import com.google.gwt.dom.client.NativeEvent;
32
import com.google.gwt.event.dom.client.ClickEvent;
33
import com.google.gwt.event.dom.client.ClickHandler;
34
import com.google.gwt.event.dom.client.FocusEvent;
35
import com.google.gwt.event.dom.client.FocusHandler;
36
import com.google.gwt.event.dom.client.KeyCodes;
37
import com.google.gwt.event.dom.client.KeyUpEvent;
38
import com.google.gwt.event.dom.client.KeyUpHandler;
39
import com.google.gwt.http.client.URL;
40
import com.google.gwt.user.client.DeferredCommand;
41
import com.google.gwt.user.client.Event.NativePreviewEvent;
42
import com.google.gwt.user.client.ui.Button;
43
import com.google.gwt.user.client.ui.CheckBox;
44
import com.google.gwt.user.client.ui.DialogBox;
45
import com.google.gwt.user.client.ui.FlexTable;
46
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
47
import com.google.gwt.user.client.ui.HorizontalPanel;
48
import com.google.gwt.user.client.ui.ListBox;
49
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
50
import com.google.gwt.user.client.ui.SuggestBox;
51
import com.google.gwt.user.client.ui.VerticalPanel;
52

    
53
/**
54
 * @author kman
55
 */
56
public class PermissionsAddDialog extends DialogBox {
57

    
58
        private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
59
        private SuggestBox suggestBox = new SuggestBox(oracle);
60

    
61
        private String selectedUser = null;
62

    
63
        private List<GroupResource> groups;
64

    
65
        private ListBox groupBox = new ListBox();
66

    
67
        private CheckBox read = new CheckBox();
68

    
69
        private CheckBox write = new CheckBox();
70

    
71
        private CheckBox modifyACL = new CheckBox();
72

    
73
        private final PermissionsList permList;
74

    
75
        boolean userAdd;
76

    
77
        public PermissionsAddDialog(List<GroupResource> _groups, PermissionsList _permList, boolean _userAdd) {
78
                groups = _groups;
79
                userAdd = _userAdd;
80
                permList = _permList;
81
                for (GroupResource group : _groups)
82
                        groupBox.addItem(group.getName(), group.getName());
83
                final VerticalPanel panel = new VerticalPanel();
84
                final HorizontalPanel buttons = new HorizontalPanel();
85
                setWidget(panel);
86
                final FlexTable permTable = new FlexTable();
87
                permTable.setText(0, 0, "Users/Groups");
88
                permTable.setText(0, 1, "Read");
89
                permTable.setText(0, 2, "Write");
90
                permTable.setText(0, 3, "Modify Access");
91
                permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
92
                permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
93
                permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
94
                permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
95
                if (userAdd) {
96
                        suggestBox.getTextBox().addFocusHandler(new FocusHandler() {
97

    
98
                                @Override
99
                                public void onFocus(FocusEvent event) {
100
                                        if (selectedUser != null && selectedUser.endsWith("@"))
101
                                                updateSuggestions();
102

    
103
                                }
104
                        });
105

    
106
                        suggestBox.addKeyUpHandler(new KeyUpHandler() {
107

    
108
                                @Override
109
                                public void onKeyUp(KeyUpEvent event) {
110
                                        // Ignore the arrow keys.
111
                                        int keyCode = event.getNativeKeyCode();
112
                                        if (keyCode == KeyCodes.KEY_UP ||
113
                                                        keyCode == KeyCodes.KEY_DOWN ||
114
                                                        keyCode == KeyCodes.KEY_LEFT ||
115
                                                        keyCode == KeyCodes.KEY_RIGHT)
116
                                                return;
117
                                        if (keyCode==KeyCodes.KEY_ESCAPE) {
118
                                                suggestBox.hideSuggestionList();
119
                                                return;
120
                                        }
121
                                        String text = suggestBox.getText().trim();
122
                                        // Avoid useless queries for keystrokes that do not modify
123
                                        // the text.
124
                                        if (text.equals(selectedUser))
125
                                                return;
126
                                        selectedUser = text;
127
                                        // Go to the server only if the user typed the @ character.
128
                                        if (selectedUser.endsWith("@"))
129
                                                updateSuggestions();
130
                                }
131
                        });
132
                        permTable.setWidget(1, 0, suggestBox);
133
                } else
134
                        permTable.setWidget(1, 0, groupBox);
135
                permTable.setWidget(1, 1, read);
136
                permTable.setWidget(1, 2, write);
137
                permTable.setWidget(1, 3, modifyACL);
138

    
139
                permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
140
                permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
141
                permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
142
                permTable.getFlexCellFormatter().setHorizontalAlignment(1, 3, HasHorizontalAlignment.ALIGN_CENTER);
143
                panel.add(permTable);
144

    
145
                final Button ok = new Button("OK", new ClickHandler() {
146
                        @Override
147
                        public void onClick(ClickEvent event) {
148
                                addPermission();
149
                                hide();
150
                        }
151
                });
152
                buttons.add(ok);
153
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
154
                // Create the 'Cancel' button, along with a listener that hides the
155
                // dialog
156
                // when the button is clicked.
157
                final Button cancel = new Button("Cancel", new ClickHandler() {
158
                        @Override
159
                        public void onClick(ClickEvent event) {
160
                                hide();
161
                        }
162
                });
163
                buttons.add(cancel);
164
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
165
                buttons.setSpacing(8);
166
                buttons.addStyleName("gss-TabPanelBottom");
167
                panel.add(buttons);
168
                panel.addStyleName("gss-TabPanelBottom");
169
        }
170

    
171
        private void addPermission() {
172
                PermissionHolder perm = new PermissionHolder();
173
                if (userAdd) {
174
                        selectedUser = suggestBox.getText();
175
                        for(PermissionHolder p : permList.permissions)
176
                                if (selectedUser.equals(p.getUser())){
177
                                        GSS.get().displayError("User already has access to the resource");
178
                                        return;
179
                                }
180
                        perm.setUser(selectedUser);
181
                } else {
182
                        String groupId = groupBox.getValue(groupBox.getSelectedIndex());
183
                        GroupResource selected = null;
184
                        for (GroupResource g : groups)
185
                                if (g.getName().equals(groupId))
186
                                        selected = g;
187
                        if (selected == null)
188
                                return;
189
                        for(PermissionHolder p : permList.permissions)
190
                                if (selected.getName().equals(p.getGroup())){
191
                                        GSS.get().displayError("Group already has access to the resource");
192
                                        return;
193
                                }
194
                        perm.setGroup(selected.getName());
195
                }
196
                boolean readValue = read.getValue();
197
                boolean writeValue = write.getValue();
198
                boolean modifyValue = modifyACL.getValue();
199

    
200
                perm.setRead(readValue);
201
                perm.setWrite(writeValue);
202
                perm.setModifyACL(modifyValue);
203
                permList.addPermission(perm);
204
                permList.updateTable();
205
        }
206

    
207
        @Override
208
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
209
                super.onPreviewNativeEvent(preview);
210

    
211
                NativeEvent evt = preview.getNativeEvent();
212
                if (evt.getType().equals("keydown"))
213
                        // Use the popup's key preview hooks to close the dialog when either
214
                        // enter or escape is pressed.
215
                        switch (evt.getKeyCode()) {
216
                                case KeyCodes.KEY_ENTER:
217
                                        addPermission();
218
                                        hide();
219
                                        break;
220
                                case KeyCodes.KEY_ESCAPE:
221
                                        hide();
222
                                        break;
223
                        }
224
        }
225

    
226

    
227
        @Override
228
        public void center() {
229
                super.center();
230
                if (userAdd)
231
                        suggestBox.setFocus(true);
232
        }
233

    
234
        /**
235
         * Update the list of suggestions.
236
         */
237
        protected void updateSuggestions() {
238
                final GSS app = GSS.get();
239
                String query = selectedUser.substring(0, selectedUser.length()-1);
240
                GWT.log("Searching for " + query, null);
241

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

    
245
                        @Override
246
                        public void onComplete() {
247
                                suggestBox.hideSuggestionList();
248
                                oracle.clear();
249
                                UserSearchResource s = getResult();
250
                                for (UserResource user : s.getUsers()) {
251
                                        GWT.log("Found " + user.getUsername(), null);
252
                                        oracle.add(user.getUsername());
253
                                }
254
                                suggestBox.showSuggestionList();
255
                        }
256

    
257
                        @Override
258
                        public void onError(Throwable t) {
259
                                if(t instanceof RestException)
260
                                        app.displayError("Unable to perform search: "+((RestException)t).getHttpStatusText());
261
                                else
262
                                        app.displayError("System error while searching for users: "+t.getMessage());
263
                                GWT.log("", t);
264
                                DisplayHelper.log(t.getMessage());
265
                        }
266

    
267
                };
268
                DeferredCommand.addCommand(eg);
269
        }
270
}