001    /*
002     * Copyright (c) 2002-2005 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.security;
017    
018    import org.springframework.rules.constraint.Constraint;
019    import org.springframework.richclient.application.ApplicationServicesLocator;
020    import org.springframework.rules.PropertyConstraintProvider;
021    import org.springframework.rules.Rules;
022    import org.springframework.rules.constraint.property.PropertyConstraint;
023    import org.springframework.security.Authentication;
024    
025    /**
026     * This class provides a bean suitable for use in a login form, providing properties for
027     * storing the user name and password.
028     * <p>
029     * This bean provides basic constraints for the username and password properties. Each is
030     * required to be at least 2 characters long. If you need more specific constraints, then
031     * you should implement a subtype and override the initRules method.
032     * 
033     * @author Larry Streepy
034     * @author Ben Alex
035     * 
036     */
037    public class LoginDetails implements PropertyConstraintProvider {
038    
039        public static final String PROPERTY_USERNAME = "username";
040    
041        public static final String PROPERTY_PASSWORD = "password";
042    
043        private String username;
044    
045        private String password;
046    
047        private Rules validationRules;
048    
049        /**
050         * Constructor. Pre-load our username field with the data currently stored in the
051         * security context, if any.
052         */
053        public LoginDetails() {
054            // Retrieve any existing login information and install it
055            ApplicationSecurityManager sm = (ApplicationSecurityManager)ApplicationServicesLocator.services().getService(ApplicationSecurityManager.class);
056            Authentication authentication = sm.getAuthentication();
057            if( authentication != null ) {
058                setUsername( authentication.getName() );
059            }
060            initRules();
061        }
062    
063        /**
064         * Initialize the field constraints for our properties. Minimal constraints are
065         * enforced here. If you need more control, you should override this in a subtype.
066         */
067        protected void initRules() {
068            this.validationRules = new Rules( getClass() ) {
069                protected void initRules() {
070                    add( PROPERTY_USERNAME, all( new Constraint[] { required(), minLength( getUsernameMinLength() ) } ) );
071                    add( PROPERTY_PASSWORD, all( new Constraint[] { required(), minLength( getPasswordMinLength() ) } ) );
072                }
073    
074                protected int getUsernameMinLength() {
075                    return 2;
076                }
077    
078                protected int getPasswordMinLength() {
079                    return 2;
080                }
081    
082            };
083        }
084    
085        public String getUsername() {
086            return username;
087        }
088    
089        public void setUsername(String username) {
090            this.username = username;
091        }
092    
093        public String getPassword() {
094            return password;
095        }
096    
097        public void setPassword(String password) {
098            this.password = password;
099        }
100    
101        /**
102         * Return the property constraints.
103         * @see org.springframework.rules.PropertyConstraintProvider#getPropertyConstraint(java.lang.String)
104         */
105        public PropertyConstraint getPropertyConstraint(String propertyName) {
106            return validationRules.getPropertyConstraint( propertyName );
107        }
108    
109    }