001/* 002 * Copyright 2013 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.survey.repository; 017 018import java.time.ZonedDateTime; 019import java.time.format.DateTimeParseException; 020import java.util.Date; 021import java.util.Map; 022 023import org.apache.avalon.framework.component.Component; 024import org.apache.avalon.framework.logger.AbstractLogEnabled; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.avalon.framework.service.Serviceable; 028import org.apache.avalon.framework.thread.ThreadSafe; 029import org.apache.cocoon.environment.Cookie; 030import org.apache.cocoon.environment.Request; 031import org.apache.commons.lang.StringUtils; 032 033import org.ametys.core.user.UserIdentity; 034import org.ametys.plugins.survey.SurveyDateUtils; 035import org.ametys.plugins.survey.answer.ProcessInputAction; 036import org.ametys.plugins.survey.data.SurveyAnswerDao; 037import org.ametys.plugins.survey.data.SurveySession; 038 039/** 040 * helper to check survey access 041 * 042 */ 043public class SurveyAccessHelper extends AbstractLogEnabled implements Component, ThreadSafe, Serviceable 044{ 045 /** Avalon role */ 046 public static final String ROLE = SurveyAccessHelper.class.getName(); 047 048 /** The ametys object resolver. */ 049 protected SurveyAnswerDao _answerDao; 050 051 @Override 052 public void service(ServiceManager manager) throws ServiceException 053 { 054 _answerDao = (SurveyAnswerDao) manager.lookup(SurveyAnswerDao.ROLE); 055 } 056 057 /** 058 * Returns the date on which the user answered to the survey or <code>null</code> if he was never answered 059 * @param surveyId the survey ID. 060 * @param user the user. 061 * @return the date on which the survey was taken, or <code>null</code> if the user never took the survey. 062 */ 063 public Date getSubmissionDate(String surveyId, UserIdentity user) 064 { 065 Date alreadyTakenOn = null; 066 067 if (user != null && StringUtils.isNotBlank(user.getLogin()) && StringUtils.isNotBlank(user.getPopulationId())) 068 { 069 SurveySession userSession = _answerDao.getSession(surveyId, user); 070 071 if (userSession != null) 072 { 073 alreadyTakenOn = userSession.getSubmittedAt(); 074 } 075 } 076 077 return alreadyTakenOn; 078 } 079 080 /** 081 * Return the name of cookie if the survey was already taken or <code>null</code> otherwise. 082 * @param request the request. 083 * @param survey the survey. 084 * @return the name of cookie if the survey was already taken, or <code>null</code> otherwise. 085 */ 086 public String getCookieName (Request request, Survey survey) 087 { 088 Map<String, Cookie> cookieMap = request.getCookieMap(); 089 090 if (cookieMap.containsKey(ProcessInputAction.COOKIE_NAME)) 091 { 092 Cookie cookie = cookieMap.get(ProcessInputAction.COOKIE_NAME); 093 String takenSurveys = cookie.getValue(); 094 if (StringUtils.isNotEmpty(takenSurveys)) 095 { 096 String surveyId = survey.getId(); 097 ZonedDateTime reinitDate = survey.getReinitDate(); 098 String[] takenSurveyCookies = StringUtils.split(takenSurveys, '|'); 099 // We check all cookies for the taken surveys 100 for (int i = 0; i < takenSurveyCookies.length; i++) 101 { 102 // A survey cookie is constructed as surveyId#creationCookieDate when the anonymous user has answered 103 String takenSurveyCookie = takenSurveyCookies[i]; 104 String cookieSurveyDate = StringUtils.substringAfter(takenSurveyCookie, "#"); 105 String cookieSurveyId = StringUtils.substringBefore(takenSurveyCookie, "#"); 106 107 // The survey has been reinitialized at least one time 108 if (reinitDate != null) 109 { 110 // So we compare the last re-initialization date with the date of creation of the cookie 111 try 112 { 113 ZonedDateTime cookieDate = SurveyDateUtils.parseZonedDateTime(cookieSurveyDate); 114 if (reinitDate.isBefore(cookieDate)) 115 { 116 // If the re-initialization date is before the date of creation of the cookie, the cookie is valid and we check the survey id 117 if (surveyId.equals(cookieSurveyId)) 118 { 119 return ProcessInputAction.COOKIE_NAME; 120 } 121 } 122 } 123 catch (DateTimeParseException e) 124 { 125 // The cookie is bad, don't check it 126 } 127 } 128 else if (surveyId.equals(cookieSurveyId)) 129 { 130 return ProcessInputAction.COOKIE_NAME; 131 } 132 133 } 134 } 135 } 136 137 return null; 138 } 139 140}