root / src / gr / grnet / pithos / web / client / FolderPermissionsDialog.java @ f6c9957e
History | View | Annotate | Download (12.1 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2011-2012 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.commands.CreateGroupCommand; |
38 |
import gr.grnet.pithos.web.client.foldertree.File; |
39 |
import gr.grnet.pithos.web.client.foldertree.Folder; |
40 |
import gr.grnet.pithos.web.client.foldertree.Resource; |
41 |
import gr.grnet.pithos.web.client.rest.PostRequest; |
42 |
import gr.grnet.pithos.web.client.rest.PutRequest; |
43 |
import gr.grnet.pithos.web.client.rest.RestException; |
44 |
|
45 |
import java.util.Iterator; |
46 |
import java.util.Map; |
47 |
|
48 |
import com.google.gwt.core.client.GWT; |
49 |
import com.google.gwt.core.client.Scheduler; |
50 |
import com.google.gwt.dom.client.NativeEvent; |
51 |
import com.google.gwt.event.dom.client.ClickEvent; |
52 |
import com.google.gwt.event.dom.client.ClickHandler; |
53 |
import com.google.gwt.event.dom.client.KeyCodes; |
54 |
import com.google.gwt.event.dom.client.KeyDownEvent; |
55 |
import com.google.gwt.http.client.Response; |
56 |
import com.google.gwt.http.client.URL; |
57 |
import com.google.gwt.user.client.Command; |
58 |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
59 |
import com.google.gwt.user.client.ui.Anchor; |
60 |
import com.google.gwt.user.client.ui.Button; |
61 |
import com.google.gwt.user.client.ui.DialogBox; |
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.Label; |
65 |
import com.google.gwt.user.client.ui.VerticalPanel; |
66 |
|
67 |
/**
|
68 |
* The 'Folder properties' dialog box implementation.
|
69 |
*/
|
70 |
public class FolderPermissionsDialog extends DialogBox { |
71 |
|
72 |
protected Pithos app;
|
73 |
|
74 |
/**
|
75 |
* The widget that holds the folderName of the folder.
|
76 |
*/
|
77 |
Label folderName = new Label(); |
78 |
|
79 |
protected PermissionsList permList;
|
80 |
|
81 |
final Folder folder;
|
82 |
|
83 |
final VerticalPanel inner;
|
84 |
|
85 |
/**
|
86 |
* The widget's constructor.
|
87 |
*/
|
88 |
public FolderPermissionsDialog(final Pithos app, Folder selected) { |
89 |
this.app = app;
|
90 |
Anchor close = new Anchor("close"); |
91 |
close.addStyleName("close");
|
92 |
close.addClickHandler(new ClickHandler() {
|
93 |
|
94 |
@Override
|
95 |
public void onClick(ClickEvent event) { |
96 |
hide(); |
97 |
} |
98 |
}); |
99 |
|
100 |
setAnimationEnabled(true);
|
101 |
setGlassEnabled(true);
|
102 |
setStyleName("pithos-DialogBox");
|
103 |
|
104 |
// Enable IE selection for the dialog (must disable it upon closing it)
|
105 |
Pithos.enableIESelection(); |
106 |
|
107 |
folder = selected; |
108 |
|
109 |
setText("Folder permissions");
|
110 |
|
111 |
// Outer contains inner and buttons
|
112 |
VerticalPanel outer = new VerticalPanel();
|
113 |
outer.add(close); |
114 |
// Inner contains generalPanel and permPanel
|
115 |
inner = new VerticalPanel();
|
116 |
inner.addStyleName("inner");
|
117 |
|
118 |
|
119 |
folderName.setText(folder.getName()); |
120 |
|
121 |
VerticalPanel permPanel = new VerticalPanel();
|
122 |
FilePermissionsDialog.Images images = GWT.create(FilePermissionsDialog.Images.class); |
123 |
permList = new PermissionsList(images, folder.getPermissions(), folder.getOwner(), false, null); |
124 |
permPanel.add(permList); |
125 |
|
126 |
HorizontalPanel permButtons = new HorizontalPanel();
|
127 |
Button addUser = new Button("Add User", new ClickHandler() { |
128 |
@Override
|
129 |
public void onClick(ClickEvent event) { |
130 |
PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true); |
131 |
dlg.center(); |
132 |
} |
133 |
}); |
134 |
addUser.addStyleName("button");
|
135 |
permButtons.add(addUser); |
136 |
permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER); |
137 |
|
138 |
Button add = new Button("Add Group", new ClickHandler() { |
139 |
@Override
|
140 |
public void onClick(ClickEvent event) { |
141 |
if (app.getAccount().getGroups().isEmpty()) {
|
142 |
new GroupCreateDialog(app, new Command() { |
143 |
|
144 |
@Override
|
145 |
public void execute() { |
146 |
if (app.getAccount().getGroups().isEmpty())
|
147 |
return;
|
148 |
PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false); |
149 |
dlg.center(); |
150 |
} |
151 |
}).center(); |
152 |
} |
153 |
else {
|
154 |
PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false); |
155 |
dlg.center(); |
156 |
} |
157 |
} |
158 |
}); |
159 |
add.addStyleName("button");
|
160 |
permButtons.add(add); |
161 |
permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER); |
162 |
|
163 |
permButtons.setSpacing(8);
|
164 |
permPanel.add(permButtons); |
165 |
|
166 |
inner.add(permPanel); |
167 |
|
168 |
outer.add(inner); |
169 |
|
170 |
// Create the 'Create/Update' button, along with a listener that hides the dialog
|
171 |
// when the button is clicked and quits the application.
|
172 |
String okLabel = "Update"; |
173 |
final Button ok = new Button(okLabel, new ClickHandler() { |
174 |
@Override
|
175 |
public void onClick(ClickEvent event) { |
176 |
updateFolder(); |
177 |
closeDialog(); |
178 |
} |
179 |
}); |
180 |
ok.addStyleName("button");
|
181 |
outer.add(ok); |
182 |
outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER); |
183 |
|
184 |
setWidget(outer); |
185 |
} |
186 |
|
187 |
@Override
|
188 |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
189 |
super.onPreviewNativeEvent(preview);
|
190 |
|
191 |
NativeEvent evt = preview.getNativeEvent(); |
192 |
if (evt.getType().equals(KeyDownEvent.getType().getName()))
|
193 |
// Use the popup's key preview hooks to close the dialog when either
|
194 |
// enter or escape is pressed.
|
195 |
switch (evt.getKeyCode()) {
|
196 |
case KeyCodes.KEY_ENTER:
|
197 |
updateFolder(); |
198 |
closeDialog(); |
199 |
break;
|
200 |
case KeyCodes.KEY_ESCAPE:
|
201 |
closeDialog(); |
202 |
break;
|
203 |
} |
204 |
} |
205 |
|
206 |
|
207 |
/**
|
208 |
* Enables IE selection prevention and hides the dialog
|
209 |
* (we disable the prevention on creation of the dialog)
|
210 |
*/
|
211 |
public void closeDialog() { |
212 |
Pithos.preventIESelection(); |
213 |
hide(); |
214 |
} |
215 |
|
216 |
void updateFolder() {
|
217 |
final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null); |
218 |
updateMetadata(folder.getUri() + "?update=", perms);
|
219 |
} |
220 |
|
221 |
protected void updateMetadata(final String path, final Map<String, Boolean[]> newPermissions) { |
222 |
if (newPermissions != null) { |
223 |
PostRequest updateFolder = new PostRequest(app.getApiPath(), folder.getOwner(), path) {
|
224 |
@Override
|
225 |
public void onSuccess(Resource result) { |
226 |
app.updateFolder(folder.getParent(), false, new Command() { |
227 |
|
228 |
@Override
|
229 |
public void execute() { |
230 |
app.updateMySharedRoot(); |
231 |
} |
232 |
}, true);
|
233 |
} |
234 |
|
235 |
@Override
|
236 |
public void onError(Throwable t) { |
237 |
GWT.log("", t);
|
238 |
app.setError(t); |
239 |
if (t instanceof RestException) { |
240 |
if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) { //Probably a virtual folder |
241 |
final String path1 = folder.getUri(); |
242 |
PutRequest newFolder = new PutRequest(app.getApiPath(), folder.getOwner(), path1) {
|
243 |
@Override
|
244 |
public void onSuccess(Resource result) { |
245 |
updateMetadata(path, newPermissions); |
246 |
} |
247 |
|
248 |
@Override
|
249 |
public void onError(Throwable _t) { |
250 |
GWT.log("", _t);
|
251 |
app.setError(_t); |
252 |
if(_t instanceof RestException){ |
253 |
app.displayError("Unable to update folder: " + ((RestException) _t).getHttpStatusText());
|
254 |
} |
255 |
else
|
256 |
app.displayError("System error modifying folder: " + _t.getMessage());
|
257 |
} |
258 |
|
259 |
@Override
|
260 |
protected void onUnauthorized(Response response) { |
261 |
app.sessionExpired(); |
262 |
} |
263 |
}; |
264 |
newFolder.setHeader("X-Auth-Token", app.getToken());
|
265 |
newFolder.setHeader("Content-Type", "application/folder"); |
266 |
newFolder.setHeader("Accept", "*/*"); |
267 |
newFolder.setHeader("Content-Length", "0"); |
268 |
Scheduler.get().scheduleDeferred(newFolder); |
269 |
} |
270 |
else if (((RestException) t).getHttpStatusCode() == Response.SC_CONFLICT) { |
271 |
app.displayError("Cannot set permissions. Probably subfolders or files already have permissions set");
|
272 |
} |
273 |
else
|
274 |
app.displayError("Εrror modifying folder: " + t.getMessage());
|
275 |
} |
276 |
else
|
277 |
app.displayError("System error modifying folder: " + t.getMessage());
|
278 |
} |
279 |
|
280 |
@Override
|
281 |
protected void onUnauthorized(Response response) { |
282 |
app.sessionExpired(); |
283 |
} |
284 |
}; |
285 |
updateFolder.setHeader("X-Auth-Token", app.getToken());
|
286 |
String readPermHeader = "read="; |
287 |
String writePermHeader = "write="; |
288 |
for (String u : newPermissions.keySet()) { |
289 |
Boolean[] p = newPermissions.get(u); |
290 |
if (p[0] != null && p[0]) |
291 |
readPermHeader += u + ",";
|
292 |
if (p[1] != null && p[1]) |
293 |
writePermHeader += u + ",";
|
294 |
} |
295 |
if (readPermHeader.endsWith("=")) |
296 |
readPermHeader = "";
|
297 |
else if (readPermHeader.endsWith(",")) |
298 |
readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1); |
299 |
if (writePermHeader.endsWith("=")) |
300 |
writePermHeader = "";
|
301 |
else if (writePermHeader.endsWith(",")) |
302 |
writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1); |
303 |
String permHeader = readPermHeader + ((readPermHeader.length() > 0 && writePermHeader.length() > 0) ? ";" : "") + writePermHeader; |
304 |
if (permHeader.length() == 0) |
305 |
permHeader = "~";
|
306 |
else
|
307 |
permHeader = URL.encodePathSegment(permHeader);
|
308 |
updateFolder.setHeader("X-Object-Sharing", permHeader);
|
309 |
Scheduler.get().scheduleDeferred(updateFolder); |
310 |
} |
311 |
else
|
312 |
app.updateFolder(folder.getParent(), false, new Command() { |
313 |
|
314 |
@Override
|
315 |
public void execute() { |
316 |
app.updateMySharedRoot(); |
317 |
} |
318 |
}, true);
|
319 |
} |
320 |
} |