| | |
| | | package com.zy.acs.manager.system.controller; |
| | | |
| | | import com.zy.acs.common.utils.Utils; |
| | | import com.zy.acs.framework.common.BaseRes; |
| | | import com.zy.acs.framework.common.Cools; |
| | | import com.zy.acs.framework.common.R; |
| | | import com.zy.acs.manager.common.annotation.OperationLog; |
| | | import com.zy.acs.manager.common.config.ConfigProperties; |
| | | import com.zy.acs.manager.common.domain.BusinessRes; |
| | | import com.zy.acs.manager.common.security.JwtSubject; |
| | | import com.zy.acs.manager.common.service.EmailService; |
| | | import com.zy.acs.manager.common.utils.JwtUtil; |
| | | import com.zy.acs.manager.manager.enums.StatusType; |
| | | import com.zy.acs.manager.system.controller.param.LoginParam; |
| | | import com.zy.acs.manager.system.controller.param.UpdatePasswordParam; |
| | | import com.zy.acs.manager.system.controller.result.LoginResult; |
| | | import com.zy.acs.manager.system.controller.result.MenuVo; |
| | | import com.zy.acs.manager.system.controller.result.SystemInfoVo; |
| | | import com.zy.acs.manager.system.entity.Menu; |
| | | import com.zy.acs.manager.system.entity.User; |
| | | import com.zy.acs.manager.system.entity.UserLogin; |
| | |
| | | import com.zy.acs.manager.system.service.TenantService; |
| | | import com.zy.acs.manager.system.service.UserLoginService; |
| | | import com.zy.acs.manager.system.service.UserService; |
| | | import com.zy.acs.framework.common.Cools; |
| | | import com.zy.acs.framework.common.R; |
| | | import org.springframework.security.access.prepost.PreAuthorize; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 认证控制器 |
| | |
| | | private RoleMenuService roleMenuService; |
| | | @Resource |
| | | private TenantService tenantService; |
| | | @Autowired |
| | | private EmailService emailService; |
| | | |
| | | @GetMapping("/system/info") |
| | | public R systemInfo() { |
| | | SystemInfoVo infoVo = new SystemInfoVo(); |
| | | infoVo.setName(configProperties.getSystemName()); |
| | | infoVo.setVersion(configProperties.getSystemVersion()); |
| | | infoVo.setMode(configProperties.getSystemMode().toString()); |
| | | return R.ok(infoVo); |
| | | } |
| | | |
| | | @PostMapping("/login") |
| | | public R login(@RequestBody LoginParam param, HttpServletRequest request) { |
| | |
| | | Long tenantId = param.getTenantId(); |
| | | User user = userService.getByUsername(username, tenantId); |
| | | if (user == null) { |
| | | return R.error("Username Does Not Exist"); |
| | | return R.parse(BusinessRes.USERNAME_NOT_EXIST); |
| | | } |
| | | if (!user.getStatus().equals(1)) { |
| | | return R.error("Account Frozen"); |
| | | if (!user.getStatus().equals(StatusType.ENABLE.val)) { |
| | | return R.parse(BusinessRes.USERNAME_FROZEN); |
| | | } |
| | | if (!userService.comparePassword(user.getPassword(), param.getPassword())) { |
| | | return R.error("Wrong Password"); |
| | | return R.parse(BusinessRes.INVALID_PASSWORD); |
| | | } |
| | | String accessToken = JwtUtil.buildToken(new JwtSubject(username, user.getTenantId()), |
| | | configProperties.getTokenExpireTime(), configProperties.getTokenKey()); |
| | | userLoginService.saveAsync(user.getId(), accessToken, UserLogin.TYPE_LOGIN, tenantId, null, request); |
| | | return R.ok("Login Success").add(new LoginResult(accessToken, user)); |
| | | } |
| | | |
| | | @GetMapping("/auth/user") |
| | | public R userInfo() { |
| | | return R.ok(userService.getByIdRel(getLoginUserId())); |
| | | return R.ok("Sign In Success").add(new LoginResult(accessToken, user)); |
| | | } |
| | | |
| | | @GetMapping("/auth/menu") |
| | | public R userMenu() { |
| | | List<Menu> menus = roleMenuService.listMenuByUserId(getLoginUserId(), Menu.TYPE_MENU); |
| | | return R.ok().add(Utils.toTreeData(menus, 0L, Menu::getParentId, Menu::getId, Menu::setChildren)); |
| | | // return R.ok().add(Utils.toTreeData(menus, 0L, Menu::getParentId, Menu::getId, Menu::setChildren)); |
| | | List<MenuVo> voList = menus.stream().map(this::convertToVo).collect(Collectors.toList()); |
| | | return R.ok().add(Utils.toTreeData(voList, 0L, MenuVo::getParentId, MenuVo::getId, MenuVo::setChildren)); |
| | | } |
| | | |
| | | @GetMapping("/auth/tenant") |
| | | public R authHost() { |
| | | return R.ok().add(tenantService.list()); |
| | | @GetMapping("/auth/user") |
| | | public R userInfo() { |
| | | User user = userService.getById(getLoginUserId()); |
| | | return R.ok(userService.setUserAuthInfo(user)); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('sys:auth:user')") |
| | | @OperationLog |
| | | @PutMapping("/auth/user") |
| | | @OperationLog("Update UserInfo") |
| | | @PostMapping("/auth/user") |
| | | public R updateInfo(@RequestBody User user) { |
| | | user.setId(getLoginUserId()); |
| | | // 不能修改的字段 |
| | | user.setUsername(null); |
| | | user.setPassword(null); |
| | | user.setEmailVerified(null); |
| | | user.setTenantId(null); |
| | | user.setStatus(null); |
| | | if (!Cools.isEmpty(user.getEmail())) { |
| | | if (!emailService.isValid(user.getEmail())) { |
| | | return R.parse(BusinessRes.INVALID_EMAIL); |
| | | } |
| | | // User one = userService.getByEmail(user.getEmail(), null); |
| | | // if (null != one && !one.getId().equals(user.getId())) { |
| | | // return R.parse(BusinessRes.EMAIL_EXIT); |
| | | // } |
| | | } |
| | | if (userService.updateById(user)) { |
| | | return R.ok().add(userService.getByIdRel(user.getId())); |
| | | return R.ok("Save Success").add(userService.getById(user.getId())); |
| | | } |
| | | return R.error("Save Fail"); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('sys:auth:password')") |
| | | @OperationLog |
| | | @PutMapping("/auth/password") |
| | | public R updatePassword(@RequestBody UpdatePasswordParam param) { |
| | | if (Cools.isEmpty(param.getOldPassword(), param.getPassword())) { |
| | | return R.error("Parameters Cannot Be Empty"); |
| | | @OperationLog("Reset Password") |
| | | @PostMapping("/auth/reset/password") |
| | | public R resetPassword(@RequestBody UpdatePasswordParam param) { |
| | | if (Cools.isEmpty(param.getOldPassword(), param.getNewPassword())) { |
| | | return R.parse(BaseRes.PARAM); |
| | | } |
| | | Long userId = getLoginUserId(); |
| | | if (userId == null) { |
| | | return R.error("Please Login First"); |
| | | } |
| | | if (!userService.comparePassword(userService.getById(userId).getPassword(), param.getOldPassword())) { |
| | | return R.error("The Origin Password Was Incorrect"); |
| | | return R.parse(BusinessRes.INVALID_PASSWORD); |
| | | } |
| | | User user = new User(); |
| | | user.setId(userId); |
| | | user.setPassword(userService.encodePassword(param.getPassword())); |
| | | user.setPassword(userService.encodePassword(param.getNewPassword())); |
| | | if (userService.updateById(user)) { |
| | | return R.ok("Update Success"); |
| | | return R.ok("Reset Password Success"); |
| | | } |
| | | return R.error("Update Fail"); |
| | | return R.error("Reset Password Fail"); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | // ---------------------------------------------------- |
| | | |
| | | private MenuVo convertToVo(Menu menu) { |
| | | if (menu == null) { |
| | | return null; |
| | | } |
| | | MenuVo vo = new MenuVo(); |
| | | vo.setId(menu.getId()); |
| | | vo.setName(menu.getName()); |
| | | vo.setParentId(menu.getParentId()); |
| | | // vo.setParentName(menu.getParentName()); |
| | | vo.setPath(menu.getPath()); |
| | | // vo.setPathName(menu.getPathName()); |
| | | vo.setRoute(menu.getRoute()); |
| | | vo.setComponent(menu.getComponent()); |
| | | vo.setType(menu.getType()); |
| | | vo.setIcon(menu.getIcon()); |
| | | vo.setSort(menu.getSort()); |
| | | if (menu.getChildren() != null && !menu.getChildren().isEmpty()) { |
| | | List<MenuVo> childDTOs = menu.getChildren().stream() |
| | | .map(this::convertToVo) |
| | | .collect(Collectors.toList()); |
| | | vo.setChildren(childDTOs); |
| | | } |
| | | return vo; |
| | | } |
| | | |
| | | } |