| | |
| | | package com.vincent.rsf.server.system.controller; |
| | | |
| | | import com.vincent.rsf.common.enums.SystemModeType; |
| | | import com.vincent.rsf.common.utils.Utils; |
| | | import com.vincent.rsf.framework.common.BaseRes; |
| | | import com.vincent.rsf.framework.common.Cools; |
| | | import com.vincent.rsf.framework.common.R; |
| | | import com.vincent.rsf.framework.exception.CoolException; |
| | | import com.vincent.rsf.server.common.annotation.OperationLog; |
| | | import com.vincent.rsf.server.common.config.ConfigProperties; |
| | | import com.vincent.rsf.server.common.security.JwtSubject; |
| | | import com.vincent.rsf.server.common.service.EmailService; |
| | | import com.vincent.rsf.server.common.service.RedisService; |
| | | import com.vincent.rsf.server.common.utils.JwtUtil; |
| | | import com.vincent.rsf.server.system.controller.param.LoginParam; |
| | | import com.vincent.rsf.server.system.controller.param.RegisterParam; |
| | | import com.vincent.rsf.server.system.controller.param.TenantInitParam; |
| | | import com.vincent.rsf.server.system.controller.param.UpdatePasswordParam; |
| | | import com.vincent.rsf.server.system.controller.result.LoginResult; |
| | | import com.vincent.rsf.server.system.controller.result.MenuVo; |
| | | import com.vincent.rsf.server.system.controller.result.SystemInfoVo; |
| | | 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.service.*; |
| | | import com.vincent.rsf.server.system.enums.EmailType; |
| | | 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.beans.factory.annotation.Autowired; |
| | | 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 org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | |
| | | * Created by vincent on 1/30/2024 |
| | | */ |
| | | @RestController |
| | | //@RequestMapping("") |
| | | public class AuthController extends BaseController { |
| | | |
| | | @Resource |
| | |
| | | @Resource |
| | | private TenantService tenantService; |
| | | @Autowired |
| | | private UserRoleService userRoleService; |
| | | private EmailService emailService; |
| | | @Autowired |
| | | private RedisService redisService; |
| | | |
| | | @PostMapping("/login") |
| | | public R login(@RequestBody LoginParam param, HttpServletRequest request) { |
| | |
| | | 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("/email/code") |
| | | public R emailCode(@RequestParam(value = "email", required = false) String email) { |
| | | if (Cools.isEmpty(email)) { |
| | | return R.parse(BaseRes.PARAM); |
| | | } |
| | | if (!emailService.isValid(email)) { |
| | | return R.error("Please enter a valid email address"); |
| | | } |
| | | if (null != userService.getByEmail(email, null)) { |
| | | return R.error("Email Already Exist"); |
| | | } |
| | | Long expire = redisService.getExpire(EmailType.REGISTER_VERIFY.toString(), email); |
| | | if (expire > (configProperties.getCodeTime() - 60)) { |
| | | return R.error("Please don't request code too frequently."); |
| | | } |
| | | String code = Utils.randomNumbers(configProperties.getCodeLength()); |
| | | if (emailService.sendEmail(email, EmailType.REGISTER_VERIFY, Cools.add("code", code))) { |
| | | redisService.setValue(EmailType.REGISTER_VERIFY.toString(), email, code, configProperties.getCodeTime()); |
| | | } |
| | | return R.ok(); |
| | | } |
| | | |
| | | @OperationLog("Register") |
| | | @PostMapping("/register") |
| | | public R register(@RequestBody RegisterParam param, HttpServletRequest request) { |
| | | if (Cools.isEmpty(param.getUsername(), param.getPassword(), param.getEmail(), param.getCode())) { |
| | | return R.parse(BaseRes.PARAM); |
| | | } |
| | | // verify code |
| | | String cacheCode = redisService.getValue(EmailType.REGISTER_VERIFY.toString(), param.getEmail()); |
| | | if (Cools.isEmpty(cacheCode)) { |
| | | return R.error("The verification code has expired."); |
| | | } |
| | | if (!cacheCode.equals(param.getCode())) { |
| | | return R.error("The verification code is incorrect."); |
| | | } |
| | | |
| | | // register |
| | | TenantInitParam initParam = new TenantInitParam(); |
| | | initParam.setEmail(param.getEmail()); |
| | | initParam.setUsername(param.getUsername()); |
| | | initParam.setPassword(param.getPassword()); |
| | | initParam.setName(param.getUsername()); |
| | | initParam.setFlag(param.getUsername()); |
| | | if (!tenantService.initTenant(initParam)) { |
| | | throw new CoolException("Failed to register"); |
| | | } |
| | | |
| | | redisService.delete(EmailType.REGISTER_VERIFY.toString(), param.getEmail()); |
| | | return R.ok(); |
| | | } |
| | | |
| | | @GetMapping("/system/info") |
| | | public R systemInfo() { |
| | | SystemModeType systemMode = configProperties.getSystemMode(); |
| | | SystemInfoVo infoVo = new SystemInfoVo(); |
| | | infoVo.setName(configProperties.getSystemName()); |
| | | infoVo.setVersion(configProperties.getSystemVersion()); |
| | | infoVo.setMode(systemMode.toString()); |
| | | return R.ok(infoVo); |
| | | } |
| | | |
| | | @GetMapping("/tenant/list") |
| | |
| | | |
| | | @GetMapping("/auth/user") |
| | | public R userInfo() { |
| | | User user = userService.getByIdRel(getLoginUserId()); |
| | | user.setRoles(userRoleService.listByUserId(user.getId())); |
| | | return R.ok(user); |
| | | 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)); |
| | | } |
| | | |
| | |
| | | @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 (userService.updateById(user)) { |
| | | return R.ok("Save Success").add(userService.getByIdRel(user.getId())); |
| | | return R.ok("Save Success").add(userService.getById(user.getId())); |
| | | } |
| | | return R.error("Save Fail"); |
| | | } |
| | | |
| | | @OperationLog("Reset Password") |
| | | @PostMapping("/auth/password") |
| | | public R updatePassword(@RequestBody UpdatePasswordParam param) { |
| | | if (Cools.isEmpty(param.getOldPassword(), param.getPassword())) { |
| | | @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"); |
| | | } |
| | | |
| | | |