|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| LoginVerifier.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 1 | // | |
| 2 | // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. | |
| 3 | // All rights reserved. | |
| 4 | // | |
| 5 | // Redistribution and use in source and binary forms, with or without modification, | |
| 6 | // are permitted provided that the following conditions are met: | |
| 7 | // | |
| 8 | // * Redistributions of source code must retain the above copyright notice, | |
| 9 | // this list of conditions and the following disclaimer. | |
| 10 | // * Redistributions in binary form must reproduce the above copyright notice, | |
| 11 | // this list of conditions and the following disclaimer in the documentation | |
| 12 | // and/or other materials provided with the distribution. | |
| 13 | // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. | |
| 14 | // nor the names of its contributors may be used to endorse or promote products | |
| 15 | // derived from this software without specific prior written permission. | |
| 16 | // | |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 19 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
| 20 | // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 21 | // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 22 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | |
| 23 | // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 24 | // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | // POSSIBILITY OF SUCH DAMAGE. | |
| 27 | // | |
| 28 | package org.objectledge.authentication; | |
| 29 | ||
| 30 | import java.util.HashSet; | |
| 31 | import java.util.Set; | |
| 32 | import java.util.StringTokenizer; | |
| 33 | import java.util.regex.Matcher; | |
| 34 | import java.util.regex.Pattern; | |
| 35 | ||
| 36 | import org.jcontainer.dna.Configuration; | |
| 37 | ||
| 38 | /** | |
| 39 | * Verifies a login name against a set of reserved ones and pattern. | |
| 40 | * | |
| 41 | * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a> | |
| 42 | * @version $Id: LoginVerifier.java,v 1.4 2006/02/08 18:19:37 zwierzem Exp $ | |
| 43 | */ | |
| 44 | public class LoginVerifier | |
| 45 | { | |
| 46 | /** The regexp login pattern. */ | |
| 47 | public static final String LOGIN_PATTERN = "[-a-zA-Z0-9]+"; | |
| 48 | ||
| 49 | /** the reserved logins set */ | |
| 50 | private Set<String> reserved; | |
| 51 | ||
| 52 | /** the login pattern */ | |
| 53 | private Pattern loginPattern; | |
| 54 | ||
| 55 | /** | |
| 56 | * Component constructor. | |
| 57 | * | |
| 58 | * @param config the configuration. | |
| 59 | */ | |
| 60 | 782 | public LoginVerifier(Configuration config) |
| 61 | { | |
| 62 | 782 | reserved = new HashSet<String>(); |
| 63 | 782 | String list = config.getChild("reserved").getValue(""); |
| 64 | 782 | StringTokenizer st = new StringTokenizer(list); |
| 65 | 782 | while(st.hasMoreTokens()) |
| 66 | { | |
| 67 | 1564 | String login = st.nextToken(); |
| 68 | 1564 | reserved.add(login); |
| 69 | } | |
| 70 | 782 | String pattern = config.getChild("loginPattern") |
| 71 | .getValue(LOGIN_PATTERN); | |
| 72 | 782 | loginPattern = Pattern.compile(pattern); |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * Checks if a login name is a non-reserved one. | |
| 77 | * | |
| 78 | * @param login the login name to be checked. | |
| 79 | * @return <code>true</code> if a login name is a non-occupied and non-reserved. | |
| 80 | */ | |
| 81 | 598 | public boolean checkLogin(String login) |
| 82 | { | |
| 83 | 598 | return !reserved.contains(login); |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Verify login. | |
| 88 | * | |
| 89 | * @param login the login. | |
| 90 | * @return <code>true</code> if login is valid. | |
| 91 | */ | |
| 92 | 92 | public boolean validate(String login) |
| 93 | { | |
| 94 | 92 | Matcher m = loginPattern.matcher(login); |
| 95 | 92 | return m.matches(); |
| 96 | } | |
| 97 | } |
|
||||||||||