| | |
| | | import com.vincent.rsf.server.system.controller.result.LoginResult; |
| | | import com.vincent.rsf.server.system.controller.result.MenuVo; |
| | | import com.vincent.rsf.server.system.entity.Menu; |
| | | import com.vincent.rsf.server.system.entity.Tenant; |
| | | import com.vincent.rsf.server.system.entity.User; |
| | | import com.vincent.rsf.server.system.entity.UserLogin; |
| | | import com.vincent.rsf.server.system.enums.StatusType; |
| | | import com.vincent.rsf.server.system.service.RoleMenuService; |
| | | import com.vincent.rsf.server.system.service.TenantService; |
| | | import com.vincent.rsf.server.system.service.UserLoginService; |
| | | import com.vincent.rsf.server.system.service.UserService; |
| | | import org.springframework.security.access.prepost.PreAuthorize; |
| | | import org.springframework.web.bind.annotation.*; |
| | | 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.RestController; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | |
| | | * Created by vincent on 1/30/2024 |
| | | */ |
| | | @RestController |
| | | //@RequestMapping("") |
| | | public class AuthController extends BaseController { |
| | | |
| | | @Resource |
| | |
| | | if (user == null) { |
| | | return R.error("Username Does Not Exist"); |
| | | } |
| | | if (!user.getStatus().equals(1)) { |
| | | if (!user.getStatus().equals(StatusType.ENABLE.val)) { |
| | | return R.error("Account Frozen"); |
| | | } |
| | | if (!userService.comparePassword(user.getPassword(), param.getPassword())) { |
| | | return R.error("Wrong Password"); |
| | | return R.error("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)); |
| | | Tenant tenant = tenantService.getById(user.getTenantId()); |
| | | return R.ok("Sign In Success").add(new LoginResult(accessToken, user, tenant.getName())); |
| | | } |
| | | |
| | | @GetMapping("/tenant/list") |
| | | public R tenantList() { |
| | | return R.ok().add(tenantService.list()); |
| | | } |
| | | |
| | | @GetMapping("/auth/user") |
| | | public R userInfo() { |
| | | return R.ok(userService.getByIdRel(getLoginUserId())); |
| | | User user = userService.getById(getLoginUserId()); |
| | | return R.ok(userService.setUserAuthInfo(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)); |
| | | List<MenuVo> voList = menus.stream().map(this::convertToVo).collect(Collectors.toList()); |
| | | // exclude tenant |
| | | if (!configProperties.getSuperUserList().contains(getLoginUser().getUsername())) { |
| | | voList = voList.stream().filter(vo -> |
| | | !vo.getName().equals("menu.tenant") |
| | | && !vo.getName().equals("menu.menu") |
| | | ).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()); |
| | | } |
| | | |
| | | @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.setTenantId(null); |
| | | user.setStatus(null); |
| | | 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())) { |
| | | @OperationLog("Reset Password") |
| | | @PostMapping("/auth/reset/password") |
| | | public R resetPassword(@RequestBody UpdatePasswordParam param) { |
| | | if (Cools.isEmpty(param.getOldPassword(), param.getNewPassword())) { |
| | | return R.error("Parameters Cannot Be Empty"); |
| | | } |
| | | Long userId = getLoginUserId(); |
| | |
| | | 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("408-The Current Password Was Incorrect"); |
| | | } |
| | | 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"); |
| | | } |
| | | |
| | | |