Search in sources :

Example 6 with User

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

the class ResetPasswordController method changePassword.

@PostMapping(value = "/resetPassword", params = "action=changePassword")
public void changePassword(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "u", required = false) String userId, @RequestParam(value = "vc", required = false) String vcode) throws IOException {
    response.setContentType("text/javascript;charset=UTF-8");
    PrintWriter out = response.getWriter();
    Optional<User> optional = resetPasswordService.checkVcode(userId, vcode);
    if (!optional.isPresent()) {
        out.print("alert(\"效链接已失效,请重新获取链接\");");
        return;
    }
    String newPassword = request.getParameter("newPassword");
    ValueCheck.checkPassword(newPassword);
    User user = optional.get();
    userMapper.updateSelective(user.getId(), User.builder().password(passwordEncoder.encode(newPassword)).build());
    resetPasswordService.remove(userId);
    out.print("alert(\"密码修改成功!\");");
    out.print("document.location='" + request.getContextPath() + "'");
}
Also used : User(cn.edu.zjnu.acm.judge.domain.User) PrintWriter(java.io.PrintWriter) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 7 with User

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

the class SearchUserController method searchuser.

@GetMapping("/searchuser")
public String searchuser(Model model, @RequestParam(value = "user_id", defaultValue = "") String keyword, @RequestParam(value = "position", required = false) String position, @PageableDefault(1000) Pageable pageable) {
    if (keyword.replace("%", "").length() < 2) {
        throw new BusinessException(BusinessCode.USER_SEARCH_KEYWORD_SHORT);
    }
    String like = keyword;
    if (position == null) {
        like = "%" + like + "%";
    } else if (position.equalsIgnoreCase("begin")) {
        like += "%";
    } else {
        like = "%" + like;
    }
    Pageable t = pageable;
    if (t.getSort() == null) {
        t = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), new Sort(new Sort.Order("solved").with(Sort.Direction.DESC), new Sort.Order("submit")));
    }
    Page<User> users = accountService.findAll(AccountForm.builder().disabled(Boolean.FALSE).query(like).build(), t);
    model.addAttribute("query", keyword);
    model.addAttribute("users", users);
    return "users/search";
}
Also used : BusinessException(cn.edu.zjnu.acm.judge.exception.BusinessException) PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) User(cn.edu.zjnu.acm.judge.domain.User) Sort(org.springframework.data.domain.Sort) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 8 with User

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

the class UserDetailsServiceImpl method loadUserByUsername.

@Nonnull
@Override
public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException {
    User user = userMapper.findOne(userId);
    if (user == null) {
        throw new UsernameNotFoundException(userId);
    }
    int role = 0;
    for (String rightstr : userRoleMapper.findAllByUserId(user.getId())) {
        if (rightstr != null) {
            switch(rightstr.toLowerCase()) {
                case "administrator":
                    role = 2;
                    break;
                case "source_browser":
                    role = Math.max(role, 1);
                    break;
                case "news_publisher":
                    // TODO
                    break;
                default:
                    break;
            }
        }
    }
    return org.springframework.security.core.userdetails.User.withUsername(userId).password(user.getPassword()).authorities(ROLES.get(role)).build();
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(cn.edu.zjnu.acm.judge.domain.User) Nonnull(javax.annotation.Nonnull)

Example 9 with User

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

the class ModifyUserController method update.

@PostMapping("/modifyuser")
@SuppressWarnings("AssignmentToMethodParameter")
public String update(Model model, @RequestParam("oldPassword") String oldPassword, @RequestParam("newPassword") String newPassword, @RequestParam("rptPassword") String rptPassword, @RequestParam("email") String email, @RequestParam("nick") String nick, @RequestParam("school") String school, Authentication authentication) {
    if (!Objects.equals(newPassword, rptPassword)) {
        throw new MessageException("Passwords are not match");
    }
    String userId = authentication.getName();
    User user = accountService.findOne(userId);
    String password = user.getPassword();
    if (!passwordEncoder.matches(oldPassword, password)) {
        throw new MessageException("password is not correct");
    }
    if (StringUtils.isEmpty(nick)) {
        nick = userId;
    }
    if (StringUtils.isEmpty(newPassword)) {
        newPassword = oldPassword;
    } else {
        ValueCheck.checkPassword(newPassword);
    }
    if (StringUtils.hasText(email)) {
        ValueCheck.checkEmail(email);
    } else {
        email = "";
    }
    ValueCheck.checkNick(nick);
    user = User.builder().id(userId).email(email).nick(nick).password(newPassword).school(school).build();
    accountService.updateSelective(userId, user);
    model.addAttribute("user", user);
    return "modifyusersuccess";
}
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 10 with User

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

the class UserStatusController method userStatus.

@GetMapping("/userstatus")
@SuppressWarnings("AssignmentToMethodParameter")
public String userStatus(Model model, @RequestParam(value = "size", defaultValue = "3") int display, @RequestParam(value = "user_id", required = false) String userId) {
    User user = accountService.findOne(userId);
    display = Math.max(Math.min(display, 9), 1);
    userId = user.getId();
    long rank = userMapper.rank(userId) + 1;
    long rankFirst = Math.max(rank - display, 1);
    List<User> neighbours = userMapper.neighbours(userId, display);
    List<Long> solvedProblems = userProblemMapper.findAllSolvedByUserId(userId);
    model.addAttribute("neighbours", neighbours);
    model.addAttribute("solvedProblems", solvedProblems);
    model.addAttribute("rankFirst", rankFirst);
    model.addAttribute("user", user);
    model.addAttribute("rank", rank);
    log.debug("rankFirst = {}", rankFirst);
    return "users/status";
}
Also used : User(cn.edu.zjnu.acm.judge.domain.User) GetMapping(org.springframework.web.bind.annotation.GetMapping)

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