Search in sources :

Example 1 with User

use of cn.edu.zjnu.acm.judge.domain.User in project judge by zjnu-acm.

the class SecurityConfigurationTest method testLogin.

/**
 * Test of login method, of class SecurityConfiguration.
 *
 * @see SecurityConfiguration#configure(HttpSecurity)
 */
@Test
public void testLogin() throws Exception {
    log.info("login");
    String url = "/test";
    User user = mockDataService.user();
    MvcResult result = mvc.perform(post("/login").param("user_id1", user.getId()).param("password1", user.getPassword()).param("url", url)).andExpect(status().isFound()).andExpect(redirectedUrl(url)).andReturn();
}
Also used : User(cn.edu.zjnu.acm.judge.domain.User) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with User

use of cn.edu.zjnu.acm.judge.domain.User in project judge by zjnu-acm.

the class MockDataService method user.

@Nonnull
public User user(boolean create) {
    String userId = idGenerator.apply("user");
    User user = User.builder().id(userId).password(userId).school("").nick(userId).build();
    if (create) {
        accountService.save(user);
    }
    return user;
}
Also used : User(cn.edu.zjnu.acm.judge.domain.User) Nonnull(javax.annotation.Nonnull)

Example 3 with User

use of cn.edu.zjnu.acm.judge.domain.User in project judge by zjnu-acm.

the class ModifyUserController method updatePage.

@GetMapping({ "/modifyuserpage", "/modifyuser" })
public String updatePage(Model model, Authentication authentication) {
    String userId = authentication != null ? authentication.getName() : null;
    User user = accountService.findOne(userId);
    model.addAttribute("user", user);
    return "users/edit";
}
Also used : User(cn.edu.zjnu.acm.judge.domain.User) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 4 with User

use of cn.edu.zjnu.acm.judge.domain.User in project judge by zjnu-acm.

the class RegisterController method register.

@PostMapping("/register")
@SuppressWarnings("AssignmentToMethodParameter")
public String register(HttpServletRequest request, @RequestParam("user_id") String userId, @RequestParam("school") String school, @RequestParam("nick") String nick, @RequestParam("password") String password, @RequestParam("email") String email, @RequestParam("rptPassword") String rptPassword) {
    contestOnlyService.checkRegister();
    ValueCheck.checkUserId(userId);
    ValueCheck.checkPassword(password);
    ValueCheck.checkEmail(email);
    if (!Objects.equals(password, rptPassword)) {
        throw new MessageException("Passwords are not match");
    }
    if (StringUtils.hasText(nick)) {
        nick = nick.trim();
    } else {
        nick = userId;
    }
    ValueCheck.checkNick(nick);
    if (userMapper.findOne(userId) != null) {
        throw new MessageException("The ID( " + userId + ") existed");
    }
    User user = User.builder().id(userId).password(passwordEncoder.encode(password)).email(StringUtils.isEmpty(email) ? null : email).nick(nick).school(school).ip(request.getRemoteAddr()).build();
    userMapper.save(user);
    log.info("{}", user);
    request.setAttribute("user", user);
    return "register";
}
Also used : User(cn.edu.zjnu.acm.judge.domain.User) MessageException(cn.edu.zjnu.acm.judge.exception.MessageException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 5 with User

use of cn.edu.zjnu.acm.judge.domain.User in project judge by zjnu-acm.

the class ResetPasswordController method doPost.

@PostMapping("/resetPassword")
public void doPost(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "action", required = false) String action, @RequestParam(value = "verify", required = false) String verify, @RequestParam(value = "username", required = false) String username, Locale locale) throws IOException {
    response.setContentType("text/javascript;charset=UTF-8");
    PrintWriter out = response.getWriter();
    HttpSession session = request.getSession(false);
    String word = null;
    if (session != null) {
        word = (String) session.getAttribute("word");
        session.removeAttribute("word");
    }
    if (word == null || !word.equalsIgnoreCase(verify)) {
        out.print("alert('验证码错误');");
        return;
    }
    User user = userMapper.findOne(username);
    if (user == null) {
        out.print("alert('用户不存在');");
        return;
    }
    String email = user.getEmail();
    if (email == null || !email.toLowerCase().matches(ValueCheck.EMAIL_PATTERN)) {
        out.print("alert('该用户未设置邮箱或邮箱格式不对,如需重置密码,请联系管理员!');");
        return;
    }
    try {
        String vc = resetPasswordService.getOrCreate(user.getId());
        String url = getPath(request, "/resetPassword.html?vc=", vc + "&u=", user.getId());
        String title = systemService.getResetPasswordTitle();
        Map<String, Object> map = ImmutableMap.of("url", url, "title", Objects.toString(title, ""));
        String content = templateEngine.process("users/password", new Context(locale, map));
        emailService.send(email, title, content);
    } catch (MailException | MessagingException ex) {
        log.error("fail to send email", ex);
        out.print("alert('邮件发送失败,请稍后再试')");
        return;
    }
    out.print("alert('已经将邮件发送到" + user.getEmail() + ",请点击链接重设密码');");
}
Also used : Context(org.thymeleaf.context.Context) User(cn.edu.zjnu.acm.judge.domain.User) MessagingException(javax.mail.MessagingException) HttpSession(javax.servlet.http.HttpSession) MailException(org.springframework.mail.MailException) PrintWriter(java.io.PrintWriter) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

User (cn.edu.zjnu.acm.judge.domain.User)14 Test (org.junit.Test)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 PostMapping (org.springframework.web.bind.annotation.PostMapping)4 MvcResult (org.springframework.test.web.servlet.MvcResult)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 MessageException (cn.edu.zjnu.acm.judge.exception.MessageException)2 PrintWriter (java.io.PrintWriter)2 Nonnull (javax.annotation.Nonnull)2 BusinessException (cn.edu.zjnu.acm.judge.exception.BusinessException)1 MessagingException (javax.mail.MessagingException)1 HttpSession (javax.servlet.http.HttpSession)1 PageRequest (org.springframework.data.domain.PageRequest)1 Pageable (org.springframework.data.domain.Pageable)1 Sort (org.springframework.data.domain.Sort)1 MailException (org.springframework.mail.MailException)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1 WithMockUser (org.springframework.security.test.context.support.WithMockUser)1 Context (org.thymeleaf.context.Context)1