<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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 GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.xpn.xwiki.user.impl.LDAP;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.classes.BaseClass;

/**
 * Helper to manager LDAP profile XClass and XObject.
 * 
 * @version $Id$
 */
public class LDAPProfileXClass
{
    public static final String LDAP_XCLASS = "XWiki.LDAPProfileClass";

    public static final String LDAP_XFIELD_DN = "dn";

    public static final String LDAP_XFIELDPN_DN = "LDAP DN";

    public static final String LDAP_XFIELD_UID = "uid";

    public static final String LDAP_XFIELDPN_UID = "LDAP user unique identifier";

    public static final EntityReference LDAPPROFILECLASS_REFERENCE = new EntityReference("LDAPProfileClass",
        EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE));

    /**
     * The XWiki space where users are stored.
     */
    private static final String XWIKI_USER_SPACE = "XWiki";

    /**
     * Logging tool.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(XWikiLDAPAuthServiceImpl.class);

    private XWikiContext context;

    private BaseClass ldapClass;

    public LDAPProfileXClass(XWikiContext context) throws XWikiException
    {
        this.context = context;

        XWikiDocument ldapClassDoc = context.getWiki().getDocument(LDAPPROFILECLASS_REFERENCE, context);

        this.ldapClass = ldapClassDoc.getXClass();

        boolean needsUpdate = this.ldapClass.addTextField(LDAP_XFIELD_DN, LDAP_XFIELDPN_DN, 80);
        needsUpdate |= this.ldapClass.addTextField(LDAP_XFIELD_UID, LDAP_XFIELDPN_UID, 80);

        if (needsUpdate) {
            context.getWiki().saveDocument(ldapClassDoc, "Update LDAP user profile class", context);
        }
    }

    /**
     * @param userDocument the user profile page.
     * @return the dn store in the user profile. Null if it can't find any or if it's empty.
     */
    public String getDn(XWikiDocument userDocument)
    {
        BaseObject ldapObject = userDocument.getXObject(this.ldapClass.getDocumentReference());

        return ldapObject == null ? null : getDn(ldapObject);
    }<commit_msg>XWIKI-11184: XWiki.LDAPProfileClass should be hidden<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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 GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.xpn.xwiki.user.impl.LDAP;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.classes.BaseClass;
import com.xpn.xwiki.user.api.XWikiRightService;

/**
 * Helper to manager LDAP profile XClass and XObject.
 * 
 * @version $Id$
 */
public class LDAPProfileXClass
{
    public static final String LDAP_XCLASS = "XWiki.LDAPProfileClass";

    public static final String LDAP_XFIELD_DN = "dn";

    public static final String LDAP_XFIELDPN_DN = "LDAP DN";

    public static final String LDAP_XFIELD_UID = "uid";

    public static final String LDAP_XFIELDPN_UID = "LDAP user unique identifier";

    public static final EntityReference LDAPPROFILECLASS_REFERENCE = new EntityReference("LDAPProfileClass",
        EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE));

    /**
     * The XWiki space where users are stored.
     */
    private static final String XWIKI_USER_SPACE = "XWiki";

    /**
     * Logging tool.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(XWikiLDAPAuthServiceImpl.class);

    private XWikiContext context;

    private BaseClass ldapClass;

    public LDAPProfileXClass(XWikiContext context) throws XWikiException
    {
        this.context = context;

        XWikiDocument ldapClassDoc = context.getWiki().getDocument(LDAPPROFILECLASS_REFERENCE, context);

        this.ldapClass = ldapClassDoc.getXClass();

        boolean needsUpdate = this.ldapClass.addTextField(LDAP_XFIELD_DN, LDAP_XFIELDPN_DN, 80);
        needsUpdate |= this.ldapClass.addTextField(LDAP_XFIELD_UID, LDAP_XFIELDPN_UID, 80);

        if (ldapClassDoc.getCreatorReference() == null) {
            needsUpdate = true;
            ldapClassDoc.setCreator(XWikiRightService.SUPERADMIN_USER);
        }
        if (ldapClassDoc.getAuthorReference() == null) {
            needsUpdate = true;
            ldapClassDoc.setAuthorReference(ldapClassDoc.getCreatorReference());
        }
        if (!ldapClassDoc.isHidden()) {
            needsUpdate = true;
            ldapClassDoc.setHidden(true);
        }

        if (needsUpdate) {
            context.getWiki().saveDocument(ldapClassDoc, "Update LDAP user profile class", context);
        }
    }

    /**
     * @param userDocument the user profile page.
     * @return the dn store in the user profile. Null if it can't find any or if it's empty.
     */
    public String getDn(XWikiDocument userDocument)
    {
        BaseObject ldapObject = userDocument.getXObject(this.ldapClass.getDocumentReference());

        return ldapObject == null ? null : getDn(ldapObject);
    }
{'fraction_non_alphanumeric': 0.08389261744966443, 'fraction_numerical': 0.005906040268456376, 'mean_word_length': 3.6164807930607186, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7256284', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2202, 'n_words': 665}