Package org.internna.iwebmvc.core.security

Source Code of org.internna.iwebmvc.core.security.SessionUser

/*
* Copyright 2002-2007 the original author or authors.
*
* 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 org.internna.iwebmvc.core.security;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.internna.iwebmvc.core.dao.SecurityDAO;
import org.internna.iwebmvc.model.security.GuestUser;
import org.internna.iwebmvc.model.User;
import org.internna.iwebmvc.model.security.UserImpl;
import org.internna.iwebmvc.spring.util.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.web.util.WebUtils;

/**
* Gets the active {@link org.internna.iwebmvc.model.User} from the session.
*
* @author Jose Noheda
* @since 1.0
*/
@Component
@Scope("session")
public class SessionUser implements UserManager {

    protected static final Log log = LogFactory.getLog(SessionUser.class);

    @Autowired protected SecurityDAO securityDAO;

    /**
     * Get the {@link org.internna.iwebmvc.model.User} of the current request.
     *
     * @return the user of the active request.
     */
    public User getActiveUser() {
        HttpServletRequest request = RequestUtils.getActiveRequest();
        User user = (User) WebUtils.getSessionAttribute(request, SESSION_USER);
        if (user instanceof UserImpl) return user;
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null) {
            if (auth.getPrincipal() instanceof UserDetails) {
                user = securityDAO.findUser(((UserDetails) auth.getPrincipal()).getUsername());
                WebUtils.setSessionAttribute(request, SESSION_USER, user);
                return user;
            }
        }
        if (user == null) {
            user = new GuestUser();
            WebUtils.setSessionAttribute(request, SESSION_USER, user);
        }
        return user;
    }

}
TOP

Related Classes of org.internna.iwebmvc.core.security.SessionUser

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.