001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.security;
029
030import org.opencms.main.CmsIllegalArgumentException;
031import org.opencms.util.CmsStringUtil;
032
033import java.util.Locale;
034
035/**
036 * Default implementation for the validation handler.<p>
037 *
038 * @since 6.3.0
039 */
040public class CmsDefaultValidationHandler implements I_CmsValidationHandler {
041
042    /** The email regular expression. */
043    public static final String EMAIL_REGEX = "\\A[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9äöüÄÖÜ](?:[a-z0-9äöüÄÖÜ-]*[a-z0-9äöüÄÖÜ])?\\.)+[a-z0-9äöüÄÖÜ](?:[a-z0-9äöüÄÖÜ-]*[a-z0-9äöüÄÖÜ])?\\z";
044
045    /** The user name constraints. */
046    public static final String USERNAME_CONSTRAINTS = "-._~$@";
047
048    /** The zipcode regular expression. */
049    public static final String ZIPCODE_REGEX = "[\\w]*";
050
051    /**
052     * The email should only be composed by digits and standard english letters, points,
053     * underscores and exact one "At" symbol.<p>
054     *
055     * @see org.opencms.security.I_CmsValidationHandler#checkEmail(java.lang.String)
056     */
057    public void checkEmail(String email) throws CmsIllegalArgumentException {
058
059        if (CmsStringUtil.isNotEmpty(email)) {
060            email = email.trim();
061            email = email.toLowerCase(Locale.ROOT);
062        }
063        if (!CmsStringUtil.validateRegex(email, EMAIL_REGEX, false)) {
064            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_EMAIL_VALIDATION_1, email));
065        }
066    }
067
068    /**
069     * @see org.opencms.security.I_CmsValidationHandler#checkFirstname(java.lang.String)
070     */
071    public void checkFirstname(String firstname) throws CmsIllegalArgumentException {
072
073        if (CmsStringUtil.isEmptyOrWhitespaceOnly(firstname)) {
074            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_FIRSTNAME_EMPTY_0));
075        }
076    }
077
078    /**
079     * @see org.opencms.security.I_CmsValidationHandler#checkGroupName(java.lang.String)
080     */
081    public void checkGroupName(String name) throws CmsIllegalArgumentException {
082
083        if (CmsStringUtil.isEmptyOrWhitespaceOnly(name)) {
084            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_BAD_GROUPNAME_EMPTY_0));
085        }
086    }
087
088    /**
089     * @see org.opencms.security.I_CmsValidationHandler#checkLastname(java.lang.String)
090     */
091    public void checkLastname(String lastname) throws CmsIllegalArgumentException {
092
093        if (CmsStringUtil.isEmptyOrWhitespaceOnly(lastname)) {
094            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_LASTNAME_EMPTY_0));
095        }
096    }
097
098    /**
099     * A user name can only be composed of digits,
100     * standard ASCII letters and the symbols defined in {@link #USERNAME_CONSTRAINTS}.<p>
101     *
102     * @see org.opencms.security.I_CmsValidationHandler#checkUserName(java.lang.String)
103     */
104    public void checkUserName(String userName) throws CmsIllegalArgumentException {
105
106        if (CmsStringUtil.isEmptyOrWhitespaceOnly(userName)) {
107            throw new CmsIllegalArgumentException(
108                Messages.get().container(Messages.ERR_BAD_USERNAME_EMPTY_0, userName));
109        }
110
111        CmsStringUtil.checkName(userName, USERNAME_CONSTRAINTS, Messages.ERR_BAD_USERNAME_4, Messages.get());
112    }
113
114    /**
115     * That means, the parameter should only be composed by digits and standard english letters.<p>
116     *
117     * @see org.opencms.security.I_CmsValidationHandler#checkZipCode(java.lang.String)
118     */
119    public void checkZipCode(String zipcode) throws CmsIllegalArgumentException {
120
121        if (!CmsStringUtil.validateRegex(zipcode, ZIPCODE_REGEX, true)) {
122            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_ZIPCODE_VALIDATION_1, zipcode));
123        }
124    }
125
126}