source: trunk/www/admin/groups/edit_group.jsp @ 5077

Last change on this file since 5077 was 5077, checked in by Nicklas Nordborg, 14 years ago

Fixes #1182: Groups with hidden members

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.2 KB
Line 
1<%-- $Id: edit_group.jsp 5077 2009-08-26 12:03:37Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2005 Nicklas Nordborg
4  Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson
5  Copyright (C) 2007 Nicklas Nordborg
6
7  This file is part of BASE - BioArray Software Environment.
8  Available at http://base.thep.lu.se/
9
10  BASE is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License
12  as published by the Free Software Foundation; either version 3
13  of the License, or (at your option) any later version.
14
15  BASE is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with BASE. If not, see <http://www.gnu.org/licenses/>.
22  ------------------------------------------------------------------
23
24
25  @author Nicklas
26  @version 2.0
27--%>
28<%@ page session="false"
29  import="net.sf.basedb.core.SessionControl"
30  import="net.sf.basedb.core.DbControl"
31  import="net.sf.basedb.core.SystemItems"
32  import="net.sf.basedb.core.Item"
33  import="net.sf.basedb.core.Type"
34  import="net.sf.basedb.core.ItemContext"
35  import="net.sf.basedb.core.Include"
36  import="net.sf.basedb.core.Permission"
37  import="net.sf.basedb.core.Group"
38  import="net.sf.basedb.core.User"
39  import="net.sf.basedb.core.Quota"
40  import="net.sf.basedb.core.QuotaType"
41  import="net.sf.basedb.core.Location"
42  import="net.sf.basedb.core.ItemQuery"
43  import="net.sf.basedb.core.ItemResultList"
44  import="net.sf.basedb.core.PermissionDeniedException"
45  import="net.sf.basedb.core.query.Orders"
46  import="net.sf.basedb.core.query.Hql"
47  import="net.sf.basedb.core.query.Restrictions"
48  import="net.sf.basedb.core.query.Expressions"
49  import="net.sf.basedb.clients.web.Base"
50  import="net.sf.basedb.clients.web.util.HTML"
51  import="net.sf.basedb.util.Values"
52  import="java.util.Arrays"
53%>
54<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
55<%@ taglib prefix="t" uri="/WEB-INF/tab.tld" %>
56<%
57final Item itemType = Item.GROUP;
58final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
59final ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, null);
60final int itemId = cc.getId();
61final String ID = sc.getId();
62final float scale = Base.getScale(sc);
63final DbControl dc = sc.newDbControl();
64try
65{
66  String title = null;
67  Group group = null;
68
69  boolean isEveryone = false;
70  boolean isDefault = false;
71  boolean hiddenMembers = false;
72  final QuotaType total = QuotaType.getById(dc, SystemItems.getId(QuotaType.TOTAL));
73  Quota currentQuota = null;
74 
75  // Query to retrieve child groups
76  ItemQuery<Group> groupQuery = null;
77  // Query to retrieve user members
78  ItemQuery<User> userQuery = null;
79
80  if (itemId == 0)
81  {
82    title = "Create group";
83    cc.removeObject("item");
84    if (cc.getPropertyFilter("quota.name") != null)
85    {
86      currentQuota = Base.getFirstMatching(dc, Quota.getQuery(), "name", cc.getPropertyFilter("quota.name"));
87    }
88    String[] selected = request.getParameterValues("user_id");
89    if (selected != null)
90    {
91      userQuery = User.getQuery();
92      userQuery.include(Include.ALL);
93      userQuery.restrict(Restrictions.in(Hql.property("id"), Expressions.parameter("selected")));
94      userQuery.setParameter("selected", Arrays.asList(Values.getInt(selected)), Type.INT);
95    }
96    isDefault = Values.getBoolean(cc.getPropertyValue("default"), false);
97    hiddenMembers = Values.getBoolean(cc.getPropertyValue("hiddenMembers"), false);
98  }
99  else
100  {
101    group = Group.getById(dc, itemId);
102    cc.setObject("item", group);
103    title = "Edit group -- " + HTML.encodeTags(group.getName());
104    isEveryone = Group.EVERYONE.equals(group.getSystemId());
105    isDefault = group.isDefault();
106    hiddenMembers = group.hasHiddenMembers();
107
108    try
109    {
110      currentQuota = group.getQuota();
111    }
112    catch (PermissionDeniedException ex)
113    {}
114   
115    groupQuery = group.getGroups();
116    groupQuery.include(Include.ALL);
117    groupQuery.order(Orders.asc(Hql.property("name")));
118    userQuery = group.getUsers();
119    userQuery.include(Include.ALL);
120    userQuery.order(Orders.asc(Hql.property("name")));
121  }
122  if (group != null && !group.hasPermission(Permission.WRITE))
123  {
124    throw new PermissionDeniedException(Permission.WRITE, itemType.toString());
125  }
126 
127  final boolean readQuota = sc.hasPermission(Permission.READ, Item.QUOTA);
128  final boolean useQuota = sc.hasPermission(Permission.USE, Item.QUOTA);
129  final boolean useUsers = sc.hasPermission(Permission.WRITE, Item.USER);
130  final boolean writeGroups = sc.hasPermission(Permission.WRITE, Item.GROUP);
131  final boolean writeMembers = useUsers && writeGroups;
132 
133  // Query to retrieve quota
134  final ItemQuery<Quota> quotaQuery = Quota.getQuery();
135  quotaQuery.include(Include.ALL);
136  quotaQuery.order(Orders.asc(Hql.property("name")));
137  quotaQuery.setCacheResult(true);
138
139  final String clazz = "class=\"text\"";
140  final String requiredClazz = "class=\"text required\"";
141  %>
142  <base:page type="popup" title="<%=title%>">
143  <base:head scripts="tabcontrol.js,linkitems.js" styles="tabcontrol.css">
144    <script language="JavaScript">
145    // Validate the "Group" tab
146    function validateGroup()
147    {
148      var frm = document.forms['group'];
149      if (Main.trimString(frm.name.value) == '')
150      {
151        alert("You must enter a name");
152        frm.name.focus();
153        return false;
154      }
155      return true;
156    }
157    // Validate the "Members" tab
158    function validateMembers()
159    {
160      return true;
161    }
162
163    // Submit the form
164    function saveSettings()
165    {
166      var frm = document.forms['group'];
167      if (TabControl.validateActiveTab('settings'))
168      {
169        frm.addUsers.value = Link.getActionIds(1, 'U').join(',');
170        frm.removeUsers.value = Link.getActionIds(-1, 'U').join(',');
171        frm.addGroups.value = Link.getActionIds(1, 'G').join(',');
172        frm.removeGroups.value = Link.getActionIds(-1, 'G').join(',');
173        frm.submit();
174      }
175    }
176   
177    function init()
178    {
179      <%
180      if (group == null)
181      {
182        %>
183        var frm = document.forms['group'];
184        frm.name.focus();
185        frm.name.select();
186        <%
187      }
188      %>
189      initMembers();
190    }
191    function initMembers()
192    {
193      var members = document.forms['group'].members;
194      Link.addNewSection(members, new Section('U', 'Users'));
195      <%
196      if (userQuery != null)
197      {
198        ItemResultList<User> users = userQuery.list(dc);
199        String linkMethod = group == null ? "addItem" : "addNewItem";
200        for (User user : users)
201        {
202          %>
203          Link.<%=linkMethod%>(members, new Item('U', <%=user.getId()%>, '<%=HTML.javaScriptEncode(user.getName())%>'));
204          <%
205        }
206      }
207      %>
208      Link.addNewSection(members, new Section('G', 'Groups'));
209      <%
210      if (groupQuery != null)
211      {
212        ItemResultList<Group> children = groupQuery.list(dc);
213        for (Group child : children)
214        {
215          %>
216          Link.addNewItem(members, new Item('G', <%=child.getId()%>, '<%=HTML.javaScriptEncode(child.getName())%>'));
217          <%
218        }
219      }
220      %>
221    }
222    function addUsersOnClick()
223    {
224      var users = Link.getListIds(document.forms['group'].members, 'U');
225      var url = '../users/index.jsp?ID=<%=ID%>&cmd=UpdateContext&mode=selectmultiple&callback=addUserCallback';
226      url += '&exclude='+users.join(',');
227      Main.openPopup(url, 'AddUsers', 1000, 700);
228    }
229    function addUserCallback(userId, name)
230    {
231      var item = Link.getItem('U', userId);
232      if (!item) item = new Item('U', userId, name);
233      Link.addItem(document.forms['group'].members, item);
234    }
235    function addGroupsOnClick()
236    {
237      var groups = Link.getListIds(document.forms['group'].members, 'G');
238      var url = '../groups/index.jsp?ID=<%=ID%>&cmd=UpdateContext&mode=selectmultiple&callback=addGroupCallback';
239      url += '&exclude='+groups.join(',');
240      Main.openPopup(url, 'AddGroups', 1000, 700);
241    }
242    function addGroupCallback(groupId, name)
243    {
244      var item = Link.getItem('G', groupId);
245      if (!item) item = new Item('G', groupId, name);
246      Link.addItem(document.forms['group'].members, item);
247    }
248    function removeOnClick()
249    {
250      Link.removeSelected(document.forms['group'].members);
251    }
252 
253    </script>
254  </base:head>
255  <base:body onload="init()">
256    <p>
257    <form action="index.jsp?ID=<%=ID%>" method="post" name="group" onsubmit="return false;">
258    <input type="hidden" name="cmd" value="UpdateItem">
259
260    <h3 class="docked"><%=title%> <base:help tabcontrol="settings" /></h3>
261    <t:tabcontrol id="settings" contentstyle="<%="height: "+(int)(scale*280)+"px;"%>" 
262      position="bottom" remember="<%=group != null%>">
263    <t:tab id="info" title="Group" validate="validateGroup()" helpid="group.edit">
264      <table class="form" cellspacing=0>
265      <tr>
266        <td class="prompt">Name</td>
267        <td><input <%=requiredClazz%> type="text" name="name" 
268          value="<%=HTML.encodeTags(group == null ? Values.getString(cc.getPropertyValue("name"), "New group") : group.getName())%>" 
269          size="40" maxlength="<%=Group.MAX_NAME_LENGTH%>"></td>
270      </tr>
271      <tr>
272        <td class="prompt">Default</td>
273        <td>
274          <input type="radio" name="is_default" value="0" <%=!isDefault ? "checked" : ""%>>no
275          <input type="radio" name="is_default" value="1" <%=isDefault ? "checked" : ""%>>yes
276        </td>
277      </tr> 
278      <tr>
279        <td class="prompt">Hidden members</td>
280        <td>
281          <input type="radio" name="hidden_members" value="0" <%=!hiddenMembers ? "checked" : ""%>>no
282          <input type="radio" name="hidden_members" value="1" <%=hiddenMembers ? "checked" : ""%>>yes
283        </td>
284      </tr> 
285      <tr valign="top">
286        <td class="prompt">Quota</td>
287        <td>
288          <select name="quota_id" <%=isEveryone || !useQuota ? "disabled readonly class=\"disabled\"" : ""%>>
289          <%
290          if (!readQuota)
291          {
292            %>
293            <option value="-1">- denied -
294            <%
295          }
296          else
297          {
298            %>
299            <option value="0">- none -
300            <%
301            ItemResultList<Quota> quotas = quotaQuery.list(dc);
302            for (Quota quota : quotas)
303            {
304              boolean current = quota.equals(currentQuota);
305              if (!current && quota.isRemoved()) continue;
306              int id = quota.getId();
307              long totalBytes = quota.getQuotaValue(total, Location.PRIMARY);
308              String fTotal = totalBytes == Quota.UNLIMITED ? "unlimited" : Values.formatBytes(totalBytes);
309              %>
310              <option 
311                value="<%=current && group != null ? -id : id%>" 
312                <%=current ? "selected" : ""%>
313                ><%=HTML.encodeTags(quota.getName())%> (<%=fTotal%> total)
314              <%
315            }
316          }
317          %>
318        </td>
319      </tr>
320      <tr valign=top>
321        <td class="prompt">Description</td>
322        <td nowrap>
323          <textarea <%=clazz%> rows="4" cols="40" name="description" wrap="virtual"
324            ><%=HTML.encodeTags(group == null ? cc.getPropertyValue("description") : group.getDescription())%></textarea>
325          <a href="javascript:Main.zoom('Description', 'group', 'description')"
326            title="Edit in larger window"><base:icon image="zoom.gif" /></a>
327        </td>
328      </tr>
329      </table>
330      <div align=right>&nbsp;<i><base:icon image="required.gif" /> = required information</i></div>
331    </t:tab>
332   
333    <t:tab id="members" title="Members" tooltip="Add/remove members of this group" 
334      validate="validateMembers()" helpid="group.edit.membership">
335    <table >
336    <tr valign="top">
337    <td>
338      <b>Members</b><br>
339      <select name="members" size="14" multiple <%=isEveryone || !writeMembers ? "disabled readonly class=\"disabled\"" : ""%> style="width: 15em;">
340      </select>
341      <input type="hidden" name="removeUsers" value="">
342      <input type="hidden" name="addUsers" value="">
343      <input type="hidden" name="removeGroups" value="">
344      <input type="hidden" name="addGroups" value="">
345    </td>
346    <td>
347      <br>
348      <table width="150">
349      <tr><td><base:button 
350        onclick="addUsersOnClick()" 
351        title="Add&nbsp;users&hellip;" 
352        tooltip="Add users to this group"
353        disabled="<%=isEveryone || !writeMembers %>" 
354        /></td></tr>
355      <tr><td><base:button 
356        onclick="addGroupsOnClick()" 
357        title="Add&nbsp;groups&hellip;" 
358        tooltip="Add child groups to this group"
359        disabled="<%=isEveryone || !writeMembers %>" 
360      /></td></tr>
361      <tr><td><base:button 
362        onclick="removeOnClick()" 
363        title="Remove" 
364        tooltip="Remove the selected items from this group"
365        disabled="<%=isEveryone || !writeMembers%>" 
366      /></td></tr>
367      </table>
368    </td>
369    </tr>
370    </table>
371    </t:tab>
372    </t:tabcontrol>
373
374    <table align="center">
375    <tr>
376      <td width="50%"><base:button onclick="saveSettings()" title="Save" /></td>
377      <td width="50%"><base:button onclick="window.close()" title="Cancel" /></td>
378    </tr>
379    </table>
380    </form>
381  </base:body>
382  </base:page>
383  <%
384}
385finally
386{
387  if (dc != null) dc.close();
388}
389%>
Note: See TracBrowser for help on using the repository browser.