WIP: Resolve "Статистика по активностям" #232
@ -124,6 +124,7 @@ dependencies {
|
||||
|
||||
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.6.0'
|
||||
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.6.0'
|
||||
compile group: 'net.sourceforge.htmlunit', name: 'htmlunit', version: '2.35.0'
|
||||
|
||||
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'
|
||||
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.3.1'
|
||||
|
@ -13,8 +13,10 @@ import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
||||
import ru.ulstu.core.model.AuthFailureHandler;
|
||||
import ru.ulstu.user.controller.UserController;
|
||||
import ru.ulstu.user.model.UserRoleConstants;
|
||||
import ru.ulstu.user.service.UserService;
|
||||
@ -35,17 +37,20 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||
private final AuthenticationSuccessHandler authenticationSuccessHandler;
|
||||
private final LogoutSuccessHandler logoutSuccessHandler;
|
||||
private final ApplicationProperties applicationProperties;
|
||||
private final AuthenticationFailureHandler authenticationFailureHandler;
|
||||
|
||||
public SecurityConfiguration(UserService userService,
|
||||
BCryptPasswordEncoder bCryptPasswordEncoder,
|
||||
AuthenticationSuccessHandler authenticationSuccessHandler,
|
||||
LogoutSuccessHandler logoutSuccessHandler,
|
||||
ApplicationProperties applicationProperties) {
|
||||
ApplicationProperties applicationProperties,
|
||||
AuthFailureHandler authenticationFailureHandler) {
|
||||
this.userService = userService;
|
||||
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
|
||||
this.authenticationSuccessHandler = authenticationSuccessHandler;
|
||||
this.logoutSuccessHandler = logoutSuccessHandler;
|
||||
this.applicationProperties = applicationProperties;
|
||||
this.authenticationFailureHandler = authenticationFailureHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -66,6 +71,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||
.antMatchers(UserController.ACTIVATE_URL).permitAll()
|
||||
.antMatchers(Constants.PASSWORD_RESET_REQUEST_PAGE).permitAll()
|
||||
.antMatchers(Constants.PASSWORD_RESET_PAGE).permitAll()
|
||||
.antMatchers("/users/block").permitAll()
|
||||
.antMatchers(UserController.URL + UserController.REGISTER_URL).permitAll()
|
||||
.antMatchers(UserController.URL + UserController.ACTIVATE_URL).permitAll()
|
||||
.antMatchers(UserController.URL + UserController.PASSWORD_RESET_REQUEST_URL).permitAll()
|
||||
@ -76,6 +82,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||
.formLogin()
|
||||
.loginPage("/login")
|
||||
.successHandler(authenticationSuccessHandler)
|
||||
.failureHandler(authenticationFailureHandler)
|
||||
.permitAll()
|
||||
.and()
|
||||
.logout()
|
||||
|
21
src/main/java/ru/ulstu/core/model/AuthFailureHandler.java
Normal file
21
src/main/java/ru/ulstu/core/model/AuthFailureHandler.java
Normal file
@ -0,0 +1,21 @@
|
||||
package ru.ulstu.core.model;
|
||||
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ulstu.user.error.UserBlockedException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
public class AuthFailureHandler implements AuthenticationFailureHandler {
|
||||
@Override
|
||||
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
|
||||
AuthenticationException ex) throws IOException {
|
||||
if (ex.getClass() == UserBlockedException.class) {
|
||||
response.sendRedirect("/users/block");
|
||||
}
|
||||
}
|
||||
}
|
@ -50,7 +50,7 @@ public class GrantController {
|
||||
@GetMapping("/grant")
|
||||
public void getGrant(ModelMap modelMap, @RequestParam(value = "id") Integer id) {
|
||||
if (id != null && id > 0) {
|
||||
GrantDto grantDto = grantService.findOneDto(id);
|
||||
GrantDto grantDto = grantService.getExistGrantById(id);
|
||||
attachPaper(grantDto);
|
||||
modelMap.put("grantDto", grantDto);
|
||||
} else {
|
||||
|
@ -1,8 +1,13 @@
|
||||
package ru.ulstu.grant.page;
|
||||
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import com.gargoylesoftware.htmlunit.html.DomElement;
|
||||
import com.gargoylesoftware.htmlunit.html.DomNode;
|
||||
import com.gargoylesoftware.htmlunit.html.DomNodeList;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlElement;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlTableDataCell;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
@ -11,16 +16,17 @@ import java.util.List;
|
||||
|
||||
public class KiasPage {
|
||||
private final static String KIAS_GRANT_DATE_FORMAT = "dd.MM.yyyy HH:mm";
|
||||
private WebDriver driver;
|
||||
private final HtmlPage page;
|
||||
|
||||
public KiasPage(WebDriver webDriver) {
|
||||
this.driver = webDriver;
|
||||
public KiasPage(HtmlPage page) {
|
||||
this.page = page;
|
||||
}
|
||||
|
||||
public boolean goToNextPage() {
|
||||
try {
|
||||
if (driver.findElements(By.id("js-ctrlNext")).size() > 0) {
|
||||
driver.findElement(By.id("js-ctrlNext")).click();
|
||||
HtmlElement nextPageLink = page.getHtmlElementById("js-ctrlNext");
|
||||
if (nextPageLink.isDisplayed()) {
|
||||
nextPageLink.click();
|
||||
return true;
|
||||
}
|
||||
} finally {
|
||||
@ -28,23 +34,25 @@ public class KiasPage {
|
||||
}
|
||||
}
|
||||
|
||||
public List<WebElement> getPageOfGrants() {
|
||||
WebElement listContest = driver.findElement(By.tagName("tBody"));
|
||||
List<WebElement> grants = listContest.findElements(By.cssSelector("tr.tr"));
|
||||
return grants;
|
||||
public List<DomNode> getPageOfGrants() {
|
||||
return page.getByXPath("/html/body/div[2]/div/div[2]/main/div[1]/table/tbody/tr");
|
||||
}
|
||||
|
||||
public String getGrantTitle(WebElement grant) {
|
||||
return grant.findElement(By.cssSelector("td.tertiary")).findElement(By.tagName("a")).getText();
|
||||
public String getGrantTitle(DomNode grant) {
|
||||
return ((DomNode)grant.getFirstByXPath("td[@class='tertiary']/a")).getTextContent();
|
||||
}
|
||||
|
||||
public Date parseDeadLineDate(WebElement grantElement) throws ParseException {
|
||||
public Date parseDeadLineDate(DomNode grantElement) throws ParseException {
|
||||
String deadlineDate = getFirstDeadline(grantElement); //10.06.2019 23:59
|
||||
SimpleDateFormat formatter = new SimpleDateFormat(KIAS_GRANT_DATE_FORMAT);
|
||||
return formatter.parse(deadlineDate);
|
||||
}
|
||||
|
||||
private String getFirstDeadline(WebElement grantElement) {
|
||||
return grantElement.findElement(By.xpath("./td[5]")).getText();
|
||||
private String getFirstDeadline(DomNode grantElement) {
|
||||
return ((DomNode)grantElement.getFirstByXPath("td[5]")).getTextContent();
|
||||
}
|
||||
|
||||
public boolean isTrGrantLine(DomNode grantElement) {
|
||||
return !((HtmlTableRow)grantElement).getAttribute("class").contains("pagerSavedHeightSpacer");
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ru.ulstu.grant.service;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -17,7 +18,6 @@ import ru.ulstu.paper.model.Paper;
|
||||
import ru.ulstu.paper.model.PaperDto;
|
||||
import ru.ulstu.paper.service.PaperService;
|
||||
import ru.ulstu.ping.service.PingService;
|
||||
import ru.ulstu.project.model.Project;
|
||||
import ru.ulstu.project.model.ProjectDto;
|
||||
import ru.ulstu.project.service.ProjectService;
|
||||
import ru.ulstu.timeline.service.EventService;
|
||||
@ -77,6 +77,11 @@ public class GrantService extends BaseService {
|
||||
this.pingService = pingService;
|
||||
}
|
||||
|
||||
public GrantDto getExistGrantById(Integer id) {
|
||||
GrantDto grantDto = new GrantDto(findById(id));
|
||||
return grantDto;
|
||||
}
|
||||
|
||||
public List<Grant> findAll() {
|
||||
return grantRepository.findAll();
|
||||
}
|
||||
@ -85,17 +90,13 @@ public class GrantService extends BaseService {
|
||||
return convert(findAll(), GrantDto::new);
|
||||
}
|
||||
|
||||
public GrantDto findOneDto(Integer id) {
|
||||
return new GrantDto(grantRepository.findOne(id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Integer create(GrantDto grantDto) throws IOException {
|
||||
public Grant create(GrantDto grantDto) throws IOException {
|
||||
Grant newGrant = copyFromDto(new Grant(), grantDto);
|
||||
newGrant = grantRepository.save(newGrant);
|
||||
eventService.createFromObject(newGrant, Collections.emptyList(), false, "гранта");
|
||||
grantNotificationService.sendCreateNotification(newGrant);
|
||||
return newGrant.getId();
|
||||
return newGrant;
|
||||
}
|
||||
|
||||
private Grant copyFromDto(Grant grant, GrantDto grantDto) throws IOException {
|
||||
@ -106,9 +107,11 @@ public class GrantService extends BaseService {
|
||||
grant.setProject(projectService.findById(grantDto.getProject().getId()));
|
||||
}
|
||||
grant.setDeadlines(deadlineService.saveOrCreate(grantDto.getDeadlines()));
|
||||
grant.setFiles(fileService.saveOrCreate(grantDto.getFiles().stream()
|
||||
.filter(f -> !f.isDeleted())
|
||||
.collect(toList())));
|
||||
if (!grant.getFiles().isEmpty()) {
|
||||
grant.setFiles(fileService.saveOrCreate(grantDto.getFiles().stream()
|
||||
.filter(f -> !f.isDeleted())
|
||||
.collect(toList())));
|
||||
}
|
||||
grant.getAuthors().clear();
|
||||
if (grantDto.getAuthorIds() != null && !grantDto.getAuthorIds().isEmpty()) {
|
||||
grantDto.getAuthorIds().forEach(authorIds -> grant.getAuthors().add(userService.findById(authorIds)));
|
||||
@ -123,6 +126,7 @@ public class GrantService extends BaseService {
|
||||
return grant;
|
||||
}
|
||||
|
||||
|
||||
public void createProject(GrantDto grantDto) throws IOException {
|
||||
grantDto.setProject(
|
||||
new ProjectDto(projectService.save(new ProjectDto(grantDto.getTitle()))));
|
||||
@ -130,7 +134,7 @@ public class GrantService extends BaseService {
|
||||
|
||||
@Transactional
|
||||
public Integer update(GrantDto grantDto) throws IOException {
|
||||
Grant grant = grantRepository.findOne(grantDto.getId());
|
||||
Grant grant = findById(grantDto.getId());
|
||||
Set<User> oldAuthors = new HashSet<>(grant.getAuthors());
|
||||
User oldLeader = grant.getLeader();
|
||||
for (FileDataDto file : grantDto.getFiles().stream()
|
||||
@ -159,34 +163,19 @@ public class GrantService extends BaseService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void delete(Integer grantId) throws IOException {
|
||||
Grant grant = grantRepository.findOne(grantId);
|
||||
grantRepository.delete(grant);
|
||||
public boolean delete(Integer grantId) throws IOException {
|
||||
Grant grant = findById(grantId);
|
||||
if (grant != null) {
|
||||
grantRepository.delete(grant);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<Grant.GrantStatus> getGrantStatuses() {
|
||||
return Arrays.asList(Grant.GrantStatus.values());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Grant create(String title, Project projectId, Date deadlineDate, User user, Paper paper) {
|
||||
Grant grant = new Grant();
|
||||
grant.setTitle(title);
|
||||
grant.setComment("Комментарий к гранту 1");
|
||||
grant.setProject(projectId);
|
||||
grant.setStatus(APPLICATION);
|
||||
grant.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн"));
|
||||
grant.getAuthors().add(user);
|
||||
grant.setLeader(user);
|
||||
grant.getPapers().add(paper);
|
||||
grant = grantRepository.save(grant);
|
||||
|
||||
eventService.createFromObject(grant, Collections.emptyList(), false, "гранта");
|
||||
grantNotificationService.sendCreateNotification(grant);
|
||||
|
||||
return grant;
|
||||
}
|
||||
|
||||
public boolean save(GrantDto grantDto, Errors errors) throws IOException {
|
||||
grantDto.setName(grantDto.getTitle());
|
||||
filterEmptyDeadlines(grantDto);
|
||||
@ -274,20 +263,22 @@ public class GrantService extends BaseService {
|
||||
return paperService.findAllNotCompleted();
|
||||
}
|
||||
|
||||
public void attachPaper(GrantDto grantDto) {
|
||||
public List<PaperDto> attachPaper(GrantDto grantDto) {
|
||||
if (!grantDto.getPaperIds().isEmpty()) {
|
||||
grantDto.getPapers().clear();
|
||||
grantDto.setPapers(getGrantPapers(grantDto.getPaperIds()));
|
||||
} else {
|
||||
grantDto.getPapers().clear();
|
||||
}
|
||||
return grantDto.getPapers();
|
||||
}
|
||||
|
||||
public void removeDeadline(GrantDto grantDto, Integer deadlineId) {
|
||||
public GrantDto removeDeadline(GrantDto grantDto, Integer deadlineId) {
|
||||
if (grantDto.getDeadlines().get(deadlineId).getId() != null) {
|
||||
grantDto.getRemovedDeadlineIds().add(grantDto.getDeadlines().get(deadlineId).getId());
|
||||
}
|
||||
grantDto.getDeadlines().remove((int) deadlineId);
|
||||
return grantDto;
|
||||
}
|
||||
|
||||
private List<User> getCompletedPapersAuthors(Paper.PaperType type) {
|
||||
@ -313,10 +304,11 @@ public class GrantService extends BaseService {
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
public void filterEmptyDeadlines(GrantDto grantDto) {
|
||||
public List<Deadline> filterEmptyDeadlines(GrantDto grantDto) {
|
||||
grantDto.setDeadlines(grantDto.getDeadlines().stream()
|
||||
.filter(dto -> dto.getDate() != null || !org.springframework.util.StringUtils.isEmpty(dto.getDescription()))
|
||||
.filter(dto -> dto.getDate() != null || !StringUtils.isEmpty(dto.getDescription()))
|
||||
.collect(Collectors.toList()));
|
||||
return grantDto.getDeadlines();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@ -346,8 +338,4 @@ public class GrantService extends BaseService {
|
||||
public void ping(int grantId) throws IOException {
|
||||
pingService.addPing(findById(grantId));
|
||||
}
|
||||
|
||||
public Grant findGrantById(Integer grantId) {
|
||||
return grantRepository.findOne(grantId);
|
||||
}
|
||||
}
|
||||
|
@ -1,35 +1,29 @@
|
||||
package ru.ulstu.grant.service;
|
||||
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
import org.openqa.selenium.chrome.ChromeOptions;
|
||||
import com.gargoylesoftware.htmlunit.WebClient;
|
||||
import com.gargoylesoftware.htmlunit.html.DomElement;
|
||||
import com.gargoylesoftware.htmlunit.html.DomNode;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlElement;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.configuration.ApplicationProperties;
|
||||
import ru.ulstu.grant.model.GrantDto;
|
||||
import ru.ulstu.grant.page.KiasPage;
|
||||
import ru.ulstu.user.service.UserService;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
|
||||
import static org.apache.commons.lang3.StringUtils.isEmpty;
|
||||
|
||||
@Service
|
||||
public class KiasService {
|
||||
private final static String BASE_URL = "https://www.rfbr.ru/rffi/ru/contest_search?CONTEST_STATUS_ID=%s&CONTEST_TYPE=%s&CONTEST_YEAR=%s";
|
||||
private final static String CONTEST_STATUS_ID = "1";
|
||||
private final static String CONTEST_TYPE = "-1";
|
||||
|
||||
private final static String DRIVER_LOCATION = "drivers/%s";
|
||||
private final static String WINDOWS_DRIVER = "chromedriver.exe";
|
||||
private final static String LINUX_DRIVER = "chromedriver";
|
||||
private final static String DRIVER_TYPE = "webdriver.chrome.driver";
|
||||
|
||||
private final UserService userService;
|
||||
private final ApplicationProperties applicationProperties;
|
||||
|
||||
@ -39,22 +33,22 @@ public class KiasService {
|
||||
this.applicationProperties = applicationProperties;
|
||||
}
|
||||
|
||||
public List<GrantDto> getNewGrantsDto() throws ParseException {
|
||||
WebDriver webDriver = getDriver();
|
||||
public List<GrantDto> getNewGrantsDto() throws ParseException, IOException {
|
||||
Integer leaderId = userService.findOneByLoginIgnoreCase("admin").getId();
|
||||
List<GrantDto> grants = new ArrayList<>();
|
||||
for (Integer year : generateGrantYears()) {
|
||||
webDriver.get(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, year));
|
||||
grants.addAll(getKiasGrants(webDriver));
|
||||
try (final WebClient webClient = new WebClient()) {
|
||||
for (Integer year : generateGrantYears()) {
|
||||
final HtmlPage page = webClient.getPage(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, year));
|
||||
grants.addAll(getKiasGrants(page));
|
||||
}
|
||||
}
|
||||
grants.forEach(grantDto -> grantDto.setLeaderId(leaderId));
|
||||
webDriver.quit();
|
||||
return grants;
|
||||
}
|
||||
|
||||
public List<GrantDto> getKiasGrants(WebDriver webDriver) throws ParseException {
|
||||
public List<GrantDto> getKiasGrants(HtmlPage page) throws ParseException {
|
||||
List<GrantDto> newGrants = new ArrayList<>();
|
||||
KiasPage kiasPage = new KiasPage(webDriver);
|
||||
KiasPage kiasPage = new KiasPage(page);
|
||||
do {
|
||||
newGrants.addAll(getGrantsFromPage(kiasPage));
|
||||
} while (kiasPage.goToNextPage()); //проверка существования следующей страницы с грантами
|
||||
@ -63,11 +57,13 @@ public class KiasService {
|
||||
|
||||
private List<GrantDto> getGrantsFromPage(KiasPage kiasPage) throws ParseException {
|
||||
List<GrantDto> grants = new ArrayList<>();
|
||||
for (WebElement grantElement : kiasPage.getPageOfGrants()) {
|
||||
GrantDto grantDto = new GrantDto(
|
||||
kiasPage.getGrantTitle(grantElement),
|
||||
kiasPage.parseDeadLineDate(grantElement));
|
||||
grants.add(grantDto);
|
||||
for (DomNode grantElement : kiasPage.getPageOfGrants()) {
|
||||
if (kiasPage.isTrGrantLine(grantElement)) {
|
||||
GrantDto grantDto = new GrantDto(
|
||||
kiasPage.getGrantTitle(grantElement),
|
||||
kiasPage.parseDeadLineDate(grantElement));
|
||||
grants.add(grantDto);
|
||||
}
|
||||
}
|
||||
return grants;
|
||||
}
|
||||
@ -76,26 +72,4 @@ public class KiasService {
|
||||
return Arrays.asList(Calendar.getInstance().get(Calendar.YEAR),
|
||||
Calendar.getInstance().get(Calendar.YEAR) + 1);
|
||||
}
|
||||
|
||||
private WebDriver getDriver() {
|
||||
System.setProperty(DRIVER_TYPE, getDriverExecutablePath());
|
||||
final ChromeOptions chromeOptions = new ChromeOptions();
|
||||
chromeOptions.addArguments("--headless");
|
||||
return new ChromeDriver(chromeOptions);
|
||||
}
|
||||
|
||||
private String getDriverExecutablePath() {
|
||||
return isEmpty(applicationProperties.getDriverPath())
|
||||
? KiasService.class.getClassLoader()
|
||||
.getResource(String.format(DRIVER_LOCATION, getDriverExecutable(isWindows()))).getFile()
|
||||
: Paths.get(applicationProperties.getDriverPath(), getDriverExecutable(isWindows())).toString();
|
||||
}
|
||||
|
||||
private String getDriverExecutable(boolean isWindows) {
|
||||
return isWindows ? WINDOWS_DRIVER : LINUX_DRIVER;
|
||||
}
|
||||
|
||||
private boolean isWindows() {
|
||||
return System.getProperty("os.name").toLowerCase().contains("windows");
|
||||
}
|
||||
}
|
||||
|
@ -180,6 +180,11 @@ public class UserController extends OdinController<UserListDto, UserDto> {
|
||||
return new Response<>(userService.getActivitiesPings(userId, activity));
|
||||
}
|
||||
|
||||
@PostMapping("/block")
|
||||
public void blockUser(@RequestParam("userId") Integer userId) {
|
||||
userService.blockUser(userId);
|
||||
}
|
||||
|
||||
@GetMapping("/activities")
|
||||
public Response<Map<String, ActivityElement>> getActivitiesList(@RequestParam("userId") Integer userId,
|
||||
@RequestParam("dateFrom") @DateTimeFormat(pattern = "yyyy-MM-dd") Date dateFrom,
|
||||
|
@ -75,6 +75,10 @@ public class UserMvcController extends OdinController<UserListDto, UserDto> {
|
||||
public void getPings() {
|
||||
}
|
||||
|
||||
@GetMapping("/block")
|
||||
public void getBlock() {
|
||||
}
|
||||
|
||||
@GetMapping("/activities")
|
||||
public void getActivities() {
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
package ru.ulstu.user.error;
|
||||
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
|
||||
public class UserBlockedException extends AuthenticationException {
|
||||
public UserBlockedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@ import javax.persistence.Enumerated;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
@ -91,6 +92,10 @@ public class User extends BaseEntity {
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Date birthDate;
|
||||
|
||||
@ManyToOne()
|
||||
@JoinColumn(name = "blocker_id")
|
||||
private User blocker;
|
||||
|
||||
public enum UserDegree {
|
||||
CANDIDATE("Кандидат технических наук"),
|
||||
DOCTOR("Доктор технических наук");
|
||||
@ -229,6 +234,14 @@ public class User extends BaseEntity {
|
||||
this.degree = degree;
|
||||
}
|
||||
|
||||
public User getBlocker() {
|
||||
return blocker;
|
||||
}
|
||||
|
||||
public void setBlocker(User blocker) {
|
||||
this.blocker = blocker;
|
||||
}
|
||||
|
||||
public String getUserAbbreviate() {
|
||||
return String.format(USER_ABBREVIATE_TEMPLATE,
|
||||
lastName == null ? "" : lastName,
|
||||
|
@ -24,6 +24,7 @@ import ru.ulstu.core.model.response.PageableItems;
|
||||
import ru.ulstu.ping.model.Ping;
|
||||
import ru.ulstu.ping.service.PingService;
|
||||
import ru.ulstu.user.error.UserActivationError;
|
||||
import ru.ulstu.user.error.UserBlockedException;
|
||||
import ru.ulstu.user.error.UserEmailExistsException;
|
||||
import ru.ulstu.user.error.UserIdExistsException;
|
||||
import ru.ulstu.user.error.UserIsUndeadException;
|
||||
@ -328,6 +329,9 @@ public class UserService implements UserDetailsService {
|
||||
if (!user.getActivated()) {
|
||||
throw new UserNotActivatedException();
|
||||
}
|
||||
if (user.getBlocker() != null) {
|
||||
throw new UserBlockedException(String.format("Вы заблокированы пользователем %s", user.getBlocker().getUserAbbreviate()));
|
||||
}
|
||||
return new org.springframework.security.core.userdetails.User(user.getLogin(),
|
||||
user.getPassword(),
|
||||
Optional.ofNullable(user.getRoles()).orElse(Collections.emptySet()).stream()
|
||||
@ -430,6 +434,12 @@ public class UserService implements UserDetailsService {
|
||||
return activitiesPings;
|
||||
}
|
||||
|
||||
public void blockUser(int userId) {
|
||||
User userToBlock = findById(userId);
|
||||
userToBlock.setBlocker(getCurrentUser());
|
||||
userRepository.save(userToBlock);
|
||||
}
|
||||
|
||||
public void createActivityTookToWork(List<User> users) {
|
||||
List<Activity> activities = new ArrayList<>();
|
||||
for (User user : users) {
|
||||
|
@ -13,6 +13,8 @@ server.ssl.key-store-password=secret
|
||||
server.ssl.key-password=password
|
||||
# Log settings (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF)
|
||||
logging.level.ru.ulstu=DEBUG
|
||||
#HtmlUnit
|
||||
logging.level.com.gargoylesoftware.htmlunit=ERROR
|
||||
# Mail Settings
|
||||
spring.mail.host=smtp.yandex.ru
|
||||
spring.mail.port=465
|
||||
@ -31,6 +33,7 @@ spring.jpa.hibernate.ddl-auto=validate
|
||||
liquibase.drop-first=false
|
||||
liquibase.enabled=true
|
||||
liquibase.change-log=classpath:db/changelog-master.xml
|
||||
|
||||
# Application Settings
|
||||
ng-tracker.base-url=http://127.0.0.1:8080
|
||||
ng-tracker.undead-user-login=admin
|
||||
|
@ -8,4 +8,22 @@
|
||||
<column name="activity_id" type="integer"/>
|
||||
</addColumn>
|
||||
</changeSet>
|
||||
<changeSet author="orion" id="20190525_000000-2">
|
||||
<modifyDataType tableName="grants"
|
||||
columnName="title"
|
||||
newDataType="text"
|
||||
schemaName="public"/>
|
||||
</changeSet>
|
||||
<changeSet author="orion" id="20190525_000000-3">
|
||||
<modifyDataType tableName="event"
|
||||
columnName="title"
|
||||
newDataType="text"
|
||||
schemaName="public"/>
|
||||
</changeSet>
|
||||
<changeSet author="orion" id="20190525_000000-4">
|
||||
<modifyDataType tableName="event"
|
||||
columnName="description"
|
||||
newDataType="text"
|
||||
schemaName="public"/>
|
||||
</changeSet>
|
||||
</databaseChangeLog>
|
||||
|
12
src/main/resources/db/changelog-20190607_000002-schema.xml
Normal file
12
src/main/resources/db/changelog-20190607_000002-schema.xml
Normal file
@ -0,0 +1,12 @@
|
||||
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
|
||||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
|
||||
<changeSet author="arefyev" id="20190528_000000-3">
|
||||
<addColumn tableName="users">
|
||||
<column name="blocker_id" type="integer"/>
|
||||
</addColumn>
|
||||
<addForeignKeyConstraint baseTableName="users" baseColumnNames="blocker_id" constraintName="fk_blocker"
|
||||
referencedTableName="users" referencedColumnNames="id"/>
|
||||
</changeSet>
|
||||
</databaseChangeLog>
|
@ -53,5 +53,6 @@
|
||||
<include file="db/changelog-20190529_000001-schema.xml"/>
|
||||
<include file="db/changelog-20190601_000001-schema.xml"/>
|
||||
<include file="db/changelog-20190605_000000-schema.xml"/>
|
||||
<include file="db/changelog-20190607_000002-schema.xml"/>
|
||||
<include file="db/changelog-20190603_000002-schema.xml"/>
|
||||
</databaseChangeLog>
|
@ -120,6 +120,20 @@ function resetPassword() {
|
||||
})
|
||||
}
|
||||
|
||||
function blockUser() {
|
||||
userId = $('#userId').val();
|
||||
$.ajax({
|
||||
url:"/api/1.0/users/block?userId=" + userId,
|
||||
contentType: "application/json; charset=utf-8",
|
||||
method: "POST",
|
||||
success: function() {
|
||||
showFeedbackMessage("Пользователь заблокирован", MessageTypesEnum.SUCCESS)
|
||||
},
|
||||
error: function(errorData) {
|
||||
showFeedbackMessage(errorData.responseJSON.error.message, MessageTypesEnum.WARNING)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function isEmailValid(email) {
|
||||
re = /\S+@\S+\.\S+/;
|
||||
|
@ -109,6 +109,7 @@
|
||||
<script src="/js/core.js"></script>
|
||||
<script src="/js/config.js"></script>
|
||||
<script src="/js/odin.js"></script>
|
||||
<script src="/js/users.js"></script>
|
||||
<script th:inline="javascript">
|
||||
|
||||
/*<![CDATA[*/
|
||||
|
21
src/main/resources/templates/users/block.html
Normal file
21
src/main/resources/templates/users/block.html
Normal file
@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorator="default">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container" layout:fragment="content">
|
||||
<section id="services">
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-lg-12 text-center">
|
||||
<h2 class="section-heading text-uppercase">Ваш аккаунт заблокирован</h2>
|
||||
<a href="/"><h3>Вернуться на страницу авторизации</h3></a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -7,6 +7,7 @@
|
||||
<div th:fragment="userDashboard (user)" class="col-12 col-sm-12 col-md-12 col-lg-4 col-xl-3 dashboard-card">
|
||||
<div class="row">
|
||||
<div class="col col-10">
|
||||
<input type="hidden" id="userId" th:value="${user.user.id}"/>
|
||||
<b><p th:text="${user.user.lastName} + ' ' + ${user.user.firstName} + ' ' + ${user.user.patronymic}"></p>
|
||||
</b>
|
||||
<i><p th:if="${user.conference != null}" th:text="'Сейчас на конференции ' + ${user.conference.title}"></p>
|
||||
@ -15,6 +16,7 @@
|
||||
th:text="'Сейчас на паре ' + ${user.lesson.nameOfLesson} + ' в аудитории ' + ${user.lesson.room}"></p>
|
||||
</i>
|
||||
<p th:if="${user.isOnline()}">Онлайн</p>
|
||||
<button onclick="blockUser()">Заблокировать</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -14,7 +14,7 @@ public class GrantPage extends PageObject {
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return driver.findElement(By.id("id")).getAttribute("value");
|
||||
return driver.findElement(By.id("grantId")).getAttribute("value");
|
||||
}
|
||||
|
||||
public void setTitle(String name) {
|
||||
|
231
src/test/java/ru/ulstu/grant/service/GrantServiceTest.java
Normal file
231
src/test/java/ru/ulstu/grant/service/GrantServiceTest.java
Normal file
@ -0,0 +1,231 @@
|
||||
package ru.ulstu.grant.service;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import ru.ulstu.deadline.model.Deadline;
|
||||
import ru.ulstu.deadline.service.DeadlineService;
|
||||
import ru.ulstu.grant.model.Grant;
|
||||
import ru.ulstu.grant.model.GrantDto;
|
||||
import ru.ulstu.grant.repository.GrantRepository;
|
||||
import ru.ulstu.paper.model.Paper;
|
||||
import ru.ulstu.paper.model.PaperDto;
|
||||
import ru.ulstu.paper.service.PaperService;
|
||||
import ru.ulstu.timeline.service.EventService;
|
||||
import ru.ulstu.user.model.User;
|
||||
import ru.ulstu.user.service.UserService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GrantServiceTest {
|
||||
|
||||
@Mock
|
||||
GrantRepository grantRepository;
|
||||
|
||||
@Mock
|
||||
DeadlineService deadlineService;
|
||||
|
||||
@Mock
|
||||
PaperService paperService;
|
||||
|
||||
@Mock
|
||||
UserService userService;
|
||||
|
||||
@Mock
|
||||
EventService eventService;
|
||||
|
||||
@Mock
|
||||
GrantNotificationService grantNotificationService;
|
||||
|
||||
@InjectMocks
|
||||
GrantService grantService;
|
||||
|
||||
private final static Integer ID = 1;
|
||||
private final static Integer INDEX = 0;
|
||||
private final static String TITLE = "Title";
|
||||
private final static String COMMENT = "Comment";
|
||||
private final static boolean TRUE = true;
|
||||
private final static Integer YEAR = 2019;
|
||||
private final static Integer MAX_DISPLAY_SIZE = 50;
|
||||
|
||||
private List<Grant> grants;
|
||||
private List<GrantDto> grantDtos;
|
||||
private List<Deadline> deadlines;
|
||||
private List<Paper> papers;
|
||||
private PaperDto paperDto;
|
||||
private List<PaperDto> paperDtos;
|
||||
private Set<User> authors;
|
||||
private GrantDto grantDto;
|
||||
private Deadline deadline;
|
||||
private User leader;
|
||||
private User author;
|
||||
|
||||
private Grant grantWithId;
|
||||
private Paper paperWithId;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
grants = new ArrayList<>();
|
||||
grantDtos = new ArrayList<>();
|
||||
paperDtos = new ArrayList<>();
|
||||
grantWithId = new Grant();
|
||||
|
||||
deadlines = new ArrayList<>();
|
||||
deadline = new Deadline(new Date(), COMMENT);
|
||||
deadline.setId(ID);
|
||||
deadlines.add(deadline);
|
||||
|
||||
leader = Mockito.mock(User.class);
|
||||
|
||||
papers = new ArrayList<>();
|
||||
paperWithId = new Paper();
|
||||
paperWithId.setId(ID);
|
||||
paperWithId.setTitle(TITLE);
|
||||
papers.add(paperWithId);
|
||||
paperDto = new PaperDto(paperWithId);
|
||||
paperDtos.add(paperDto);
|
||||
|
||||
authors = new HashSet<>();
|
||||
author = leader;
|
||||
authors.add(author);
|
||||
|
||||
grantWithId.setId(ID);
|
||||
grantWithId.setTitle(TITLE);
|
||||
grantWithId.setComment(COMMENT);
|
||||
grantWithId.setDeadlines(deadlines);
|
||||
grantWithId.setLeader(leader);
|
||||
grantWithId.setPapers(papers);
|
||||
grantWithId.setAuthors(authors);
|
||||
|
||||
grants.add(grantWithId);
|
||||
grantDto = new GrantDto(grantWithId);
|
||||
grantDtos.add(grantDto);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getExistGrantById() {
|
||||
when(grantRepository.findOne(ID)).thenReturn(grantWithId);
|
||||
|
||||
GrantDto newGrantDto = new GrantDto(grantWithId);
|
||||
GrantDto result = grantService.getExistGrantById(ID);
|
||||
|
||||
assertEquals(newGrantDto.getId(), result.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() {
|
||||
when(grantRepository.findAll()).thenReturn(grants);
|
||||
|
||||
assertEquals(Collections.singletonList(grantWithId), grantService.findAll());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void create() throws IOException {
|
||||
when(deadlineService.saveOrCreate(new ArrayList<>())).thenReturn(deadlines);
|
||||
when(userService.getUserByLogin("admin")).thenReturn(leader);
|
||||
when(grantRepository.save(new Grant())).thenReturn(grantWithId);
|
||||
|
||||
Grant newGrant = new Grant();
|
||||
newGrant.setId(ID);
|
||||
newGrant.setTitle(TITLE);
|
||||
newGrant.setComment(COMMENT);
|
||||
newGrant.setDeadlines(deadlines);
|
||||
newGrant.setLeader(leader);
|
||||
|
||||
assertEquals(newGrant, grantService.create(grantDto));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getGrantStatuses() {
|
||||
assertEquals(Arrays.asList(Grant.GrantStatus.values()), grantService.getGrantStatuses());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getGrantPapers() {
|
||||
when(paperService.findAllSelect(Collections.singletonList(ID))).thenReturn(paperDtos);
|
||||
|
||||
assertEquals(paperDtos, grantService.getGrantPapers(Collections.singletonList(ID)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllUncompletedPapers() {
|
||||
when(paperService.findAllNotCompleted()).thenReturn(paperDtos);
|
||||
paperDtos.stream()
|
||||
.forEach(paperDto -> {
|
||||
paperDto.setTitle(StringUtils.abbreviate(paperDto.getTitle(), MAX_DISPLAY_SIZE));
|
||||
});
|
||||
|
||||
assertEquals(paperDtos, grantService.getAllUncompletedPapers());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() throws IOException {
|
||||
when(grantRepository.findOne(ID)).thenReturn(grantWithId);
|
||||
assertTrue(grantService.delete(grantWithId.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeDeadline() {
|
||||
GrantDto newGrantDto = new GrantDto();
|
||||
newGrantDto.getRemovedDeadlineIds().add(ID);
|
||||
grantDto.getDeadlines().add(deadline);
|
||||
GrantDto result = grantService.removeDeadline(grantDto, INDEX);
|
||||
|
||||
assertEquals(newGrantDto.getRemovedDeadlineIds(), result.getRemovedDeadlineIds());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findById() {
|
||||
when(grantRepository.findOne(ID)).thenReturn(grantWithId);
|
||||
Grant findGrant = grantService.findById(ID);
|
||||
|
||||
assertEquals(grantWithId.getId(), findGrant.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void attachPaper() {
|
||||
when(grantRepository.findOne(ID)).thenReturn(grantWithId);
|
||||
when(paperService.findAllSelect(Collections.singletonList(ID))).thenReturn(paperDtos);
|
||||
GrantDto newGrantDto = new GrantDto(grantWithId);
|
||||
|
||||
if (!newGrantDto.getPaperIds().isEmpty()) {
|
||||
newGrantDto.getPapers().clear();
|
||||
newGrantDto.setPapers(paperDtos);
|
||||
} else {
|
||||
newGrantDto.getPapers().clear();
|
||||
}
|
||||
|
||||
assertEquals(newGrantDto.getPapers(), grantService.attachPaper(grantDto));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterEmptyDeadlines() {
|
||||
when(grantRepository.findOne(ID)).thenReturn(grantWithId);
|
||||
GrantDto newGrantDto = new GrantDto(grantWithId);
|
||||
|
||||
newGrantDto.setDeadlines(newGrantDto.getDeadlines().stream()
|
||||
.filter(dto -> dto.getDate() != null || !StringUtils.isEmpty(dto.getDescription()))
|
||||
.collect(Collectors.toList()));
|
||||
|
||||
assertEquals(newGrantDto.getDeadlines(), grantService.filterEmptyDeadlines(grantDto));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user