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.NoSuchResourceException;
41 import com.liferay.portal.kernel.exception.PortalException;
42 import com.liferay.portal.kernel.exception.SystemException;
43 import com.liferay.portal.kernel.util.Validator;
44 import com.liferay.portal.model.Group;
45 import com.liferay.portal.model.Resource;
46 import com.liferay.portal.model.ResourceConstants;
47 import com.liferay.portal.model.Role;
48 import com.liferay.portal.model.RoleConstants;
49 import com.liferay.portal.security.permission.PermissionChecker;
50 import com.liferay.portal.service.GroupLocalServiceUtil;
51 import com.liferay.portal.service.PermissionLocalServiceUtil;
52 import com.liferay.portal.service.ResourceLocalServiceUtil;
53 import com.liferay.portal.service.RoleLocalServiceUtil;
54 import com.liferay.portlet.messageboards.model.MBCategory;
55 import com.liferay.portlet.messageboards.model.MBMessage;
56 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
57 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
58 import com.pentila.entSavoie.ENTRolesConstants;
59 import com.pentila.entSavoie.utils.ENTMainUtilsLocalServiceUtil;
61 public class MBUtilsOld {
63 // Verification des permissions pour une categorie
64 public static boolean checkCategoryPermission(PermissionChecker permissionChecker, long categoryId, String actionId) throws PortalException, SystemException {
66 MBCategory category = MBCategoryLocalServiceUtil.getMBCategory(categoryId);
68 if (permissionChecker.hasOwnerPermission(category.getCompanyId(), MBCategory.class.getName(), categoryId, category.getUserId(), actionId)) {
72 return (permissionChecker.hasPermission(category.getGroupId(), MBCategory.class.getName(),category.getCategoryId(), actionId));
76 // Verification des permissions pour un message
77 public static boolean checkMessagePermission(PermissionChecker permissionChecker, long messageId, String actionId) throws PortalException, SystemException {
79 MBMessage message = MBMessageLocalServiceUtil.getMBMessage(messageId);
81 if (permissionChecker.hasOwnerPermission(message.getCompanyId(), MBMessage.class.getName(), messageId, message.getUserId(), actionId)) {
85 return (permissionChecker.hasPermission(message.getGroupId(), MBMessage.class.getName(), messageId, actionId));
89 // Obtention de la resource a partir de la category passe en parametre
90 public static Resource getCategoryResource(MBCategory category) throws PortalException, SystemException {
91 Resource resource = null;
92 // Obtention de la resource a partir de l'id de l'objet passe en parametre
94 resource = ResourceLocalServiceUtil.getResource(category.getCompanyId(), MBCategory.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(category.getCategoryId()));
96 catch (NoSuchResourceException nsre) {
97 boolean portletActions = Validator.isNull(MBCategory.class.getName());
98 ResourceLocalServiceUtil.addResources(category.getCompanyId(), category.getGroupId(), 0, MBCategory.class.getName(), String.valueOf(category.getCategoryId()), portletActions, true, true);
99 resource = ResourceLocalServiceUtil.getResource(category.getCompanyId(), MBCategory.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(category.getCategoryId()));
104 // Obtention de la resource a partir du message passe en parametre
105 public static Resource getMessageResource(MBMessage message) throws PortalException, SystemException {
106 Resource resource = null;
107 // Obtention de la resource a partir de l'id de l'objet passe en parametre
109 resource = ResourceLocalServiceUtil.getResource(message.getCompanyId(), MBMessage.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(message.getMessageId()));
111 catch (NoSuchResourceException nsre) {
112 boolean portletActions = Validator.isNull(MBMessage.class.getName());
113 ResourceLocalServiceUtil.addResources(message.getCompanyId(), message.getGroupId(), 0, MBMessage.class.getName(), String.valueOf(message.getMessageId()), portletActions, true, true);
114 resource = ResourceLocalServiceUtil.getResource(message.getCompanyId(), MBMessage.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(message.getMessageId()));
120 // Ajout des permissions par defaut pour une category
121 public static void addDefaultPermissionsCategory(MBCategory category) throws PortalException, SystemException {
122 Resource resource = getCategoryResource(category);
124 // Groupe de la resource
125 Group group = GroupLocalServiceUtil.getGroup(category.getGroupId());
127 // Si le groupe est de type organization, alors set des permissions des membres
128 if (group.isOrganization()) {
129 setDefaultRolePermissionsForResource(true, category.getGroupId(), resource, category.getCompanyId(), "category");
131 // Sinon si le groupe est de type communaute, alors set des permissions des membres
132 else if (group.isRegularSite()) {
133 setDefaultRolePermissionsForResource(false, category.getGroupId(), resource, category.getCompanyId(), "category");
137 // Ajout des permissions par defaut pour un message
138 public static void addDefaultPermissionsMessage(MBMessage message) throws PortalException, SystemException {
139 Resource resource = getMessageResource(message);
141 // Groupe de la resource
142 Group group = GroupLocalServiceUtil.getGroup(message.getGroupId());
144 // Si le groupe est de type organization, alors set des permissions des membres
145 if (group.isOrganization()) {
146 // Si c'est le messageRoot, alors set des permissions du thread
147 if (message.isRoot()) {
148 setDefaultRolePermissionsForResource(true, message.getGroupId(), resource, message.getCompanyId(), "thread");
150 setDefaultRolePermissionsForResource(true, message.getGroupId(), resource, message.getCompanyId(), "message");
153 // Sinon si le groupe est de type communaute, alors set des permissions des membres
154 else if (group.isRegularSite()) {
155 // Si c'est le messageRoot, alors set des permissions du thread
156 if (message.isRoot()) {
157 setDefaultRolePermissionsForResource(false, message.getGroupId(), resource, message.getCompanyId(), "thread");
159 setDefaultRolePermissionsForResource(false, message.getGroupId(), resource, message.getCompanyId(), "message");
164 // Set des permissions par defaut
165 private static void setDefaultRolePermissionsForResource(boolean org, Long resourceGroupId, Resource resource, Long companyId, String type) throws PortalException, SystemException {
167 List<String> listBasicActions = new ArrayList<String>();
169 if (type.equals("category") || type.equals("message")) {
170 listBasicActions.add("VIEW");
172 if(ENTMainUtilsLocalServiceUtil.isSchemaSupann(companyId)){
173 listBasicActions.add("ADD_OBJECT");
176 } else if (type.equals("thread")) {
177 listBasicActions.add("VIEW");
178 listBasicActions.add("ADD_OBJECT");
179 listBasicActions.add("SUBSCRIBE");
182 List<String> listMediumActions = new ArrayList<String>();
184 if (type.equals("category")) {
185 listMediumActions.add("VIEW");
186 listMediumActions.add("ADD_OBJECT");
187 listMediumActions.add("MOVE_THREAD");
188 } else if (type.equals("thread")) {
189 listMediumActions.add("VIEW");
190 listMediumActions.add("ADD_OBJECT");
191 listMediumActions.add("SUBSCRIBE");
192 listMediumActions.add("MOVE_THREAD");
193 } else if (type.equals("message")) {
194 listMediumActions.add("VIEW");
197 List<String> listAdvancedActions = new ArrayList<String>();
199 if (type.equals("category")) {
200 listAdvancedActions.add("VIEW");
201 listAdvancedActions.add("EDIT");
202 listAdvancedActions.add("DELETE");
203 listAdvancedActions.add("ADD_OBJECT");
204 listAdvancedActions.add("MOVE_THREAD");
205 listAdvancedActions.add("MANAGE_PERMISSIONS");
206 } else if (type.equals("thread")) {
207 listAdvancedActions.add("VIEW");
208 listAdvancedActions.add("EDIT");
209 listAdvancedActions.add("DELETE");
210 listAdvancedActions.add("ADD_OBJECT");
211 listAdvancedActions.add("SUBSCRIBE");
212 listAdvancedActions.add("MOVE_THREAD");
213 listAdvancedActions.add("MANAGE_PERMISSIONS");
214 } else if (type.equals("message")) {
215 listAdvancedActions.add("EDIT");
216 listAdvancedActions.add("DELETE");
217 listAdvancedActions.add("VIEW");
218 listAdvancedActions.add("MANAGE_PERMISSIONS");
223 Role forumAdmin = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.FORUM_ADMIN);
224 PermissionLocalServiceUtil.setRolePermissions(forumAdmin.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
226 Role forumMember = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.FORUM_MEMBER);
227 PermissionLocalServiceUtil.setRolePermissions(forumMember.getRoleId(), listBasicActions.toArray(new String[listBasicActions.size()]), resource.getResourceId());
229 Role owner = RoleLocalServiceUtil.getRole(companyId, RoleConstants.OWNER);
230 PermissionLocalServiceUtil.setRolePermissions(owner.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
235 Role eleve = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_1);
236 PermissionLocalServiceUtil.setRolePermissions(eleve.getRoleId(), listBasicActions.toArray(new String[listBasicActions.size()]), resource.getResourceId());
238 Role parent = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_2);
239 PermissionLocalServiceUtil.setRolePermissions(parent.getRoleId(), listBasicActions.toArray(new String[listBasicActions.size()]), resource.getResourceId());
241 Role enseignant = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_3);
242 PermissionLocalServiceUtil.setRolePermissions(enseignant.getRoleId(), listMediumActions.toArray(new String[listMediumActions.size()]), resource.getResourceId());
244 Role personnelDirection = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_4);
245 PermissionLocalServiceUtil.setRolePermissions(personnelDirection.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
247 Role documentaliste = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_24);
248 PermissionLocalServiceUtil.setRolePermissions(documentaliste.getRoleId(), listMediumActions.toArray(new String[listMediumActions.size()]), resource.getResourceId());
250 Role persoEducation = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_25);
251 PermissionLocalServiceUtil.setRolePermissions(persoEducation.getRoleId(), listMediumActions.toArray(new String[listMediumActions.size()]), resource.getResourceId());
253 Role groupAdmin = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.GROUP_ADMIN);
254 PermissionLocalServiceUtil.setRolePermissions(groupAdmin.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
256 Role orgMember = RoleLocalServiceUtil.getRole(companyId, RoleConstants.ORGANIZATION_USER);
257 PermissionLocalServiceUtil.setRolePermissions(orgMember.getRoleId(), listBasicActions.toArray(new String[listBasicActions.size()]), resource.getResourceId());
259 Role orgAdmin = RoleLocalServiceUtil.getRole(companyId, RoleConstants.ORGANIZATION_ADMINISTRATOR);
260 PermissionLocalServiceUtil.setRolePermissions(orgAdmin.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
262 Role orgOwner = RoleLocalServiceUtil.getRole(companyId, RoleConstants.ORGANIZATION_OWNER);
263 PermissionLocalServiceUtil.setRolePermissions(orgOwner.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
266 Role comMember = RoleLocalServiceUtil.getRole(companyId, RoleConstants.SITE_MEMBER);
267 PermissionLocalServiceUtil.setRolePermissions(comMember.getRoleId(), listMediumActions.toArray(new String[listMediumActions.size()]), resource.getResourceId());
269 Role comAdmin = RoleLocalServiceUtil.getRole(companyId, RoleConstants.SITE_ADMINISTRATOR);
270 PermissionLocalServiceUtil.setRolePermissions(comAdmin.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
272 Role comOwner = RoleLocalServiceUtil.getRole(companyId, RoleConstants.SITE_OWNER);
273 PermissionLocalServiceUtil.setRolePermissions(comOwner.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());