| | |
| | | import com.sandu.common.log.Log; |
| | | import com.sandu.common.object.BaseConditionVO; |
| | | import com.sandu.common.redis.RedisService; |
| | | import com.sandu.common.redis.UserModel; |
| | | 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.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.IPUtils; |
| | | import com.sandu.ximon.admin.utils.RedisUtils; |
| | | import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils; |
| | | import com.sandu.ximon.dao.bo.MenuNode; |
| | | import com.sandu.ximon.dao.domain.*; |
| | | import com.sandu.ximon.dao.enums.AdministratorEnums; |
| | | import com.sandu.ximon.dao.enums.MenuEnum; |
| | | import eu.bitwalker.useragentutils.Browser; |
| | | import eu.bitwalker.useragentutils.OperatingSystem; |
| | | import eu.bitwalker.useragentutils.UserAgent; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.BeanUtils; |
| | |
| | | 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; |
| | | |
| | | /** |
| | |
| | | @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()); |
| | | } |
| | |
| | | put("token", properties.getTokenStartWith() + token); |
| | | put("user", admin); |
| | | }}; |
| | | //保存登录信息到日志 |
| | | UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent")); |
| | | Browser browser = userAgent.getBrowser(); |
| | | OperatingSystem os = userAgent.getOperatingSystem(); |
| | | String content = "登录IP地址:" + IPUtils.getPublicIp() + ",操作系统:" + os + ",浏览器:" + browser; |
| | | |
| | | StoreOperationRecordsUtils.storeOperationData(null, null, "用户登录", content, null); |
| | | return ResponseUtil.success(authInfo); |
| | | } |
| | | |
| | |
| | | |
| | | @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("退出登录成功"); |
| | |
| | | return ResponseUtil.fail("缺少对应用户权限"); |
| | | } |
| | | return ResponseUtil.successPage(adminService.listAdmin(baseConditionVO, keyword)); |
| | | |
| | | // 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("注册用户成功"); |
| | |
| | | } |
| | | } |
| | | |
| | | // @PreAuthorize("@el.check('admin:edit')") |
| | | |
| | | /** |
| | | * 修改管理员信息 |
| | |
| | | @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; |
| | | if (SecurityUtils.getClientId() == null) { |
| | |
| | | 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("修改成功"); |
| | |
| | | } |
| | | } |
| | | |
| | | // @PutMapping("/updateStatus/{id}") |
| | | // public ResponseVO<Object> updateStatus(@PathVariable Long id, @RequestParam(value = "status") Integer status) { |
| | | // checkLevel(id); |
| | | // Admin update = new Admin(); |
| | | // update.setId(id); |
| | | // update.setStatus(status); |
| | | // adminService.updateById(update); |
| | | // return ResponseUtil.success(null); |
| | | // } |
| | | |
| | | @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("修改成功"); |
| | |
| | | } |
| | | } |
| | | |
| | | // /** |
| | | // * 给管理员分配角色 |
| | | // * |
| | | // * @return |
| | | // */ |
| | | // @PostMapping("/updateAdminRole") |
| | | // @PreAuthorize("@el.check('admin:edit')") |
| | | // public ResponseVO<Object> updateAdminRole(@RequestParam("adminId") Long adminId, |
| | | // @RequestParam("roleIds") List<Long> roleIds) { |
| | | // checkLevel(adminId); |
| | | // adminsRolesService.updateAdminRole(adminId, roleIds); |
| | | // return ResponseUtil.success(""); |
| | | // } |
| | | |
| | | /** |
| | | * 获取用户权限 |
| | | * |
| | | * @return |
| | | */ |
| | | @GetMapping("/getUserPermissionList") |
| | | public ResponseVO<Object> getUserPermissionList() { |
| | | List<MenuNode> userPermissionList; |
| | | if (SecurityUtils.getClientId() == null) { |
| | | userPermissionList = adminService.getUserPermissionList(); |
| | | } else { |
| | | userPermissionList = clientService.getUserPermissionList(); |
| | | } |
| | | |
| | | // /** |
| | | // * 编辑我的信息 |
| | | // * |
| | | // * @param resources |
| | | // * @return |
| | | // */ |
| | | // @PutMapping("/editUserInfo") |
| | | // public ResponseVO<Object> editUserInfo(@RequestBody AdminParam resources) { |
| | | // Long userId = SecurityUtils.getUserId(); |
| | | // Admin admin = new Admin(); |
| | | // admin.setId(userId); |
| | | // admin.setMobile(resources.getMobile()); |
| | | // admin.setEmail(resources.getEmail()); |
| | | // admin.setNickName(resources.getNickName()); |
| | | // adminService.updateById(admin); |
| | | // return ResponseUtil.success(null); |
| | | // } |
| | | |
| | | return ResponseUtil.success(userPermissionList); |
| | | } |
| | | } |