Package utils.acegi

Source Code of utils.acegi.UserInfo

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package utils.acegi;

import java.util.LinkedList;
import java.util.List;
import org.acegisecurity.Authentication;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.log4j.Logger;

/**
*
* @author axe
*/
public class UserInfo {

    protected static final Logger log = Logger.getLogger(UserInfo.class);
    public static final int CURRENT_COLLABORATOR_ID = 451;

    public String getLogin() {
        return "axe";
//        Authentication context = SecurityContextHolder.getContext().getAuthentication();
//        if(context == null || !context.isAuthenticated()) {
//            return "unregistered";
//        }
//        return context.getName();
    }

    public int getCurrentCollaboratorId() {
        //STUB
        return CURRENT_COLLABORATOR_ID;
        //ENDSTUB
    }
    /**
     * Attempts to authenticate a user that has provided the given username and password.
     * @param username current username
     * @param password current password
     * @return <code>true</code> if authentication succeeds, <code>false</code> otherwise
     */
    /*
    public boolean authenticate(String username, String password) {
        String u = username == null ? "" : username;
        String p = password == null ? "" : password;

        // Create an Acegi authentication request.
        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(u, p);


        // Attempt authentication.
        try {
            AuthenticationManager authenticationManager =
                SecurityContextHolder.getContext().getAuthentication();
            Authentication authResult = authenticationManager.authenticate(authRequest);
            setAuthentication(authResult);

            log.info("Login by user '" + username + "'.");
            return true;

        } catch (BadCredentialsException e) {
            log.info("Failed login by user '" + username + "'.");
            setAuthentication(null);
            return false;

        } catch (AuthenticationException e) {
            log.error("Could not authenticate a user", e);
            setAuthentication(null);
            throw e;

        } catch (RuntimeException e) {
            log.error("Unexpected exception while authenticating a user", e);
            setAuthentication(null);
            throw e;
        }
    }

     *
    /**
     * @return the currently logged in user, or null when no user is logged in
     */
    public AccountUserDetails getUser() {
        Authentication context = SecurityContextHolder.getContext().getAuthentication();
        if(context == null || !context.isAuthenticated()) {
            return null;
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return (AccountUserDetails) authentication.getPrincipal();
    }

    /**
     * Returns the current user roles.
     * @return current user roles
     */
    public List<String> getRoles() {
        Authentication context = SecurityContextHolder.getContext().getAuthentication();
        if(context == null || !context.isAuthenticated()) {
            return null;
        }

        List<String> roles = new LinkedList<String>();
        GrantedAuthority[] authorities = context.getAuthorities();
        for (int i = 0; i < authorities.length; i++) {
            GrantedAuthority authority = authorities[i];
            roles.add(authority.getAuthority());
        }
        return roles;
    }

    /**
     * Signout, invalidates the session. After a signout, you should redirect the browser to the home page.
     */
    public void logout() {
        AccountUserDetails user = getUser();
        if (user != null) {
            log.info("Logout by user '" + user.getUsername() + "'.");
        }
        setAuthentication(null);
    }

    /**
     * Sets the acegi authentication.
     * @param authentication the authentication or null to clear
     */
    private void setAuthentication(Authentication authentication) {
        SecurityContextHolder.getContext().setAuthentication(authentication);
    }
}
TOP

Related Classes of utils.acegi.UserInfo

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.