source: trunk/www/admin/users/index.jsp @ 4906

Last change on this file since 4906 was 4906, checked in by Martin Svensson, 13 years ago

References #1128 More item-pages updated

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 17.2 KB
Line 
1<%-- $Id: index.jsp 4906 2009-04-24 13:53:14Z martin $
2  ------------------------------------------------------------------
3  Copyright (C) 2005 Nicklas Nordborg
4  Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson
5  Copyright (C) 2007 Nicklas Nordborg, Martin Svensson
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  @author Nicklas
25  @version 2.0
26--%>
27<%@ page session="false"
28  import="net.sf.basedb.core.SessionControl"
29  import="net.sf.basedb.core.DbControl"
30  import="net.sf.basedb.core.Item"
31  import="net.sf.basedb.core.Include"
32  import="net.sf.basedb.core.Group"
33  import="net.sf.basedb.core.User"
34  import="net.sf.basedb.core.Role"
35  import="net.sf.basedb.core.Quota"
36  import="net.sf.basedb.core.Directory"
37  import="net.sf.basedb.core.ItemQuery"
38  import="net.sf.basedb.core.Permission"
39  import="net.sf.basedb.core.ItemContext"
40  import="net.sf.basedb.core.ExtendedProperties"
41  import="net.sf.basedb.core.ExtendedProperty" 
42  import="net.sf.basedb.core.MultiPermissions"
43  import="net.sf.basedb.core.PermissionDeniedException"
44  import="net.sf.basedb.core.ItemAlreadyExistsException"
45  import="net.sf.basedb.core.DatabaseException"
46  import="net.sf.basedb.core.query.Expressions"
47  import="net.sf.basedb.core.query.Hql"
48  import="net.sf.basedb.core.query.Orders"
49  import="net.sf.basedb.core.query.Restrictions"
50  import="net.sf.basedb.util.RemovableUtil" 
51  import="net.sf.basedb.util.Values"
52  import="net.sf.basedb.clients.web.Base"
53  import="net.sf.basedb.clients.web.WebException"
54  import="net.sf.basedb.clients.web.util.HTML"
55  import="net.sf.basedb.util.formatter.Formatter"
56  import="net.sf.basedb.util.formatter.NameableFormatter"
57  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
58  import="net.sf.basedb.clients.web.plugins.ItemQueryLoader"
59  import="java.util.Date"
60  import="java.util.Enumeration"
61  import="java.util.Set"
62  import="java.util.HashSet"
63  import="java.util.List"
64  import="java.util.ArrayList"
65  import="java.util.Collections"
66%>
67<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
68<%!
69  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,login,expirationDate,groups,roles,email,description");
70  private static final Item itemType = Item.USER;
71 
72  private static void registerExportUtils(ItemContext cc)
73  {
74    // Register formatters
75    cc.setObject("export.formatter.&groups(name)", new NameableFormatter());
76    cc.setObject("export.formatter.&roles(name)", new NameableFormatter());
77   
78    // Register dataloaders
79    String userParameter = "user";
80    // Groups
81    ItemQuery<Group> groupQuery = Group.getQuery();
82    groupQuery.include(cc.getInclude());
83    groupQuery.join(Hql.innerJoin("users", "usr"));
84    groupQuery.restrict(Restrictions.eq(Hql.alias("usr"), Expressions.parameter(userParameter)));
85    groupQuery.order(Orders.asc(Hql.property("name")));
86    cc.setObject("export.dataloader.&groups(name)", new ItemQueryLoader(groupQuery, userParameter));
87    // Roles
88    ItemQuery<Role> roleQuery = Role.getQuery();
89    roleQuery.include(cc.getInclude());
90    roleQuery.join(Hql.innerJoin("users", "usr"));
91    roleQuery.restrict(Restrictions.eq(Hql.alias("usr"), Expressions.parameter(userParameter)));
92    roleQuery.order(Orders.asc(Hql.property("name"))); 
93    cc.setObject("export.dataloader.&roles(name)", new ItemQueryLoader(roleQuery, userParameter));   
94  }
95%>
96<%
97final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
98final String ID = sc.getId();
99final String cmd = request.getParameter("cmd");
100final String root = request.getContextPath()+"/";
101final String mode = request.getParameter("mode");
102final String callback = request.getParameter("callback");
103final String itemId = request.getParameter("item_id");
104final String listPage = "list_users.jsp?ID="+ID
105  +(mode == null ? "" : "&mode="+mode)
106  +(callback == null ? "" : "&callback="+callback)
107  +(itemId == null ? "" : "&item_id="+itemId);
108final String viewPage = "view_user.jsp?ID="+ID;
109final String editPage = "edit_user.jsp?ID="+ID;
110
111String forward = null;
112String redirect = null;
113String message = null;
114DbControl dc = null;
115
116try
117{
118  if (cmd == null || "List".equals(cmd))
119  {
120    // Display the list page without updatinging the current context
121    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
122    redirect = listPage;
123  }
124  else if ("UpdateContext".equals(cmd))
125  {
126    // Display the list page after updating the current context from the request parameters
127    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
128    redirect = listPage;
129  }
130  else if ("LoadContext".equals(cmd))
131  {
132    // Display the list page after loading a saved context
133    int contextId = Values.getInt(request.getParameter("context"));
134    Base.loadContext(sc, contextId, defaultContext);
135    redirect = listPage;
136  }
137
138  else if ("ViewItem".equals(cmd))
139  {
140    // Display the view page for a single item
141    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
142    forward = viewPage;
143  }
144  else if ("EditItem".equals(cmd))
145  {
146    // Display the edit page for a single item (should be opened in a popup)
147    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
148    redirect = editPage;
149  }
150  else if ("NewItem".equals(cmd))
151  {
152    // Display the edit page for a new item (should be opened in a popup)
153    if (!sc.hasPermission(Permission.CREATE, itemType))
154    {
155      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
156    }
157    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
158    cc.setId(0);
159    redirect = editPage;
160  }
161  else if ("UpdateItem".equals(cmd))
162  {
163    // Update the properties on an item (will close the popup)
164    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
165    dc = sc.newDbControl();
166   
167    final String email = Values.getStringOrNull(request.getParameter("email"));
168    if (email != null && !HTML.isValidEmail(email))
169    {
170      throw new WebException("popup", "Invalid email address",
171        "The email address {1} is not a valid email address.",
172        HTML.encodeTags(email)
173      );
174    }
175    final String login = Values.getStringOrNull(request.getParameter("login"));
176    final String password = Values.getStringOrNull(request.getParameter("new_password"));
177    String oldLogin = null;   
178    String oldExternalId = null;
179    boolean loginHasChanged = true;
180    boolean externalIdHasChanged = false;
181
182    User user = (User)cc.getObject("item");
183    if (user == null)
184    {
185      user = User.getNew(dc, login, password);
186      user.addToDefaultRolesAndGroups();
187      message = "User created";
188      dc.saveItem(user);
189    }
190    else
191    {
192      dc.reattachItem(user);
193      oldLogin = user.getLogin();
194      user.setLogin(login);
195      loginHasChanged = oldLogin != login;
196      if (password != null) user.setPassword(password);
197      message = "User updated";
198    }
199    user.setName(Values.getStringOrNull(request.getParameter("name")));
200    oldExternalId = user.getExternalId();
201    user.setExternalId(Values.getStringOrNull(request.getParameter("external_id")));
202    externalIdHasChanged = oldExternalId != user.getExternalId();
203
204    int quotaId = Values.getInt(request.getParameter("quota_id"), -1);
205    if (quotaId >= 0) // < 0 = denied or unchanged
206    {
207      user.setQuota(quotaId == 0 ? null : Quota.getById(dc, quotaId));
208    }
209    int quotaGroupId = Values.getInt(request.getParameter("quotagroup_id"), -1);
210    if (quotaGroupId >= 0) // < 0 = denied or unchanged
211    {
212      user.setQuotaGroup(quotaGroupId == 0 ? null : Group.getById(dc, quotaGroupId));
213    }
214    String homeDirectory = request.getParameter("homedirectory_id");
215    if ("new".equals(homeDirectory))
216    {
217      Directory.createHomeDirectory(dc, user, false);
218    }
219    else if ("template".equals(homeDirectory))
220    {
221      Directory.createHomeDirectory(dc, user, true);
222    }
223    else
224    {
225      int homeDirectoryId = Values.getInt(homeDirectory, -1);
226      if (homeDirectoryId >= 0) // < 0 = denied or unchanged
227      {
228        user.setHomeDirectory(homeDirectoryId == 0 ? null : Directory.getById(dc, homeDirectoryId));
229      }
230    }
231   
232    Formatter<Date> dateFormatter = FormatterFactory.getDateFormatter(sc);
233    user.setExpirationDate(dateFormatter.parseString(Values.getStringOrNull(request.getParameter("expiration_date"))));
234    user.setMultiuserAccount(Values.getBoolean(request.getParameter("multiuser_account")));
235    user.setDisabled(Values.getBoolean(request.getParameter("disabled")));
236   
237    // Contact information
238    user.setEmail(email);
239    user.setOrganisation(Values.getStringOrNull(request.getParameter("organisation")));
240    user.setAddress(Values.getStringOrNull(request.getParameter("address")));
241    user.setPhone(Values.getStringOrNull(request.getParameter("phone")));
242    user.setFax(Values.getStringOrNull(request.getParameter("fax")));
243    user.setUrl(Values.getStringOrNull(request.getParameter("url")));
244    user.setDescription(Values.getStringOrNull(request.getParameter("description")));
245   
246    // Extended properties
247    List<ExtendedProperty> extendedProperties = ExtendedProperties.getProperties("UserData");
248    if (extendedProperties != null)
249    {
250      for (ExtendedProperty ep : extendedProperties)
251      {
252        String name = ep.getName();
253        Object value = ep.getType().parseString(Values.getStringOrNull(request.getParameter(name)));
254        user.setExtended(name, value);
255      }
256    }
257
258    // Membership
259    String[] removeGroups = Values.getString(request.getParameter("removeGroups")).split(",");
260    for (int i = 0; i < removeGroups.length; ++i)
261    {
262      int groupId = Values.getInt(removeGroups[i], -1);
263      if (groupId != -1) Group.getById(dc, groupId).removeUser(user);
264    }
265   
266    String[] addGroups = Values.getString(request.getParameter("addGroups")).split(",");
267    for (int i = 0; i < addGroups.length; ++i)
268    {
269      int groupId = Values.getInt(addGroups[i], -1);
270      if (groupId != -1) Group.getById(dc, groupId).addUser(user);
271    }
272
273    String[] removeRoles = Values.getString(request.getParameter("removeRoles")).split(",");
274    for (int i = 0; i < removeRoles.length; ++i)
275    {
276      int roleId = Values.getInt(removeRoles[i], -1);
277      if (roleId != -1) Role.getById(dc, roleId).removeUser(user);
278    }
279   
280    String[] addRoles = Values.getString(request.getParameter("addRoles")).split(",");
281    for (int i = 0; i < addRoles.length; ++i)
282    {
283      int roleId = Values.getInt(addRoles[i], -1);
284      if (roleId != -1) Role.getById(dc, roleId).addUser(user);
285    }
286    try
287    {
288      dc.commit();
289    }
290    catch (DatabaseException dbex)
291    {
292      dc = sc.newDbControl();
293      if (loginHasChanged && login != null && !login.equals(oldLogin) && User.loginIsUsed(dc, login))
294      {
295        throw new ItemAlreadyExistsException("User[login=" + login + "]");
296      }
297      String externalId = Values.getStringOrNull(request.getParameter("external_id"));
298      if (externalIdHasChanged && externalId != null && !externalId.equals(oldExternalId) && User.externalIdIsUsed(dc, externalId))
299      {
300        throw new ItemAlreadyExistsException("User[externalId=" + externalId + "]");
301      }
302      throw dbex;
303    }
304    cc.removeObject("item");
305  }
306  else if ("DeleteItem".equals(cmd))
307  {
308    // Delete a single item and then return to the view page
309    dc = sc.newDbControl();
310    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
311    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
312    dc.commit();
313    redirect = viewPage;
314  }
315  else if ("DeleteItems".equals(cmd))
316  {
317    // Delete all selected items on the list page
318    dc = sc.newDbControl();
319    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
320    int numTotal = cc.getSelected().size();
321    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
322    dc.commit();
323    if (numTotal != numRemoved)
324    {
325      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
326    }
327    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
328  }
329  else if ("RestoreItem".equals(cmd))
330  {
331    // Restore a single item and then return to the view page
332    dc = sc.newDbControl();
333    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
334    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
335    dc.commit();
336    redirect = viewPage;
337  }
338  else if ("RestoreItems".equals(cmd))
339  {
340    // Restore all selected items on the list page
341    dc = sc.newDbControl();
342    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
343    int numTotal = cc.getSelected().size();
344    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
345    dc.commit();
346    if (numTotal != numRemoved)
347    {
348      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
349    }
350    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
351  }
352  else if ("ExportItems".equals(cmd))
353  {
354    // Run an export plugin in a list context
355    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
356    final ItemQuery<User> query = User.getQuery();
357    cc.configureQuery(query, true);
358    cc.setQuery(query);
359    registerExportUtils(cc);
360    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+users";
361  }
362  else if ("ExportItem".equals(cmd))
363  {
364    // Run an export plugin in single-item context
365    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
366    registerExportUtils(cc);
367    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+user";
368  }
369  else if ("ImportItems".equals(cmd))
370  {
371    // Run an import plugin in a list context
372    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
373    final ItemQuery<User> query = User.getQuery();
374    cc.configureQuery(query, true);
375    cc.setQuery(query);
376    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+users";
377  }
378  else if ("ImportItem".equals(cmd))
379  {
380    // Run an import plugin in single-item context
381    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
382    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+user";
383  }
384  else if ("RunListPlugin".equals(cmd))
385  {
386    // Run another plugin in a list context
387    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
388    final ItemQuery<User> query = User.getQuery();
389    cc.configureQuery(query, true);
390    cc.setQuery(query);
391    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
392  }
393  else if ("RunPlugin".equals(cmd))
394  {
395    // Run another plugin in single-item context
396    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
397    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
398  }
399  else if ("EditDefaultMembership".equals(cmd))
400  {
401    // Display the edit page for a single item (should be opened in a popup)
402    forward = "edit_default_membership.jsp";
403  }
404  else if ("UpdateDefaultMembership".equals(cmd))
405  {
406    dc = sc.newDbControl();
407    // Membership
408    String[] removeGroups = Values.getString(request.getParameter("removeGroups")).split(",");
409    for (int i = 0; i < removeGroups.length; ++i)
410    {
411      int groupId = Values.getInt(removeGroups[i], -1);
412      if (groupId != -1) Group.getById(dc, groupId).setDefault(false);
413    }
414   
415    String[] addGroups = Values.getString(request.getParameter("addGroups")).split(",");
416    for (int i = 0; i < addGroups.length; ++i)
417    {
418      int groupId = Values.getInt(addGroups[i], -1);
419      if (groupId != -1) Group.getById(dc, groupId).setDefault(true);
420    }
421
422    String[] removeRoles = Values.getString(request.getParameter("removeRoles")).split(",");
423    for (int i = 0; i < removeRoles.length; ++i)
424    {
425      int roleId = Values.getInt(removeRoles[i], -1);
426      if (roleId != -1) Role.getById(dc, roleId).setDefault(false);
427    }
428   
429    String[] addRoles = Values.getString(request.getParameter("addRoles")).split(",");
430    for (int i = 0; i < addRoles.length; ++i)
431    {
432      int roleId = Values.getInt(addRoles[i], -1);
433      if (roleId != -1) Role.getById(dc, roleId).setDefault(true);
434    }
435    dc.commit();
436  }
437
438  else
439  {
440    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
441  }
442}
443finally
444{
445  if (dc != null) dc.close();
446}
447
448if (forward != null)
449{
450  pageContext.forward(forward);
451}
452else if (redirect != null)
453{
454  response.sendRedirect(redirect);
455}
456else if (message == null)
457{
458  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
459}
460else
461{
462  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
463}
464%>
465
Note: See TracBrowser for help on using the repository browser.