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.applicationManager;
37 import java.util.HashSet;
38 import java.util.List;
40 import com.liferay.contacts.util.OrganizationFilter;
41 import com.liferay.portal.kernel.dao.orm.QueryUtil;
42 import com.liferay.portal.kernel.exception.PortalException;
43 import com.liferay.portal.kernel.exception.SystemException;
44 import com.liferay.portal.kernel.log.Log;
45 import com.liferay.portal.kernel.log.LogFactoryUtil;
46 import com.liferay.portal.model.Organization;
47 import com.liferay.portal.model.User;
48 import com.liferay.portal.model.UserGroup;
49 import com.liferay.portal.service.OrganizationLocalServiceUtil;
50 import com.liferay.portal.service.RoleLocalServiceUtil;
51 import com.liferay.portal.service.UserGroupLocalServiceUtil;
52 import com.pentila.entSavoie.applicationManager.model.AMFilter;
53 import com.pentila.entSavoie.applicationManager.model.AMFilterContainer;
54 import com.pentila.entSavoie.applicationManager.model.AMSingleUsers;
55 import com.pentila.entSavoie.applicationManager.model.ApplicationManagerPopulation;
56 import com.pentila.entSavoie.applicationManager.model.ApplicationManagerRoles;
57 import com.pentila.entSavoie.applicationManager.service.AMFilterContainerLocalServiceUtil;
58 import com.pentila.entSavoie.applicationManager.service.AMFilterLocalServiceUtil;
59 import com.pentila.entSavoie.applicationManager.service.AMSingleUsersLocalServiceUtil;
60 import com.pentila.entSavoie.applicationManager.service.ApplicationManagerPopulationLocalServiceUtil;
61 import com.pentila.entSavoie.applicationManager.service.ApplicationManagerRolesLocalServiceUtil;
63 public class ApplicationManagerUtils {
65 private static Log _log = LogFactoryUtil.getLog(ApplicationManagerUtils.class);
67 public static List<UserGroup> getAllApplicationGroups(long companyId) throws SystemException, PortalException {
69 List<UserGroup> userGrpsApp = UserGroupLocalServiceUtil.search(
70 companyId, "application-%", null, null, QueryUtil.ALL_POS,
71 QueryUtil.ALL_POS, null);
77 public static HashSet<UserGroup> getUserApplicationRoles(User user) throws SystemException, PortalException {
79 // retourne l'ensemble des role applicatif auxquels le user a droit
80 HashSet<UserGroup> userGrps = new HashSet<UserGroup>();
82 long time0 = System.currentTimeMillis();
83 // on boucle sur les populations
84 List<ApplicationManagerPopulation> listPopulation = ApplicationManagerPopulationLocalServiceUtil.getPopulationByCompanyId(user.getCompanyId());
85 List<Organization> userSchools = OrganizationFilter.getUserSchools(user);
86 for (ApplicationManagerPopulation amp: listPopulation) {
88 // Population names start with 0 (all) or schoolId or letters only (manual populations)
89 // Filter over user's schools
90 for (Organization school : userSchools) {
91 String schoolIdStr = "" + school.getOrganizationId();
92 if (amp.getNom().startsWith("0") || amp.getNom().startsWith(schoolIdStr) || Character.isLetter(amp.getNom().charAt(0))) {
93 Boolean isSingleUserException = isSingleUsers(user, amp.getPopulationId());
94 if (isSingleUserException || hasFilterContainerRight(user, AMFilterContainerLocalServiceUtil.getFilterByParentId_populationId(new Long(0), amp.getPopulationId()).get(0))) {
95 List<ApplicationManagerRoles> listRoles = ApplicationManagerRolesLocalServiceUtil.getRolesByPopulation(amp.getPopulationId());
96 for (ApplicationManagerRoles popRole: listRoles) {
97 userGrps.add(UserGroupLocalServiceUtil.getUserGroup(popRole.getRoleApplicatifId()));
102 } catch(Exception e) {
103 _log.error("Application Manager : Non-valid rule in population " + amp.getNom());
107 long time1 = System.currentTimeMillis();
108 _log.info("Built user application roles in "+(time1 - time0)+" ms");
112 private static Boolean isSingleUsers(User user, long populationId) throws SystemException {
113 // On check en premier lieu les exceptions par personnes simple
114 List<AMSingleUsers> singleUsers = AMSingleUsersLocalServiceUtil.getUsersByPopulationId(populationId);
115 for (AMSingleUsers amsu: singleUsers) {
116 if (amsu.getUserId()==user.getUserId()) {
123 private static Boolean hasFilterContainerRight(User user, AMFilterContainer currentfc) throws SystemException, PortalException {
124 String fcOperator = currentfc.getOperator();
126 Boolean hasChilds = false;
128 // premier test avec les filters
129 for (AMFilter f: AMFilterLocalServiceUtil.getFilterByParentId(currentfc.getFilterContainerId())) {
131 Boolean hfr = hasFilterRight(user, f);
132 if (hfr && fcOperator.equals("or")) {
135 if (!hfr && fcOperator.equals("and")) {
140 // on test avec maintenant les filters container
141 for (AMFilterContainer fc: AMFilterContainerLocalServiceUtil.getFilterByParentId(currentfc.getFilterContainerId())) {
143 Boolean hfr = hasFilterContainerRight(user, fc);
144 if (hfr && fcOperator.equals("or")) {
147 if (!hfr && fcOperator.equals("and")) {
154 // on doit tester si on a au moins un filter
156 // on a pas eu de resultat direct dans la boucle
157 if (fcOperator.equals("or")) {
169 private static Boolean hasFilterRight(User user, AMFilter f) throws PortalException, SystemException {
170 String criteria = f.getCriteria();
171 String fOperator = f.getOperator();
172 Long value = f.getValue();
174 Boolean result = null;
175 if (criteria.equals("etab") || criteria.equals("classe")) {
176 // on verifie les organisations
177 Organization orgVal = OrganizationLocalServiceUtil.getOrganization(value);
178 result = user.getOrganizations().contains(orgVal);
180 if (criteria.equals("userRoleNat")) {
181 // on verifie les role
182 result = RoleLocalServiceUtil.hasUserRole(user.getUserId(), value);
185 if (fOperator.equals("equal")) {
189 // On est forcement dans le cas "except"
190 //(fOperator.equals("except"))