Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.1 kB)

1 a57faaf0 Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3 63366925 Christos Stathis
 *
4 63366925 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 63366925 Christos Stathis
 * without modification, are permitted provided that the following
6 63366925 Christos Stathis
 * conditions are met:
7 63366925 Christos Stathis
 *
8 63366925 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 63366925 Christos Stathis
 *      disclaimer.
11 63366925 Christos Stathis
 *
12 63366925 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 63366925 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 63366925 Christos Stathis
 *      provided with the distribution.
16 63366925 Christos Stathis
 *
17 63366925 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 63366925 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 63366925 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 63366925 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 63366925 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 63366925 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 63366925 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 63366925 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 63366925 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 63366925 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 63366925 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 63366925 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 63366925 Christos Stathis
 *
30 63366925 Christos Stathis
 * The views and conclusions contained in the software and
31 63366925 Christos Stathis
 * documentation are those of the authors and should not be
32 63366925 Christos Stathis
 * interpreted as representing official policies, either expressed
33 63366925 Christos Stathis
 * or implied, of GRNET S.A.
34 a57faaf0 Christos Stathis
 */
35 a57faaf0 Christos Stathis
package gr.grnet.pithos.web.client;
36 a57faaf0 Christos Stathis
37 8d09767f Christos KK Loverdos
import com.google.gwt.http.client.Request;
38 8d09767f Christos KK Loverdos
import com.google.gwt.http.client.Response;
39 8d09767f Christos KK Loverdos
import com.google.gwt.json.client.JSONObject;
40 8d09767f Christos KK Loverdos
import gr.grnet.pithos.web.client.catalog.GetUserCatalogs;
41 eee72ed5 Christos KK Loverdos
import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
42 8d09767f Christos KK Loverdos
import gr.grnet.pithos.web.client.catalog.UserCatalogs;
43 4baffab1 Christos Stathis
import gr.grnet.pithos.web.client.grouptree.Group;
44 a57faaf0 Christos Stathis
45 a57faaf0 Christos Stathis
import java.util.List;
46 a57faaf0 Christos Stathis
47 a57faaf0 Christos Stathis
import com.google.gwt.dom.client.NativeEvent;
48 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
49 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
50 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.KeyCodes;
51 1e1441bf Christos Stathis
import com.google.gwt.regexp.shared.RegExp;
52 a57faaf0 Christos Stathis
import com.google.gwt.user.client.Event.NativePreviewEvent;
53 4420a247 Christos Stathis
import com.google.gwt.user.client.ui.Anchor;
54 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.Button;
55 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.DialogBox;
56 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.FlexTable;
57 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
58 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.ListBox;
59 4ded5310 Christos Stathis
import com.google.gwt.user.client.ui.RadioButton;
60 ebead1b5 Christos Stathis
import com.google.gwt.user.client.ui.TextBox;
61 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
62 a57faaf0 Christos Stathis
63 a57faaf0 Christos Stathis
public class PermissionsAddDialog extends DialogBox {
64 8d09767f Christos KK Loverdos
    final static RegExp EmailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i");
65 a57faaf0 Christos Stathis
66 d5c6298f Christos Stathis
        private TextBox userBox = new TextBox();
67 a57faaf0 Christos Stathis
68 a57faaf0 Christos Stathis
        private ListBox groupBox = new ListBox();
69 a57faaf0 Christos Stathis
70 4ded5310 Christos Stathis
        private RadioButton read = new RadioButton("permissions");
71 a57faaf0 Christos Stathis
72 4ded5310 Christos Stathis
        private RadioButton write = new RadioButton("permissions");
73 a57faaf0 Christos Stathis
74 d5c6298f Christos Stathis
        private PermissionsList permList;
75 a57faaf0 Christos Stathis
76 a57faaf0 Christos Stathis
        boolean userAdd;
77 a57faaf0 Christos Stathis
78 a7c43f26 Christos Stathis
    private Pithos app;
79 a7c43f26 Christos Stathis
80 d5c6298f Christos Stathis
        public PermissionsAddDialog(Pithos _app, List<Group> _groups, PermissionsList _permList, boolean _userAdd) {
81 a7c43f26 Christos Stathis
        app = _app;
82 a57faaf0 Christos Stathis
                userAdd = _userAdd;
83 a57faaf0 Christos Stathis
                permList = _permList;
84 a57faaf0 Christos Stathis
85 948a9b34 Christos Stathis
                Anchor close = new Anchor("close");
86 4420a247 Christos Stathis
                close.addStyleName("close");
87 4420a247 Christos Stathis
                close.addClickHandler(new ClickHandler() {
88 4420a247 Christos Stathis
                        
89 4420a247 Christos Stathis
                        @Override
90 4420a247 Christos Stathis
                        public void onClick(ClickEvent event) {
91 4420a247 Christos Stathis
                                hide();
92 4420a247 Christos Stathis
                        }
93 4420a247 Christos Stathis
                });
94 4420a247 Christos Stathis
                setText("Add permission");
95 4420a247 Christos Stathis
                setStyleName("pithos-DialogBox");
96 4420a247 Christos Stathis
97 d5c6298f Christos Stathis
        final VerticalPanel panel = new VerticalPanel();
98 4420a247 Christos Stathis
        panel.add(close);
99 4420a247 Christos Stathis
100 4420a247 Christos Stathis
        VerticalPanel inner = new VerticalPanel();
101 4420a247 Christos Stathis
                inner.addStyleName("inner");
102 d5c6298f Christos Stathis
103 d5c6298f Christos Stathis
        final FlexTable permTable = new FlexTable();
104 d5c6298f Christos Stathis
        permTable.setText(0, 0, "Users/Groups");
105 4ded5310 Christos Stathis
        permTable.setText(0, 1, "Read Only");
106 4ded5310 Christos Stathis
        permTable.setText(0, 2, "Read/Write");
107 d5c6298f Christos Stathis
        permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
108 d5c6298f Christos Stathis
        permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
109 d5c6298f Christos Stathis
        permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
110 d5c6298f Christos Stathis
111 d5c6298f Christos Stathis
        if (userAdd) {
112 d5c6298f Christos Stathis
            permTable.setWidget(1, 0, userBox);
113 d5c6298f Christos Stathis
        }
114 d5c6298f Christos Stathis
        else {
115 8d09767f Christos KK Loverdos
            for (Group group : _groups) {
116 d5c6298f Christos Stathis
                groupBox.addItem(group.getName(), group.getName());
117 8d09767f Christos KK Loverdos
            }
118 d5c6298f Christos Stathis
            permTable.setWidget(1, 0, groupBox);
119 d5c6298f Christos Stathis
        }
120 4ded5310 Christos Stathis
                
121 4ded5310 Christos Stathis
        read.setValue(true);
122 d5c6298f Christos Stathis
        permTable.setWidget(1, 1, read);
123 d5c6298f Christos Stathis
        permTable.setWidget(1, 2, write);
124 d5c6298f Christos Stathis
125 d5c6298f Christos Stathis
        permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
126 d5c6298f Christos Stathis
        permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
127 d5c6298f Christos Stathis
        permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
128 4420a247 Christos Stathis
        inner.add(permTable);
129 d5c6298f Christos Stathis
130 d5c6298f Christos Stathis
        final Button ok = new Button("OK", new ClickHandler() {
131 d5c6298f Christos Stathis
            @Override
132 ebead1b5 Christos Stathis
            public void onClick(ClickEvent event) {
133 d5c6298f Christos Stathis
                addPermission();
134 d5c6298f Christos Stathis
                hide();
135 d5c6298f Christos Stathis
            }
136 d5c6298f Christos Stathis
        });
137 d5c6298f Christos Stathis
138 4420a247 Christos Stathis
        ok.addStyleName("button");
139 4420a247 Christos Stathis
        inner.add(ok);
140 d5c6298f Christos Stathis
141 4420a247 Christos Stathis
        panel.add(inner);
142 4420a247 Christos Stathis
        panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
143 4420a247 Christos Stathis
        
144 d5c6298f Christos Stathis
        setWidget(panel);
145 a57faaf0 Christos Stathis
        }
146 a57faaf0 Christos Stathis
147 7811b9d1 Christos Stathis
        protected void addPermission() {
148 8d09767f Christos KK Loverdos
        final boolean readValue = read.getValue();
149 8d09767f Christos KK Loverdos
        final boolean writeValue = write.getValue();
150 8d09767f Christos KK Loverdos
151 d5c6298f Christos Stathis
        String selected = null;
152 a57faaf0 Christos Stathis
                if (userAdd) {
153 8d09767f Christos KK Loverdos
                        final String userDisplayName = userBox.getText().trim();
154 8d09767f Christos KK Loverdos
                        addUserPermission(userDisplayName, readValue, writeValue);
155 8d09767f Christos KK Loverdos
            return;
156 c1638c18 Christos Stathis
                } else if (groupBox.getSelectedIndex() > -1) {
157 d5c6298f Christos Stathis
                        String groupName = groupBox.getValue(groupBox.getSelectedIndex());
158 cc0120ab Christos KK Loverdos
                        selected = app.getUserID() + ":" + groupName;
159 a57faaf0 Christos Stathis
                }
160 d5c6298f Christos Stathis
        if (permList.getPermissions().get(selected) != null) {
161 ecf95c9e Christos Stathis
            return;
162 d5c6298f Christos Stathis
        }
163 cc0120ab Christos KK Loverdos
        if (selected == null || selected.length() == 0 || selected.equals(app.getUserID() + ":")) {
164 1e1441bf Christos Stathis
                app.displayWarning("You have to select a username or group");
165 c1638c18 Christos Stathis
                return;
166 c1638c18 Christos Stathis
        }
167 1e1441bf Christos Stathis
168 d5c6298f Christos Stathis
                permList.addPermission(selected, readValue, writeValue);
169 a57faaf0 Christos Stathis
        }
170 a57faaf0 Christos Stathis
171 8d09767f Christos KK Loverdos
    private boolean alreadyHasPermission(String selected) {
172 8d09767f Christos KK Loverdos
        return permList.getPermissions().get(selected) != null;
173 8d09767f Christos KK Loverdos
    }
174 8d09767f Christos KK Loverdos
175 8d09767f Christos KK Loverdos
    private void addUserPermission(final String userDisplayName, final boolean readValue, final boolean writeValue) {
176 8d09767f Christos KK Loverdos
        if (!EmailValidator.test(userDisplayName)) {
177 8d09767f Christos KK Loverdos
            app.displayWarning("Username must be a valid email address");
178 8d09767f Christos KK Loverdos
            return;
179 8d09767f Christos KK Loverdos
        }
180 8d09767f Christos KK Loverdos
181 8d09767f Christos KK Loverdos
        // Now get the userID
182 8d09767f Christos KK Loverdos
        final String userID = app.getUserIDForDisplayName(userDisplayName);
183 8d09767f Christos KK Loverdos
        if(userID != null) {
184 8d09767f Christos KK Loverdos
            // Check if already have the permission
185 8d09767f Christos KK Loverdos
            if(!alreadyHasPermission(userID)) {
186 8d09767f Christos KK Loverdos
                permList.addPermission(userID, readValue, writeValue);
187 8d09767f Christos KK Loverdos
            }
188 8d09767f Christos KK Loverdos
        }
189 8d09767f Christos KK Loverdos
        else {
190 8d09767f Christos KK Loverdos
            // Must call server to obtain userID
191 eee72ed5 Christos KK Loverdos
            new UpdateUserCatalogs(app, null, Helpers.toList(userDisplayName)) {
192 8d09767f Christos KK Loverdos
                @Override
193 eee72ed5 Christos KK Loverdos
                public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
194 eee72ed5 Christos KK Loverdos
                    final String userID = updatedUserCatalogs.getUserID(userDisplayName);
195 8d09767f Christos KK Loverdos
                    if(userID == null) {
196 8d09767f Christos KK Loverdos
                        app.displayWarning("Unknown user " + userDisplayName);
197 8d09767f Christos KK Loverdos
                    }
198 8d09767f Christos KK Loverdos
                    else if(!alreadyHasPermission(userID)) {
199 8d09767f Christos KK Loverdos
                        permList.addPermission(userID, readValue, writeValue);
200 8d09767f Christos KK Loverdos
                    }
201 8d09767f Christos KK Loverdos
                }
202 8d09767f Christos KK Loverdos
            }.scheduleDeferred();
203 8d09767f Christos KK Loverdos
        }
204 8d09767f Christos KK Loverdos
    }
205 8d09767f Christos KK Loverdos
206 a57faaf0 Christos Stathis
        @Override
207 a57faaf0 Christos Stathis
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
208 a57faaf0 Christos Stathis
                super.onPreviewNativeEvent(preview);
209 a57faaf0 Christos Stathis
210 a57faaf0 Christos Stathis
                NativeEvent evt = preview.getNativeEvent();
211 a57faaf0 Christos Stathis
                if (evt.getType().equals("keydown"))
212 a57faaf0 Christos Stathis
                        // Use the popup's key preview hooks to close the dialog when either
213 a57faaf0 Christos Stathis
                        // enter or escape is pressed.
214 a57faaf0 Christos Stathis
                        switch (evt.getKeyCode()) {
215 a57faaf0 Christos Stathis
                                case KeyCodes.KEY_ENTER:
216 a57faaf0 Christos Stathis
                                        addPermission();
217 a57faaf0 Christos Stathis
                                        hide();
218 a57faaf0 Christos Stathis
                                        break;
219 a57faaf0 Christos Stathis
                                case KeyCodes.KEY_ESCAPE:
220 a57faaf0 Christos Stathis
                                        hide();
221 a57faaf0 Christos Stathis
                                        break;
222 a57faaf0 Christos Stathis
                        }
223 a57faaf0 Christos Stathis
        }
224 a57faaf0 Christos Stathis
225 a57faaf0 Christos Stathis
226 a57faaf0 Christos Stathis
        @Override
227 a57faaf0 Christos Stathis
        public void center() {
228 a57faaf0 Christos Stathis
                super.center();
229 a57faaf0 Christos Stathis
                if (userAdd)
230 d5c6298f Christos Stathis
                        userBox.setFocus(true);
231 a57faaf0 Christos Stathis
        }
232 a57faaf0 Christos Stathis
}