| | |
| | | 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.domain.BusinessRes; |
| | | 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.system.controller.result.LoginResult; |
| | | import com.vincent.rsf.server.system.controller.result.MenuVo; |
| | | import com.vincent.rsf.server.system.controller.result.SystemInfoVo; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | 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.EmailType; |
| | | import com.vincent.rsf.server.system.enums.StatusType; |
| | | import com.vincent.rsf.server.system.service.MenuService; |
| | | 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 javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 认证控制器 |
| | | * |
| | | * Authority Controller |
| | | * Created by vincent on 1/30/2024 |
| | | */ |
| | | @RestController |
| | |
| | | @Resource |
| | | private RoleMenuService roleMenuService; |
| | | @Resource |
| | | private MenuService menuService; |
| | | @Resource |
| | | private TenantService tenantService; |
| | | @Autowired |
| | | private EmailService emailService; |
| | |
| | | |
| | | @PostMapping("/login") |
| | | public R login(@RequestBody LoginParam param, HttpServletRequest request) { |
| | | // System.out.println(userService.encodePassword("123456")); |
| | | String username = param.getUsername(); |
| | | Long tenantId = param.getTenantId(); |
| | | User user = userService.getByUsername(username, tenantId); |
| | | User user = userService.getByUsername(username |
| | | , configProperties.getSystemMode() |
| | | .equals(SystemModeType.OFFLINE) ? param.getTenantId() : null |
| | | ); |
| | | if (user == null) { |
| | | return R.error("Username Does Not Exist"); |
| | | return R.parse(BusinessRes.USERNAME_NOT_EXIST); |
| | | } |
| | | if (!user.getStatus().equals(StatusType.ENABLE.val)) { |
| | | return R.error("Account Frozen"); |
| | | return R.parse(BusinessRes.USERNAME_FROZEN); |
| | | } |
| | | if (!userService.comparePassword(user.getPassword(), param.getPassword())) { |
| | | return R.error("Invalid 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); |
| | | userLoginService.saveAsync(user.getId(), accessToken, UserLogin.TYPE_LOGIN, user.getTenantId(), null, request); |
| | | 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("email") String email) { |
| | | 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.parse(BusinessRes.INVALID_EMAIL); |
| | | } |
| | | if (null != userService.getByEmail(email, null)) { |
| | | return R.error("Email Already Exist"); |
| | | return R.parse(BusinessRes.EMAIL_EXIT); |
| | | } |
| | | Long expire = redisService.getExpire(EmailType.REGISTER_VERIFY.toString(), email); |
| | | if (expire > (configProperties.getCodeTime() - 60)) { |
| | |
| | | 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 (!param.getCode().equals(configProperties.getSecurityCode())) { |
| | | 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."); |
| | | } |
| | | } |
| | | if (!cacheCode.equals(param.getCode())) { |
| | | return R.error("The verification code is incorrect."); |
| | | } |
| | | |
| | | // register |
| | | TenantInitParam initParam = new TenantInitParam(); |
| | | initParam.setEmail(param.getEmail()); |
| | |
| | | initParam.setPassword(param.getPassword()); |
| | | initParam.setName(param.getUsername()); |
| | | initParam.setFlag(param.getUsername()); |
| | | if (!tenantService.initTenant(initParam)) { |
| | | Long tenantId = tenantService.initTenant(initParam); |
| | | if (null == tenantId) { |
| | | throw new CoolException("Failed to register"); |
| | | } |
| | | |
| | | redisService.delete(EmailType.REGISTER_VERIFY.toString(), param.getEmail()); |
| | | return R.ok(); |
| | | // token |
| | | User user = userService.getByUsername(param.getUsername(), tenantId); |
| | | String accessToken = JwtUtil.buildToken(new JwtSubject(user.getUsername(), user.getTenantId()), |
| | | configProperties.getTokenExpireTime(), configProperties.getTokenKey()); |
| | | userLoginService.saveAsync(user.getId(), accessToken, UserLogin.TYPE_LOGIN, tenantId, null, request); |
| | | Tenant tenant = tenantService.getById(user.getTenantId()); |
| | | return R.ok("Sign Up Success").add(new LoginResult(accessToken, user, tenant.getName())); |
| | | } |
| | | |
| | | @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()); |
| | | infoVo.setMode(configProperties.getSystemMode().toString()); |
| | | return R.ok(infoVo); |
| | | } |
| | | |
| | |
| | | |
| | | @GetMapping("/auth/menu") |
| | | public R userMenu() { |
| | | List<Menu> menus = roleMenuService.listMenuByUserId(getLoginUserId(), Menu.TYPE_MENU); |
| | | Long uid = getLoginUserId(); |
| | | List<Menu> grantedAll = roleMenuService.listMenuByUserId(uid, null); |
| | | Set<Long> grantedMenuIds = grantedAll.stream().map(Menu::getId).collect(Collectors.toCollection(HashSet::new)); |
| | | List<Menu> allMenus = menuService.list(new LambdaQueryWrapper<Menu>().eq(Menu::getDeleted, 0)); |
| | | List<Menu> menus = roleMenuService.listMenuByUserId(uid, Menu.TYPE_MENU).stream() |
| | | .filter(m -> menuPageGranted(m, grantedMenuIds, allMenus)) |
| | | .collect(Collectors.toList()); |
| | | List<MenuVo> voList = menus.stream().map(this::convertToVo).collect(Collectors.toList()); |
| | | // exclude tenant |
| | | if (!configProperties.getSuperUserList().contains(getLoginUser().getUsername())) { |
| | |
| | | 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("Save Success").add(userService.getById(user.getId())); |
| | | } |
| | |
| | | @PostMapping("/auth/reset/password") |
| | | public R resetPassword(@RequestBody UpdatePasswordParam param) { |
| | | if (Cools.isEmpty(param.getOldPassword(), param.getNewPassword())) { |
| | | return R.error("Parameters Cannot Be Empty"); |
| | | 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.parse("408-The Current Password Was Incorrect"); |
| | | return R.parse(BusinessRes.INVALID_PASSWORD); |
| | | } |
| | | User user = new User(); |
| | | user.setId(userId); |
| | |
| | | |
| | | // ---------------------------------------------------- |
| | | |
| | | /** |
| | | * 页面级菜单:若存在带 :list 的按钮权限子节点,则必须同时勾选至少一个该类按钮,才展示该页(与接口 @PreAuthorize 一致) |
| | | */ |
| | | private boolean menuPageGranted(Menu m, Set<Long> grantedMenuIds, List<Menu> allMenus) { |
| | | if (m == null || !Integer.valueOf(Menu.TYPE_MENU).equals(m.getType())) { |
| | | return true; |
| | | } |
| | | if (Cools.isEmpty(m.getComponent())) { |
| | | return true; |
| | | } |
| | | List<Menu> btnChildren = allMenus.stream() |
| | | .filter(c -> m.getId().equals(c.getParentId()) && Integer.valueOf(Menu.TYPE_BTN).equals(c.getType())) |
| | | .collect(Collectors.toList()); |
| | | if (btnChildren.isEmpty()) { |
| | | return true; |
| | | } |
| | | boolean needListBtn = btnChildren.stream().anyMatch(c -> |
| | | c.getAuthority() != null && c.getAuthority().contains(":list")); |
| | | if (!needListBtn) { |
| | | return true; |
| | | } |
| | | return btnChildren.stream() |
| | | .filter(c -> c.getAuthority() != null && c.getAuthority().contains(":list")) |
| | | .anyMatch(c -> grantedMenuIds.contains(c.getId())); |
| | | } |
| | | |
| | | private MenuVo convertToVo(Menu menu) { |
| | | if (menu == null) { |
| | | return null; |