Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / PermissionsAddDialog.java @ fc0fa492

History | View | Annotate | Download (8.6 kB)

1 14ad7326 pastith
/*
2 14ad7326 pastith
 * Copyright 2008, 2009 Electronic Business Systems Ltd.
3 14ad7326 pastith
 *
4 14ad7326 pastith
 * This file is part of GSS.
5 14ad7326 pastith
 *
6 14ad7326 pastith
 * GSS is free software: you can redistribute it and/or modify
7 14ad7326 pastith
 * it under the terms of the GNU General Public License as published by
8 14ad7326 pastith
 * the Free Software Foundation, either version 3 of the License, or
9 14ad7326 pastith
 * (at your option) any later version.
10 14ad7326 pastith
 *
11 14ad7326 pastith
 * GSS is distributed in the hope that it will be useful,
12 14ad7326 pastith
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14ad7326 pastith
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14ad7326 pastith
 * GNU General Public License for more details.
15 14ad7326 pastith
 *
16 14ad7326 pastith
 * You should have received a copy of the GNU General Public License
17 14ad7326 pastith
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 14ad7326 pastith
 */
19 14ad7326 pastith
package gr.ebs.gss.client;
20 14ad7326 pastith
21 895035a2 pastith
import gr.ebs.gss.client.rest.GetCommand;
22 41d05cc8 pastith
import gr.ebs.gss.client.rest.RestException;
23 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.GroupResource;
24 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.PermissionHolder;
25 41d05cc8 pastith
import gr.ebs.gss.client.rest.resource.UserResource;
26 41d05cc8 pastith
import gr.ebs.gss.client.rest.resource.UserSearchResource;
27 14ad7326 pastith
28 14ad7326 pastith
import java.util.List;
29 14ad7326 pastith
30 41d05cc8 pastith
import com.google.gwt.core.client.GWT;
31 afd3a0ef Giannis Koutsoubos
import com.google.gwt.dom.client.NativeEvent;
32 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.ClickEvent;
33 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.ClickHandler;
34 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.FocusEvent;
35 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.FocusHandler;
36 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.KeyCodes;
37 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.KeyUpEvent;
38 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.KeyUpHandler;
39 41d05cc8 pastith
import com.google.gwt.http.client.URL;
40 41d05cc8 pastith
import com.google.gwt.user.client.DeferredCommand;
41 afd3a0ef Giannis Koutsoubos
import com.google.gwt.user.client.Event.NativePreviewEvent;
42 14ad7326 pastith
import com.google.gwt.user.client.ui.Button;
43 14ad7326 pastith
import com.google.gwt.user.client.ui.CheckBox;
44 14ad7326 pastith
import com.google.gwt.user.client.ui.DialogBox;
45 14ad7326 pastith
import com.google.gwt.user.client.ui.FlexTable;
46 14ad7326 pastith
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
47 14ad7326 pastith
import com.google.gwt.user.client.ui.HorizontalPanel;
48 14ad7326 pastith
import com.google.gwt.user.client.ui.ListBox;
49 41d05cc8 pastith
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
50 41d05cc8 pastith
import com.google.gwt.user.client.ui.SuggestBox;
51 14ad7326 pastith
import com.google.gwt.user.client.ui.VerticalPanel;
52 14ad7326 pastith
53 14ad7326 pastith
/**
54 14ad7326 pastith
 * @author kman
55 14ad7326 pastith
 */
56 14ad7326 pastith
public class PermissionsAddDialog extends DialogBox {
57 14ad7326 pastith
58 41d05cc8 pastith
        private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
59 41d05cc8 pastith
        private SuggestBox suggestBox = new SuggestBox(oracle);
60 14ad7326 pastith
61 a52ea5e4 pastith
        private String selectedUser = null;
62 a52ea5e4 pastith
63 a52ea5e4 pastith
        private List<GroupResource> groups;
64 14ad7326 pastith
65 14ad7326 pastith
        private ListBox groupBox = new ListBox();
66 14ad7326 pastith
67 14ad7326 pastith
        private CheckBox read = new CheckBox();
68 14ad7326 pastith
69 14ad7326 pastith
        private CheckBox write = new CheckBox();
70 14ad7326 pastith
71 14ad7326 pastith
        private CheckBox modifyACL = new CheckBox();
72 14ad7326 pastith
73 41d05cc8 pastith
        private final PermissionsList permList;
74 14ad7326 pastith
75 14ad7326 pastith
        boolean userAdd;
76 14ad7326 pastith
77 41d05cc8 pastith
        public PermissionsAddDialog(List<GroupResource> _groups, PermissionsList _permList, boolean _userAdd) {
78 41d05cc8 pastith
                groups = _groups;
79 41d05cc8 pastith
                userAdd = _userAdd;
80 41d05cc8 pastith
                permList = _permList;
81 41d05cc8 pastith
                for (GroupResource group : _groups)
82 a52ea5e4 pastith
                        groupBox.addItem(group.getName(), group.getName());
83 14ad7326 pastith
                final VerticalPanel panel = new VerticalPanel();
84 14ad7326 pastith
                final HorizontalPanel buttons = new HorizontalPanel();
85 14ad7326 pastith
                setWidget(panel);
86 14ad7326 pastith
                final FlexTable permTable = new FlexTable();
87 14ad7326 pastith
                permTable.setText(0, 0, "Users/Groups");
88 14ad7326 pastith
                permTable.setText(0, 1, "Read");
89 14ad7326 pastith
                permTable.setText(0, 2, "Write");
90 b4771f6a fstamatelopoulos
                permTable.setText(0, 3, "Modify Access");
91 14ad7326 pastith
                permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
92 14ad7326 pastith
                permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
93 14ad7326 pastith
                permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
94 14ad7326 pastith
                permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
95 41d05cc8 pastith
                if (userAdd) {
96 afd3a0ef Giannis Koutsoubos
                        suggestBox.getTextBox().addFocusHandler(new FocusHandler() {
97 afd3a0ef Giannis Koutsoubos
98 41d05cc8 pastith
                                @Override
99 afd3a0ef Giannis Koutsoubos
                                public void onFocus(FocusEvent event) {
100 41d05cc8 pastith
                                        if (selectedUser != null && selectedUser.endsWith("@"))
101 41d05cc8 pastith
                                                updateSuggestions();
102 afd3a0ef Giannis Koutsoubos
103 41d05cc8 pastith
                                }
104 41d05cc8 pastith
                        });
105 afd3a0ef Giannis Koutsoubos
106 afd3a0ef Giannis Koutsoubos
                        suggestBox.addKeyUpHandler(new KeyUpHandler() {
107 afd3a0ef Giannis Koutsoubos
108 41d05cc8 pastith
                                @Override
109 afd3a0ef Giannis Koutsoubos
                                public void onKeyUp(KeyUpEvent event) {
110 6e6e914e Panagiotis Astithas
                                        // Ignore the arrow keys.
111 6e6e914e Panagiotis Astithas
                                        int keyCode = event.getNativeKeyCode();
112 6e6e914e Panagiotis Astithas
                                        if (keyCode == KeyCodes.KEY_UP ||
113 6e6e914e Panagiotis Astithas
                                                        keyCode == KeyCodes.KEY_DOWN ||
114 6e6e914e Panagiotis Astithas
                                                        keyCode == KeyCodes.KEY_LEFT ||
115 6e6e914e Panagiotis Astithas
                                                        keyCode == KeyCodes.KEY_RIGHT)
116 41d05cc8 pastith
                                                return;
117 5c2e8209 Panagiotis Astithas
                                        if (keyCode==KeyCodes.KEY_ESCAPE) {
118 5c2e8209 Panagiotis Astithas
                                                suggestBox.hideSuggestionList();
119 5c2e8209 Panagiotis Astithas
                                                return;
120 5c2e8209 Panagiotis Astithas
                                        }
121 41d05cc8 pastith
                                        String text = suggestBox.getText().trim();
122 6e6e914e Panagiotis Astithas
                                        // Avoid useless queries for keystrokes that do not modify
123 6e6e914e Panagiotis Astithas
                                        // the text.
124 41d05cc8 pastith
                                        if (text.equals(selectedUser))
125 41d05cc8 pastith
                                                return;
126 41d05cc8 pastith
                                        selectedUser = text;
127 41d05cc8 pastith
                                        // Go to the server only if the user typed the @ character.
128 41d05cc8 pastith
                                        if (selectedUser.endsWith("@"))
129 41d05cc8 pastith
                                                updateSuggestions();
130 41d05cc8 pastith
                                }
131 41d05cc8 pastith
                        });
132 14ad7326 pastith
                        permTable.setWidget(1, 0, suggestBox);
133 41d05cc8 pastith
                } else
134 14ad7326 pastith
                        permTable.setWidget(1, 0, groupBox);
135 14ad7326 pastith
                permTable.setWidget(1, 1, read);
136 14ad7326 pastith
                permTable.setWidget(1, 2, write);
137 14ad7326 pastith
                permTable.setWidget(1, 3, modifyACL);
138 14ad7326 pastith
139 14ad7326 pastith
                permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
140 14ad7326 pastith
                permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
141 14ad7326 pastith
                permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
142 14ad7326 pastith
                permTable.getFlexCellFormatter().setHorizontalAlignment(1, 3, HasHorizontalAlignment.ALIGN_CENTER);
143 14ad7326 pastith
                panel.add(permTable);
144 14ad7326 pastith
145 afd3a0ef Giannis Koutsoubos
                final Button ok = new Button("OK", new ClickHandler() {
146 afd3a0ef Giannis Koutsoubos
                        @Override
147 afd3a0ef Giannis Koutsoubos
                        public void onClick(ClickEvent event) {
148 14ad7326 pastith
                                addPermission();
149 14ad7326 pastith
                                hide();
150 14ad7326 pastith
                        }
151 14ad7326 pastith
                });
152 14ad7326 pastith
                buttons.add(ok);
153 14ad7326 pastith
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
154 14ad7326 pastith
                // Create the 'Cancel' button, along with a listener that hides the
155 14ad7326 pastith
                // dialog
156 14ad7326 pastith
                // when the button is clicked.
157 afd3a0ef Giannis Koutsoubos
                final Button cancel = new Button("Cancel", new ClickHandler() {
158 afd3a0ef Giannis Koutsoubos
                        @Override
159 afd3a0ef Giannis Koutsoubos
                        public void onClick(ClickEvent event) {
160 14ad7326 pastith
                                hide();
161 14ad7326 pastith
                        }
162 14ad7326 pastith
                });
163 14ad7326 pastith
                buttons.add(cancel);
164 14ad7326 pastith
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
165 14ad7326 pastith
                buttons.setSpacing(8);
166 5c6b2883 Panagiotis Astithas
                buttons.addStyleName("gss-TabPanelBottom");
167 14ad7326 pastith
                panel.add(buttons);
168 5c6b2883 Panagiotis Astithas
                panel.addStyleName("gss-TabPanelBottom");
169 14ad7326 pastith
        }
170 14ad7326 pastith
171 14ad7326 pastith
        private void addPermission() {
172 a52ea5e4 pastith
                PermissionHolder perm = new PermissionHolder();
173 14ad7326 pastith
                if (userAdd) {
174 a52ea5e4 pastith
                        selectedUser = suggestBox.getText();
175 a52ea5e4 pastith
                        for(PermissionHolder p : permList.permissions)
176 a52ea5e4 pastith
                                if (selectedUser.equals(p.getUser())){
177 fd84943c Fotis Stamatelopoulos
                                        GSS.get().displayError("User already has access to the resource");
178 14ad7326 pastith
                                        return;
179 14ad7326 pastith
                                }
180 14ad7326 pastith
                        perm.setUser(selectedUser);
181 14ad7326 pastith
                } else {
182 a52ea5e4 pastith
                        String groupId = groupBox.getValue(groupBox.getSelectedIndex());
183 a52ea5e4 pastith
                        GroupResource selected = null;
184 a52ea5e4 pastith
                        for (GroupResource g : groups)
185 a52ea5e4 pastith
                                if (g.getName().equals(groupId))
186 14ad7326 pastith
                                        selected = g;
187 fd84943c Fotis Stamatelopoulos
                        if (selected == null)
188 fd84943c Fotis Stamatelopoulos
                                return;
189 a52ea5e4 pastith
                        for(PermissionHolder p : permList.permissions)
190 fd84943c Fotis Stamatelopoulos
                                if (selected.getName().equals(p.getGroup())){
191 fd84943c Fotis Stamatelopoulos
                                        GSS.get().displayError("Group already has access to the resource");
192 14ad7326 pastith
                                        return;
193 14ad7326 pastith
                                }
194 a52ea5e4 pastith
                        perm.setGroup(selected.getName());
195 14ad7326 pastith
                }
196 afd3a0ef Giannis Koutsoubos
                boolean readValue = read.getValue();
197 afd3a0ef Giannis Koutsoubos
                boolean writeValue = write.getValue();
198 afd3a0ef Giannis Koutsoubos
                boolean modifyValue = modifyACL.getValue();
199 14ad7326 pastith
200 14ad7326 pastith
                perm.setRead(readValue);
201 14ad7326 pastith
                perm.setWrite(writeValue);
202 14ad7326 pastith
                perm.setModifyACL(modifyValue);
203 409e932c koutsoub
                permList.addPermission(perm);
204 14ad7326 pastith
                permList.updateTable();
205 14ad7326 pastith
        }
206 14ad7326 pastith
207 41d05cc8 pastith
        @Override
208 afd3a0ef Giannis Koutsoubos
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
209 afd3a0ef Giannis Koutsoubos
                super.onPreviewNativeEvent(preview);
210 afd3a0ef Giannis Koutsoubos
211 afd3a0ef Giannis Koutsoubos
                NativeEvent evt = preview.getNativeEvent();
212 afd3a0ef Giannis Koutsoubos
                if (evt.getType().equals("keydown"))
213 afd3a0ef Giannis Koutsoubos
                        // Use the popup's key preview hooks to close the dialog when either
214 afd3a0ef Giannis Koutsoubos
                        // enter or escape is pressed.
215 afd3a0ef Giannis Koutsoubos
                        switch (evt.getKeyCode()) {
216 afd3a0ef Giannis Koutsoubos
                                case KeyCodes.KEY_ENTER:
217 afd3a0ef Giannis Koutsoubos
                                        addPermission();
218 afd3a0ef Giannis Koutsoubos
                                        hide();
219 afd3a0ef Giannis Koutsoubos
                                        break;
220 afd3a0ef Giannis Koutsoubos
                                case KeyCodes.KEY_ESCAPE:
221 afd3a0ef Giannis Koutsoubos
                                        hide();
222 afd3a0ef Giannis Koutsoubos
                                        break;
223 afd3a0ef Giannis Koutsoubos
                        }
224 14ad7326 pastith
        }
225 14ad7326 pastith
226 afd3a0ef Giannis Koutsoubos
227 14ad7326 pastith
        @Override
228 14ad7326 pastith
        public void center() {
229 14ad7326 pastith
                super.center();
230 14ad7326 pastith
                if (userAdd)
231 14ad7326 pastith
                        suggestBox.setFocus(true);
232 14ad7326 pastith
        }
233 41d05cc8 pastith
234 41d05cc8 pastith
        /**
235 41d05cc8 pastith
         * Update the list of suggestions.
236 41d05cc8 pastith
         */
237 41d05cc8 pastith
        protected void updateSuggestions() {
238 41d05cc8 pastith
                final GSS app = GSS.get();
239 41d05cc8 pastith
                String query = selectedUser.substring(0, selectedUser.length()-1);
240 41d05cc8 pastith
                GWT.log("Searching for " + query, null);
241 41d05cc8 pastith
242 895035a2 pastith
                GetCommand<UserSearchResource> eg = new GetCommand<UserSearchResource>(UserSearchResource.class,
243 62f168b2 Giannis Koutsoubos
                                        app.getApiPath() + "users/" + URL.encodeComponent(query), false, null) {
244 41d05cc8 pastith
245 41d05cc8 pastith
                        @Override
246 41d05cc8 pastith
                        public void onComplete() {
247 43d317b2 Panagiotis Astithas
                                suggestBox.hideSuggestionList();
248 41d05cc8 pastith
                                oracle.clear();
249 41d05cc8 pastith
                                UserSearchResource s = getResult();
250 41d05cc8 pastith
                                for (UserResource user : s.getUsers()) {
251 41d05cc8 pastith
                                        GWT.log("Found " + user.getUsername(), null);
252 41d05cc8 pastith
                                        oracle.add(user.getUsername());
253 41d05cc8 pastith
                                }
254 43d317b2 Panagiotis Astithas
                                suggestBox.showSuggestionList();
255 41d05cc8 pastith
                        }
256 41d05cc8 pastith
257 41d05cc8 pastith
                        @Override
258 41d05cc8 pastith
                        public void onError(Throwable t) {
259 41d05cc8 pastith
                                if(t instanceof RestException)
260 41d05cc8 pastith
                                        app.displayError("Unable to perform search: "+((RestException)t).getHttpStatusText());
261 41d05cc8 pastith
                                else
262 41d05cc8 pastith
                                        app.displayError("System error while searching for users: "+t.getMessage());
263 41d05cc8 pastith
                                GWT.log("", t);
264 41d05cc8 pastith
                                DisplayHelper.log(t.getMessage());
265 41d05cc8 pastith
                        }
266 41d05cc8 pastith
267 41d05cc8 pastith
                };
268 41d05cc8 pastith
                DeferredCommand.addCommand(eg);
269 41d05cc8 pastith
        }
270 14ad7326 pastith
}