source: trunk/www/common/share/share.jsp @ 5907

Last change on this file since 5907 was 5907, checked in by Nicklas Nordborg, 10 years ago

References #1655: GUI improvements

New design on some more pages:

  • Impersonate
  • Switch user
  • Calendar
  • Close popup message
  • Configure columns
  • Manage lists contexts
  • View help
  • Set owner
  • Share items
  • About
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 19.0 KB
Line 
1<%-- $Id: share.jsp 5907 2011-12-13 08:36:28Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2005 Nicklas Nordborg
4  Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson
5
6  This file is part of BASE - BioArray Software Environment.
7  Available at http://base.thep.lu.se/
8
9  BASE is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License
11  as published by the Free Software Foundation; either version 3
12  of the License, or (at your option) any later version.
13
14  BASE is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with BASE. If not, see <http://www.gnu.org/licenses/>.
21  ------------------------------------------------------------------
22
23  Configure the order and visibility of columns in a table of items.
24  This page will use the TableColumn.getTableColumns(item) to retreive the
25  list of all available columns for that item type and
26  getSession().getSetting(item+".columns") to retreive a comma-separated
27  list of the current order of visible columns.
28
29  @param item The type of items in the table
30
31  @author Nicklas
32  @version 2.0
33--%>
34<%@ page pageEncoding="UTF-8" session="false"
35  import="net.sf.basedb.core.SessionControl"
36  import="net.sf.basedb.core.DbControl"
37  import="net.sf.basedb.core.SystemItems"
38  import="net.sf.basedb.core.Item"
39  import="net.sf.basedb.core.ItemKey"
40  import="net.sf.basedb.core.Nameable"
41  import="net.sf.basedb.core.SharedItem"
42  import="net.sf.basedb.core.ProjectKey"
43  import="net.sf.basedb.core.Permission"
44  import="net.sf.basedb.core.MultiPermissions"
45  import="net.sf.basedb.core.User"
46  import="net.sf.basedb.core.Group"
47  import="net.sf.basedb.core.Project"
48  import="net.sf.basedb.core.ItemQuery"
49  import="net.sf.basedb.core.ItemResultIterator"
50  import="net.sf.basedb.core.query.Orders"
51  import="net.sf.basedb.core.query.Hql"
52  import="net.sf.basedb.clients.web.Base"
53  import="net.sf.basedb.clients.web.PermissionUtil"
54  import="net.sf.basedb.clients.web.WebException"
55  import="net.sf.basedb.clients.web.util.HTML"
56  import="net.sf.basedb.util.Values"
57  import="java.util.EnumSet"
58  import="java.util.Set"
59  import="java.util.Map"
60%>
61<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
62<%@ taglib prefix="t" uri="/WEB-INF/tab.tld" %>
63<%!
64private static final int READ_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.READ));
65private static final int USE_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.USE));
66private static final int WRITE_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.WRITE));
67private static final int DELETE_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.DELETE));
68private static final int SET_OWNER_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.SET_OWNER));
69private static final int SET_PERMISSION_CODE = PermissionUtil.getPermissionCode(EnumSet.of(Permission.SET_PERMISSION));
70
71private String getPermissionString(Set<Permission> permissions)
72{
73  StringBuilder sb = new StringBuilder();
74  sb.append(permissions.contains(Permission.READ) ? "R" : "-");
75  sb.append(permissions.contains(Permission.USE) ? "U" : "-");
76  sb.append(permissions.contains(Permission.WRITE) ? "W" : "-");
77  sb.append(permissions.contains(Permission.DELETE) ? "D" : "-");
78  sb.append(permissions.contains(Permission.SET_OWNER) ? "O" : "-");
79  sb.append(permissions.contains(Permission.SET_PERMISSION) ? "P" : "-");
80  return sb.toString();
81}
82%>
83<%
84final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
85final String ID = sc.getId();
86final Item itemType = Item.valueOf(request.getParameter("item_type"));
87final String subContext = Values.getString(request.getParameter("subcontext"), "");
88
89final DbControl dc = sc.newDbControl();
90ItemResultIterator<User> users = null;
91ItemResultIterator<Group> groups = null;
92ItemResultIterator<Project> projects = null;
93try
94{
95  MultiPermissions mp = (MultiPermissions)sc.getCurrentContext(itemType, subContext).getObject("MultiPermissions");
96  if (mp == null)
97  {
98    throw new WebException("popup", "Item not found", "No MultiPermissions object found for item: " + itemType);
99  }
100
101  // Query to retrieve users
102  final ItemQuery<User> userQuery = mp.getUsers();
103  userQuery.order(Orders.asc(Hql.property("name")));
104
105  // Query to retrieve groups
106  final ItemQuery<Group> groupQuery = mp.getGroups();
107  groupQuery.order(Orders.asc(Hql.property("name")));
108
109  // Query to retrieve projects
110  final ItemQuery<Project> projectQuery = mp.getProjects();
111  projectQuery.order(Orders.asc(Hql.property("name")));
112 
113  final boolean writePermission = true;
114 
115  /*
116    Set the headline with the item's name or,
117    if there are more then one item, the text 'multiple item'
118  */
119  String headLine = "";
120  Set<SharedItem> currentItems = mp.getItems();
121  if(currentItems.size() == 1)
122  {
123    SharedItem item = currentItems.iterator().next(); 
124    if(item instanceof Nameable)
125    {
126      headLine = "Set access permissions -- " + HTML.encodeTags(((Nameable)item).getName());
127    }
128    else
129    {
130      headLine = "Set access permissions -- " + HTML.encodeTags(item.toString());
131    }
132  }
133  else
134  {
135    headLine = "Set access permissions -- Multiple items (" + currentItems.size() + ")";
136  }
137  boolean hasDirectory = mp.getItems(Item.DIRECTORY).size() > 0;
138
139  %>
140  <base:page type="popup" title="Set access permissions">
141  <base:head scripts="linkitems.js" >
142    <script language="JavaScript">
143    var share_to_everyone = <%=sc.hasSystemPermission(Permission.SHARE_TO_EVERYONE)%>;
144    var everyone_id = <%=SystemItems.getId(Group.EVERYONE)%>;
145    function initList()
146    {
147      var members = document.forms['share'].members;
148      Link.addNewSection(members, new Section('U', 'Users'));
149      <%
150      users = userQuery.iterate(dc);
151      while (users.hasNext())
152      {
153        User user = users.next();
154        Map<ItemKey, Set<Permission>> p = mp.getPermissions(user);
155        String permissionString = "varying";
156        int permissionCode = 0;
157        if (MultiPermissions.allSame(p.values()))
158        {
159          Set<Permission> first = p.values().iterator().next();
160          permissionString = getPermissionString(first);
161          permissionCode = PermissionUtil.getPermissionCode(first);
162        }
163        %>
164        Link.addNewItem(members, new Item('U', <%=user.getId()%>, '<%=HTML.javaScriptEncode(user.getName())%> [<%=permissionString%>]', <%=permissionCode%>));
165        <%
166      }
167      %>
168      Link.addNewSection(members, new Section('G', 'Groups'));
169      <%
170      groups = groupQuery.iterate(dc);
171      while (groups.hasNext())
172      {
173        Group group = groups.next();
174        Map<ItemKey, Set<Permission>> p = mp.getPermissions(group);
175        String permissionString = "varying";
176        int permissionCode = 0;
177        if (MultiPermissions.allSame(p.values()))
178        {
179          Set<Permission> first = p.values().iterator().next();
180          permissionString = getPermissionString(first);
181          permissionCode = PermissionUtil.getPermissionCode(first);
182        }
183        %>
184        Link.addNewItem(members, new Item('G', <%=group.getId()%>, '<%=HTML.javaScriptEncode(group.getName())%> [<%=permissionString%>]', <%=permissionCode%>));
185        <%
186      }
187      %>
188      Link.addNewSection(members, new Section('P', 'Projects'));
189      <%
190      projects = projectQuery.iterate(dc);
191      while (projects.hasNext())
192      {
193        Project project = projects.next();
194        Map<ProjectKey, Set<Permission>> p = mp.getPermissions(project);
195        String permissionString = "varying";
196        int permissionCode = 0;
197        if (MultiPermissions.allSame(p.values()))
198        {
199          Set<Permission> first = p.values().iterator().next();
200          permissionString = getPermissionString(first);
201          permissionCode = PermissionUtil.getPermissionCode(first);
202        }
203        %>
204        Link.addNewItem(members, new Item('P', <%=project.getId()%>, '<%=HTML.javaScriptEncode(project.getName())%> [<%=permissionString%>]', <%=permissionCode%>));
205        <%
206      }
207      %>
208      Link.addNewSection(members, new Section('T', 'Permission templates'));
209    }
210    // Submit the form
211    function saveSettings()
212    {
213      var frm = document.forms['share'];
214      frm.modifiedUsers.value = Link.exportModified(frm, 'U').join(',');
215      frm.modifiedGroups.value = Link.exportModified(frm, 'G').join(',');
216      frm.modifiedProjects.value = Link.exportModified(frm, 'P').join(',');
217      frm.permissionTemplates.value = Link.getListIds(frm.members, 'T').join(',');
218      frm.submit();
219    }
220    function correctPermissions(checked)
221    {
222      var frm = document.forms['share'];
223      if (checked)
224      {
225        frm['set_permission'].checked = frm['set_permission'].checked;
226        frm['set_owner'].checked = frm['set_owner'].checked;
227        frm['delete'].checked = frm['delete'].checked ;
228        frm['write'].checked = frm['write'].checked || frm['delete'].checked || frm['set_owner'].checked || frm['set_permission'].checked;
229        frm['use'].checked = frm['use'].checked || frm['write'].checked;
230        frm['read'].checked = frm['read'].checked || frm['use'].checked;
231      }
232      else
233      {
234        frm['read'].checked = frm['read'].checked;
235        frm['use'].checked = frm['use'].checked && frm['read'].checked;
236        frm['write'].checked = frm['write'].checked && frm['use'].checked;
237        frm['delete'].checked = frm['delete'].checked && frm['write'].checked;
238        frm['set_owner'].checked = frm['set_owner'].checked && frm['write'].checked;
239        frm['set_permission'].checked = frm['set_permission'].checked && frm['write'].checked;
240      }
241    }
242    function getPermissionCode()
243    {
244      var permissionCode = 0;
245      var frm = document.forms['share'];
246      permissionCode += frm['read'].checked ? <%=READ_CODE%>: 0;
247      permissionCode += frm['use'].checked ? <%=USE_CODE%>: 0;
248      permissionCode += frm['write'].checked ? <%=WRITE_CODE%>: 0;
249      permissionCode += frm['delete'].checked ? <%=DELETE_CODE%>: 0;
250      permissionCode += frm['set_owner'].checked ? <%=SET_OWNER_CODE%>: 0;
251      permissionCode += frm['set_permission'].checked ? <%=SET_PERMISSION_CODE%>: 0;
252      return permissionCode;
253    }
254    function getPermissionString(permissionCode)
255    {
256      var s = '';
257      s += permissionCode & <%=READ_CODE%> ? "R" : "-";
258      s += permissionCode & <%=USE_CODE%> ? "U" : "-";
259      s += permissionCode & <%=WRITE_CODE%> ? "W" : "-";
260      s += permissionCode & <%=DELETE_CODE%> ? "D" : "-";
261      s += permissionCode & <%=SET_OWNER_CODE%> ? "O" : "-";
262      s += permissionCode & <%=SET_PERMISSION_CODE%> ? "P" : "-";
263      return s;
264    }
265    function showPermissions(permissionCode, isEveryone, isTemplate)
266    {
267      var disabled = isTemplate || (isEveryone && !share_to_everyone);
268      var frm = document.forms['share'];
269      frm['read'].checked = permissionCode & <%=READ_CODE%>;
270      frm['use'].checked = permissionCode & <%=USE_CODE%>;
271      frm['write'].checked = permissionCode & <%=WRITE_CODE%>;
272      frm['delete'].checked = permissionCode & <%=DELETE_CODE%>;
273      frm['set_owner'].checked = permissionCode & <%=SET_OWNER_CODE%>;
274      frm['set_permission'].checked = permissionCode & <%=SET_PERMISSION_CODE%>;
275      frm['read'].disabled = disabled;
276      frm['use'].disabled = disabled;
277      frm['write'].disabled = disabled;
278      frm['delete'].disabled = disabled;
279      frm['set_owner'].disabled = disabled;
280      frm['set_permission'].disabled = disabled;
281      if (isEveryone && !share_to_everyone)
282      {
283        Main.show('share_disabled');
284      }
285      else
286      {
287        Main.hide('share_disabled');
288      }
289    }
290   
291    function permissionsOnClick(name)
292    {
293      var frm = document.forms['share'];
294      var radio = frm[name];
295      correctPermissions(radio.checked);
296      var permissionCode = getPermissionCode();
297      var permissionString = getPermissionString(permissionCode);
298      for (var i = 0; i < frm.members.length; i++)  // >
299      {
300        var option = frm.members[i];
301        if (option.selected && option.item && option.item.id)
302        {
303          var item = option.item;
304          var disabled = item.type == 'T' || (item.id == everyone_id && item.type == 'G' && !share_to_everyone);
305          if (!disabled)
306          {
307            option.item.value = permissionCode;
308            var text = option.text.replace(/\[.*\]/, '['+permissionString+']');
309            option.text = text;
310          }
311        }
312      }
313    }
314 
315    function membersOnChange()
316    {
317      var frm = document.forms['share'];
318      var item = frm.members[frm.members.selectedIndex].item;
319      if (item && item.id)
320      {
321        var isTemplate = item.type == 'T';
322        var isEveryone = item.id == everyone_id && item.type == 'G';
323        showPermissions(item.value, isEveryone, isTemplate);
324      }
325    }
326
327    function addUsersOnClick()
328    {
329      var ids = Link.getListIds(document.forms['share'].members, 'U');
330      var excludes = ids.join(',');
331      Main.openPopup('../../admin/users/index.jsp?ID=<%=ID%>&cmd=UpdateContext&mode=selectmultiple&callback=addUserCallback&permission=READ&exclude='+excludes, 'AddUsers', 1000, 700);
332    }
333    function addUserCallback(userId, name)
334    {
335      var permissionCode = getPermissionCode();
336      var permissionString = getPermissionString(permissionCode);
337      var item = Link.getItem('U', userId);
338      if (!item) item = new Item('U', userId, name+' ['+permissionString+']', permissionCode, 0);
339      Link.addItem(document.forms['share'].members, item);
340      membersOnChange();
341    }
342    function addGroupsOnClick()
343    {
344      var ids = Link.getListIds(document.forms['share'].members, 'G');
345      <%
346      if (!sc.hasSystemPermission(Permission.SHARE_TO_EVERYONE))
347      {
348        %>
349        ids[ids.length] = '<%=SystemItems.getId(Group.EVERYONE)%>';
350        <%
351      }
352      %>
353      var excludes = ids.join(',');
354      Main.openPopup('../../admin/groups/index.jsp?ID=<%=ID%>&cmd=UpdateContext&mode=selectmultiple&callback=addGroupCallback&permission=READ&exclude='+excludes, 'AddGroups', 1000, 700);
355    }
356    function addGroupCallback(groupId, name)
357    {
358      var permissionCode = getPermissionCode();
359      var permissionString = getPermissionString(permissionCode);
360      var item = Link.getItem('G', groupId);
361      if (!item) item = new Item('G', groupId, name+' ['+permissionString+']', permissionCode, 0);
362      Link.addItem(document.forms['share'].members, item);
363      membersOnChange();
364    }
365    function addProjectsOnClick()
366    {
367      var ids = Link.getListIds(document.forms['share'].members, 'P');
368      var excludes = ids.join(',');
369      Main.openPopup('../../my_base/projects/index.jsp?ID=<%=ID%>&cmd=UpdateContext&mode=selectmultiple&callback=addProjectCallback&exclude='+excludes, 'AddProjects', 1000, 700);
370    }
371    function addProjectCallback(projectId, name)
372    {
373      var permissionCode = getPermissionCode();
374      var permissionString = getPermissionString(permissionCode);
375      var item = Link.getItem('P', projectId);
376      if (!item) item = new Item('P', projectId, name+' ['+permissionString+']', permissionCode, 0);
377      Link.addItem(document.forms['share'].members, item);
378      membersOnChange();
379    }
380    function addPermissionTemplateOnClick()
381    {
382      var ids = Link.getListIds(document.forms['share'].members, 'T');
383      var excludes = ids.join(',');
384      Main.openPopup('../../views/permissiontemplates/index.jsp?ID=<%=ID%>&cmd=UpdateContext&mode=selectmultiple&callback=addPermissionTemplateCallback&permission=READ&exclude='+excludes, 'AddPermissionTemplates', 1000, 700);
385    }
386    function addPermissionTemplateCallback(templateId, name)
387    {
388      var item = Link.getItem('T', templateId);
389      if (!item) item = new Item('T', templateId, name, 0, 0);
390      Link.addItem(document.forms['share'].members, item);
391      membersOnChange();
392    }
393    function removeOnClick()
394    {
395      Link.removeSelected(document.forms['share'].members);
396      membersOnChange();
397    }
398    </script>
399  </base:head>
400  <base:body onload="<%="initList()"%>">
401    <h1><%=headLine%> <base:help helpid="share.setpermissions" /></h1>
402    <form name="share" action="submit_share.jsp" onsubmit="return false;">
403    <input type="hidden" name="ID" value="<%=ID%>">
404    <input type="hidden" name="cmd" value="Save">
405    <input type="hidden" name="item_type" value="<%=itemType.name()%>">
406    <input type="hidden" name="subcontext" value="<%=subContext%>">
407 
408    <div class="content filled">
409    <table style="width: 100%;">
410    <tr>
411      <td class="padded"><b>Members</b></td>
412      <td class="padded"><b>Permissions</b></td>
413      <td></td>
414    </tr>
415    <tr style="vertical-align: top;">
416      <td rowspan="2" style="padding-left: 5px; padding-right: 5px;">
417        <select name="members" size="16" multiple onChange="membersOnChange()" style="width: 100%;" 
418          <%=!writePermission ? "disabled" : ""%>>
419        </select>
420        <input type="hidden" name="modifiedUsers" value="">
421        <input type="hidden" name="modifiedGroups" value="">
422        <input type="hidden" name="modifiedProjects" value="">
423        <input type="hidden" name="permissionTemplates" value="">
424      </td>
425      <td>
426        <input type="checkbox" id="read" checked name="read" onClick="permissionsOnClick('read')"><label for="read">Read</label><br>
427        <input type="checkbox" id="use" checked name="use" onClick="permissionsOnClick('use')"><label for="use">Use</label><br>
428        <input type="checkbox" id="write" name="write" onClick="permissionsOnClick('write')"><label for="write">Write</label><br>
429        <input type="checkbox" id="delete" name="delete" onClick="permissionsOnClick('delete')"><label for="delete">Delete</label><br>
430        <input type="checkbox" id="setOwner" name="set_owner" onClick="permissionsOnClick('set_owner')"><label for="setOwner">Set owner</label><br>
431        <input type="checkbox" id="setPermission" name="set_permission" onClick="permissionsOnClick('set_permission')"><label for="setPermission">Set permission</label><br>
432      </td>
433      <td>
434        <base:buttongroup vertical="true" style="width: 12em;">
435          <base:button 
436            subclass="leftaligned"
437            onclick="addUsersOnClick()" 
438            title="Add&nbsp;users&hellip;" 
439            tooltip="Add users"
440            disabled="<%=!writePermission %>" 
441            />
442          <base:button 
443            subclass="leftaligned"
444            onclick="addGroupsOnClick()" 
445            title="Add&nbsp;groups&hellip;" 
446            tooltip="Add groups"
447            disabled="<%=!writePermission %>" 
448          />
449          <base:button 
450            subclass="leftaligned"
451            onclick="addProjectsOnClick()" 
452            title="Add&nbsp;projects&hellip;" 
453            tooltip="Add projects"
454            disabled="<%=!writePermission %>" 
455            />
456          <base:button 
457            subclass="leftaligned"
458            onclick="addPermissionTemplateOnClick()" 
459            title="Templates&hellip;" 
460            tooltip="Add permission templates"
461            image="add.png"
462            disabled="<%=!writePermission %>" 
463            />
464          <base:button 
465            subclass="leftaligned"
466            onclick="removeOnClick()" 
467            title="Remove" 
468            tooltip="Remove the selected items from this group"
469            disabled="<%=!writePermission%>" 
470          />
471        </base:buttongroup>
472      </td>
473    </tr>
474    <tr>
475      <td colspan="2">
476      <%
477      if (hasDirectory)
478      {
479        %>
480        <input type="checkbox" name="recursive" value="1" id="recursive">
481        <label for="recursive">
482        Apply permissions to all subdirectories and their files</label>
483        <%
484      }
485      %>
486      </td>
487    </tr>
488    </table>
489
490      <div class="error" id="share_disabled" style="display:none;">
491        You don't have permission to change permissions for the
492        Everyone group. Please contact the server administrator
493        if you think you should have that permission.
494      </div>
495   
496    </div>
497    </form>
498   
499    <base:buttongroup subclass="dialogbuttons">
500      <base:button onclick="saveSettings();" title="Ok" />
501      <base:button onclick="window.close();" title="Cancel" />
502    </base:buttongroup>
503
504
505  </base:body>
506  </base:page>
507  <%
508}
509finally
510{
511  if (users != null) users.close();
512  if (groups != null) groups.close();
513  if (projects != null) projects.close();
514  if (dc != null) dc.close();
515}
516%>
Note: See TracBrowser for help on using the repository browser.