root / src / pithos / content / groups / groups.js @ 46:f16f6e0e5b69
History | View | Annotate | Download (14.8 kB)
1 |
// The Pithos File Manager Firefox Extension is funded by GRNET S.A.
|
---|---|
2 |
// (http://www.grnet.gr)
|
3 |
//
|
4 |
// Copyright (c) 2009, Christos KK Loverdos, Vassilios Karakoidas.
|
5 |
// All rights reserved.
|
6 |
//
|
7 |
// Redistribution and use in source and binary forms, with or without
|
8 |
// modification, are permitted provided that the following conditions are
|
9 |
// met:
|
10 |
//
|
11 |
// - Redistributions of source code must retain the above copyright
|
12 |
// notice, this list of conditions and the following disclaimer.
|
13 |
// - Redistributions in binary form must reproduce the above
|
14 |
// copyright notice, this list of conditions and the following
|
15 |
// disclaimer in the documentation and/or other materials provided
|
16 |
// with the distribution.
|
17 |
// - Neither the name of GRNET S.A. nor the names of its contributors
|
18 |
// may be used to endorse or promote products derived from this
|
19 |
// software without specific prior written permission.
|
20 |
//
|
21 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
22 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
23 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
24 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
25 |
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
26 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
27 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
28 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
29 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
30 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
31 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32 |
|
33 |
connection_obj = null;
|
34 |
mimes = null;
|
35 |
cache = null;
|
36 |
vfs = null;
|
37 |
jsongroups = null;
|
38 |
users = new Object();
|
39 |
debug = new Array();
|
40 |
$mb = null; |
41 |
currentElement = null;
|
42 |
ui = null;
|
43 |
|
44 |
function groupInit() { |
45 |
connection_obj = new ConnectionObject();
|
46 |
mimes = new Mimes();
|
47 |
cache = new FileCache();
|
48 |
vfs = new VFS();
|
49 |
|
50 |
$mb = jQuery.noConflict();
|
51 |
|
52 |
fetchGroups(); |
53 |
ui = new buildList();
|
54 |
ui.buildUI(); |
55 |
} |
56 |
|
57 |
function fetchGroups() { |
58 |
groups = connection_obj.rest_request_get(connection_obj.user_homepage().groups,'');
|
59 |
if(connection_obj.success(groups.status)) {
|
60 |
jsongroups = JSON.parse(groups.responseText); |
61 |
} |
62 |
|
63 |
} |
64 |
function buildList() { |
65 |
/* <treechildren>
|
66 |
<treeitem container="true" open="true">
|
67 |
<treerow>
|
68 |
<treecell label="Guys"/>
|
69 |
</treerow>
|
70 |
|
71 |
<treechildren>
|
72 |
<treeitem>
|
73 |
<treerow>
|
74 |
<treecell label="Bob"/>
|
75 |
<treecell label="Carpenter"/>
|
76 |
</treerow>
|
77 |
</treeitem>
|
78 |
<treeitem>
|
79 |
<treerow>
|
80 |
<treecell label="Jerry"/>
|
81 |
<treecell label="Hodge"/>
|
82 |
</treerow>
|
83 |
</treeitem>
|
84 |
</treechildren>
|
85 |
</treeitem>
|
86 |
</treechildren>
|
87 |
*/
|
88 |
this.buildUI = function() { |
89 |
for each(var groupdetails in jsongroups) { |
90 |
debug.push(groupdetails); |
91 |
var groupitem = document.createElement('treeitem'); |
92 |
var groupitem_row = document.createElement('treerow'); |
93 |
var groupitem_cell = document.createElement('treecell'); |
94 |
|
95 |
var ch1 = document.createElement('treechildren'); |
96 |
|
97 |
$mb('#firstchild').append(groupitem); |
98 |
$mb(groupitem).append(groupitem_row);
|
99 |
$mb(groupitem_row).append(groupitem_cell);
|
100 |
$mb(groupitem).attr({
|
101 |
container : true, |
102 |
open : true |
103 |
}); |
104 |
|
105 |
$mb(groupitem_cell).attr({
|
106 |
label : groupdetails.name,
|
107 |
id : groupdetails.uri,
|
108 |
flag : 'group' |
109 |
}); |
110 |
|
111 |
// Is this group empty?
|
112 |
if (users[groupdetails.name].length == 0) { |
113 |
continue;
|
114 |
} |
115 |
|
116 |
$mb(groupitem).append(ch1);
|
117 |
|
118 |
// For every user in the group, add <treechildren>
|
119 |
for each( var currentuser in users[groupdetails.name]) { |
120 |
//alert('in '+groupdetails.name+' i have'+currentuser);
|
121 |
var it = document.createElement('treeitem'); |
122 |
var row = document.createElement('treerow'); |
123 |
var cell = document.createElement('treecell'); |
124 |
$mb(ch1).append(it);
|
125 |
$mb(it).append(row);
|
126 |
if(currentuser.length == 0) { |
127 |
continue;
|
128 |
} else {
|
129 |
$mb(row).append(cell);
|
130 |
$mb(cell).attr({
|
131 |
label: currentuser.username,
|
132 |
id : currentuser.uri
|
133 |
}); |
134 |
} |
135 |
} |
136 |
} |
137 |
} |
138 |
|
139 |
this.cleanRows = function(uri) { |
140 |
$mb('#firstchild').empty(); |
141 |
} |
142 |
|
143 |
this.refreshTree = function() { |
144 |
this.cleanRows();
|
145 |
this.buildUI();
|
146 |
} |
147 |
this.userRefresh = function() { |
148 |
buildUsers(); |
149 |
} |
150 |
// constructor ...
|
151 |
|
152 |
// Build the users array based on group entries
|
153 |
buildUsers(); |
154 |
|
155 |
|
156 |
// Private functions
|
157 |
|
158 |
// build users array from scratch based on groups. Will clean up any changes already done to it.
|
159 |
function buildUsers() { |
160 |
users = new Object();
|
161 |
for each(var groupdetails in jsongroups) { |
162 |
fetchFriendsFromNet(groupdetails); |
163 |
} |
164 |
|
165 |
} |
166 |
/* fetch the users of a group and update them in the array 'users' */
|
167 |
function fetchFriendsFromNet(currentgroup) { |
168 |
var response = connection_obj.rest_request_get(currentgroup.uri,''); |
169 |
|
170 |
/* if the users associative array does not have a key based on the groupname, create one */
|
171 |
if(!users.hasOwnProperty(currentgroup.name)) {
|
172 |
users[currentgroup.name] = new Array();
|
173 |
} |
174 |
if(connection_obj.success(response.status)) {
|
175 |
// valid status, parse the array of user uris
|
176 |
var jsonusers = JSON.parse(response.responseText);
|
177 |
// Is the group empty
|
178 |
if (jsonusers.length == 0) { |
179 |
return;
|
180 |
} |
181 |
//Request user details for each user uri
|
182 |
for each(var i in jsonusers) { |
183 |
var jsonuserresp = connection_obj.rest_request_get(i,''); |
184 |
|
185 |
var jsonuser = null; |
186 |
if (! connection_obj.success(jsonuserresp.status)) {
|
187 |
return;
|
188 |
} |
189 |
else {
|
190 |
jsonuser = JSON.parse(jsonuserresp.responseText); |
191 |
jsonuser.uri = i; |
192 |
/* assign the json user response. json has the form
|
193 |
"{"home":"http://pithos.grnet.gr/pithos/rest/ebstest@grnet-hq.admin.grnet.gr","username":"ebstest@grnet-hq.admin.grnet.gr","name":"ebs test"}"
|
194 |
*/
|
195 |
users[currentgroup.name].push(jsonuser); |
196 |
} |
197 |
|
198 |
} |
199 |
|
200 |
//users[currentgroup.name] = jsonusers;
|
201 |
// alert('user list is '+users[currentgroup.name]);
|
202 |
} |
203 |
} |
204 |
} |
205 |
|
206 |
function closeWindow() { |
207 |
window.close(); |
208 |
} |
209 |
|
210 |
// Function to delete users or empty groups
|
211 |
function deleteUserGroup(event) { |
212 |
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
213 |
|
214 |
thetree = document.getElementById('maintree');
|
215 |
if (thetree.currentIndex == -1) { |
216 |
// User has not selected a row using his mouse
|
217 |
promptAlert('Please select an item to delete before pushing this button');
|
218 |
return;
|
219 |
} |
220 |
|
221 |
var selection = thetree.view.selection;
|
222 |
var cellText = thetree.view.getCellText(thetree.currentIndex, thetree.columns.getColumnAt(0)); |
223 |
var selectedElement = thetree.view.getItemAtIndex(thetree.currentIndex);
|
224 |
var selElemId = $mb('treecell',selectedElement).attr('id'); |
225 |
var selElemFlag = $mb('treecell',selectedElement).attr('flag'); |
226 |
var hasChild = $mb('treechildren',selectedElement); |
227 |
|
228 |
// check if user has selected a group
|
229 |
if (selElemFlag == 'group') { |
230 |
// Check if there is a child
|
231 |
if (hasChild.length == 0) { |
232 |
// safe to delete it ...
|
233 |
var answer=promptBox('Delete the group '+cellText+' ?'); |
234 |
if(answer == false) { |
235 |
return;
|
236 |
} |
237 |
|
238 |
var success = connection_obj.rest_request_delete(selElemId,''); |
239 |
if (!success) {
|
240 |
promptAlert('Operation failed');
|
241 |
} |
242 |
else {
|
243 |
// Refresh the tree
|
244 |
$mb(selectedElement).remove();
|
245 |
fetchGroups(); |
246 |
ui.userRefresh(); |
247 |
} |
248 |
} |
249 |
else {
|
250 |
promptAlert('Please delete the users first before attempting to erase a group');
|
251 |
} |
252 |
} |
253 |
else {
|
254 |
// User has selected a 'user' to be removed from the group ...
|
255 |
var answer = promptBox('Delete the user '+cellText+' ?'); |
256 |
if (answer == false) { |
257 |
return;
|
258 |
} |
259 |
var success = connection_obj.rest_request_delete(selElemId,''); |
260 |
|
261 |
if (!success) {
|
262 |
promptAlert('Operation failed');
|
263 |
} else {
|
264 |
// Refresh the tree ...
|
265 |
$mb(selectedElement).remove();
|
266 |
fetchGroups(); |
267 |
ui.userRefresh(); |
268 |
ui.refreshTree(); |
269 |
} |
270 |
|
271 |
} |
272 |
} |
273 |
|
274 |
// This will create a new group (regardless of what the user has selected)
|
275 |
function addGroup() { |
276 |
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
277 |
|
278 |
//prompt for new group name
|
279 |
var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] |
280 |
.getService(Components.interfaces.nsIPromptService); |
281 |
input = {value: ""}; |
282 |
check = {value: false}; |
283 |
|
284 |
var results = prompts.prompt(window, 'Create new group', 'Group name:',input,null,check); |
285 |
if (results) {
|
286 |
// Ok has been clicked
|
287 |
queryurl = connection_obj.user_homepage().groups; |
288 |
var res = connection_obj.rest_request_post(queryurl,'?name='+input.value,''); |
289 |
if(!connection_obj.success(res.status)) {
|
290 |
promptAlert('Unable to create group '+input.value);
|
291 |
} |
292 |
else {
|
293 |
// On success update the view
|
294 |
fetchGroups(); |
295 |
ui.userRefresh(); |
296 |
ui.refreshTree(); |
297 |
} |
298 |
} |
299 |
} |
300 |
|
301 |
function addUser() { |
302 |
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
303 |
|
304 |
thetree = document.getElementById('maintree');
|
305 |
if (thetree.currentIndex == -1) { |
306 |
// User has not selected a row using his mouse
|
307 |
promptAlert('Please select a group where to add the user before pushing this button');
|
308 |
return;
|
309 |
} |
310 |
|
311 |
var selection = thetree.view.selection;
|
312 |
var cellText = thetree.view.getCellText(thetree.currentIndex, thetree.columns.getColumnAt(0)); |
313 |
var selectedElement = thetree.view.getItemAtIndex(thetree.currentIndex);
|
314 |
var selElemId = $mb('treecell',selectedElement).attr('id'); |
315 |
var selElemName = $mb('treecell',selectedElement).attr('label'); |
316 |
var selElemFlag = $mb('treecell',selectedElement).attr('flag'); |
317 |
var hasChild = $mb('treechildren',selectedElement); |
318 |
|
319 |
if (selElemFlag != 'group') { |
320 |
promptAlert('Please select a group where to add the user before pushing this button');
|
321 |
return;
|
322 |
} |
323 |
|
324 |
//prompt for username
|
325 |
var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] |
326 |
.getService(Components.interfaces.nsIPromptService); |
327 |
input = {value: ""}; |
328 |
check = {value: false}; |
329 |
|
330 |
var results = prompts.prompt(window, 'Add user in group', 'Username:',input,null,check); |
331 |
if (results) {
|
332 |
// Ok has been clicked
|
333 |
|
334 |
// Bring up a dialog with similar names to choose
|
335 |
// example userparams: {username :'ebstest@mplapla', name: 'ebstest'}
|
336 |
var userparams = findUser(input.value);
|
337 |
if (userparams == false) { |
338 |
promptAlert('Name not found');
|
339 |
} else {
|
340 |
var ok = checkUserExistsInGroup(userparams.username,selElemName);
|
341 |
if (ok == false) { |
342 |
promptAlert('User already exists in group');
|
343 |
return;
|
344 |
} |
345 |
else {
|
346 |
pushNewUserInGroup(userparams.username,selElemId); |
347 |
} |
348 |
} |
349 |
} |
350 |
|
351 |
} |
352 |
|
353 |
// Check if the supplied user exists in the supplied groupname
|
354 |
function checkUserExistsInGroup(user,groupname) { |
355 |
for each( var i in users[groupname]) { |
356 |
if(i.username == user) {
|
357 |
return false; |
358 |
} |
359 |
} |
360 |
return(true); |
361 |
} |
362 |
|
363 |
function pushNewUserInGroup(username,groupuri) { |
364 |
var answer = addUserInGroup(username,groupuri);
|
365 |
if (answer) {
|
366 |
fetchGroups(); |
367 |
ui.userRefresh(); |
368 |
ui.refreshTree(); |
369 |
|
370 |
} |
371 |
} |
372 |
|
373 |
/* This should be moved to connection.js or something */
|
374 |
function addUserInGroup(username,groupuri) { |
375 |
var response = connection_obj.rest_request_post(groupuri,'?name='+username,''); |
376 |
if(!connection_obj.success(response.status)) {
|
377 |
promptAlert('Unable to add user in group');
|
378 |
return false; |
379 |
} else {
|
380 |
return true; |
381 |
} |
382 |
} |
383 |
|
384 |
// Find a username based on a name, e.g. search for 'ebstest' should return ebstest@....
|
385 |
// on error return false
|
386 |
function findUser(username) { |
387 |
queryurl = connection_obj.rest_url+'/users/'+username;
|
388 |
|
389 |
// Check if the user has supplied the complete username e.g. ebstest@mplampla
|
390 |
var pos = username.indexOf('@'); |
391 |
if (pos != -1) { |
392 |
var basename = username.substring(0,pos); |
393 |
queryurl = connection_obj.rest_url+'/users/'+basename;
|
394 |
} |
395 |
|
396 |
var response = connection_obj.rest_request_get(queryurl,''); |
397 |
if (connection_obj.success(response.status)) {
|
398 |
var jsonlist = JSON.parse(response.responseText);
|
399 |
|
400 |
// empty array returned when nothing found
|
401 |
if (jsonlist.length == 0) { |
402 |
return false; |
403 |
} |
404 |
// Bring up a window with the users found ...
|
405 |
var retvals = {inp: jsonlist, out : null}; |
406 |
|
407 |
window.openDialog("../dialogs/selectlist.xul", "Users found", "chrome,dialog,modal,resizable", retvals).focus(); |
408 |
|
409 |
// Check if we have valid return values
|
410 |
if (retvals.out != null) { |
411 |
// Ok has been clicked
|
412 |
// return the username
|
413 |
return (retvals.out);
|
414 |
} |
415 |
else {
|
416 |
// No user selected ...
|
417 |
return false; |
418 |
} |
419 |
} else {
|
420 |
return false; |
421 |
} |
422 |
|
423 |
} |
424 |
/* Alert functions */
|
425 |
|
426 |
function promptBox(message) { |
427 |
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
428 |
var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService); |
429 |
var result = prompts.confirm(window, "Confirm action", message); |
430 |
return(result);
|
431 |
} |
432 |
|
433 |
function promptAlert(message) { |
434 |
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
435 |
var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] |
436 |
.getService(Components.interfaces.nsIPromptService); |
437 |
prompts.alert(window, "Information", message);
|
438 |
} |