Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/ru/mystamps/web/Url.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public final class Url {
public static final String INDEX_PAGE = "/";
public static final String ROBOTS_TXT = "/robots.txt";
public static final String SITEMAP_XML = "/sitemap.xml";


public static final String DAILY_STATISTICS = "/report/daily";
public static final String SITE_EVENTS_PAGE = "/site/events";

public static final String REGISTRATION_PAGE = "/account/register";
Expand Down Expand Up @@ -141,6 +142,7 @@ public static Map<String, String> asMap(boolean serveContentFromSingleHost) {
map.put("INFO_COLLECTION_PAGE", INFO_COLLECTION_PAGE);
map.put("SITE_EVENTS_PAGE", SITE_EVENTS_PAGE);
map.put("BOOTSTRAP_LANGUAGE", BOOTSTRAP_LANGUAGE);
map.put("DAILY_STATISTICS", DAILY_STATISTICS);

if (serveContentFromSingleHost) {
map.put("BOOTSTRAP_CSS", BOOTSTRAP_CSS);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/ru/mystamps/web/config/ControllersConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ public ParticipantController getParticipantController() {
public RobotsTxtController getRobotsTxtController() {
return new RobotsTxtController();
}

@Bean
public ReportController getReportController() {
return new ReportController(
servicesConfig.getMailService(),
servicesConfig.getCronService()
);
}

@Bean
public SeriesController getSeriesController() {
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/ru/mystamps/web/controller/ReportController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2009-2017 Slava Semushin <slava.semushin@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ru.mystamps.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import lombok.RequiredArgsConstructor;

import ru.mystamps.web.Url;
import ru.mystamps.web.service.CronService;
import ru.mystamps.web.service.MailService;

/**
* @author Maxim Shestakov
*/
@Controller
@RequiredArgsConstructor
public class ReportController {

private final MailService mailService;
private final CronService cronService;

@GetMapping(path = Url.DAILY_STATISTICS, produces = "text/plain; charset=UTF-8")
@ResponseBody
public String showDailyReport() {
return mailService.prepareDailyStatistics(
cronService.getDailyReport()
);
}

}
3 changes: 3 additions & 0 deletions src/main/java/ru/mystamps/web/service/CronService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
*/
package ru.mystamps.web.service;

import ru.mystamps.web.service.dto.AdminDailyReport;

public interface CronService {
int PURGE_AFTER_DAYS = 3;

void sendDailyStatistics();
AdminDailyReport getDailyReport();
void purgeUsersActivations();
}
11 changes: 10 additions & 1 deletion src/main/java/ru/mystamps/web/service/CronServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.security.access.prepost.PreAuthorize;

import lombok.RequiredArgsConstructor;

import ru.mystamps.web.dao.dto.UsersActivationFullDto;
import ru.mystamps.web.service.dto.AdminDailyReport;
import ru.mystamps.web.support.spring.security.HasAuthority;

@RequiredArgsConstructor
public class CronServiceImpl implements CronService {
Expand All @@ -55,6 +58,12 @@ public class CronServiceImpl implements CronService {
@Scheduled(cron = EVERY_DAY_AT_00_00)
@Transactional(readOnly = true)
public void sendDailyStatistics() {
mailService.sendDailyStatisticsToAdmin(getDailyReport());
}

@Override
@PreAuthorize(HasAuthority.VIEW_DAILY_STATS)
public AdminDailyReport getDailyReport() {
Date today = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
Date yesterday = DateUtils.addDays(today, -1);

Expand Down Expand Up @@ -100,7 +109,7 @@ public void sendDailyStatistics() {
);
report.setInvalidCsrfCounter(invalidCsrfCounter);

mailService.sendDailyStatisticsToAdmin(report);
return report;
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/java/ru/mystamps/web/service/MailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
public interface MailService {
void sendActivationKeyToUser(SendUsersActivationDto activation);
void sendDailyStatisticsToAdmin(AdminDailyReport report);
String prepareDailyStatistics(AdminDailyReport report);
}
63 changes: 34 additions & 29 deletions src/main/java/ru/mystamps/web/service/MailServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.scheduling.annotation.Async;

import org.springframework.security.access.prepost.PreAuthorize;

import ru.mystamps.web.Url;
import ru.mystamps.web.service.dto.AdminDailyReport;
import ru.mystamps.web.service.dto.SendUsersActivationDto;
import ru.mystamps.web.service.exception.EmailSendingException;
import ru.mystamps.web.support.spring.security.HasAuthority;

public class MailServiceImpl implements MailService {
private static final Logger LOG = LoggerFactory.getLogger(MailServiceImpl.class);
Expand Down Expand Up @@ -101,7 +104,7 @@ public void sendDailyStatisticsToAdmin(AdminDailyReport report) {
sendMail(
adminEmail,
getSubjectOfDailyStatisticsMail(report),
getTextOfDailyStatisticsMail(report),
prepareDailyStatistics(report),
"daily_statistics"
);

Expand All @@ -114,7 +117,37 @@ public void sendDailyStatisticsToAdmin(AdminDailyReport report) {
adminLang
);
}

@Override
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add @PreAuthorize(HasAuthority.VIEW_DAILY_STATS)

@PreAuthorize(HasAuthority.VIEW_DAILY_STATS)
public String prepareDailyStatistics(AdminDailyReport report) {
String template = messageSource.getMessage("daily_stat.text", null, adminLang);
String fromDate = shortDatePrinter.format(report.getStartDate());
String tillDate = shortDatePrinter.format(report.getEndDate());

Map<String, String> ctx = new HashMap<>();
ctx.put("from_date", fromDate);
ctx.put("to_date", tillDate);

put(ctx, "added_countries_cnt", report.getAddedCountriesCounter());
put(ctx, "untranslated_countries_cnt", report.getUntranslatedCountriesCounter());
put(ctx, "added_categories_cnt", report.getAddedCategoriesCounter());
put(ctx, "untranslated_categories_cnt", report.getUntranslatedCategoriesCounter());
put(ctx, "added_series_cnt", report.getAddedSeriesCounter());
put(ctx, "updated_series_cnt", report.getUpdatedSeriesCounter());
put(ctx, "updated_collections_cnt", report.getUpdatedCollectionsCounter());
put(ctx, "registration_requests_cnt", report.getRegistrationRequestsCounter());
put(ctx, "registered_users_cnt", report.getRegisteredUsersCounter());
put(ctx, "events_cnt", report.countEvents());
put(ctx, "not_found_cnt", report.getNotFoundCounter());
put(ctx, "failed_auth_cnt", report.getFailedAuthCounter());
put(ctx, "missing_csrf_cnt", report.getMissingCsrfCounter());
put(ctx, "invalid_csrf_cnt", report.getInvalidCsrfCounter());
put(ctx, "bad_request_cnt", -1L); // TODO: #122

return new StrSubstitutor(ctx).replace(template);
}

@SuppressWarnings("PMD.UseObjectForClearerAPI")
private void sendMail(
final String email,
Expand Down Expand Up @@ -183,34 +216,6 @@ private String getSubjectOfDailyStatisticsMail(AdminDailyReport report) {
StrSubstitutor substitutor = new StrSubstitutor(ctx);
return substitutor.replace(template);
}

private String getTextOfDailyStatisticsMail(AdminDailyReport report) {
String template = messageSource.getMessage("daily_stat.text", null, adminLang);
String fromDate = shortDatePrinter.format(report.getStartDate());
String tillDate = shortDatePrinter.format(report.getEndDate());

Map<String, String> ctx = new HashMap<>();
ctx.put("from_date", fromDate);
ctx.put("to_date", tillDate);

put(ctx, "added_countries_cnt", report.getAddedCountriesCounter());
put(ctx, "untranslated_countries_cnt", report.getUntranslatedCountriesCounter());
put(ctx, "added_categories_cnt", report.getAddedCategoriesCounter());
put(ctx, "untranslated_categories_cnt", report.getUntranslatedCategoriesCounter());
put(ctx, "added_series_cnt", report.getAddedSeriesCounter());
put(ctx, "updated_series_cnt", report.getUpdatedSeriesCounter());
put(ctx, "updated_collections_cnt", report.getUpdatedCollectionsCounter());
put(ctx, "registration_requests_cnt", report.getRegistrationRequestsCounter());
put(ctx, "registered_users_cnt", report.getRegisteredUsersCounter());
put(ctx, "events_cnt", report.countEvents());
put(ctx, "not_found_cnt", report.getNotFoundCounter());
put(ctx, "failed_auth_cnt", report.getFailedAuthCounter());
put(ctx, "missing_csrf_cnt", report.getMissingCsrfCounter());
put(ctx, "invalid_csrf_cnt", report.getInvalidCsrfCounter());
put(ctx, "bad_request_cnt", -1L); // TODO: #122

return new StrSubstitutor(ctx).replace(template);
}

private static void put(Map<String, String> ctx, String key, long value) {
ctx.put(key, String.valueOf(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class Authority {
public static final GrantedAuthority UPDATE_COLLECTION = new SimpleGrantedAuthority(StringAuthority.UPDATE_COLLECTION);
public static final GrantedAuthority VIEW_SITE_EVENTS = new SimpleGrantedAuthority(StringAuthority.VIEW_SITE_EVENTS);
public static final GrantedAuthority VIEW_SERIES_SALES = new SimpleGrantedAuthority(StringAuthority.VIEW_SERIES_SALES);
public static final GrantedAuthority VIEW_DAILY_STATS = new SimpleGrantedAuthority(StringAuthority.VIEW_DAILY_STATS);

private Authority() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private static Collection<? extends GrantedAuthority> getAuthorities(UserDetails
authorities.add(Authority.ADD_SERIES_SALES);
authorities.add(Authority.VIEW_SERIES_SALES);
authorities.add(Authority.MANAGE_TOGGLZ);
authorities.add(Authority.VIEW_DAILY_STATS);
}

return authorities;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class HasAuthority {
public static final String CREATE_SERIES = "hasAuthority('" + StringAuthority.CREATE_SERIES + "')";
public static final String CREATE_CATEGORY = "hasAuthority('" + StringAuthority.CREATE_CATEGORY + "')";
public static final String CREATE_COUNTRY = "hasAuthority('" + StringAuthority.CREATE_COUNTRY + "')";
public static final String VIEW_DAILY_STATS = "hasAuthority('" + StringAuthority.VIEW_DAILY_STATS + "')";
public static final String UPDATE_COLLECTION = "hasAuthority('" + StringAuthority.UPDATE_COLLECTION + "')";
public static final String VIEW_SITE_EVENTS = "hasAuthority('" + StringAuthority.VIEW_SITE_EVENTS + "')";
public static final String VIEW_SERIES_SALES = "hasAuthority('" + StringAuthority.VIEW_SERIES_SALES + "')";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ protected void configure(HttpSecurity http) throws Exception {
.mvcMatchers(Url.ADD_SERIES_PAGE).hasAuthority(StringAuthority.CREATE_SERIES)
.mvcMatchers(Url.SITE_EVENTS_PAGE).hasAuthority(StringAuthority.VIEW_SITE_EVENTS)
.mvcMatchers(Url.SUGGEST_SERIES_COUNTRY).hasAuthority(StringAuthority.CREATE_SERIES)
.mvcMatchers(Url.DAILY_STATISTICS).hasAuthority(StringAuthority.VIEW_DAILY_STATS)
.regexMatchers(HttpMethod.POST, "/series/[0-9]+")
.hasAnyAuthority(
StringAuthority.UPDATE_COLLECTION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public final class StringAuthority {
public static final String UPDATE_COLLECTION = "UPDATE_COLLECTION";
public static final String VIEW_SITE_EVENTS = "VIEW_SITE_EVENTS";
public static final String VIEW_SERIES_SALES = "VIEW_SERIES_SALES";
public static final String VIEW_DAILY_STATS = "VIEW_DAILY_STATS";

private StringAuthority() {
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/ru/mystamps/i18n/Messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ t_example = Example
t_catalog = Catalog
t_search = Search
t_view_suspicious_activities = view suspicious activities
t_view_daily_statistics = view daily statistics

# account/register.html
t_registration_on_site = Register on site
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ t_example = Пример
t_catalog = Каталог
t_search = Найти
t_view_suspicious_activities = посмотреть подозрительные события
t_view_daily_statistics = посмотреть дневную статистику

# account/register.html
t_registration_on_site = Регистрация на сайте
Expand Down
3 changes: 3 additions & 0 deletions src/main/webapp/WEB-INF/views/site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<li togglz:active="VIEW_SITE_EVENTS" sec:authorize="hasAuthority('VIEW_SITE_EVENTS')">
<a th:href="@{${SITE_EVENTS_PAGE}}" th:text="#{t_view_suspicious_activities}" href="events.html">view suspicious activities</a>
</li>
<li sec:authorize="hasAuthority('VIEW_DAILY_STATS')">
<a th:href="@{${DAILY_STATISTICS}}" th:text="#{t_view_daily_statistics}" href="javascript:void(0)">view daily statistics</a>
</li>
</ul>
</nav>
</div>
Expand Down