--- /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.portal.ldap;
+
+
+import javax.naming.Binding;
+import javax.naming.NamingException;
+import javax.naming.directory.Attributes;
+import javax.naming.ldap.LdapContext;
+
+
+
+import com.liferay.portal.NoSuchOrganizationException;
+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.OrganizationConstants;
+import com.liferay.portal.model.User;
+import com.liferay.portal.security.ldap.PortalLDAPUtil;
+import com.liferay.portal.service.OrganizationLocalServiceUtil;
+import com.liferay.portal.service.ServiceContext;
+import com.liferay.portal.service.UserLocalServiceUtil;
+import com.pentila.entSavoie.utils.ENTMainUtilsLocalServiceUtil;
+
+/**
+ * @author Lancelot SIX
+ *
+ * Fonctionnalités utiler permettant de simplifier la synchronisation d'un
+ * compte ldap avec la base Liferay
+ *
+ */
+public class ENTSavoieLdapUtil {
+
+ /**
+ * Recherche et retourne un arrtibut de l'etablissmeent de la personne dans le LDAP
+ *
+ * @param u L'utilisateur donc nous voulons un attr de l'etab
+ * @param attr le nom de l'attrribut
+ * @return
+ * @throws Exception
+ */
+ public static String getEtabLDAPAttribute(User u, String attr) throws Exception {
+ LdapContext ctx = PortalLDAPUtil.getContext(u.getCompanyId());
+ String result = "";
+
+ try {
+ Binding b = PortalLDAPUtil.getUser(u.getCompanyId(),u.getScreenName());
+ final String dn =PortalLDAPUtil.getNameInNamespace(u.getCompanyId(), b);
+
+ Attributes atts = ctx.getAttributes(dn,
+ new String[]{"ENTPersonStructRattach"});
+
+ if (atts.get("ENTPersonStructRattach")!=null) {
+ Attributes etabAtts = ctx.getAttributes(
+ atts.get("ENTPersonStructRattach").get().toString(),
+ new String[]{attr});
+
+ if (etabAtts.get(attr)!=null) {
+ result = etabAtts.get(attr).get().toString();
+ }
+ }
+ } finally {
+ ctx.close();
+ }
+ return result;
+ }
+
+
+ /**
+ * Recherche et retourne l'organization correspondant a la structure de
+ * rattachement de l'utilisateur donné. Si celle-ci n'existe pas, elle est
+ * crée.
+ *
+ * @param u L'utilisateur donc nous voulons la structure de ratachement
+ * @return
+ * @throws Exception
+ */
+ public static Organization getPrincipalOrganization(User u) throws Exception {
+ LdapContext ctx = PortalLDAPUtil.getContext(u.getCompanyId());
+ try {
+ Binding b = PortalLDAPUtil.getUser(u.getCompanyId(),u.getScreenName());
+ final String dn =PortalLDAPUtil.getNameInNamespace(u.getCompanyId(), b);
+ Attributes atts = ctx.getAttributes(dn,
+ new String[]{"ENTPersonStructRattach"});
+ return getPrincipalOrganization(u.getCompanyId(), ctx, atts);
+ } finally {
+ ctx.close();
+ }
+ }
+
+ /**
+ * Retourne l'organization correspondant a celle visee par l'attribut
+ * ENTStructRattach. Si l'attribut est abscent, retourne <code>null</code>.
+ * @param companyId
+ * @param ctx Contexte ldap permettant de recuperer les infos necessaire a
+ * la recuperation ou la creation de l'organization.
+ * @param atts Ensemble d'attributs LDAP (devant contenir
+ * ENTPersStructRattach)
+ * @return L'organization correspondante, si celle-ci existe ou peut être
+ * crée, <code>null</code> sinon.
+ * @throws NamingException
+ * @throws SystemException
+ * @throws PortalException
+ */
+ public static Organization getPrincipalOrganization(long companyId,
+ LdapContext ctx, Attributes atts) throws NamingException, PortalException, SystemException {
+ if (atts.get("ENTPersonStructRattach")==null) {
+ if (_log.isWarnEnabled()) {
+ _log.warn("Current user do not have attribute "+
+ "ENTPersonStructRattach set.");
+ }
+ return null;
+ }
+ Attributes orgAtts = ctx.getAttributes(
+ atts.get("ENTPersonStructRattach").get().toString(),
+ new String[]{"ENTStructureNomCourant", "l", "ENTStructureUAI"});
+
+ if (orgAtts.get("ENTStructureNomCourant")==null) {
+ if (_log.isWarnEnabled()) {
+ _log.warn("Struct rattach do not have the attribute " +
+ "ENTStructureNomCourant ("+
+ atts.get("ENTPersonStructRattach").get().toString()+
+ ")");
+ }
+ return null;
+ }
+
+ String orgName = orgAtts.get("ENTStructureNomCourant").get().toString();
+
+ // si classic --> ENTStructureNomCourant
+ // si full --> ENTStructureNomCourant + l + RNE
+ String etabNameType = ENTMainUtilsLocalServiceUtil.getENTSynchroEtabName(companyId);
+ if (etabNameType.equals("full")) {
+ if (orgAtts.get("l")!=null) {
+ orgName += " - " + orgAtts.get(
+ "l").get().toString();
+ }
+ if (orgAtts.get("ENTStructureUAI")!=null) {
+ orgName += " - " + orgAtts.get(
+ "ENTStructureUAI").get().toString();
+ }
+
+ }
+
+ if (_log.isDebugEnabled() && !orgName.startsWith("CLG-") && !orgName.startsWith("CLG PR-")) {
+ _log.debug("Creating principal organization with name "+
+ orgName);
+ }
+ Organization structRattach = null;
+ try {
+ structRattach = OrganizationLocalServiceUtil.getOrganization(
+ companyId, orgName);
+ } catch (NoSuchOrganizationException e) {
+ structRattach = OrganizationLocalServiceUtil.addOrganization(
+ UserLocalServiceUtil.getDefaultUserId(companyId),
+ getRootOrganization(companyId).getOrganizationId(),
+ orgName,
+ OrganizationConstants.TYPE_REGULAR_ORGANIZATION,
+ true, 0, 0, 12017, "", true, new ServiceContext());
+ }
+
+ return structRattach;
+ }
+
+ /**
+ * Retourne (et cree si necessaire) l'organization parente Cartable de
+ * savoie.
+ *
+ * @param companyId
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ */
+ public static Organization getRootOrganization(long companyId) throws
+ PortalException, SystemException {
+ Organization o = null;
+ try {
+ //o = OrganizationLocalServiceUtil.getOrganization(companyId,
+ //"Cartable de savoie");
+ o = OrganizationLocalServiceUtil.getOrganization(companyId,
+ ENTMainUtilsLocalServiceUtil.getENTOrgRootName(companyId));
+ } catch (NoSuchOrganizationException e) {
+ /*o =OrganizationLocalServiceUtil.addOrganization(
+ UserLocalServiceUtil.getDefaultUserId(companyId),
+ OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID,
+ "Cartable de savoie",
+ OrganizationConstants.TYPE_REGULAR_ORGANIZATION,
+ true, 0, 0, 12017, "", new ServiceContext());*/
+ o =OrganizationLocalServiceUtil.addOrganization(
+ UserLocalServiceUtil.getDefaultUserId(companyId),
+ OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID,
+ ENTMainUtilsLocalServiceUtil.getENTOrgRootName(companyId),
+ OrganizationConstants.TYPE_REGULAR_ORGANIZATION,
+ true, 0, 0, 12017, "", true, new ServiceContext());
+ }
+ return o;
+ }
+
+
+ /**
+ * Recherche et retourne la classe de rattachement de l'utilisateur élève donné.
+ *
+ * @param u L'utilisateur donc nous voulons la classe de ratachement
+ * @return
+ * @throws Exception
+ */
+ public static String getStudentClass(User u) throws Exception {
+ Binding b = PortalLDAPUtil.getUser(u.getCompanyId(),u.getScreenName());
+ final String dn =PortalLDAPUtil.getNameInNamespace(u.getCompanyId(), b);
+ LdapContext ctx = PortalLDAPUtil.getContext(u.getCompanyId());
+ try {
+ Attributes atts = ctx.getAttributes(dn,
+ new String[]{"ENTEleveClasses"});
+ return getStudentClass(u.getCompanyId(), ctx, atts);
+
+ } finally {
+ ctx.close();
+ }
+ }
+
+ public static String getStudentClass(long companyId,
+ LdapContext ctx, Attributes atts) throws NamingException, PortalException, SystemException {
+ if (atts.get("ENTEleveClasses")==null) {
+ if (_log.isWarnEnabled()) {
+ _log.warn("Current user do not have attribute "+
+ "ENTEleveClasses set.");
+ }
+
+ return "";
+ }
+ Attributes orgAtts = ctx.getAttributes(
+ atts.get("ENTEleveClasses").get().toString(),
+ new String[]{"cn"});
+
+ // on a forcement un cn! pourquoi passer par la description
+ /*if (orgAtts.get("description")==null) {
+ if (_log.isWarnEnabled()) {
+ _log.warn("Group do not have the attribute " +
+ "description ("+
+ atts.get("ENTEleveClasses").get().toString()+
+ ")");
+ }
+
+ return "";
+ }*/
+
+ final String classeName = orgAtts.get("cn").get().toString();
+ String classe = classeName.substring(classeName.lastIndexOf("$")+1);
+ return classe;
+ }
+
+
+ private static Log _log = LogFactoryUtil.getLog(ENTSavoieLdapUtil.class);
+
+ /**
+ * Recherche et retourne la valeur de l'attribut Ldap "attributeName" de l'utilisateur élève donné.
+ *
+ * @param u L'utilisateur donc nous voulons la valeur de l'attribut Ldap "attributeName"
+ * @return
+ * @throws Exception
+ */
+ public static String getLdapAttribute(User u, String attributeName) throws Exception {
+ Binding b = PortalLDAPUtil.getUser(u.getCompanyId(),u.getScreenName());
+ final String dn =PortalLDAPUtil.getNameInNamespace(u.getCompanyId(), b);
+
+ Attributes atts = PortalLDAPUtil.doContextAttributes(u.getCompanyId(), dn, new String[]{attributeName});
+
+ return getLdapAttributeTest(u.getScreenName(), u.getCompanyId(), atts, attributeName);
+ }
+
+ public static String getLdapAttribute(long companyId,
+ LdapContext ctx, Attributes atts, String attributeName) throws NamingException, PortalException, SystemException {
+ if (atts.get(attributeName)==null) {
+ if (_log.isWarnEnabled()) {
+ _log.warn("Current user do not have attribute "+
+ attributeName +" set.");
+ }
+ return "";
+ }
+
+ final String attName = atts.get(attributeName).get().toString();
+
+ return attName;
+ }
+
+ public static String getLdapAttribute(long companyId, Attributes atts, String attributeName) throws NamingException, PortalException, SystemException {
+ if (atts.get(attributeName)==null) {
+ if (_log.isWarnEnabled()) {
+ _log.warn("Current user do not have attribute "+
+ attributeName +" set.");
+ }
+ return "";
+ }
+
+ final String attName = atts.get(attributeName).get().toString();
+ return attName;
+ }
+
+ public static String getLdapAttributeTest(String userId, long companyId, Attributes atts, String attributeName) throws NamingException, PortalException, SystemException {
+ if (atts.get(attributeName)==null) {
+ return "";
+ }
+
+ final String attName = atts.get(attributeName).get().toString();
+ return attName;
+ }
+
+}