Skip to content

Commit b15bea1

Browse files
cssruphp-coder
authored andcommitted
/site/events: add interface for viewing suspicious activity.
Fix #248
1 parent f4759b1 commit b15bea1

18 files changed

+442
-2
lines changed

src/main/java/ru/mystamps/web/Url.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public final class Url {
3838
public static final String ROBOTS_TXT = "/robots.txt";
3939
public static final String SITEMAP_XML = "/sitemap.xml";
4040

41+
public static final String SITE_EVENTS_PAGE = "/site/events";
42+
4143
public static final String REGISTRATION_PAGE = "/account/register";
4244

4345
public static final String AUTHENTICATION_PAGE = "/account/auth";
@@ -124,6 +126,7 @@ public static Map<String, String> asMap(boolean serveContentFromSingleHost) {
124126
map.put("INFO_COUNTRY_PAGE", INFO_COUNTRY_PAGE);
125127
map.put("LIST_COUNTRIES_PAGE", LIST_COUNTRIES_PAGE);
126128
map.put("INFO_COLLECTION_PAGE", INFO_COLLECTION_PAGE);
129+
map.put("SITE_EVENTS_PAGE", SITE_EVENTS_PAGE);
127130
map.put("BOOTSTRAP_LANGUAGE", BOOTSTRAP_LANGUAGE);
128131

129132
if (serveContentFromSingleHost) {

src/main/java/ru/mystamps/web/config/ControllersConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public SiteController getSiteController() {
100100
servicesConfig.getCategoryService(),
101101
servicesConfig.getCollectionService(),
102102
servicesConfig.getCountryService(),
103-
servicesConfig.getSeriesService()
103+
servicesConfig.getSeriesService(),
104+
servicesConfig.getSuspiciousActivityService()
104105
);
105106
}
106107

src/main/java/ru/mystamps/web/config/ServicesConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public class ServicesConfig {
5858
@Autowired
5959
private MessageSource messageSource;
6060

61+
@Bean
62+
public SuspiciousActivityService getSuspiciousActivityService() {
63+
return new SuspiciousActivityServiceImpl(daoConfig.getSuspiciousActivityDao());
64+
}
65+
6166
@Bean
6267
public CountryService getCountryService() {
6368
return new CountryServiceImpl(daoConfig.getJdbcCountryDao());

src/main/java/ru/mystamps/web/controller/SiteController.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import ru.mystamps.web.service.CollectionService;
3131
import ru.mystamps.web.service.CountryService;
3232
import ru.mystamps.web.service.SeriesService;
33+
import ru.mystamps.web.service.SuspiciousActivityService;
3334
import ru.mystamps.web.util.LocaleUtils;
3435

3536
@Controller
@@ -43,6 +44,7 @@ public class SiteController {
4344
private final CollectionService collectionService;
4445
private final CountryService countryService;
4546
private final SeriesService seriesService;
47+
private final SuspiciousActivityService suspiciousActivityService;
4648

4749
@RequestMapping(Url.INDEX_PAGE)
4850
public String showIndexPage(Model model, Locale userLocale) {
@@ -66,4 +68,15 @@ public String showIndexPage(Model model, Locale userLocale) {
6668
return "site/index";
6769
}
6870

71+
/**
72+
* @author Sergey Chechenev
73+
*/
74+
@RequestMapping(Url.SITE_EVENTS_PAGE)
75+
public void viewSiteEvents(Model model) {
76+
model.addAttribute(
77+
"activities",
78+
suspiciousActivityService.findSuspiciousActivities()
79+
);
80+
}
81+
6982
}

src/main/java/ru/mystamps/web/dao/SuspiciousActivityDao.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
*/
1818
package ru.mystamps.web.dao;
1919

20+
import java.util.List;
21+
2022
import ru.mystamps.web.dao.dto.AddSuspiciousActivityDbDto;
23+
import ru.mystamps.web.dao.dto.SuspiciousActivityDto;
2124

2225
public interface SuspiciousActivityDao {
2326
void add(AddSuspiciousActivityDbDto activity);
27+
List<SuspiciousActivityDto> findAll();
2428
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (C) 2009-2016 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.dao.dto;
19+
20+
import java.util.Date;
21+
22+
import lombok.Getter;
23+
import lombok.RequiredArgsConstructor;
24+
25+
/**
26+
* @author Sergey Chechenev
27+
*/
28+
@Getter
29+
@RequiredArgsConstructor
30+
public class SuspiciousActivityDto {
31+
private final String type;
32+
private final Date occurredAt;
33+
private final String page;
34+
private final String method;
35+
private final String userLogin;
36+
private final String ip;
37+
private final String refererPage;
38+
private final String userAgent;
39+
}

src/main/java/ru/mystamps/web/dao/impl/JdbcSuspiciousActivityDao.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
*/
1818
package ru.mystamps.web.dao.impl;
1919

20+
import java.util.Collections;
2021
import java.util.HashMap;
22+
import java.util.List;
2123
import java.util.Map;
2224

2325
import org.apache.commons.lang3.Validate;
@@ -29,6 +31,7 @@
2931

3032
import ru.mystamps.web.dao.SuspiciousActivityDao;
3133
import ru.mystamps.web.dao.dto.AddSuspiciousActivityDbDto;
34+
import ru.mystamps.web.dao.dto.SuspiciousActivityDto;
3235

3336
@RequiredArgsConstructor
3437
public class JdbcSuspiciousActivityDao implements SuspiciousActivityDao {
@@ -38,6 +41,9 @@ public class JdbcSuspiciousActivityDao implements SuspiciousActivityDao {
3841
@Value("${suspicious_activity.create}")
3942
private String addSuspiciousActivitySql;
4043

44+
@Value("${suspicious_activity.find_all}")
45+
private String findAllSuspiciousActivitiesSql;
46+
4147
@Override
4248
public void add(AddSuspiciousActivityDbDto activity) {
4349
Map<String, Object> params = new HashMap<>();
@@ -62,4 +68,16 @@ public void add(AddSuspiciousActivityDbDto activity) {
6268
);
6369
}
6470

71+
/**
72+
* @author Sergey Chechenev
73+
*/
74+
@Override
75+
public List<SuspiciousActivityDto> findAll() {
76+
return jdbcTemplate.query(
77+
findAllSuspiciousActivitiesSql,
78+
Collections.emptyMap(),
79+
RowMappers::forSuspiciousActivityDto
80+
);
81+
}
82+
6583
}

src/main/java/ru/mystamps/web/dao/impl/RowMappers.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
import java.math.BigDecimal;
2121
import java.sql.ResultSet;
2222
import java.sql.SQLException;
23+
import java.util.Date;
2324

2425
import ru.mystamps.web.dao.dto.CollectionInfoDto;
2526
import ru.mystamps.web.dao.dto.SeriesFullInfoDto;
27+
import ru.mystamps.web.dao.dto.SuspiciousActivityDto;
2628
import ru.mystamps.web.dao.dto.UsersActivationDto;
2729
import ru.mystamps.web.dao.dto.UsersActivationFullDto;
2830
import ru.mystamps.web.service.dto.LinkEntityDto;
@@ -31,7 +33,7 @@
3133
import ru.mystamps.web.service.dto.SitemapInfoDto;
3234
import ru.mystamps.web.service.dto.UrlEntityDto;
3335

34-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
36+
@SuppressWarnings({ "PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods" })
3537
final class RowMappers {
3638

3739
private RowMappers() {
@@ -158,7 +160,33 @@ public static SeriesFullInfoDto forSeriesFullInfoDto(ResultSet rs, int i) throws
158160
);
159161
}
160162

163+
/**
164+
* @author Sergey Chechenev
165+
*/
161166
// CheckStyle: ignore LineLength for next 1 line
167+
public static SuspiciousActivityDto forSuspiciousActivityDto(ResultSet rs, int i) throws SQLException {
168+
String type = rs.getString("activity_name");
169+
Date occurredAt = rs.getTimestamp("occurred_at");
170+
String page = rs.getString("page");
171+
String method = rs.getString("method");
172+
String userLogin = rs.getString("user_login");
173+
String ip = rs.getString("ip");
174+
String refererPage = rs.getString("referer_page");
175+
String userAgent = rs.getString("user_agent");
176+
177+
return new SuspiciousActivityDto(
178+
type,
179+
occurredAt,
180+
page,
181+
method,
182+
userLogin,
183+
ip,
184+
refererPage,
185+
userAgent
186+
);
187+
}
188+
189+
// CheckStyle: ignore LineLength for next 1 line
162190
public static UsersActivationDto forUsersActivationDto(ResultSet rs, int i) throws SQLException {
163191
return new UsersActivationDto(
164192
rs.getString("email"),
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (C) 2009-2016 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.service;
19+
20+
import java.util.List;
21+
22+
import ru.mystamps.web.dao.dto.SuspiciousActivityDto;
23+
24+
/**
25+
* @author Sergey Chechenev
26+
*/
27+
public interface SuspiciousActivityService {
28+
List<SuspiciousActivityDto> findSuspiciousActivities();
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2009-2016 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.service;
19+
20+
import java.util.List;
21+
22+
import org.springframework.transaction.annotation.Transactional;
23+
24+
import org.springframework.security.access.prepost.PreAuthorize;
25+
26+
import lombok.RequiredArgsConstructor;
27+
28+
import ru.mystamps.web.dao.SuspiciousActivityDao;
29+
import ru.mystamps.web.dao.dto.SuspiciousActivityDto;
30+
31+
/**
32+
* @author Sergey Chechenev
33+
*/
34+
@RequiredArgsConstructor
35+
public class SuspiciousActivityServiceImpl implements SuspiciousActivityService {
36+
private final SuspiciousActivityDao suspiciousActivityDao;
37+
38+
@Override
39+
@Transactional(readOnly = true)
40+
@PreAuthorize("hasAuthority('VIEW_SITE_EVENTS')")
41+
public List<SuspiciousActivityDto> findSuspiciousActivities() {
42+
return suspiciousActivityDao.findAll();
43+
}
44+
}

src/main/java/ru/mystamps/web/support/spring/security/CustomUserDetailsService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ private static Collection<? extends GrantedAuthority> getAuthorities(User user)
7777
authorities.add(new SimpleGrantedAuthority("CREATE_CATEGORY"));
7878
authorities.add(new SimpleGrantedAuthority("CREATE_COUNTRY"));
7979
authorities.add(new SimpleGrantedAuthority("ADD_COMMENTS_TO_SERIES"));
80+
authorities.add(new SimpleGrantedAuthority("VIEW_SITE_EVENTS"));
8081

8182
// gives access to Togglz web console
8283
authorities.add(new SimpleGrantedAuthority("CHANGE_FEATURES"));

src/main/java/ru/mystamps/web/support/spring/security/SecurityConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ protected void configure(HttpSecurity http) throws Exception {
6969
.antMatchers(Url.ADD_CATEGORY_PAGE).hasAuthority("CREATE_CATEGORY")
7070
.antMatchers(Url.ADD_COUNTRY_PAGE).hasAuthority("CREATE_COUNTRY")
7171
.antMatchers(Url.ADD_SERIES_PAGE).hasAuthority("CREATE_SERIES")
72+
.antMatchers(Url.SITE_EVENTS_PAGE).hasAuthority("VIEW_SITE_EVENTS")
7273
.regexMatchers(HttpMethod.POST, "/series/[0-9]+")
7374
.hasAnyAuthority("UPDATE_COLLECTION", "ADD_IMAGES_TO_SERIES")
7475
.anyRequest().permitAll()

src/main/java/ru/mystamps/web/support/togglz/Features.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public enum Features implements Feature {
4040
@EnabledByDefault
4141
SHOW_SEARCH_PANEL_ON_INDEX_PAGE,
4242

43+
@Label("View site events")
44+
@EnabledByDefault
45+
VIEW_SITE_EVENTS,
46+
4347
@Label("Show statistics of collection on collection page")
4448
@EnabledByDefault
4549
SHOW_COLLECTION_STATISTICS,

src/main/resources/ru/mystamps/i18n/Messages.properties

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ t_site_author_name = Slava Semushin
2929
t_site_author_email = slava.semushin@gmail.com
3030
t_write_email = Write e-mail
3131

32+
# site/events.html
33+
t_suspicious_activities = suspicious activities
34+
t_no_suspicious_activities_found = No suspicious activities found
35+
t_type = Type
36+
t_occurred = Date
37+
t_page = Page
38+
t_method = Method
39+
t_user_login = Login
40+
t_ip = IP
41+
t_referer_page = Referer page
42+
t_user_agent = User agent
43+
t_invalid_csrf_token = Invalid CSRF token
44+
t_missing_csrf_token = Missing CSRF token
45+
t_page_not_found = Page not found
46+
t_auth_failed = Authentication failed
47+
3248
# site/index.html
3349
t_index_title = create your own virtual collection!
3450
t_you_may = You may
@@ -47,6 +63,7 @@ t_number = Number
4763
t_example = Example
4864
t_catalog = Catalog
4965
t_search = Search
66+
t_watch_suspicious_activities = view suspicious activities
5067

5168
# account/register.html
5269
t_registration_on_site = Register on site

src/main/resources/ru/mystamps/i18n/Messages_ru.properties

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ t_site_author_name = Слава Семушин
2929
t_site_author_email = slava.semushin@gmail.com
3030
t_write_email = Написать письмо
3131

32+
# site/events.html
33+
t_suspicious_activities = подозрительная активность
34+
t_no_suspicious_activities_found = Подозрительная активность не обнаружена
35+
t_type = Тип
36+
t_occurred = Дата
37+
t_page = Страница
38+
t_method = Метод
39+
t_user_login = Логин
40+
t_ip = IP
41+
t_referer_page = Ссылающаяся страница
42+
t_user_agent = Браузер
43+
t_invalid_csrf_token = Неверный CSRF токен
44+
t_missing_csrf_token = Несуществующий CSRF токен
45+
t_page_not_found = Страница не найдена
46+
t_auth_failed = Неверный логин/пароль
47+
3248
# site/index.html
3349
t_index_title = создай свою виртуальную коллекцию!
3450
t_you_may = Вы можете
@@ -47,6 +63,7 @@ t_number = Номер
4763
t_example = Пример
4864
t_catalog = Каталог
4965
t_search = Найти
66+
t_watch_suspicious_activities = посмотреть подозрительные события
5067

5168
# account/register.html
5269
t_registration_on_site = Регистрация на сайте

0 commit comments

Comments
 (0)