001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH (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, 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
033/**
034 * Default implementation for the validation handler.<p>
035 *
036 * @since 6.3.0
037 */
038public class CmsDefaultValidationHandler implements I_CmsValidationHandler {
039
040    /** The email regular expression. */
041    public static final String EMAIL_REGEX = "^([a-zA-Z0-9_\\.\\-'])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$";
042
043    /** The user name constraints. */
044    public static final String USERNAME_CONSTRAINTS = "-._~$@";
045
046    /** The zipcode regular expression. */
047    public static final String ZIPCODE_REGEX = "[\\w]*";
048
049    /**
050     * The email should only be composed by digits and standard english letters, points,
051     * underscores and exact one "At" symbol.<p>
052     *
053     * @see org.opencms.security.I_CmsValidationHandler#checkEmail(java.lang.String)
054     */
055    public void checkEmail(String email) throws CmsIllegalArgumentException {
056
057        if (CmsStringUtil.isNotEmpty(email)) {
058            email = email.trim();
059        }
060        if (!CmsStringUtil.validateRegex(email, EMAIL_REGEX, false)) {
061            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_EMAIL_VALIDATION_1, email));
062        }
063    }
064
065    /**
066     * @see org.opencms.security.I_CmsValidationHandler#checkFirstname(java.lang.String)
067     */
068    public void checkFirstname(String firstname) throws CmsIllegalArgumentException {
069
070        if (CmsStringUtil.isEmptyOrWhitespaceOnly(firstname)) {
071            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_FIRSTNAME_EMPTY_0));
072        }
073    }
074
075    /**
076     * @see org.opencms.security.I_CmsValidationHandler#checkGroupName(java.lang.String)
077     */
078    public void checkGroupName(String name) throws CmsIllegalArgumentException {
079
080        if (CmsStringUtil.isEmptyOrWhitespaceOnly(name)) {
081            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_BAD_GROUPNAME_EMPTY_0));
082        }
083    }
084
085    /**
086     * @see org.opencms.security.I_CmsValidationHandler#checkLastname(java.lang.String)
087     */
088    public void checkLastname(String lastname) throws CmsIllegalArgumentException {
089
090        if (CmsStringUtil.isEmptyOrWhitespaceOnly(lastname)) {
091            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_LASTNAME_EMPTY_0));
092        }
093    }
094
095    /**
096     * A user name can only be composed of digits,
097     * standard ASCII letters and the symbols defined in {@link #USERNAME_CONSTRAINTS}.<p>
098     *
099     * @see org.opencms.security.I_CmsValidationHandler#checkUserName(java.lang.String)
100     */
101    public void checkUserName(String userName) throws CmsIllegalArgumentException {
102
103        if (CmsStringUtil.isEmptyOrWhitespaceOnly(userName)) {
104            throw new CmsIllegalArgumentException(
105                Messages.get().container(Messages.ERR_BAD_USERNAME_EMPTY_0, userName));
106        }
107
108        CmsStringUtil.checkName(userName, USERNAME_CONSTRAINTS, Messages.ERR_BAD_USERNAME_4, Messages.get());
109    }
110
111    /**
112     * That means, the parameter should only be composed by digits and standard english letters.<p>
113     *
114     * @see org.opencms.security.I_CmsValidationHandler#checkZipCode(java.lang.String)
115     */
116    public void checkZipCode(String zipcode) throws CmsIllegalArgumentException {
117
118        if (!CmsStringUtil.validateRegex(zipcode, ZIPCODE_REGEX, true)) {
119            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_ZIPCODE_VALIDATION_1, zipcode));
120        }
121    }
122
123}