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.portal.ldap;
38 import javax.naming.Binding;
39 import javax.naming.NamingException;
40 import javax.naming.directory.Attributes;
41 import javax.naming.ldap.LdapContext;
45 import com.liferay.portal.NoSuchOrganizationException;
46 import com.liferay.portal.kernel.exception.PortalException;
47 import com.liferay.portal.kernel.exception.SystemException;
48 import com.liferay.portal.kernel.log.Log;
49 import com.liferay.portal.kernel.log.LogFactoryUtil;
50 import com.liferay.portal.model.Organization;
51 import com.liferay.portal.model.OrganizationConstants;
52 import com.liferay.portal.model.User;
53 import com.liferay.portal.security.ldap.PortalLDAPUtil;
54 import com.liferay.portal.service.OrganizationLocalServiceUtil;
55 import com.liferay.portal.service.ServiceContext;
56 import com.liferay.portal.service.UserLocalServiceUtil;
57 import com.pentila.entSavoie.utils.ENTMainUtilsLocalServiceUtil;
60 * @author Lancelot SIX
62 * Fonctionnalités utiler permettant de simplifier la synchronisation d'un
63 * compte ldap avec la base Liferay
66 public class ENTSavoieLdapUtil {
69 * Recherche et retourne un arrtibut de l'etablissmeent de la personne dans le LDAP
71 * @param u L'utilisateur donc nous voulons un attr de l'etab
72 * @param attr le nom de l'attrribut
76 public static String getEtabLDAPAttribute(User u, String attr) throws Exception {
77 LdapContext ctx = PortalLDAPUtil.getContext(u.getCompanyId());
81 Binding b = PortalLDAPUtil.getUser(u.getCompanyId(),u.getScreenName());
82 final String dn =PortalLDAPUtil.getNameInNamespace(u.getCompanyId(), b);
84 Attributes atts = ctx.getAttributes(dn,
85 new String[]{"ENTPersonStructRattach"});
87 if (atts.get("ENTPersonStructRattach")!=null) {
88 Attributes etabAtts = ctx.getAttributes(
89 atts.get("ENTPersonStructRattach").get().toString(),
92 if (etabAtts.get(attr)!=null) {
93 result = etabAtts.get(attr).get().toString();
104 * Recherche et retourne l'organization correspondant a la structure de
105 * rattachement de l'utilisateur donné. Si celle-ci n'existe pas, elle est
108 * @param u L'utilisateur donc nous voulons la structure de ratachement
112 public static Organization getPrincipalOrganization(User u) throws Exception {
113 LdapContext ctx = PortalLDAPUtil.getContext(u.getCompanyId());
115 Binding b = PortalLDAPUtil.getUser(u.getCompanyId(),u.getScreenName());
116 final String dn =PortalLDAPUtil.getNameInNamespace(u.getCompanyId(), b);
117 Attributes atts = ctx.getAttributes(dn,
118 new String[]{"ENTPersonStructRattach"});
119 return getPrincipalOrganization(u.getCompanyId(), ctx, atts);
126 * Retourne l'organization correspondant a celle visee par l'attribut
127 * ENTStructRattach. Si l'attribut est abscent, retourne <code>null</code>.
129 * @param ctx Contexte ldap permettant de recuperer les infos necessaire a
130 * la recuperation ou la creation de l'organization.
131 * @param atts Ensemble d'attributs LDAP (devant contenir
132 * ENTPersStructRattach)
133 * @return L'organization correspondante, si celle-ci existe ou peut être
134 * crée, <code>null</code> sinon.
135 * @throws NamingException
136 * @throws SystemException
137 * @throws PortalException
139 public static Organization getPrincipalOrganization(long companyId,
140 LdapContext ctx, Attributes atts) throws NamingException, PortalException, SystemException {
141 if (atts.get("ENTPersonStructRattach")==null) {
142 if (_log.isWarnEnabled()) {
143 _log.warn("Current user do not have attribute "+
144 "ENTPersonStructRattach set.");
148 Attributes orgAtts = ctx.getAttributes(
149 atts.get("ENTPersonStructRattach").get().toString(),
150 new String[]{"ENTStructureNomCourant", "l", "ENTStructureUAI"});
152 if (orgAtts.get("ENTStructureNomCourant")==null) {
153 if (_log.isWarnEnabled()) {
154 _log.warn("Struct rattach do not have the attribute " +
155 "ENTStructureNomCourant ("+
156 atts.get("ENTPersonStructRattach").get().toString()+
162 String orgName = orgAtts.get("ENTStructureNomCourant").get().toString();
164 // si classic --> ENTStructureNomCourant
165 // si full --> ENTStructureNomCourant + l + RNE
166 String etabNameType = ENTMainUtilsLocalServiceUtil.getENTSynchroEtabName(companyId);
167 if (etabNameType.equals("full")) {
168 if (orgAtts.get("l")!=null) {
169 orgName += " - " + orgAtts.get(
170 "l").get().toString();
172 if (orgAtts.get("ENTStructureUAI")!=null) {
173 orgName += " - " + orgAtts.get(
174 "ENTStructureUAI").get().toString();
179 if (_log.isDebugEnabled() && !orgName.startsWith("CLG-") && !orgName.startsWith("CLG PR-")) {
180 _log.debug("Creating principal organization with name "+
183 Organization structRattach = null;
185 structRattach = OrganizationLocalServiceUtil.getOrganization(
187 } catch (NoSuchOrganizationException e) {
188 structRattach = OrganizationLocalServiceUtil.addOrganization(
189 UserLocalServiceUtil.getDefaultUserId(companyId),
190 getRootOrganization(companyId).getOrganizationId(),
192 OrganizationConstants.TYPE_REGULAR_ORGANIZATION,
193 true, 0, 0, 12017, "", true, new ServiceContext());
196 return structRattach;
200 * Retourne (et cree si necessaire) l'organization parente Cartable de
205 * @throws PortalException
206 * @throws SystemException
208 public static Organization getRootOrganization(long companyId) throws
209 PortalException, SystemException {
210 Organization o = null;
212 //o = OrganizationLocalServiceUtil.getOrganization(companyId,
213 //"Cartable de savoie");
214 o = OrganizationLocalServiceUtil.getOrganization(companyId,
215 ENTMainUtilsLocalServiceUtil.getENTOrgRootName(companyId));
216 } catch (NoSuchOrganizationException e) {
217 /*o =OrganizationLocalServiceUtil.addOrganization(
218 UserLocalServiceUtil.getDefaultUserId(companyId),
219 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID,
220 "Cartable de savoie",
221 OrganizationConstants.TYPE_REGULAR_ORGANIZATION,
222 true, 0, 0, 12017, "", new ServiceContext());*/
223 o =OrganizationLocalServiceUtil.addOrganization(
224 UserLocalServiceUtil.getDefaultUserId(companyId),
225 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID,
226 ENTMainUtilsLocalServiceUtil.getENTOrgRootName(companyId),
227 OrganizationConstants.TYPE_REGULAR_ORGANIZATION,
228 true, 0, 0, 12017, "", true, new ServiceContext());
235 * Recherche et retourne la classe de rattachement de l'utilisateur élève donné.
237 * @param u L'utilisateur donc nous voulons la classe de ratachement
241 public static String getStudentClass(User u) throws Exception {
242 Binding b = PortalLDAPUtil.getUser(u.getCompanyId(),u.getScreenName());
243 final String dn =PortalLDAPUtil.getNameInNamespace(u.getCompanyId(), b);
244 LdapContext ctx = PortalLDAPUtil.getContext(u.getCompanyId());
246 Attributes atts = ctx.getAttributes(dn,
247 new String[]{"ENTEleveClasses"});
248 return getStudentClass(u.getCompanyId(), ctx, atts);
255 public static String getStudentClass(long companyId,
256 LdapContext ctx, Attributes atts) throws NamingException, PortalException, SystemException {
257 if (atts.get("ENTEleveClasses")==null) {
258 if (_log.isWarnEnabled()) {
259 _log.warn("Current user do not have attribute "+
260 "ENTEleveClasses set.");
265 Attributes orgAtts = ctx.getAttributes(
266 atts.get("ENTEleveClasses").get().toString(),
269 // on a forcement un cn! pourquoi passer par la description
270 /*if (orgAtts.get("description")==null) {
271 if (_log.isWarnEnabled()) {
272 _log.warn("Group do not have the attribute " +
274 atts.get("ENTEleveClasses").get().toString()+
281 final String classeName = orgAtts.get("cn").get().toString();
282 String classe = classeName.substring(classeName.lastIndexOf("$")+1);
287 private static Log _log = LogFactoryUtil.getLog(ENTSavoieLdapUtil.class);
290 * Recherche et retourne la valeur de l'attribut Ldap "attributeName" de l'utilisateur élève donné.
292 * @param u L'utilisateur donc nous voulons la valeur de l'attribut Ldap "attributeName"
296 public static String getLdapAttribute(User u, String attributeName) throws Exception {
297 Binding b = PortalLDAPUtil.getUser(u.getCompanyId(),u.getScreenName());
298 final String dn =PortalLDAPUtil.getNameInNamespace(u.getCompanyId(), b);
300 Attributes atts = PortalLDAPUtil.doContextAttributes(u.getCompanyId(), dn, new String[]{attributeName});
302 return getLdapAttributeTest(u.getScreenName(), u.getCompanyId(), atts, attributeName);
305 public static String getLdapAttribute(long companyId,
306 LdapContext ctx, Attributes atts, String attributeName) throws NamingException, PortalException, SystemException {
307 if (atts.get(attributeName)==null) {
308 if (_log.isWarnEnabled()) {
309 _log.warn("Current user do not have attribute "+
310 attributeName +" set.");
315 final String attName = atts.get(attributeName).get().toString();
320 public static String getLdapAttribute(long companyId, Attributes atts, String attributeName) throws NamingException, PortalException, SystemException {
321 if (atts.get(attributeName)==null) {
322 if (_log.isWarnEnabled()) {
323 _log.warn("Current user do not have attribute "+
324 attributeName +" set.");
329 final String attName = atts.get(attributeName).get().toString();
333 public static String getLdapAttributeTest(String userId, long companyId, Attributes atts, String attributeName) throws NamingException, PortalException, SystemException {
334 if (atts.get(attributeName)==null) {
338 final String attName = atts.get(attributeName).get().toString();