001    /* Copyright 2006-2009 the original author or authors.
002     *
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *      http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software
010     * distributed under the License is distributed on an "AS IS" BASIS,
011     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     * See the License for the specific language governing permissions and
013     * limitations under the License.
014     */
015    package org.codehaus.groovy.grails.plugins.springsecurity;
016    
017    import org.springframework.security.GrantedAuthority;
018    import org.springframework.security.providers.dao.DaoAuthenticationProvider;
019    import org.springframework.security.userdetails.User;
020    
021    /**
022     * Extends Spring Security's {@link User} class to set Grails Domain Class at login,
023     * to load auth class from context.
024     *
025     * @author T.Yamamoto
026     * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a>
027     */
028    public class GrailsUserImpl extends User implements GrailsUser {
029    
030            private static final long serialVersionUID = 6089520028447407158L;
031    
032            private final Object domainClass;
033    
034            /**
035             * Constructor.
036             * @param username the username presented to the
037             *        {@link DaoAuthenticationProvider}
038             * @param password the password that should be presented to the
039             *        {@link DaoAuthenticationProvider}
040             * @param enabled set to <code>true</code> if the user is enabled
041             * @param accountNonExpired set to <code>true</code> if the account has not
042             *        expired
043             * @param credentialsNonExpired set to <code>true</code> if the credentials
044             *        have not expired
045             * @param accountNonLocked set to <code>true</code> if the account is not
046             *        locked
047             * @param authorities the authorities that should be granted to the caller
048             *        if they presented the correct username and password and the user
049             *        is enabled
050             * @param user  the user domain instance
051             *
052             * @throws IllegalArgumentException if a <code>null</code> value was passed
053             *         either as a parameter or as an element in the
054             *         {@link GrantedAuthority}[] array
055             */
056            public GrailsUserImpl(
057                            final String username, final String password, final boolean enabled,
058                            final boolean accountNonExpired, final boolean credentialsNonExpired,
059                            final boolean accountNonLocked, final GrantedAuthority[] authorities,
060                            final Object user) throws IllegalArgumentException {
061                    super(username, password, enabled, accountNonExpired,
062                                    credentialsNonExpired, accountNonLocked, authorities);
063                    domainClass = user;
064            }
065    
066            /**
067             * {@inheritDoc}
068             * @see org.codehaus.groovy.grails.plugins.springsecurity.GrailsUser#getDomainClass()
069             */
070            public Object getDomainClass() {
071                    return domainClass;
072            }
073    }