| | |
| | | package com.sandu.ximon.admin.controller; |
| | | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.core.map.MapBuilder; |
| | | import cn.hutool.core.map.MapUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.sandu.common.domain.ResponseVO; |
| | | import com.sandu.common.enums.AdminStatusStatus; |
| | | import com.sandu.common.enums.ResponseStatusEnums; |
| | | import com.sandu.common.enums.RoleLevelStatus; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.log.Log; |
| | | import com.sandu.common.object.BaseConditionVO; |
| | | import com.sandu.common.redis.RedisService; |
| | | import com.sandu.common.security.LoginUserInfo; |
| | | import com.sandu.common.security.annotation.AnonymousAccess; |
| | | import com.sandu.common.security.config.SecurityProperties; |
| | | import com.sandu.common.security.token.TokenProvider; |
| | | import com.sandu.common.util.IpUtil; |
| | | import com.sandu.common.util.ResponseUtil; |
| | | import com.sandu.ximon.admin.dto.AdminDto; |
| | | import com.sandu.ximon.admin.dto.ClientDto; |
| | | import com.sandu.ximon.admin.param.AdminLoginParam; |
| | | import com.sandu.ximon.admin.param.AdminParam; |
| | | import com.sandu.ximon.admin.param.PwdParam; |
| | | import com.sandu.ximon.admin.param.ReceiveParam; |
| | | import com.sandu.ximon.admin.param.*; |
| | | import com.sandu.ximon.admin.security.PermissionConfig; |
| | | import com.sandu.ximon.admin.security.SecurityUtils; |
| | | import com.sandu.ximon.admin.service.*; |
| | | import com.sandu.ximon.admin.utils.RedisUtils; |
| | | import com.sandu.ximon.dao.bo.MenuNode; |
| | | import com.sandu.ximon.dao.domain.*; |
| | | import com.sandu.ximon.dao.enums.AdministratorEnums; |
| | |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | import java.util.Collection; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | private final TokenProvider tokenProvider; |
| | | private final AdminRoleRelationService adminRoleRelationService; |
| | | private PermissionConfig permissionConfig; |
| | | private final RedisService redisService; |
| | | |
| | | @Log("后台用户登录") |
| | | @AnonymousAccess |
| | | @PostMapping(value = "/login") |
| | | public ResponseVO<Object> login(@RequestBody AdminLoginParam loginParam) { |
| | | public ResponseVO<Object> login(HttpServletRequest request, @RequestBody AdminLoginParam loginParam) { |
| | | Admin admin = adminService.getOne(Wrappers.lambdaQuery(Admin.class).eq(Admin::getUsername, loginParam.getUsername()).last("limit 1")); |
| | | if (admin == null) { |
| | | return ResponseUtil.error(ResponseStatusEnums.BAD_CREDENTIALS.getCode(), ResponseStatusEnums.BAD_CREDENTIALS.getMessage()); |
| | | } |
| | | String s = RedisUtils.getBean().get(loginParam.getKey()); |
| | | if (!loginParam.getVlue().equals(s)) { |
| | | log.error(loginParam.getVlue() + "验证码"); |
| | | return ResponseUtil.error(ResponseStatusEnums.BAD_AUTHENTICATION.getCode(), ResponseStatusEnums.BAD_AUTHENTICATION.getMessage()); |
| | | } |
| | | RedisUtils.getBean().delete(loginParam.getKey()); |
| | | if (!passwordEncoder.matches(loginParam.getPassword(), admin.getPassword())) { |
| | | return ResponseUtil.error(ResponseStatusEnums.BAD_CREDENTIALS.getCode(), ResponseStatusEnums.BAD_CREDENTIALS.getMessage()); |
| | | } |
| | |
| | | loginUserInfo.setAccount(admin.getUsername()); |
| | | loginUserInfo.setStatus(admin.getStatus()); |
| | | loginUserInfo.setIp(IpUtil.getRealIp()); |
| | | loginUserInfo.setAdministratorType(AdministratorEnums.ADMIN.getCode()); |
| | | |
| | | //确定用户角色权限 |
| | | AdminRoleRelation adminRoleRelation = adminRoleRelationService.getOne(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId, loginUserInfo.getUserId())); |
| | | Role role = roleService.getOne(Wrappers.lambdaQuery(Role.class).eq(Role::getId, adminRoleRelation.getRoleId())); |
| | | if (role != null && role.getLevel().equals(RoleLevelStatus.SUPER.getCode())) { |
| | | loginUserInfo.setAdministratorType(AdministratorEnums.ADMIN.getCode()); |
| | | } else { |
| | | loginUserInfo.setAdministratorType(AdministratorEnums.CUSTOMER.getCode()); |
| | | } |
| | | |
| | | loginUserInfo.setPermission(grantedAuthorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.joining(","))); |
| | | |
| | | // 生成令牌 |
| | |
| | | // @Log("后台用户信息") |
| | | @GetMapping(value = "/info") |
| | | public ResponseVO<Object> getUserInfo() { |
| | | System.out.println(SecurityUtils.getUserDetails().getAdministratorType() + " ---------"); |
| | | Long userId = SecurityUtils.getUserId(); |
| | | List<Role> roles; |
| | | MapBuilder<Object, Object> builder; |
| | |
| | | return ResponseUtil.success(builder.build()); |
| | | } |
| | | |
| | | @GetMapping(value = "/logout") |
| | | public ResponseVO<Object> LogOut() { |
| | | // redisService.set(UserModel.USER_INFO.key(SecurityUtils.getUserId().toString() + SecurityUtils.getUserDetails().getAdministratorType()), null); |
| | | // redisService.set(UserModel.USER_TOKEN.key(UserModel.USER_TOKEN.key(SecurityUtils.getUserDetails().getToken())), null); |
| | | |
| | | |
| | | String key = String.format("%d_%d", SecurityUtils.getUserDetails().getUserId(), SecurityUtils.getUserDetails().getAdministratorType()); |
| | | redisService.set(key, null); |
| | | return ResponseUtil.success("退出登录成功"); |
| | | } |
| | | |
| | | private MenuNode covertMenuNode(Menu menu, List<Menu> menuList) { |
| | | MenuNode node = new MenuNode(); |
| | | BeanUtils.copyProperties(menu, node); |
| | |
| | | if (!permissionConfig.check(MenuEnum.ADMIN_LIST.getCode())) { |
| | | return ResponseUtil.fail("缺少对应用户权限"); |
| | | } |
| | | PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize()); |
| | | LambdaQueryWrapper<Admin> wrapper = Wrappers.lambdaQuery(Admin.class); |
| | | if (keyword != null) { |
| | | wrapper.like(Admin::getNickName, keyword) |
| | | .or(adminLambdaQueryWrapper -> { |
| | | adminLambdaQueryWrapper.like(Admin::getUsername, keyword); |
| | | }); |
| | | } |
| | | List<Admin> list = adminService.list(wrapper); |
| | | return ResponseUtil.successPage(adminService.listAdmin(baseConditionVO, keyword)); |
| | | |
| | | List<AdminDto> adminDtoList = new ArrayList<>(); |
| | | for (Admin demo : list) { |
| | | AdminDto adminDto = new AdminDto(); |
| | | BeanUtil.copyProperties(demo, adminDto); |
| | | AdminRoleRelation one = adminRoleRelationService.getOne(Wrappers.lambdaQuery(AdminRoleRelation.class) |
| | | .eq(AdminRoleRelation::getAdminId, demo.getId())); |
| | | |
| | | if (one != null) { |
| | | adminDto.setRoleId(one.getRoleId()); |
| | | } |
| | | adminDtoList.add(adminDto); |
| | | |
| | | } |
| | | return ResponseUtil.successPage(adminDtoList); |
| | | // PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize()); |
| | | // LambdaQueryWrapper<Admin> wrapper = Wrappers.lambdaQuery(Admin.class); |
| | | // if (keyword != null) { |
| | | // wrapper.like(Admin::getNickName, keyword) |
| | | // .or(adminLambdaQueryWrapper -> { |
| | | // adminLambdaQueryWrapper.like(Admin::getUsername, keyword); |
| | | // }); |
| | | // } |
| | | // List<Admin> list = adminService.list(wrapper); |
| | | // |
| | | // List<AdminDto> adminDtoList = new ArrayList<>(); |
| | | // for (Admin demo : list) { |
| | | // AdminDto adminDto = new AdminDto(); |
| | | // BeanUtil.copyProperties(demo, adminDto); |
| | | // AdminRoleRelation one = adminRoleRelationService.getOne(Wrappers.lambdaQuery(AdminRoleRelation.class) |
| | | // .eq(AdminRoleRelation::getAdminId, demo.getId())); |
| | | // |
| | | // if (one != null) { |
| | | // adminDto.setRoleId(one.getRoleId()); |
| | | // } |
| | | // adminDtoList.add(adminDto); |
| | | // |
| | | // } |
| | | // return ResponseUtil.successPage(adminDtoList); |
| | | } |
| | | |
| | | @Log("后台用户注册") |
| | | @PostMapping("/register") |
| | | public ResponseVO<Object> register(@Validated @RequestBody AdminParam param) { |
| | | if (!permissionConfig.check(MenuEnum.ADMIN_ADD.getCode())) { |
| | | return ResponseUtil.fail("缺少对应用户权限"); |
| | | } |
| | | boolean result = adminService.register(param); |
| | | if (result) { |
| | | return ResponseUtil.success("注册用户成功"); |
| | |
| | | @Log("后台用户修改信息") |
| | | @PostMapping("/update/{adminId}") |
| | | public ResponseVO<Object> updateAdmin(@RequestBody AdminParam param, @PathVariable Long adminId) { |
| | | if (!permissionConfig.check(MenuEnum.ADMIN_INFO_UPDATE.getCode())) { |
| | | return ResponseUtil.fail("缺少对应用户权限"); |
| | | } |
| | | boolean result = adminService.updateAdmin(adminId, param); |
| | | if (result) { |
| | | return ResponseUtil.success("修改成功"); |
| | |
| | | /** |
| | | * 修改我的密码 |
| | | */ |
| | | @Log("后台用户修改密码") |
| | | @PutMapping("/updateMyPassword") |
| | | @Log("修改当前用户密码") |
| | | @PostMapping("/updateMyPassword") |
| | | public ResponseVO<Object> updateMyPassword(@Validated @RequestBody PwdParam param) { |
| | | boolean result = adminService.updateMyPassword(param); |
| | | boolean result; |
| | | if (SecurityUtils.getClientId() == null) { |
| | | result = adminService.updateMyPassword(param); |
| | | } else { |
| | | result = clientService.resetPassword(param); |
| | | } |
| | | |
| | | if (result) { |
| | | return ResponseUtil.success("修改成功"); |
| | | } else { |
| | | return ResponseUtil.fail("修改失败"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 修改管理员的密码 |
| | | */ |
| | | @Log("修改管理员的密码") |
| | | @PostMapping("/updateAdminPassword") |
| | | public ResponseVO<Object> updateAdminPassword(@Validated @RequestBody UserPwsParm param) { |
| | | if (!SecurityUtils.getUserDetails().getAdministratorType().equals(AdministratorEnums.ADMIN.getCode())) { |
| | | throw new BusinessException("非超级管理员无法修改密码!"); |
| | | } |
| | | |
| | | boolean result = adminService.updateAdminPassword(param); |
| | | |
| | | if (result) { |
| | | return ResponseUtil.success("修改成功"); |
| | | } else { |
| | |
| | | @Log("后台用户删除") |
| | | @PostMapping("/del/{adminId}") |
| | | public ResponseVO<Object> deleteAdmin(@PathVariable Long adminId) { |
| | | if (!permissionConfig.check(MenuEnum.ADMIN_DELETE.getCode())) { |
| | | return ResponseUtil.fail("缺少对应用户权限"); |
| | | } |
| | | boolean result = adminService.deleteAdmin(adminId); |
| | | if (result) { |
| | | return ResponseUtil.success("修改成功"); |