--- /dev/null
+/*******************************************************************************
+ * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
+ *
+ * This file is part of the work and learning management system Pentila Nero.
+ *
+ * Pentila Nero is free software. You can redistribute it and/or modify since
+ * you respect the terms of either (at least one of the both license) :
+ * - under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ * - the CeCILL-C as published by CeCILL-C; either version 1 of the
+ * License, or any later version
+ * - the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the license,
+ * or (at your option) any later version.
+ *
+ * There are special exceptions to the terms and conditions of the
+ * licenses as they are applied to this software. View the full text of
+ * the exception in file LICENSE-PROJECT.txt in the directory of this software
+ * distribution.
+ *
+ * Pentila Nero is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Licenses for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * and the CeCILL-C and the GNU Lesser General Public License along with
+ * Pentila Nero. If not, see :
+ * <http://www.gnu.org/licenses/> and
+ * <http://www.cecill.info/licences.fr.html>.
+ ******************************************************************************/
+package com.pentila.entSavoie.messageBoards;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.liferay.portal.NoSuchResourceException;
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.util.Validator;
+import com.liferay.portal.model.Group;
+import com.liferay.portal.model.Resource;
+import com.liferay.portal.model.ResourceConstants;
+import com.liferay.portal.model.Role;
+import com.liferay.portal.model.RoleConstants;
+import com.liferay.portal.security.permission.PermissionChecker;
+import com.liferay.portal.service.GroupLocalServiceUtil;
+import com.liferay.portal.service.PermissionLocalServiceUtil;
+import com.liferay.portal.service.ResourceLocalServiceUtil;
+import com.liferay.portal.service.RoleLocalServiceUtil;
+import com.liferay.portlet.messageboards.model.MBCategory;
+import com.liferay.portlet.messageboards.model.MBMessage;
+import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
+import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
+import com.pentila.entSavoie.ENTRolesConstants;
+import com.pentila.entSavoie.utils.ENTMainUtilsLocalServiceUtil;
+
+public class MBUtilsOld {
+
+ // Verification des permissions pour une categorie
+ public static boolean checkCategoryPermission(PermissionChecker permissionChecker, long categoryId, String actionId) throws PortalException, SystemException {
+
+ MBCategory category = MBCategoryLocalServiceUtil.getMBCategory(categoryId);
+
+ if (permissionChecker.hasOwnerPermission(category.getCompanyId(), MBCategory.class.getName(), categoryId, category.getUserId(), actionId)) {
+ return true;
+ }
+
+ return (permissionChecker.hasPermission(category.getGroupId(), MBCategory.class.getName(),category.getCategoryId(), actionId));
+
+ }
+
+ // Verification des permissions pour un message
+ public static boolean checkMessagePermission(PermissionChecker permissionChecker, long messageId, String actionId) throws PortalException, SystemException {
+
+ MBMessage message = MBMessageLocalServiceUtil.getMBMessage(messageId);
+
+ if (permissionChecker.hasOwnerPermission(message.getCompanyId(), MBMessage.class.getName(), messageId, message.getUserId(), actionId)) {
+ return true;
+ }
+
+ return (permissionChecker.hasPermission(message.getGroupId(), MBMessage.class.getName(), messageId, actionId));
+
+ }
+
+ // Obtention de la resource a partir de la category passe en parametre
+ public static Resource getCategoryResource(MBCategory category) throws PortalException, SystemException {
+ Resource resource = null;
+ // Obtention de la resource a partir de l'id de l'objet passe en parametre
+ try {
+ resource = ResourceLocalServiceUtil.getResource(category.getCompanyId(), MBCategory.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(category.getCategoryId()));
+ }
+ catch (NoSuchResourceException nsre) {
+ boolean portletActions = Validator.isNull(MBCategory.class.getName());
+ ResourceLocalServiceUtil.addResources(category.getCompanyId(), category.getGroupId(), 0, MBCategory.class.getName(), String.valueOf(category.getCategoryId()), portletActions, true, true);
+ resource = ResourceLocalServiceUtil.getResource(category.getCompanyId(), MBCategory.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(category.getCategoryId()));
+ }
+ return resource;
+ }
+
+ // Obtention de la resource a partir du message passe en parametre
+ public static Resource getMessageResource(MBMessage message) throws PortalException, SystemException {
+ Resource resource = null;
+ // Obtention de la resource a partir de l'id de l'objet passe en parametre
+ try {
+ resource = ResourceLocalServiceUtil.getResource(message.getCompanyId(), MBMessage.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(message.getMessageId()));
+ }
+ catch (NoSuchResourceException nsre) {
+ boolean portletActions = Validator.isNull(MBMessage.class.getName());
+ ResourceLocalServiceUtil.addResources(message.getCompanyId(), message.getGroupId(), 0, MBMessage.class.getName(), String.valueOf(message.getMessageId()), portletActions, true, true);
+ resource = ResourceLocalServiceUtil.getResource(message.getCompanyId(), MBMessage.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(message.getMessageId()));
+ }
+ return resource;
+ }
+
+
+ // Ajout des permissions par defaut pour une category
+ public static void addDefaultPermissionsCategory(MBCategory category) throws PortalException, SystemException {
+ Resource resource = getCategoryResource(category);
+
+ // Groupe de la resource
+ Group group = GroupLocalServiceUtil.getGroup(category.getGroupId());
+
+ // Si le groupe est de type organization, alors set des permissions des membres
+ if (group.isOrganization()) {
+ setDefaultRolePermissionsForResource(true, category.getGroupId(), resource, category.getCompanyId(), "category");
+ }
+ // Sinon si le groupe est de type communaute, alors set des permissions des membres
+ else if (group.isRegularSite()) {
+ setDefaultRolePermissionsForResource(false, category.getGroupId(), resource, category.getCompanyId(), "category");
+ }
+ }
+
+ // Ajout des permissions par defaut pour un message
+ public static void addDefaultPermissionsMessage(MBMessage message) throws PortalException, SystemException {
+ Resource resource = getMessageResource(message);
+
+ // Groupe de la resource
+ Group group = GroupLocalServiceUtil.getGroup(message.getGroupId());
+
+ // Si le groupe est de type organization, alors set des permissions des membres
+ if (group.isOrganization()) {
+ // Si c'est le messageRoot, alors set des permissions du thread
+ if (message.isRoot()) {
+ setDefaultRolePermissionsForResource(true, message.getGroupId(), resource, message.getCompanyId(), "thread");
+ } else {
+ setDefaultRolePermissionsForResource(true, message.getGroupId(), resource, message.getCompanyId(), "message");
+ }
+ }
+ // Sinon si le groupe est de type communaute, alors set des permissions des membres
+ else if (group.isRegularSite()) {
+ // Si c'est le messageRoot, alors set des permissions du thread
+ if (message.isRoot()) {
+ setDefaultRolePermissionsForResource(false, message.getGroupId(), resource, message.getCompanyId(), "thread");
+ } else {
+ setDefaultRolePermissionsForResource(false, message.getGroupId(), resource, message.getCompanyId(), "message");
+ }
+ }
+ }
+
+ // Set des permissions par defaut
+ private static void setDefaultRolePermissionsForResource(boolean org, Long resourceGroupId, Resource resource, Long companyId, String type) throws PortalException, SystemException {
+
+ List<String> listBasicActions = new ArrayList<String>();
+
+ if (type.equals("category") || type.equals("message")) {
+ listBasicActions.add("VIEW");
+
+ if(ENTMainUtilsLocalServiceUtil.isSchemaSupann(companyId)){
+ listBasicActions.add("ADD_OBJECT");
+ }
+
+ } else if (type.equals("thread")) {
+ listBasicActions.add("VIEW");
+ listBasicActions.add("ADD_OBJECT");
+ listBasicActions.add("SUBSCRIBE");
+ }
+
+ List<String> listMediumActions = new ArrayList<String>();
+
+ if (type.equals("category")) {
+ listMediumActions.add("VIEW");
+ listMediumActions.add("ADD_OBJECT");
+ listMediumActions.add("MOVE_THREAD");
+ } else if (type.equals("thread")) {
+ listMediumActions.add("VIEW");
+ listMediumActions.add("ADD_OBJECT");
+ listMediumActions.add("SUBSCRIBE");
+ listMediumActions.add("MOVE_THREAD");
+ } else if (type.equals("message")) {
+ listMediumActions.add("VIEW");
+ }
+
+ List<String> listAdvancedActions = new ArrayList<String>();
+
+ if (type.equals("category")) {
+ listAdvancedActions.add("VIEW");
+ listAdvancedActions.add("EDIT");
+ listAdvancedActions.add("DELETE");
+ listAdvancedActions.add("ADD_OBJECT");
+ listAdvancedActions.add("MOVE_THREAD");
+ listAdvancedActions.add("MANAGE_PERMISSIONS");
+ } else if (type.equals("thread")) {
+ listAdvancedActions.add("VIEW");
+ listAdvancedActions.add("EDIT");
+ listAdvancedActions.add("DELETE");
+ listAdvancedActions.add("ADD_OBJECT");
+ listAdvancedActions.add("SUBSCRIBE");
+ listAdvancedActions.add("MOVE_THREAD");
+ listAdvancedActions.add("MANAGE_PERMISSIONS");
+ } else if (type.equals("message")) {
+ listAdvancedActions.add("EDIT");
+ listAdvancedActions.add("DELETE");
+ listAdvancedActions.add("VIEW");
+ listAdvancedActions.add("MANAGE_PERMISSIONS");
+ }
+
+
+
+ Role forumAdmin = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.FORUM_ADMIN);
+ PermissionLocalServiceUtil.setRolePermissions(forumAdmin.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
+
+ Role forumMember = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.FORUM_MEMBER);
+ PermissionLocalServiceUtil.setRolePermissions(forumMember.getRoleId(), listBasicActions.toArray(new String[listBasicActions.size()]), resource.getResourceId());
+
+ Role owner = RoleLocalServiceUtil.getRole(companyId, RoleConstants.OWNER);
+ PermissionLocalServiceUtil.setRolePermissions(owner.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
+
+
+
+ if (org==true) {
+ Role eleve = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_1);
+ PermissionLocalServiceUtil.setRolePermissions(eleve.getRoleId(), listBasicActions.toArray(new String[listBasicActions.size()]), resource.getResourceId());
+
+ Role parent = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_2);
+ PermissionLocalServiceUtil.setRolePermissions(parent.getRoleId(), listBasicActions.toArray(new String[listBasicActions.size()]), resource.getResourceId());
+
+ Role enseignant = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_3);
+ PermissionLocalServiceUtil.setRolePermissions(enseignant.getRoleId(), listMediumActions.toArray(new String[listMediumActions.size()]), resource.getResourceId());
+
+ Role personnelDirection = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_4);
+ PermissionLocalServiceUtil.setRolePermissions(personnelDirection.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
+
+ Role documentaliste = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_24);
+ PermissionLocalServiceUtil.setRolePermissions(documentaliste.getRoleId(), listMediumActions.toArray(new String[listMediumActions.size()]), resource.getResourceId());
+
+ Role persoEducation = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.NATIONAL_25);
+ PermissionLocalServiceUtil.setRolePermissions(persoEducation.getRoleId(), listMediumActions.toArray(new String[listMediumActions.size()]), resource.getResourceId());
+
+ Role groupAdmin = RoleLocalServiceUtil.getRole(companyId, ENTRolesConstants.GROUP_ADMIN);
+ PermissionLocalServiceUtil.setRolePermissions(groupAdmin.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
+
+ Role orgMember = RoleLocalServiceUtil.getRole(companyId, RoleConstants.ORGANIZATION_USER);
+ PermissionLocalServiceUtil.setRolePermissions(orgMember.getRoleId(), listBasicActions.toArray(new String[listBasicActions.size()]), resource.getResourceId());
+
+ Role orgAdmin = RoleLocalServiceUtil.getRole(companyId, RoleConstants.ORGANIZATION_ADMINISTRATOR);
+ PermissionLocalServiceUtil.setRolePermissions(orgAdmin.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
+
+ Role orgOwner = RoleLocalServiceUtil.getRole(companyId, RoleConstants.ORGANIZATION_OWNER);
+ PermissionLocalServiceUtil.setRolePermissions(orgOwner.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
+
+ } else {
+ Role comMember = RoleLocalServiceUtil.getRole(companyId, RoleConstants.SITE_MEMBER);
+ PermissionLocalServiceUtil.setRolePermissions(comMember.getRoleId(), listMediumActions.toArray(new String[listMediumActions.size()]), resource.getResourceId());
+
+ Role comAdmin = RoleLocalServiceUtil.getRole(companyId, RoleConstants.SITE_ADMINISTRATOR);
+ PermissionLocalServiceUtil.setRolePermissions(comAdmin.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
+
+ Role comOwner = RoleLocalServiceUtil.getRole(companyId, RoleConstants.SITE_OWNER);
+ PermissionLocalServiceUtil.setRolePermissions(comOwner.getRoleId(), listAdvancedActions.toArray(new String[listAdvancedActions.size()]), resource.getResourceId());
+ }
+ }
+
+}