1 /*******************************************************************************
2 * Copyright © Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
4 * This file is part of the work and learning management system Pentila Nero.
6 * Pentila Nero is free software. You can redistribute it and/or modify since
7 * you respect the terms of either (at least one of the both license) :
8 * - under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
12 * License, or any later version
13 * - the GNU Lesser General Public License as published by the
14 * Free Software Foundation, either version 3 of the license,
15 * or (at your option) any later version.
17 * There are special exceptions to the terms and conditions of the
18 * licenses as they are applied to this software. View the full text of
19 * the exception in file LICENSE-PROJECT.txt in the directory of this software
22 * Pentila Nero is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * Licenses for more details.
27 * You should have received a copy of the GNU Affero General Public License
28 * and the CeCILL-C and the GNU Lesser General Public License along with
29 * Pentila Nero. If not, see :
30 * <http://www.gnu.org/licenses/> and
31 * <http://www.cecill.info/licences.fr.html>.
32 ******************************************************************************/
33 package com.pentila.entSavoie.roleSelect;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
38 import org.apache.struts.action.Action;
39 import org.apache.struts.action.ActionForm;
40 import org.apache.struts.action.ActionForward;
41 import org.apache.struts.action.ActionMapping;
43 import com.liferay.portal.kernel.exception.PortalException;
44 import com.liferay.portal.kernel.exception.SystemException;
45 import com.liferay.portal.kernel.log.Log;
46 import com.liferay.portal.kernel.log.LogFactoryUtil;
47 import com.liferay.portal.kernel.util.ParamUtil;
48 import com.liferay.portal.model.Layout;
49 import com.liferay.portal.model.LayoutTypePortlet;
50 import com.liferay.portal.model.Portlet;
51 import com.liferay.portal.model.Role;
52 import com.liferay.portal.model.User;
53 import com.liferay.portal.service.LayoutLocalServiceUtil;
54 import com.liferay.portal.service.RoleLocalServiceUtil;
55 import com.liferay.portal.service.UserLocalServiceUtil;
56 import com.liferay.portal.util.PortalUtil;
58 public class SelectAction extends Action {
60 public ActionForward execute(ActionMapping mapping, ActionForm form,
61 HttpServletRequest request, HttpServletResponse response)
63 final String cmd = ParamUtil.getString(request, "cmd");
64 if (_log.isDebugEnabled()) {
65 _log.debug("Command called: "+cmd);
67 if (cmd.equals("displaySelector")) {
68 if (PortalUtil.getUser(request) != null) {
69 return mapping.findForward("roleSelector");
71 } else if (cmd.equals("changeRole")) {
72 final User u = PortalUtil.getUser(request);
73 final long roleId = ParamUtil.getLong(request, "selectedRole");
75 updateUserRole(u, roleId);
78 if (_log.isDebugEnabled()) {
79 _log.debug("Role change for "+u.getScreenName()+" to role "+roleId);
85 return mapping.findForward("/common/referer_jsp.jsp");
88 private void updateUserRole(User user, long newRoleId) {
90 Role userRole = RoleLocalServiceUtil.getRole(user.getCompanyId(), "User");
91 Role otherRole = RoleLocalServiceUtil.getRole(newRoleId);
93 //retire tous les roles globaux
94 for (Role r : user.getRoles()) {
96 UserLocalServiceUtil.deleteRoleUser(r.getRoleId(), user.getUserId());
100 UserLocalServiceUtil.addRoleUsers(userRole.getRoleId(), new long[]{user.getUserId()});
101 UserLocalServiceUtil.addRoleUsers(otherRole.getRoleId(), new long[]{user.getUserId()});
103 } catch (Exception e) {
104 if (_log.isErrorEnabled()) {
110 private void updatePages(User user) throws SystemException, PortalException {
111 for (Layout l : LayoutLocalServiceUtil.getLayouts(
112 user.getGroup().getGroupId(), true)) {
113 LayoutTypePortlet lt = (LayoutTypePortlet) l.getLayoutType();
114 // la page ne serait à cacher que si un seul portlet est dessus
115 // comme c'est le cas pour les portlets de l'entsavoie
116 if (lt.getAllPortlets().size() == 1 ) {
117 final Portlet p = lt.getAllPortlets().get(0);
118 boolean hasPermission = false;
119 for (final String role : p.getRolesArray()) {
120 if (_log.isDebugEnabled()) {
121 _log.debug("Portlet role: "+role);
123 for (Role r : user.getRoles()) {
124 if (_log.isDebugEnabled()) {
125 _log.debug("User role: "+r.getName());
127 if (r.getName().equals(role)) {
128 if (_log.isDebugEnabled()) {
129 _log.debug("match, has permission.");
131 hasPermission = true;
135 l.setHidden(!hasPermission);
136 LayoutLocalServiceUtil.updateLayout(l);
141 private Log _log = LogFactoryUtil.getLog(this.getClass());