1 /*******************************************************************************
2 * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
3 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
4 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
6 * This file is part of the work and learning management system Pentila Nero.
8 * Pentila Nero is free software. You can redistribute it and/or modify since
9 * you respect the terms of either (at least one of the both license) :
10 * - under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
14 * License, or any later version
15 * - the GNU Lesser General Public License as published by the
16 * Free Software Foundation, either version 3 of the license,
17 * or (at your option) any later version.
19 * There are special exceptions to the terms and conditions of the
20 * licenses as they are applied to this software. View the full text of
21 * the exception in file LICENSE-PROJECT.txt in the directory of this software
24 * Pentila Nero is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * Licenses for more details.
29 * You should have received a copy of the GNU Affero General Public License
30 * and the CeCILL-C and the GNU Lesser General Public License along with
31 * Pentila Nero. If not, see :
32 * <http://www.gnu.org/licenses/> and
33 * <http://www.cecill.info/licences.fr.html>.
34 ******************************************************************************/
35 package com.pentila.entSavoie.messageBoards;
37 import java.util.ArrayList;
38 import java.util.List;
40 import com.liferay.portal.kernel.exception.PortalException;
41 import com.liferay.portal.kernel.exception.SystemException;
42 import com.liferay.portal.model.Group;
43 import com.liferay.portal.model.ResourceConstants;
44 import com.liferay.portal.model.Role;
45 import com.liferay.portal.model.RoleConstants;
46 import com.liferay.portal.security.permission.PermissionChecker;
47 import com.liferay.portal.service.GroupLocalServiceUtil;
48 import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
49 import com.liferay.portal.service.RoleLocalServiceUtil;
50 import com.liferay.portlet.messageboards.model.MBCategory;
51 import com.liferay.portlet.messageboards.model.MBMessage;
52 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
53 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
54 import com.pentila.entSavoie.ENTRoleUtilFactory;
55 import com.pentila.entSavoie.ENTRolesConstants;
56 import com.pentila.entSavoie.utils.ENTMainUtilsLocalServiceUtil;
58 public class MBUtils {
60 // Verification des permissions pour une categorie
61 public static boolean checkCategoryPermission(PermissionChecker permissionChecker, long categoryId, String actionId) throws PortalException, SystemException {
63 MBCategory category = MBCategoryLocalServiceUtil.getMBCategory(categoryId);
65 if (permissionChecker.hasOwnerPermission(category.getCompanyId(), MBCategory.class.getName(), categoryId, category.getUserId(), actionId)) {
69 return (permissionChecker.hasPermission(category.getGroupId(), MBCategory.class.getName(),category.getCategoryId(), actionId));
73 // Verification des permissions pour un message
74 public static boolean checkMessagePermission(PermissionChecker permissionChecker, long messageId, String actionId) throws PortalException, SystemException {
76 MBMessage message = MBMessageLocalServiceUtil.getMBMessage(messageId);
78 if (permissionChecker.hasOwnerPermission(message.getCompanyId(), MBMessage.class.getName(), messageId, message.getUserId(), actionId)) {
82 return (permissionChecker.hasPermission(message.getGroupId(), MBMessage.class.getName(), messageId, actionId));
86 // Ajout des permissions par defaut pour une category
87 public static void addDefaultPermissionsCategory(MBCategory category) throws PortalException, SystemException {
88 // Groupe de la resource
89 Group group = GroupLocalServiceUtil.getGroup(category.getGroupId());
91 // Si le groupe est de type organization, alors set des permissions des membres
92 if (group.isOrganization()) {
93 setDefaultRolePermissionsForResource(true, category.getGroupId(), category.getCategoryId(), category.getCompanyId(), "category");
95 // Sinon si le groupe est de type communaute, alors set des permissions des membres
96 else if (group.isRegularSite()) {
97 setDefaultRolePermissionsForResource(false, category.getGroupId(), category.getCategoryId(), category.getCompanyId(), "category");
101 // Ajout des permissions par defaut pour un message
102 public static void addDefaultPermissionsMessage(MBMessage message) throws PortalException, SystemException {
103 // Groupe de la resource
104 Group group = GroupLocalServiceUtil.getGroup(message.getGroupId());
106 // Si le groupe est de type organization, alors set des permissions des membres
107 if (group.isOrganization()) {
108 // Si c'est le messageRoot, alors set des permissions du thread
109 if (message.isRoot()) {
110 setDefaultRolePermissionsForResource(true, message.getGroupId(), message.getMessageId(), message.getCompanyId(), "thread");
112 setDefaultRolePermissionsForResource(true, message.getGroupId(), message.getMessageId(), message.getCompanyId(), "message");
115 // Sinon si le groupe est de type communaute, alors set des permissions des membres
116 else if (group.isRegularSite()) {
117 // Si c'est le messageRoot, alors set des permissions du thread
118 if (message.isRoot()) {
119 setDefaultRolePermissionsForResource(false, message.getGroupId(), message.getMessageId(), message.getCompanyId(), "thread");
121 setDefaultRolePermissionsForResource(false, message.getGroupId(), message.getMessageId(), message.getCompanyId(), "message");
126 // Set des permissions par defaut
127 private static void setDefaultRolePermissionsForResource(boolean org, long groupId, long objectId, long companyId, String type) throws PortalException, SystemException {
129 String name = type.equals("category")?MBCategory.class.getName():MBMessage.class.getName();
130 int scope = ResourceConstants.SCOPE_INDIVIDUAL;
132 List<String> listBasicActions = new ArrayList<String>();
134 if (type.equals("category") || type.equals("message")) {
135 listBasicActions.add("VIEW");
137 if(ENTMainUtilsLocalServiceUtil.isSchemaSupann(companyId)){
138 listBasicActions.add("ADD_OBJECT");
141 } else if (type.equals("thread")) {
142 listBasicActions.add("VIEW");
143 listBasicActions.add("ADD_OBJECT");
144 listBasicActions.add("SUBSCRIBE");
147 List<String> listMediumActions = new ArrayList<String>();
149 if (type.equals("category")) {
150 listMediumActions.add("VIEW");
151 listMediumActions.add("ADD_OBJECT");
152 listMediumActions.add("MOVE_THREAD");
153 } else if (type.equals("thread")) {
154 listMediumActions.add("VIEW");
155 listMediumActions.add("ADD_OBJECT");
156 listMediumActions.add("SUBSCRIBE");
157 listMediumActions.add("MOVE_THREAD");
158 } else if (type.equals("message")) {
159 listMediumActions.add("VIEW");
162 List<String> listAdvancedActions = new ArrayList<String>();
164 if (type.equals("category")) {
165 listAdvancedActions.add("VIEW");
166 listAdvancedActions.add("EDIT");
167 listAdvancedActions.add("DELETE");
168 listAdvancedActions.add("ADD_OBJECT");
169 listAdvancedActions.add("MOVE_THREAD");
170 listAdvancedActions.add("MANAGE_PERMISSIONS");
171 } else if (type.equals("thread")) {
172 listAdvancedActions.add("VIEW");
173 listAdvancedActions.add("EDIT");
174 listAdvancedActions.add("DELETE");
175 listAdvancedActions.add("ADD_OBJECT");
176 listAdvancedActions.add("SUBSCRIBE");
177 listAdvancedActions.add("MOVE_THREAD");
178 listAdvancedActions.add("MANAGE_PERMISSIONS");
179 } else if (type.equals("message")) {
180 listAdvancedActions.add("EDIT");
181 listAdvancedActions.add("DELETE");
182 listAdvancedActions.add("VIEW");
183 listAdvancedActions.add("MANAGE_PERMISSIONS");
188 Role forumAdmin = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.FORUM_ADMIN);
189 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), forumAdmin.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]));
191 Role forumMember = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.FORUM_MEMBER);
192 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), forumMember.getRoleId(), listBasicActions.toArray(new String[listBasicActions.size()]));
194 Role owner = RoleLocalServiceUtil.getRole(companyId, RoleConstants.OWNER);
195 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), owner.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]));
200 Role eleve = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_1);
201 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), eleve.getRoleId(), listBasicActions.toArray(new String[listBasicActions.size()]));
203 Role parent = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_2);
204 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), parent.getRoleId(), listBasicActions.toArray(new String[listBasicActions.size()]));
206 Role enseignant = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_3);
207 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), enseignant.getRoleId(), listMediumActions.toArray(new String[listMediumActions.size()]));
209 Role personnelDirection = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_4);
210 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), personnelDirection.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]));
212 Role externalRole = ENTRoleUtilFactory.getInstance(companyId).getRole(ENTRolesConstants.EXTERNAL);
213 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), externalRole.getRoleId(), listBasicActions.toArray(new String[listBasicActions.size()]));
215 Role documentaliste = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_24);
216 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), documentaliste.getRoleId(), listMediumActions.toArray(new String[listMediumActions.size()]));
218 Role persoEducation = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_25);
219 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), persoEducation.getRoleId(), listMediumActions.toArray(new String[listMediumActions.size()]));
221 Role groupAdmin = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.GROUP_ADMIN);
222 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), groupAdmin.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]));
224 Role orgMember = RoleLocalServiceUtil.getRole(companyId, RoleConstants.ORGANIZATION_USER);
225 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), orgMember.getRoleId(), listBasicActions.toArray(new String[listBasicActions.size()]));
227 Role orgAdmin = RoleLocalServiceUtil.getRole(companyId, RoleConstants.ORGANIZATION_ADMINISTRATOR);
228 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), orgAdmin.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]));
230 Role orgOwner = RoleLocalServiceUtil.getRole(companyId, RoleConstants.ORGANIZATION_OWNER);
231 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), orgOwner.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]));
234 Role comMember = RoleLocalServiceUtil.getRole(companyId, RoleConstants.SITE_MEMBER);
235 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), comMember.getRoleId(), listMediumActions.toArray(new String[listMediumActions.size()]));
237 Role comAdmin = RoleLocalServiceUtil.getRole(companyId, RoleConstants.SITE_ADMINISTRATOR);
238 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), comAdmin.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]));
240 Role comOwner = RoleLocalServiceUtil.getRole(companyId, RoleConstants.SITE_OWNER);
241 ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, String.valueOf(objectId), comOwner.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]));