<commit_before>/*
 * Copyright 2014 satrapu.
 *
 * 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.
 */
package ro.satrapu.churchmanagement.ui;

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.enterprise.inject.Model;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import org.slf4j.Logger;
import ro.satrapu.churchmanagement.logging.LoggerInstance;
import ro.satrapu.churchmanagement.security.AuthenticatedUser;
import ro.satrapu.churchmanagement.security.AuthenticationDetails;
import ro.satrapu.churchmanagement.security.Authenticator;
import ro.satrapu.churchmanagement.security.CurrentUser;

/**
 * Handles user login/logout actions.
 *
 * @author satrapu
 */
@Model
public class LoginHome {

    @Inject
    @LoggerInstance
    Logger logger;

    @Inject
    Authenticator userAuthenticator;

    @Inject
    Messages messages;

    @Inject
    @FacesContextInstance
    FacesContext facesContext;

    @Inject
    CurrentUser currentUser;

    private AuthenticationDetails authenticationDetails;

    @PostConstruct
    public void init() {
        authenticationDetails = new AuthenticationDetails();
    }

    /**
     * Handles user login action.
     *
     * @return
     */
    public String login() {
        AuthenticatedUser authenticatedUser = null;
        boolean authenticationFailed = false;

        try {
            authenticatedUser = userAuthenticator.authenticate(authenticationDetails);
        } catch (Throwable e) {
            logger.warn("Unable to authenticate user", e);
            authenticationFailed = true;
        }

        if (!authenticationFailed && authenticatedUser != null) {
            currentUser.setAuthenticatedUser(authenticatedUser);
            messages.info("pages.login.actions.login.success");
            return Urls.Secured.HOME;
        } else {
            messages.error("pages.login.actions.login.failure");
            return null;
        }
    }

    /**
     * Handles user logout action.
     *
     * @return
     */<commit_msg>Removed unused import. Perform a redirect to the user home page after a successful authentication operation to ensure that the browser will display the correct URL.<commit_after>/*
 * Copyright 2014 satrapu.
 *
 * 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.
 */
package ro.satrapu.churchmanagement.ui;

import javax.annotation.PostConstruct;
import javax.enterprise.inject.Model;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import org.slf4j.Logger;
import ro.satrapu.churchmanagement.logging.LoggerInstance;
import ro.satrapu.churchmanagement.security.AuthenticatedUser;
import ro.satrapu.churchmanagement.security.AuthenticationDetails;
import ro.satrapu.churchmanagement.security.Authenticator;
import ro.satrapu.churchmanagement.security.CurrentUser;

/**
 * Handles user login/logout actions.
 *
 * @author satrapu
 */
@Model
public class LoginHome {

    @Inject
    @LoggerInstance
    Logger logger;

    @Inject
    Authenticator userAuthenticator;

    @Inject
    Messages messages;

    @Inject
    @FacesContextInstance
    FacesContext facesContext;

    @Inject
    CurrentUser currentUser;

    private AuthenticationDetails authenticationDetails;

    @PostConstruct
    public void init() {
        authenticationDetails = new AuthenticationDetails();
    }

    /**
     * Handles user login action.
     *
     * @return
     */
    public String login() {
        AuthenticatedUser authenticatedUser = null;
        boolean authenticationFailed = false;

        try {
            authenticatedUser = userAuthenticator.authenticate(authenticationDetails);
        } catch (Throwable e) {
            logger.warn("Unable to authenticate user", e);
            authenticationFailed = true;
        }

        if (!authenticationFailed && authenticatedUser != null) {
            currentUser.setAuthenticatedUser(authenticatedUser);
            messages.info("pages.login.actions.login.success");
            return Urls.addRedirectQueryStringParameter(Urls.Secured.HOME);
        } else {
            messages.error("pages.login.actions.login.failure");
            return null;
        }
    }

    /**
     * Handles user logout action.
     *
     * @return
     */
{'fraction_non_alphanumeric': 0.0821969696969697, 'fraction_numerical': 0.003409090909090909, 'mean_word_length': 3.071703932151118, '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': '826153', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1366, 'n_words': 397}