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.ldap;
016    
017    import javax.naming.directory.Attributes;
018    
019    import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUser;
020    import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUserImpl;
021    import org.springframework.security.GrantedAuthority;
022    import org.springframework.security.userdetails.ldap.LdapUserDetails;
023    
024    /**
025     * A {@link GrailsUser} for use in LDAP authentication.
026     *
027     * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a>
028     */
029    public class GrailsLdapUser extends GrailsUserImpl implements GrailsUser, LdapUserDetails {
030    
031            private static final long serialVersionUID = -1557817722745366207L;
032    
033            private final Attributes _attributes;
034            private final String _dn;
035    
036            /**
037             * Constructor from {@link LdapUserDetails}.
038             * @param details  the original details
039             * @param domainClass  the domain instance
040             */
041            @SuppressWarnings("deprecation") // just passing along the core impl
042            public GrailsLdapUser(final LdapUserDetails details, final Object domainClass) {
043                    super(details.getUsername(), details.getPassword(), details.isEnabled(),
044                                    details.isAccountNonExpired(), details.isCredentialsNonExpired(),
045                                    details.isAccountNonLocked(), details.getAuthorities(), domainClass);
046                    _attributes = details.getAttributes();
047                    _dn = details.getDn();
048            }
049    
050            /**
051             * Full constructor.
052             * @param username  the username
053             * @param password  the password
054             * @param enabled  whether the user is enabled
055             * @param accountNonExpired  whether the user's account is expired
056             * @param credentialsNonExpired  whether the user's credentials are locked
057             * @param accountNonLocked  whether the user's account is locked
058             * @param authorities  authorities
059             * @param attributes  attributes
060             * @param dn  distinguished name
061             * @param domainClass  the domain instance
062             */
063            public GrailsLdapUser(final String username, final String password, final boolean enabled,
064                            final boolean accountNonExpired, final boolean credentialsNonExpired,
065                            final boolean accountNonLocked, final GrantedAuthority[] authorities,
066                            final Attributes attributes, final String dn, final Object domainClass) {
067                    super(username, password, enabled, accountNonExpired, credentialsNonExpired,
068                                    accountNonLocked, authorities, domainClass);
069                    _attributes = attributes;
070                    _dn = dn;
071            }
072    
073            /**
074             * {@inheritDoc}
075             */
076            public Attributes getAttributes() {
077                    return _attributes;
078            }
079    
080            /**
081             * {@inheritDoc}
082             * @see org.springframework.security.userdetails.ldap.LdapUserDetails#getDn()
083             */
084            public String getDn() {
085                    return _dn;
086            }
087    }