--- /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.applicationManager;
+
+import java.util.HashSet;
+import java.util.List;
+
+import com.liferay.contacts.util.OrganizationFilter;
+import com.liferay.portal.kernel.dao.orm.QueryUtil;
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.liferay.portal.model.Organization;
+import com.liferay.portal.model.User;
+import com.liferay.portal.model.UserGroup;
+import com.liferay.portal.service.OrganizationLocalServiceUtil;
+import com.liferay.portal.service.RoleLocalServiceUtil;
+import com.liferay.portal.service.UserGroupLocalServiceUtil;
+import com.pentila.entSavoie.applicationManager.model.AMFilter;
+import com.pentila.entSavoie.applicationManager.model.AMFilterContainer;
+import com.pentila.entSavoie.applicationManager.model.AMSingleUsers;
+import com.pentila.entSavoie.applicationManager.model.ApplicationManagerPopulation;
+import com.pentila.entSavoie.applicationManager.model.ApplicationManagerRoles;
+import com.pentila.entSavoie.applicationManager.service.AMFilterContainerLocalServiceUtil;
+import com.pentila.entSavoie.applicationManager.service.AMFilterLocalServiceUtil;
+import com.pentila.entSavoie.applicationManager.service.AMSingleUsersLocalServiceUtil;
+import com.pentila.entSavoie.applicationManager.service.ApplicationManagerPopulationLocalServiceUtil;
+import com.pentila.entSavoie.applicationManager.service.ApplicationManagerRolesLocalServiceUtil;
+
+public class ApplicationManagerUtils {
+
+ private static Log _log = LogFactoryUtil.getLog(ApplicationManagerUtils.class);
+
+ public static List<UserGroup> getAllApplicationGroups(long companyId) throws SystemException, PortalException {
+
+ List<UserGroup> userGrpsApp = UserGroupLocalServiceUtil.search(
+ companyId, "application-%", null, null, QueryUtil.ALL_POS,
+ QueryUtil.ALL_POS, null);
+
+ return userGrpsApp;
+ }
+
+
+ public static HashSet<UserGroup> getUserApplicationRoles(User user) throws SystemException, PortalException {
+
+ // retourne l'ensemble des role applicatif auxquels le user a droit
+ HashSet<UserGroup> userGrps = new HashSet<UserGroup>();
+
+ long time0 = System.currentTimeMillis();
+ // on boucle sur les populations
+ List<ApplicationManagerPopulation> listPopulation = ApplicationManagerPopulationLocalServiceUtil.getPopulationByCompanyId(user.getCompanyId());
+ List<Organization> userSchools = OrganizationFilter.getUserSchools(user);
+ for (ApplicationManagerPopulation amp: listPopulation) {
+ try {
+ // Population names start with 0 (all) or schoolId or letters only (manual populations)
+ // Filter over user's schools
+ for (Organization school : userSchools) {
+ String schoolIdStr = "" + school.getOrganizationId();
+ if (amp.getNom().startsWith("0") || amp.getNom().startsWith(schoolIdStr) || Character.isLetter(amp.getNom().charAt(0))) {
+ Boolean isSingleUserException = isSingleUsers(user, amp.getPopulationId());
+ if (isSingleUserException || hasFilterContainerRight(user, AMFilterContainerLocalServiceUtil.getFilterByParentId_populationId(new Long(0), amp.getPopulationId()).get(0))) {
+ List<ApplicationManagerRoles> listRoles = ApplicationManagerRolesLocalServiceUtil.getRolesByPopulation(amp.getPopulationId());
+ for (ApplicationManagerRoles popRole: listRoles) {
+ userGrps.add(UserGroupLocalServiceUtil.getUserGroup(popRole.getRoleApplicatifId()));
+ }
+ }
+ }
+ }
+ } catch(Exception e) {
+ _log.error("Application Manager : Non-valid rule in population " + amp.getNom());
+ continue;
+ }
+ }
+ long time1 = System.currentTimeMillis();
+ _log.info("Built user application roles in "+(time1 - time0)+" ms");
+ return userGrps;
+ }
+
+ private static Boolean isSingleUsers(User user, long populationId) throws SystemException {
+ // On check en premier lieu les exceptions par personnes simple
+ List<AMSingleUsers> singleUsers = AMSingleUsersLocalServiceUtil.getUsersByPopulationId(populationId);
+ for (AMSingleUsers amsu: singleUsers) {
+ if (amsu.getUserId()==user.getUserId()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private static Boolean hasFilterContainerRight(User user, AMFilterContainer currentfc) throws SystemException, PortalException {
+ String fcOperator = currentfc.getOperator();
+
+ Boolean hasChilds = false;
+
+ // premier test avec les filters
+ for (AMFilter f: AMFilterLocalServiceUtil.getFilterByParentId(currentfc.getFilterContainerId())) {
+ hasChilds = true;
+ Boolean hfr = hasFilterRight(user, f);
+ if (hfr && fcOperator.equals("or")) {
+ return true;
+ }
+ if (!hfr && fcOperator.equals("and")) {
+ return false;
+ }
+ }
+
+ // on test avec maintenant les filters container
+ for (AMFilterContainer fc: AMFilterContainerLocalServiceUtil.getFilterByParentId(currentfc.getFilterContainerId())) {
+ hasChilds = true;
+ Boolean hfr = hasFilterContainerRight(user, fc);
+ if (hfr && fcOperator.equals("or")) {
+ return true;
+ }
+ if (!hfr && fcOperator.equals("and")) {
+ return false;
+ }
+ }
+
+
+
+ // on doit tester si on a au moins un filter
+ if (hasChilds) {
+ // on a pas eu de resultat direct dans la boucle
+ if (fcOperator.equals("or")) {
+ return false;
+ }
+ else {
+ // cas du AND
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private static Boolean hasFilterRight(User user, AMFilter f) throws PortalException, SystemException {
+ String criteria = f.getCriteria();
+ String fOperator = f.getOperator();
+ Long value = f.getValue();
+
+ Boolean result = null;
+ if (criteria.equals("etab") || criteria.equals("classe")) {
+ // on verifie les organisations
+ Organization orgVal = OrganizationLocalServiceUtil.getOrganization(value);
+ result = user.getOrganizations().contains(orgVal);
+ }
+ if (criteria.equals("userRoleNat")) {
+ // on verifie les role
+ result = RoleLocalServiceUtil.hasUserRole(user.getUserId(), value);
+ }
+
+ if (fOperator.equals("equal")) {
+ return result;
+ }
+ else {
+ // On est forcement dans le cas "except"
+ //(fOperator.equals("except"))
+ return !result;
+ }
+ }
+
+
+
+}