<commit_before>/*
 * Copyright 2003 - 2010 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Revision:        $Rev$
 * Last Changed:    $Date$
 * Last Changed By: $Author$
 */

package org.efaps.esjp.admin.user;

import java.util.UUID;

import javax.security.auth.login.LoginException;

import org.efaps.admin.common.SystemConfiguration;
import org.efaps.admin.datamodel.ui.FieldValue;
import org.efaps.admin.event.Parameter;
import org.efaps.admin.event.Parameter.ParameterValues;
import org.efaps.admin.event.Return;
import org.efaps.admin.event.Return.ReturnValues;
import org.efaps.admin.program.esjp.EFapsRevision;
import org.efaps.admin.program.esjp.EFapsUUID;
import org.efaps.admin.ui.AbstractUserInterfaceObject.TargetMode;
import org.efaps.admin.user.Role;
import org.efaps.db.Context;
import org.efaps.db.Instance;
import org.efaps.db.Update.Status;
import org.efaps.jaas.AppAccessHandler;
import org.efaps.jaas.SetPasswordHandler;
import org.efaps.jaas.efaps.UserLoginModule.UpdateException;
import org.efaps.util.EFapsException;

/**
 * Esjp used to change th epassword for a user.
 *
 * @author The eFaps Team
 * @version $Id$
 */
@EFapsUUID("ff1b1140-3da0-491f-8bf4-c42f71ea4343")
@EFapsRevision("$Rev$")
public class Password
{

    /**
     * Key used for the field for the old password.
     */
    private static String PWDOLD = "passwordold";

    /**
     * Key used for the field for the new password.
     */
    private static String PWDNEW = "<PASSWORD>";

    /**
     * Key used for the field for the new password repetition.
     */
    private static String PWDNEWREPEAT = "<PASSWORD>";

    /**
     * Method is called to change the password of a user in the efpas database.
     *
     * @param _parameter Parameter as passed from eFaps to esjp
     * @return Return
     * @throws EFapsException on error
     */
    public Return changePwdUI(final Parameter _parameter) throws EFapsException
    {

        final Context context = Context.getThreadContext();
        final String passwordold = context.getParameter(Password.PWDOLD);
        final String passwordnew = context.getParameter(Password.PWDNEW);
        final Return ret = new Return();

        final SetPasswordHandler handler = new SetPasswordHandler(AppAccessHandler.getApplicationKey());
        try {
            if (handler.setPassword(context.getPerson().getName(), passwordnew, passwordold)) {
                ret.put(ReturnValues.TRUE, "true");
            }
        } catch (final LoginException e) {
            if (e instanceof UpdateException) {
                ret.put(ReturnValues.VALUES, "Admin_User_PwdChgForm/Password.validatePwdValue.ShortPwd");
            } else {
                ret.put(ReturnValues.VALUES, "Admin_User_PwdChgForm/Password.changePwdUI.checkPassword");
            }
        }
        return ret;
    }

    /**<commit_msg>- kernel-install: update of set password functionalities<commit_after>/*
 * Copyright 2003 - 2010 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Revision:        $Rev$
 * Last Changed:    $Date$
 * Last Changed By: $Author$
 */

package org.efaps.esjp.admin.user;

import java.util.UUID;

import javax.security.auth.login.LoginException;

import org.efaps.admin.common.SystemConfiguration;
import org.efaps.admin.datamodel.ui.FieldValue;
import org.efaps.admin.event.Parameter;
import org.efaps.admin.event.Parameter.ParameterValues;
import org.efaps.admin.event.Return;
import org.efaps.admin.event.Return.ReturnValues;
import org.efaps.admin.program.esjp.EFapsRevision;
import org.efaps.admin.program.esjp.EFapsUUID;
import org.efaps.admin.ui.AbstractUserInterfaceObject.TargetMode;
import org.efaps.admin.user.Role;
import org.efaps.db.Context;
import org.efaps.db.Instance;
import org.efaps.db.Update.Status;
import org.efaps.jaas.AppAccessHandler;
import org.efaps.jaas.SetPasswordHandler;
import org.efaps.jaas.efaps.UserLoginModule.UpdateException;
import org.efaps.util.EFapsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Esjp used to change th epassword for a user.
 *
 * @author The eFaps Team
 * @version $Id$
 */
@EFapsUUID("ff1b1140-3da0-491f-8bf4-c42f71ea4343")
@EFapsRevision("$Rev$")
public class Password
{
    /**
     * Logger for this class.
     */
    protected static final Logger LOG = LoggerFactory.getLogger(Password.class);

    /**
     * Key used for the field for the old password.
     */
    private static String PWDOLD = "<PASSWORD>";

    /**
     * Key used for the field for the new password.
     */
    private static String PWDNEW = "<PASSWORD>";

    /**
     * Key used for the field for the new password repetition.
     */
    private static String PWDNEWREPEAT = "<PASSWORD>";

    /**
     * Method is called to change the password of a user in the efpas database.
     *
     * @param _parameter Parameter as passed from eFaps to esjp
     * @return Return
     * @throws EFapsException on error
     */
    public Return changePwdUI(final Parameter _parameter) throws EFapsException
    {

        final Context context = Context.getThreadContext();
        final String passwordold = context.getParameter(Password.PWDOLD);
        final String passwordnew = context.getParameter(Password.PWDNEW);
        final Return ret = new Return();

        final SetPasswordHandler handler = new SetPasswordHandler(AppAccessHandler.getApplicationKey());
        Password.LOG.debug("Applying SetPasswordHandler with ApplicationKey {}", AppAccessHandler.getApplicationKey());
        try {
            if (handler.setPassword(context.getPerson().getName(), passwordnew, passwordold)) {
                ret.put(ReturnValues.TRUE, "true");
            }
        } catch (final LoginException e) {
            if (e instanceof UpdateException) {
                ret.put(ReturnValues.VALUES, "Admin_User_PwdChgForm/Password.validatePwdValue.ShortPwd");
            } else {
                ret.put(ReturnValues.VALUES, "Admin_User_PwdChgForm/Password.changePwdUI.checkPassword");
            }
        }
        return ret;
    }

    /**
{'fraction_non_alphanumeric': 0.09876369766788423, 'fraction_numerical': 0.009272267490868222, 'mean_word_length': 3.219917012448133, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '3137662', 'n_tokens_mistral': 2124, 'n_tokens_neox': 2050, 'n_words': 579}