001/*
002 *  Copyright 2021 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of 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,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.newsletter.subscribe;
017
018import java.util.Date;
019import java.util.Set;
020import java.util.UUID;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.thread.ThreadSafe;
025import org.apache.cocoon.acting.ServiceableAction;
026import org.apache.cocoon.environment.Request;
027import org.apache.commons.lang.StringUtils;
028
029import org.ametys.core.captcha.CaptchaHelper;
030import org.ametys.core.util.mail.SendMailHelper;
031import org.ametys.plugins.newsletter.category.Category;
032import org.ametys.plugins.newsletter.category.CategoryProvider;
033import org.ametys.plugins.newsletter.category.CategoryProviderExtensionPoint;
034import org.ametys.plugins.newsletter.daos.Subscriber;
035import org.ametys.plugins.newsletter.daos.SubscribersDAO;
036import org.ametys.plugins.repository.AmetysObjectResolver;
037import org.ametys.web.cache.PageHelper;
038import org.ametys.web.repository.page.SitemapElement;
039import org.ametys.web.site.SiteConfigurationExtensionPoint;
040
041/**
042 * Abstract action for subscription
043 *
044 */
045public abstract class AbstractSubscribeAction extends ServiceableAction implements ThreadSafe
046{
047    /** The subscribers DAO */
048    protected SubscribersDAO _subscribersDao;
049    /** The category providers manager */
050    protected CategoryProviderExtensionPoint _categoryProviderEP;
051    /** The site configuration */
052    protected SiteConfigurationExtensionPoint _siteConfiguration;
053    /** The Ametys object resolver */
054    protected AmetysObjectResolver _resolver;
055    /** Page helper */
056    protected PageHelper _pageHelper;
057    
058    @Override
059    public void service(ServiceManager smanager) throws ServiceException
060    {
061        super.service(smanager);
062        _subscribersDao = (SubscribersDAO) smanager.lookup(SubscribersDAO.ROLE);
063        _categoryProviderEP = (CategoryProviderExtensionPoint) smanager.lookup(CategoryProviderExtensionPoint.ROLE);
064        _siteConfiguration = (SiteConfigurationExtensionPoint) smanager.lookup(SiteConfigurationExtensionPoint.ROLE);
065        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
066        _pageHelper = (PageHelper) smanager.lookup(PageHelper.ROLE);
067    }
068    
069    /**
070     * Determines if the email address is valid
071     * @param email The email address
072     * @return true if the email is valid
073     */
074    protected boolean _validEmail (String email)
075    {
076        return !StringUtils.isBlank(email) 
077                && SendMailHelper.EMAIL_VALIDATION.matcher(email).matches();
078    }
079
080    /**
081     * Determines if the captcha is valid
082     * @param request The request
083     * @param sitemapElement the page holding the service
084     * @return true if the captcha is valid
085     */
086    protected boolean _validCaptcha (Request request, SitemapElement sitemapElement)
087    {
088        if (_pageHelper.isCaptchaRequired(sitemapElement))
089        {
090            String captchaKey = request.getParameter("captcha-key");
091            String answer = request.getParameter("captcha");
092
093            // Validate captcha 
094            if (!CaptchaHelper.checkAndInvalidate(captchaKey, answer))
095            {
096                return false;
097            }
098        }
099        return true;
100    }
101
102    /**
103     * Get the category
104     * @param categoryID The category id
105     * @return the category
106     */
107    protected Category _getCategory (String categoryID)
108    {
109        Set<String> ids = _categoryProviderEP.getExtensionsIds();
110        for (String id : ids)
111        {
112            CategoryProvider provider = _categoryProviderEP.getExtension(id);
113            if (provider.hasCategory(categoryID))
114            {
115                return provider.getCategory(categoryID);
116            }
117        }
118        
119        return null;
120    }
121    
122    /**
123     * Get the category
124     * @param email the subscriber email
125     * @param siteName the site name
126     * @param categoryID The category id
127     * @return the category
128     */
129    protected Subscriber _createSubscritpion (String email, String siteName, String categoryID)
130    {
131        Subscriber subscriber = new Subscriber();
132        subscriber.setEmail(email);
133        subscriber.setSiteName(siteName);
134        subscriber.setCategoryId(categoryID);
135        subscriber.setSubscribedAt(new Date());
136        
137        // Generate unique token
138        String token = UUID.randomUUID().toString();
139        subscriber.setToken(token);
140        
141        return subscriber;
142    }
143}