Skip to content

Commit

Permalink
feat: reset group members' password
Browse files Browse the repository at this point in the history
  • Loading branch information
kastnerorz committed Nov 22, 2019
1 parent bff3188 commit 9c5493b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/cn/kastner/oj/controller/GroupRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
import cn.kastner.oj.query.GroupQuery;
import cn.kastner.oj.security.JwtUser;
import cn.kastner.oj.service.GroupService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@RestController
Expand Down Expand Up @@ -105,4 +109,19 @@ public List<JwtUser> deleteMembers(
}
return groupService.deleteMembers(id, usersId);
}

@PostMapping(value = "/{id}/members/resetPassword")
@PreAuthorize("hasAnyRole('ADMIN', 'STUFF')")
public void resetMemberPassword(@PathVariable String id, HttpServletResponse response) throws GroupException {
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment");

Workbook workbook = groupService.resetMembersPassword(id);
try (OutputStream os = response.getOutputStream()) {
workbook.write(os);
} catch (IOException e) {
throw new GroupException(GroupException.RESET_ERROR);
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/cn/kastner/oj/exception/GroupException.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class GroupException extends AppException {
public static final String NO_SUCH_GROUP = "没有该群组";
public static final String HAVE_SUCH_GROUP = "该群组名已存在";
public static final String HAS_BEEN_GENERATED = "已经批量生成过了";
public static final String RESET_ERROR = "重置失败";

public GroupException(String message) {
super(message);
Expand All @@ -23,6 +24,10 @@ public GroupException(String message) {
this.code = -4;
this.status = HttpStatus.BAD_REQUEST;
break;
case RESET_ERROR:
this.code = -5;
this.status = HttpStatus.INTERNAL_SERVER_ERROR;
break;
default:
this.code = -1;
this.status = HttpStatus.INTERNAL_SERVER_ERROR;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/cn/kastner/oj/service/GroupService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cn.kastner.oj.exception.UserException;
import cn.kastner.oj.query.GroupQuery;
import cn.kastner.oj.security.JwtUser;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.List;

Expand All @@ -26,4 +27,6 @@ public interface GroupService {
List<JwtUser> addMembers(String id, List<String> usersId) throws UserException, GroupException, AuthorizationException;

List<JwtUser> deleteMembers(String id, List<String> userId) throws GroupException, UserException, AuthorizationException;

Workbook resetMembersPassword(String id) throws GroupException;
}
31 changes: 31 additions & 0 deletions src/main/java/cn/kastner/oj/service/impl/GroupServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import cn.kastner.oj.service.GroupService;
import cn.kastner.oj.util.CommonUtil;
import cn.kastner.oj.util.DTOMapper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -235,6 +239,33 @@ public List<JwtUser> deleteMembers(String id, List<String> usersId)
return JwtUserFactory.createList(userSet);
}

@Override
public Workbook resetMembersPassword(String id) throws GroupException {
Group group =
groupRepository
.findById(id)
.orElseThrow(() -> new GroupException(GroupException.NO_SUCH_GROUP));
Set<User> userSet = group.getUserSet();
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("用户密码");
int rowNum = 0;
Row header = sheet.createRow(rowNum++);
header.createCell(0).setCellValue("Username");
header.createCell(1).setCellValue("学号");
header.createCell(2).setCellValue("姓名");
header.createCell(3).setCellValue("密码");
for (User user : userSet) {
String randomPassword = CommonUtil.generateStr(8);
user.setPassword(randomPassword);
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(user.getUsername());
row.createCell(1).setCellValue(user.getStudentNumber());
row.createCell(2).setCellValue(user.getName());
row.createCell(3).setCellValue(randomPassword);
}
return workbook;
}

private void authorize(Group group) throws AuthorizationException {
User user = UserContext.getCurrentUser();
if (!user.isAdmin() && !user.equals(group.getAuthor())) {
Expand Down

0 comments on commit 9c5493b

Please sign in to comment.