001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.kernel.KernelConstants;
038: import org.libresource.kernel.interfaces.KernelService;
039:
040: import org.libresource.membership.MembershipConstants;
041: import org.libresource.membership.ejb.model.ProfileResourceValue;
042: import org.libresource.membership.interfaces.MembershipService;
043:
044: import java.net.URI;
045:
046: import java.util.HashMap;
047: import java.util.Vector;
048:
049: import javax.servlet.http.HttpSession;
050:
051: public class Helper {
052: public static boolean checkSecurity(URI uri, String permission)
053: throws Exception {
054: KernelService kernelService = (KernelService) Libresource
055: .getService(KernelConstants.SERVICE);
056: boolean security = kernelService.checkSecurity(uri, permission);
057:
058: return security;
059: }
060:
061: public static String getResourceName(URI uri) throws Exception {
062: try {
063: return Libresource.getShortName(uri);
064: } catch (Exception e) {
065: return "<span class=\"error\">! " + getNode(uri)
066: + " !</span>";
067: }
068: }
069:
070: public static ProfileResourceValue getConnectedUserProfile()
071: throws Exception {
072: HttpSession session = SessionPropagation
073: .getSessionPropagation().getSession();
074: ProfileResourceValue connectedUser = (ProfileResourceValue) session
075: .getAttribute("connectedUserCache");
076:
077: if (connectedUser == null) {
078: MembershipService membershipService = (MembershipService) Libresource
079: .getService(MembershipConstants.SERVICE);
080:
081: try {
082: connectedUser = membershipService.getProfile();
083: } catch (Exception e) {
084: // create a default profile
085: String username = (String) session
086: .getAttribute("security_username");
087:
088: if (username != null) {
089: KernelService kernelService = (KernelService) Libresource
090: .getService(KernelConstants.SERVICE);
091: kernelService
092: .systemCreateURI(new URI(
093: membershipService.getUsersRootURI()
094: + "/"
095: + session
096: .getAttribute("security_username")));
097: membershipService.systemCreateProfile(username,
098: username, "", "", new HashMap());
099: }
100: }
101:
102: connectedUser = membershipService.getProfile();
103: session.setAttribute("connectedUserCache", connectedUser);
104: }
105:
106: return connectedUser;
107: }
108:
109: public static Vector getUsers() throws Exception {
110: MembershipService membershipService = (MembershipService) Libresource
111: .getService(MembershipConstants.SERVICE);
112:
113: return membershipService.listUsers();
114: }
115:
116: public static String getConnectedUser() throws Exception {
117: ProfileResourceValue user = getConnectedUserProfile();
118:
119: if (user != null) {
120: return user.getFullName();
121: } else {
122: return ((HttpSession) SessionPropagation
123: .getSessionPropagation().getSession())
124: .getAttribute("security_username").toString();
125: }
126: }
127:
128: public static boolean isSuperUser() throws Exception {
129: HttpSession session = SessionPropagation
130: .getSessionPropagation().getSession();
131: Boolean isSuperUser = (Boolean) session
132: .getAttribute("isSuperUserCache");
133:
134: if (isSuperUser == null) {
135: KernelService kernelService = (KernelService) Libresource
136: .getService(KernelConstants.SERVICE);
137: isSuperUser = new Boolean(kernelService.isSuperUser());
138: session.setAttribute("isSuperUserCache", isSuperUser);
139: }
140:
141: return isSuperUser.booleanValue();
142: }
143:
144: private static String getKey(URI uri) {
145: String path = uri.getPath();
146:
147: if (path.endsWith("/")) {
148: path = path.substring(0, path.length() - 1);
149: }
150:
151: return path;
152: }
153:
154: private static String getNode(URI uri) {
155: String path = uri.getPath();
156:
157: if (path.endsWith("/")) {
158: path = path.substring(0, path.length() - 1);
159: }
160:
161: return path.substring(path.lastIndexOf("/") + 1, path.length());
162: }
163: }
|