<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.api;

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.DocumentReferenceResolver;
import org.xwiki.model.reference.EntityReferenceSerializer;

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.web.Utils;

public class XWikiUser
{
    /**
     * Class used to store user properties.
     */
    private static final String USER_CLASS = "XWiki.XWikiUsers";

    /**
     * The name of the property that store the active status of the user.
     */
    private static final String ACTIVE_PROPERTY = "active";

    /**
     * The name of the property that store the disabled status of the user.
     */
    private static final String DISABLED_PROPERTY = "disabled";

    /**
     * @see com.xpn.xwiki.internal.model.reference.CurrentMixedStringDocumentReferenceResolver
     */
    private DocumentReferenceResolver<String> currentMixedDocumentReferenceResolver = Utils.getComponent(
        DocumentReferenceResolver.TYPE_STRING, "currentmixed");

    private EntityReferenceSerializer<String> localEntityReferenceSerializer =
        Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "local");

    private Logger logger = LoggerFactory.getLogger(XWikiUser.class);

    private String fullName;

    private DocumentReference userReference;

    private boolean main;

    /**
     * Create a XWikiUser from its document reference and infer if the user is global or not based on the wiki part of
     * this reference. See {@link #isMain()} for more information.
     * @param userReference the document reference of the user.
     * @since 11.6RC1
     */
    public XWikiUser(DocumentReference userReference)
    {
        this(userReference, XWiki.DEFAULT_MAIN_WIKI.equals(userReference.getWikiReference().getName()));
    }

    /**
     * Create a XWikiUser from its document reference and set the main flag. (see {@link #isMain()}).
     * @param userReference the document reference of the user.
     * @param main true if the user is global (i.e. registered in the main wiki)
     * @since 11.6RC1
     */
    public XWikiUser(DocumentReference userReference, boolean main)
    {
        this.userReference = userReference;
        setUser(localEntityReferenceSerializer.serialize(userReference));
        setMain(main);
    }

    /**
     * Create a XWikiUser for the given user.
     * @param user the full name of the user on the form <tt>XWiki.Foo</tt>.
     * @deprecated since 11.6RC1 use {@link #XWikiUser(DocumentReference)}.
     */
    @Deprecated
    public XWikiUser(String user)
    {
        this(user, false);
    }

    /**
     * Create a XWikiUser for the given user.
     * @param user the full name of the user on the form <tt>XWiki.Foo</tt>.
     * @param main true if the user is global (i.e. registered in the main wiki)
     * @deprecated since 11.6RC1 use {@link #XWikiUser(DocumentReference, boolean)}.
     */
    @Deprecated
    public XWikiUser(String user, boolean main)
    {
        setUser(user);
        setMain(main);
    }

    /**
     *
     * @return user fullname
     * @deprecated since 11.6RC1 use {@link #getFullName()}.
     */
    @Deprecated
    public String getUser()
    {
        return this.fullName;
    }

    /**
     * @return the fullname of the user like <tt>XWiki.Foo</tt>.
     */
    public String getFullName()
    {
        return this.fullName;
    }

    public DocumentReference getUserReference()
    {
        if (this.userReference == null) {
            this.userReference = this.currentMixedDocumentReferenceResolver.resolve(getUser());
        }
        return this.userReference;
    }

    private DocumentReference getUserClassReference()
    {
        return this.currentMixedDocumentReferenceResolver.resolve(USER_CLASS);
    }

    private boolean isGuest()
    {
        return XWikiRightService.isGuest(getUserReference());
    }

    private boolean isSuperAdmin()
    {
        return XWikiRightService.isSuperAdmin(getUserReference());
    }

    private XWikiDocument getUserDocument(XWikiContext context) throws XWikiException
    {
        return context.getWiki().getDocument(getUserReference(), context);
    }

    /**
     * @param context used to retrieve the user document.
     * @return true if the user is disabled. This always returns false if the user is the guest or superadmin user.
     * @since 11.6RC1
     */
    public boolean isDisabled(XWikiContext context)<commit_msg>XWIKI-12654: Add UI to activate/deactivate users<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.api;

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.DocumentReferenceResolver;
import org.xwiki.model.reference.EntityReferenceSerializer;

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.web.Utils;

public class XWikiUser
{
    /**
     * Class used to store user properties.
     */
    private static final String USER_CLASS = "XWiki.XWikiUsers";

    /**
     * The name of the property that store the active status of the user.
     */
    private static final String ACTIVE_PROPERTY = "active";

    /**
     * The name of the property that store the disabled status of the user.
     */
    private static final String DISABLED_PROPERTY = "disabled";

    /**
     * @see com.xpn.xwiki.internal.model.reference.CurrentMixedStringDocumentReferenceResolver
     */
    private DocumentReferenceResolver<String> currentMixedDocumentReferenceResolver;

    private EntityReferenceSerializer<String> localEntityReferenceSerializer;

    private Logger logger = LoggerFactory.getLogger(XWikiUser.class);

    private String fullName;

    private DocumentReference userReference;

    private boolean main;

    /**
     * Create a XWikiUser from its document reference and infer if the user is global or not based on the wiki part of
     * this reference. See {@link #isMain()} for more information.
     * @param userReference the document reference of the user.
     * @since 11.6RC1
     */
    public XWikiUser(DocumentReference userReference)
    {
        this(userReference, XWiki.DEFAULT_MAIN_WIKI.equals(userReference.getWikiReference().getName()));
    }

    /**
     * Create a XWikiUser from its document reference and set the main flag. (see {@link #isMain()}).
     * @param userReference the document reference of the user.
     * @param main true if the user is global (i.e. registered in the main wiki)
     * @since 11.6RC1
     */
    public XWikiUser(DocumentReference userReference, boolean main)
    {
        this.userReference = userReference;
        setUser(getLocalEntityReferenceSerializer().serialize(userReference));
        setMain(main);
    }

    /**
     * Create a XWikiUser for the given user.
     * @param user the full name of the user on the form <tt>XWiki.Foo</tt>.
     * @deprecated since 11.6RC1 use {@link #XWikiUser(DocumentReference)}.
     */
    @Deprecated
    public XWikiUser(String user)
    {
        this(user, false);
    }

    /**
     * Create a XWikiUser for the given user.
     * @param user the full name of the user on the form <tt>XWiki.Foo</tt>.
     * @param main true if the user is global (i.e. registered in the main wiki)
     * @deprecated since 11.6RC1 use {@link #XWikiUser(DocumentReference, boolean)}.
     */
    @Deprecated
    public XWikiUser(String user, boolean main)
    {
        setUser(user);
        setMain(main);
    }

    /**
     *
     * @return user fullname
     * @deprecated since 11.6RC1 use {@link #getFullName()}.
     */
    @Deprecated
    public String getUser()
    {
        return this.fullName;
    }

    /**
     * @return the fullname of the user like <tt>XWiki.Foo</tt>.
     */
    public String getFullName()
    {
        return this.fullName;
    }

    public DocumentReferenceResolver<String> getCurrentMixedDocumentReferenceResolver()
    {
        if (currentMixedDocumentReferenceResolver == null) {
            currentMixedDocumentReferenceResolver =
                Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed");
        }
        return currentMixedDocumentReferenceResolver;
    }

    public EntityReferenceSerializer<String> getLocalEntityReferenceSerializer()
    {
        if (localEntityReferenceSerializer == null) {
            localEntityReferenceSerializer = Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "local");
        }
        return localEntityReferenceSerializer;
    }

    public DocumentReference getUserReference()
    {
        if (this.userReference == null) {
            this.userReference = getCurrentMixedDocumentReferenceResolver().resolve(getUser());
        }
        return this.userReference;
    }

    private DocumentReference getUserClassReference()
    {
        return getCurrentMixedDocumentReferenceResolver().resolve(USER_CLASS);
    }

    private boolean isGuest()
    {
        return XWikiRightService.isGuest(getUserReference());
    }

    private boolean isSuperAdmin()
    {
        return XWikiRightService.isSuperAdmin(getUserReference());
    }

    private XWikiDocument getUserDocument(XWikiContext context) throws XWikiException
    {
        return context.getWiki().getDocument(getUserReference(), context);
    }

    /**
     * @param context used to retrieve the user document.
     * @return true if the user is disabled. This always returns false if the user is the guest or superadmin user.
     * @since 11.6RC1
     */
    public boolean isDisabled(XWikiContext context)
{'fraction_non_alphanumeric': 0.08448671138660101, 'fraction_numerical': 0.007217457756644306, 'mean_word_length': 3.1109947643979057, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '2566953', 'n_tokens_mistral': 3303, 'n_tokens_neox': 3197, 'n_words': 1039}