Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.javaclub.base.web.SysUserController Maven / Gradle / Ivy
package com.github.javaclub.base.web;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.javaclub.Constants.EnableStatus;
import com.github.javaclub.base.annotation.SysLog;
import com.github.javaclub.base.annotation.WithApiResult;
import com.github.javaclub.base.domain.AdminUser;
import com.github.javaclub.base.domain.query.AdminUserQuery;
import com.github.javaclub.base.service.AdminUserService;
import com.github.javaclub.base.service.SysRoleService;
import com.github.javaclub.base.utils.ConfigUtils;
import com.github.javaclub.base.utils.SecurityUtils;
import com.github.javaclub.sword.BizException;
import com.github.javaclub.sword.annotation.swagger.ApiRequestObject;
import com.github.javaclub.sword.annotation.swagger.ApiRequestProperty;
import com.github.javaclub.sword.core.BizObjects;
import com.github.javaclub.sword.core.Numbers;
import com.github.javaclub.sword.core.Strings;
import com.github.javaclub.sword.domain.QueryResult;
import com.github.javaclub.sword.domain.enumtype.BasicMessage;
import com.github.javaclub.sword.web.PageResultSet;
import com.github.javaclub.toolbox.conf.CompositeAppConfigProperties;
import com.github.javaclub.toolbox.enumtype.ActionType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
@RestController
@RequestMapping("/sys/user")
@WithApiResult
@Api(tags = "管理端: 账号管理")
@AllArgsConstructor
public class SysUserController {
private final AdminUserService adminUserService;
private final SysRoleService sysRoleService;
@PostMapping("/page")
@PreAuthorize("@pms.hasPermission('sys:user:page')")
@ApiOperation(value = "账号分页列表")
@ApiImplicitParams({
@ApiImplicitParam(name = "token", value = "鉴权token", paramType = "header", dataType = "string", required = true)
})
@SysLog(value = "账号列表查询", actionType = ActionType.QUERY)
public PageResultSet queryList(@RequestBody AdminUserQuery query) {
query.setQueryManageList(true);
QueryResult qr = adminUserService.findListWithCount(query);
if (!qr.isSuccess()) {
throw new BizException("查询列表失败!");
}
return PageResultSet.build(query.getPageNo(), query.getPageSize(), qr.getTotalCount(), qr.getEntry());
}
@SysLog(value = "查询账号信息", actionType = ActionType.QUERY)
@GetMapping("/info")
@PreAuthorize("@pms.hasPermission('sys:user:info')")
@ApiOperation(value = "查询账号信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "token", value = "鉴权token", paramType = "header", dataType = "string", required = true),
@ApiImplicitParam(name = "userId", value = "用户ID", paramType = "query", dataType = "long", required = true)
})
public AdminUser info(Long userId) {
AdminUser user = adminUserService.selectById((Long) BizObjects.requireNotNullGtZero(userId, "用户ID参数错误!"));
if (null != user) {
user.setPassword(null);
//获取用户所属的角色列表
List roleIdList = sysRoleService.listRoleIdByUserId(userId);
user.setRoleIdList(roleIdList);
}
return user;
}
@SysLog(value = "添加系统账号", actionType = ActionType.ADD)
@PostMapping("/add")
@PreAuthorize("@pms.hasPermission('sys:user:add')")
@ApiOperation(value = "添加账号")
@ApiImplicitParams({
@ApiImplicitParam(name = "token", value = "鉴权token", paramType = "header", dataType = "string", required = true)
})
public Boolean add(@RequestBody AdminUser user) {
AdminUser loginUser = checkAdminUser();
String username = user.getUsername();
BizObjects.requireTrue(Strings.isUsername(username), "用户名只能由英文大小写字母/数字/下划线组成!");
BizObjects.requireTrue(username.length() < 30, "用户名长度须控制在30字符以内!");
BizObjects.requireTrue(null != user.getPassword() && user.getPassword().trim().length() >= 6, "密码必须6位字符以上!");
if (null != user.getName()) {
BizObjects.requireTrue(user.getName().length() < 20, "姓名长度须控制在20字符以内!");
}
AdminUser dbUser = adminUserService.selectByUsername(username.trim());
if (dbUser != null) {
throw new BizException(BasicMessage.USERNAME_ALREADY_EXIST);
}
user.setPassword(ConfigUtils.generatePasswordMD5(user.getPassword()));
user.setCreatorId(loginUser.getId());
adminUserService.saveUserAndUserRole(user);
return true;
}
@SysLog(value = "修改系统账号", actionType = ActionType.UPDATE)
@PostMapping("/update")
@PreAuthorize("@pms.hasPermission('sys:user:update')")
@ApiOperation(value = "修改账号")
@ApiImplicitParams({
@ApiImplicitParam(name = "token", value = "鉴权token", paramType = "header", dataType = "string", required = true)
})
public Boolean update(@RequestBody AdminUser user) {
BizObjects.requireNotNullGtZero(user.getId(), "用户主键ID不能为空!");
AdminUser dbUser = adminUserService.selectById(user.getId());
if (null == dbUser) {
throw new BizException(BasicMessage.NO_USER_MATCHED);
}
AdminUser dbUserNameInfo = adminUserService.selectByUsername(user.getUsername());
if (dbUserNameInfo != null && !Objects.equals(dbUserNameInfo.getId(), user.getId())) {
throw new BizException(BasicMessage.USERNAME_ALREADY_EXIST);
}
// 更新不修改密码,使用 "密码重置" 功能
user.setPassword(null);
// user.setPassword(Strings.isBlank(password) ? null : DomainUtils.generatePasswordMD5(password));
if ("0,1".contains(user.getId().toString())
&& Objects.equals(EnableStatus.DISABLED, user.getStatus())) {
throw new BizException("超管不可以被禁用!");
}
adminUserService.updateUserAndUserRole(user);
return true;
}
@SysLog(value = "为账号重置密码", actionType = ActionType.UPDATE)
@PostMapping("/resetPassword")
@PreAuthorize("@pms.hasPermission('sys:user:reset_password')")
@ApiOperation(value = "重置密码")
@ApiImplicitParams({
@ApiImplicitParam(name = "token", value = "鉴权token", paramType = "header", dataType = "string", required = true)
})
@ApiRequestObject(name = "ResetUserPassword", description = "重置密码请求参数",
properties = {
@ApiRequestProperty(name = "userId", description = "账号主键ID", type = "long", required = true),
@ApiRequestProperty(name = "password", description = "新密码", type = "string", required = true),
@ApiRequestProperty(name = "passwordAgain", description = "新密码重复", type = "string", required = true)
}
)
public Boolean resetPassword(@RequestBody Map param) {
Long userId = Numbers.parseLong(Objects.toString(param.get("userId"), Strings.EMPTY_STRING));
BizObjects.requireNotNullGtZero(userId, "用户主键ID不能为空!");
boolean pwdRequired = Strings.areNotBlank(Objects.toString(param.get("password"), Strings.EMPTY_STRING),
Objects.toString(param.get("passwordAgain"), Strings.EMPTY_STRING));
BizObjects.requireTrue(pwdRequired, "新密码不能为空!");
AdminUser dbUser = adminUserService.selectById(userId);
if (null == dbUser) {
throw new BizException(BasicMessage.NO_USER_MATCHED);
}
String password = Objects.toString(param.get("password"));
dbUser.setPassword(ConfigUtils.generatePasswordMD5(password));
return adminUserService.updateById(dbUser);
}
@PostMapping(value = "/enable")
@ApiOperation(value = "启用操作")
@ApiImplicitParams({
@ApiImplicitParam(name = "token", value = "鉴权token", paramType = "header", dataType = "string", required = true),
@ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", paramType = "query", required = true)
})
@PreAuthorize("@pms.hasPermission('sys:user:enable')")
@SysLog(value = "启用账号", actionType = ActionType.ENABLE)
public Boolean enable(Long id) throws Exception {
BizObjects.requireNotNullGtZero(id, "主键ID不能为空!");
AdminUser entity = new AdminUser(id, EnableStatus.ENABLED);
return adminUserService.updateUserStatus(entity);
}
@PostMapping(value = "/disable")
@ApiOperation(value = "禁用操作")
@ApiImplicitParams({
@ApiImplicitParam(name = "token", value = "鉴权token", paramType = "header", dataType = "string", required = true),
@ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", paramType = "query", required = true)
})
@PreAuthorize("@pms.hasPermission('sys:user:disable')")
@SysLog(value = "禁用账号", actionType = ActionType.DISABLE)
public Boolean disable(Long id) throws Exception {
BizObjects.requireNotNullGtZero(id, "主键ID不能为空!");
AdminUser entity = new AdminUser(id, EnableStatus.DISABLED);
return adminUserService.updateUserStatus(entity);
}
@PostMapping("/delete")
@ApiOperation(value = "删除操作")
@ApiImplicitParams({
@ApiImplicitParam(name = "token", value = "鉴权token", paramType = "header", dataType = "string", required = true),
@ApiImplicitParam(name = "userId", value = "用户ID", paramType = "query", dataType = "long", required = true)
})
@PreAuthorize("@pms.hasPermission('sys:user:delete')")
@SysLog(value = "删除用户", actionType = ActionType.DELETE)
public Boolean delete(Long userId) {
AdminUser user = checkAdminUser();
Long uid = (Long) BizObjects.requireNotNullGtZero(userId, BasicMessage.NO_USER_SELECTED);
if ("0,1".contains(String.valueOf(userId))) {
throw new BizException(BasicMessage.SUPER_CANNOT_DELETE);
}
// 企图删除当前登录用户
if (Objects.equals(uid, user.getId())) {
throw new BizException(BasicMessage.USER_CANNOT_DELETE);
}
return adminUserService.removeById(uid);
}
@PostMapping("/deleteByIds")
@ApiOperation(value = "批量删除")
@ApiImplicitParams({
@ApiImplicitParam(name = "token", value = "鉴权token", paramType = "header", dataType = "string", required = true)
})
@PreAuthorize("@pms.hasPermission('sys:user:batch_delete')")
@SysLog(value = "批量删除用户", actionType = ActionType.BATCH_DELETE)
public Boolean deleteByIds(@RequestBody List userIds) {
AdminUser user = checkAdminUser();
if (BizObjects.isEmpty(userIds)) {
throw new BizException(BasicMessage.NO_USER_SELECTED);
}
Long superId = CompositeAppConfigProperties.getInstance().longValue("system.configs.super-admin-id", 1L);
if (BizObjects.contains(userIds.toArray(new Long[0]), superId)) {
throw new BizException(BasicMessage.SUPER_CANNOT_DELETE);
}
// 企图删除当前登录用户
if (BizObjects.contains(userIds.toArray(new Long[0]), user.getId())) {
throw new BizException(BasicMessage.USER_CANNOT_DELETE);
}
return adminUserService.removeByIds(userIds);
}
AdminUser checkAdminUser() {
return BizObjects.requireNotNull(SecurityUtils.getAdminUser(), BasicMessage.LOGIN_INVALID);
}
}