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.actions;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.List;
43 import javax.naming.Binding;
44 import javax.naming.directory.Attributes;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
48 import com.liferay.portal.kernel.events.Action;
49 import com.liferay.portal.kernel.events.ActionException;
50 import com.liferay.portal.kernel.log.Log;
51 import com.liferay.portal.kernel.log.LogFactoryUtil;
52 import com.liferay.portal.kernel.search.Indexer;
53 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
54 import com.liferay.portal.kernel.search.SearchException;
55 import com.liferay.portal.kernel.util.StringPool;
56 import com.liferay.portal.model.Layout;
57 import com.liferay.portal.model.Organization;
58 import com.liferay.portal.model.User;
59 import com.liferay.portal.model.UserGroup;
60 import com.liferay.portal.security.ldap.PortalLDAPUtil;
61 import com.liferay.portal.security.permission.PermissionCacheUtil;
62 import com.liferay.portal.service.LayoutLocalServiceUtil;
63 import com.liferay.portal.service.UserGroupLocalServiceUtil;
64 import com.liferay.portal.service.UserLocalServiceUtil;
65 import com.liferay.portal.util.PortalUtil;
66 import com.pentila.entSavoie.applicationManager.ApplicationManagerUtils;
67 import com.pentila.entSavoie.communityInfos.service.GroupInfosLocalServiceUtil;
68 import com.pentila.entSavoie.communityInfos.service.GroupUtilsLocalServiceUtil;
69 import com.pentila.entSavoie.synchroLdap.impl.SynchronizationManager;
70 import com.pentila.entSavoie.utils.ENTMainUtilsLocalServiceUtil;
71 import com.pentila.entSavoie.utils.ENTOrganizationsUtil;
72 import com.pentila.entSavoie.utils.GroupCacheUtil;
73 import com.pentila.entSavoie.utils.PortletConstant;
74 import com.pentila.entSavoie.utils.XitiUtil;
77 * @author Tardy Camille - Barma Igor
79 * Cette action vas permettre de
81 * <li>Inscrire l'utilisateur dans l'UserGroup correspondant. Cela aura pour consequence la
82 * génération des pages dans l'espace utilisateur</li>
86 public class UpdateGroupsOnLoginAction extends Action{
89 public void run(HttpServletRequest request, HttpServletResponse response)
90 throws ActionException {
92 //Utilisateur venant de se loguer
93 final User currUser = PortalUtil.getUser(request);
94 final long[] userIds = {currUser.getUserId()};
96 //on verifie que le user possede "cartable de savoie" (cad si il y abien eu synchro avec le LDAP)
97 Organization cartableSavoie = ENTOrganizationsUtil.getOrCreateRootOrg(currUser.getCompanyId());
99 //si pas dedans : synchroniser ce user avec le LDAP
100 if (!UserLocalServiceUtil.hasOrganizationUser(cartableSavoie.getOrganizationId(), currUser.getUserId())){
103 Binding binding = PortalLDAPUtil.getUser(currUser.getCompanyId(), currUser.getScreenName());
104 String userDN = PortalLDAPUtil.getNameInNamespace(currUser.getCompanyId(),binding);
105 Attributes attrs = PortalLDAPUtil.getUserAttributes(currUser.getCompanyId(), userDN);
108 SynchronizationManager.synchronizeUser(currUser.getCompanyId(), attrs, StringPool.BLANK, false);
109 } catch (Exception exc) {
113 //envoie d'un mail de notification (synchro partielle)
114 // String sbjt = "Bienvenue dans l'environnement numérique des collèges de " + ENTMainUtilsLocalServiceUtil.getENTOrgRootDepartment(currUser.getCompanyId()) + ".";
115 // String content = "Bienvenue dans l'environnement numérique des collèges de " + ENTMainUtilsLocalServiceUtil.getENTOrgRootDepartment(currUser.getCompanyId()) + ",<br><br>" +
116 // "Votre compte vient d'être activé partiellement.<br>" +
117 // "Attention, vous ne pouvez pas encore avoir accès à vos classes! " +
118 // "Pour cela, vous devez attendre une synchronisation avec l'annuaire SCONET qui s'effectuera sous 24 heures." +
119 // "Vous pouvez cependant commencer à utiliser l'ensemble des services mis à votre disposition. <br>" ;
120 // Set<Long> receivers = new HashSet<Long>();
121 // receivers.add(currUser.getUserId());
122 // IMUtils.sendAwarenessMessage(currUser.getGroup().getGroupId(), receivers, sbjt, content, JSONFactoryUtil.createJSONArray());
125 List<UserGroup> allAppUserGrps = ApplicationManagerUtils.getAllApplicationGroups(currUser.getCompanyId());
126 HashSet<UserGroup> userAppGrps = ApplicationManagerUtils.getUserApplicationRoles(currUser);
128 // get all layout for app groups
129 List<Long> listAllAppUserGrpIds = new ArrayList<Long>();
130 for(UserGroup userGrpApp : allAppUserGrps){
131 listAllAppUserGrpIds.add(userGrpApp.getGroup().getGroupId());
133 Map<Long,List<Layout>> mapsLayoutPerGroup = GroupUtilsLocalServiceUtil.getLayoutForGroups(listAllAppUserGrpIds, true);
136 // pages privees du user
137 List<Layout> privatePages = LayoutLocalServiceUtil.getLayouts(currUser.getGroup().getGroupId(), true);
138 List<String> currentUserPagesFriendlyUrls = new ArrayList<String>();
139 for (Layout l:privatePages) {
140 currentUserPagesFriendlyUrls.add(l.getFriendlyURL());
143 List<String> pagesFriendlyUrls = new ArrayList<String>();
145 List<UserGroup> usersUserGroup = UserGroupLocalServiceUtil.getUserUserGroups(currUser.getUserId());
147 List<Long> usersGroupsToUnset = new ArrayList<Long>();
148 for (UserGroup userGrpApp : allAppUserGrps) {
149 //Determine si l'utilisateur dispose du role
150 final boolean hasRole = userAppGrps.contains(userGrpApp);
153 // boolean pour checker si on doit verifier les pages
154 boolean checkUserPages = false;
156 // get group layout from map, init empty a array list if there no layout for the group
157 List<Layout> grpLayout = mapsLayoutPerGroup.get(userGrpApp.getGroup().getGroupId());
158 grpLayout = grpLayout != null ? grpLayout : new ArrayList<Layout>();
160 //Si l'utilisateur n'avais pas deja le groupe,
161 //l'ajoute afin qu'il puisse disposer des pages
164 if (!usersUserGroup.contains(userGrpApp)) {
166 UserLocalServiceUtil.addUserGroupUsers(userGrpApp.getUserGroupId(), userIds);
168 catch(Exception exc) {
169 // la personne est semble-t-il pas dans le groupe mais elle a deja une page du meme nom!!!
170 // on supprime les pages du memes nom que celle dans le groupe et on relance avec le checkUserPages
171 // on stocke les pages privées associées au groupe
172 _log.error("Error during group setting :" + userGrpApp.getName());
173 for (Layout userL:privatePages) {
174 for (Layout l : grpLayout) {
175 if (userL.getFriendlyURL().equals(l.getFriendlyURL())) {
176 // on supprime la page du user!!!
177 LayoutLocalServiceUtil.deleteLayout(userL);
178 _log.warn("Pge delete for user: " + userL.getFriendlyURL());
182 checkUserPages = true;
186 // la personne a deja le groupe donc on va verifier les pages
187 checkUserPages = true;
190 for (Layout l : grpLayout) {
191 pagesFriendlyUrls.add(l.getFriendlyURL());
192 // on va verifier que le user a bien les pages du groupes si on ne vient pas de l'inscrire!!! --> il devrait deja avoir les pages!!!
193 if (checkUserPages && !currentUserPagesFriendlyUrls.contains(l.getFriendlyURL())) {
194 // Probleme --> le user ne possede pas les pages attendues!!, il etait pourtant deja dans le groupe
195 // on supprime tout simplement la personne du grp de users
196 UserLocalServiceUtil.unsetUserGroupUsers(userGrpApp.getUserGroupId(), userIds);
197 // normalement le grp ne possede que une page <--> application donc on sait que c'est celle-ci et on re-inscrit le user
198 // sinon ben le user aura ses pages au prochain login!
199 if (grpLayout.size()==1) {
200 UserLocalServiceUtil.addUserGroupUsers(userGrpApp.getUserGroupId(), userIds);
205 //S'assure que la personne n'est plus dans le groupe
207 usersGroupsToUnset.add(userGrpApp.getUserGroupId());
210 this.unsetUserGroupsForAUser(usersGroupsToUnset,currUser.getUserId());
211 // On supprime les pages que l'utilisateur ne doit pas avoir!
212 final List<Layout> userPrivateLayouts = LayoutLocalServiceUtil.getLayouts(currUser.getGroup().getGroupId(), true);
213 int nbUserPages = userPrivateLayouts.size();
215 for (Layout l : userPrivateLayouts) {
216 if (!pagesFriendlyUrls.contains(l.getFriendlyURL())) {
217 //Suppression de la pages
218 LayoutLocalServiceUtil.deleteLayout(l);
221 // on verifie sa place - En fait l'affichage de la premiere page
222 // Define portlet display priority for landing page
223 if (l.isTypePortlet()) {
224 int priority = nbUserPages+1;
226 String defaultPage = ENTMainUtilsLocalServiceUtil.getLoginDefaultPageLanding(currUser.getCompanyId());
227 if (l.getFriendlyURL().equals(defaultPage)) {
230 else if (l.getTypeSettings().contains(PortletConstant.DASHBOARD_PORTLET_ID)) {
233 else if (l.getTypeSettings().contains(PortletConstant.CARTABLE_PORTLET_ID)) {
236 else if (l.getTypeSettings().contains(PortletConstant.CASIER_CARTABLE_PORTLET_ID)) {
239 else if (l.getTypeSettings().contains(PortletConstant.ACTUALITES_PORTLET_ID)) {
243 l.setPriority(priority);
244 LayoutLocalServiceUtil.updateLayout(l, true);
249 if(ENTMainUtilsLocalServiceUtil.getXitiEnable(currUser.getCompanyId())){
250 //On envoie une requete xiti
251 XitiUtil.sendXitiRequest(XitiUtil.getEtab(currUser), XitiUtil.EMPTY_LIB_SERVICE, currUser.getUserId(), XitiUtil.EMPTY_INTEGER_ID, XitiUtil.getRole(currUser), currUser.getCompanyId());
255 GroupCacheUtil.getUserGroups(currUser);
256 _log.info("Built group cache for user "+currUser.getFullName());
258 } catch (Exception e) {
259 if (_log.isErrorEnabled()) {
262 throw new ActionException(e);
267 * remove some user group for a user in table user_userGroups
268 * @param listUserGroupsId the id list of userGroup to delete
269 * @param userId the id of user it will delete the user group
270 * @throws SearchException
272 private void unsetUserGroupsForAUser (List<Long> listUserGroupsId, long userId) throws SearchException {
273 GroupInfosLocalServiceUtil.unsetUserGroupsForAUser(listUserGroupsId, userId);
274 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
276 indexer.reindex(userId);
278 PermissionCacheUtil.clearCache();
281 private Log _log = LogFactoryUtil.getLog(this.getClass());