| | |
| | | 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 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.*; |
| | | 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.security.core.GrantedAuthority; |
| | | import org.springframework.security.crypto.password.PasswordEncoder; |
| | | 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 RoleService roleService; |
| | | 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(","))); |
| | | |
| | | // 生成令牌 |
| | |
| | | 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); |
| | | } |
| | | |
| | | @Log("后台用户信息") |
| | | // @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; |
| | | if (AdministratorEnums.ADMIN.equals(SecurityUtils.getAdministratorIdentity()) |
| | | || AdministratorEnums.CUSTOMER.equals(SecurityUtils.getAdministratorIdentity())) { |
| | | if (AdministratorEnums.ADMIN.getCode().equals(SecurityUtils.getAdministratorIdentity()) |
| | | || AdministratorEnums.CUSTOMER.getCode().equals(SecurityUtils.getAdministratorIdentity())) { |
| | | Admin admin = adminService.getById(userId); |
| | | roles = roleService.listByAdminId(admin.getId()); |
| | | builder = MapUtil.builder().put("admin", admin); |
| | |
| | | } else { |
| | | Client client = clientService.getById(userId); |
| | | roles = roleService.listByClientId(client.getId()); |
| | | builder = MapUtil.builder().put("admin", client); |
| | | //为了保持返回数据格式一致 |
| | | Admin admin = new Admin(); |
| | | admin.setId(client.getId()); |
| | | admin.setIcon(client.getIcon()); |
| | | admin.setNickName(client.getLinkMan()); |
| | | admin.setMobile(client.getMobile()); |
| | | admin.setLoginTime(client.getLoginTime()); |
| | | admin.setCreateTime(client.getCreateTime()); |
| | | builder = MapUtil.builder().put("admin", admin); |
| | | |
| | | } |
| | | if (CollectionUtil.isEmpty(roles)) { |
| | |
| | | List<Menu> menus = menuService.listByRoleIds(roles.stream().map(Role::getId).collect(Collectors.toList())); |
| | | if (CollectionUtil.isNotEmpty(menus)) { |
| | | List<Menu> collect = menus.stream().filter(menu -> menu.getType() == 0 || menu.getType() == 1).collect(Collectors.toList()); |
| | | builder.put("menus", collect); |
| | | |
| | | List<MenuNode> menuNodeList = collect.stream() |
| | | .filter(menu -> menu.getPid().equals(0L)) |
| | | .map(menu -> covertMenuNode(menu, collect)).collect(Collectors.toList()); |
| | | builder.put("menus", menuNodeList); |
| | | } |
| | | return ResponseUtil.success(builder.build()); |
| | | } |
| | | |
| | | @GetMapping(value = "/logout") |
| | | public ResponseVO<Object> LogOut() { |
| | | 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); |
| | | List<MenuNode> children = menuList.stream() |
| | | .filter(subMenu -> subMenu.getPid().equals(menu.getId())) |
| | | .map(subMenu -> covertMenuNode(subMenu, menuList)).collect(Collectors.toList()); |
| | | node.setChildren(children); |
| | | return node; |
| | | } |
| | | |
| | | @Log("后台用户列表") |
| | | @GetMapping(value = "/list") |
| | | public ResponseVO<Object> listAdmin(BaseConditionVO baseConditionVO, @RequestParam(value = "keyword", required = false) String 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); |
| | | }); |
| | | if (!permissionConfig.check(MenuEnum.ADMIN_LIST.getCode())) { |
| | | return ResponseUtil.fail("缺少对应用户权限"); |
| | | } |
| | | 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); |
| | | return ResponseUtil.successPage(adminService.listAdmin(baseConditionVO, keyword)); |
| | | } |
| | | |
| | | @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 = adminService.updateMyPassword(param); |
| | | boolean result; |
| | | if (SecurityUtils.getClientId() == null) { |
| | | result = adminService.updateMyPassword(param); |
| | | } else { |
| | | result = clientService.resetPassword(param); |
| | | } |
| | | |
| | | if (result) { |
| | | return ResponseUtil.success("修改成功"); |
| | | } else { |
| | |
| | | } |
| | | } |
| | | |
| | | // @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("/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 { |
| | | return ResponseUtil.fail("修改失败"); |
| | | } |
| | | } |
| | | |
| | | @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(""); |
| | | // } |
| | | @Log("用户修改头像") |
| | | @PostMapping("/updateIcon") |
| | | public ResponseVO<Object> updateIcon(@RequestBody ReceiveParam receiveParam) { |
| | | if (receiveParam.getIconUrl().trim() == null) { |
| | | throw new BusinessException("头像URL不能为空!"); |
| | | } |
| | | boolean result; |
| | | //普通用户修改头像 |
| | | if (SecurityUtils.getAdministratorIdentity().equals(AdministratorEnums.NORMAL.getCode())) { |
| | | result = clientService.updateIcon(SecurityUtils.getUserId(), receiveParam.getIconUrl()); |
| | | } else { |
| | | //超级管理员、管理员修改头像 |
| | | result = adminService.updateIcon(SecurityUtils.getUserId(), receiveParam.getIconUrl()); |
| | | } |
| | | |
| | | if (result) { |
| | | return ResponseUtil.success("修改成功"); |
| | | } else { |
| | | return ResponseUtil.fail("修改失败"); |
| | | } |
| | | } |
| | | |
| | | |
| | | // /** |
| | | // * 编辑我的信息 |
| | | // * |
| | | // * @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 |
| | | */ |
| | | @GetMapping("/getUserPermissionList") |
| | | public ResponseVO<Object> getUserPermissionList() { |
| | | List<MenuNode> userPermissionList; |
| | | if (SecurityUtils.getClientId() == null) { |
| | | userPermissionList = adminService.getUserPermissionList(); |
| | | } else { |
| | | userPermissionList = clientService.getUserPermissionList(); |
| | | } |
| | | |
| | | return ResponseUtil.success(userPermissionList); |
| | | } |
| | | } |