From 009ac75229725c5d83f80c6d62357b65a6b11e7b Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期四, 14 十一月 2024 16:57:10 +0800
Subject: [PATCH] fix 修复 数据权限多角色与权限标识符共用导致的问题 https://gitee.com/dromara/RuoYi-Vue-Plus/issues/IB4CS4
---
ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java | 88 ++++++++++++++++++++++++++++++++++----------
1 files changed, 68 insertions(+), 20 deletions(-)
diff --git a/ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java b/ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java
index 3c03e42..89fe607 100644
--- a/ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java
+++ b/ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java
@@ -1,6 +1,8 @@
package org.dromara.web.controller;
import cn.dev33.satoken.annotation.SaIgnore;
+import cn.dev33.satoken.exception.NotLoginException;
+import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import jakarta.servlet.http.HttpServletRequest;
@@ -10,19 +12,23 @@
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
+import org.dromara.common.core.constant.SystemConstants;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginBody;
import org.dromara.common.core.domain.model.RegisterBody;
-import org.dromara.common.core.utils.MapstructUtils;
-import org.dromara.common.core.utils.MessageUtils;
-import org.dromara.common.core.utils.StreamUtils;
-import org.dromara.common.core.utils.StringUtils;
+import org.dromara.common.core.domain.model.SocialLoginBody;
+import org.dromara.common.core.utils.*;
+import org.dromara.common.encrypt.annotation.ApiEncrypt;
+import org.dromara.common.json.utils.JsonUtils;
+import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.social.config.properties.SocialLoginConfigProperties;
import org.dromara.common.social.config.properties.SocialProperties;
import org.dromara.common.social.utils.SocialUtils;
+import org.dromara.common.sse.dto.SseMessageDto;
+import org.dromara.common.sse.utils.SseMessageUtils;
import org.dromara.common.tenant.helper.TenantHelper;
-import org.dromara.system.domain.SysClient;
import org.dromara.system.domain.bo.SysTenantBo;
+import org.dromara.system.domain.vo.SysClientVo;
import org.dromara.system.domain.vo.SysTenantVo;
import org.dromara.system.service.ISysClientService;
import org.dromara.system.service.ISysConfigService;
@@ -38,7 +44,12 @@
import org.springframework.web.bind.annotation.*;
import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
/**
* 璁よ瘉
@@ -47,7 +58,6 @@
*/
@Slf4j
@SaIgnore
-@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/auth")
@@ -60,29 +70,44 @@
private final ISysTenantService tenantService;
private final ISysSocialService socialUserService;
private final ISysClientService clientService;
+ private final ScheduledExecutorService scheduledExecutorService;
/**
* 鐧诲綍鏂规硶
*
- * @param loginBody 鐧诲綍淇℃伅
+ * @param body 鐧诲綍淇℃伅
* @return 缁撴灉
*/
+ @ApiEncrypt
@PostMapping("/login")
- public R<LoginVo> login(@Validated @RequestBody LoginBody loginBody) {
+ public R<LoginVo> login(@RequestBody String body) {
+ LoginBody loginBody = JsonUtils.parseObject(body, LoginBody.class);
+ ValidatorUtils.validate(loginBody);
// 鎺堟潈绫诲瀷鍜屽鎴风id
String clientId = loginBody.getClientId();
String grantType = loginBody.getGrantType();
- SysClient client = clientService.queryByClientId(clientId);
+ SysClientVo client = clientService.queryByClientId(clientId);
// 鏌ヨ涓嶅埌 client 鎴� client 鍐呬笉鍖呭惈 grantType
if (ObjectUtil.isNull(client) || !StringUtils.contains(client.getGrantType(), grantType)) {
log.info("瀹㈡埛绔痠d: {} 璁よ瘉绫诲瀷锛歿} 寮傚父!.", clientId, grantType);
return R.fail(MessageUtils.message("auth.grant.type.error"));
+ } else if (!SystemConstants.NORMAL.equals(client.getStatus())) {
+ return R.fail(MessageUtils.message("auth.grant.type.blocked"));
}
// 鏍¢獙绉熸埛
loginService.checkTenant(loginBody.getTenantId());
// 鐧诲綍
- return R.ok(IAuthStrategy.login(loginBody, client));
+ LoginVo loginVo = IAuthStrategy.login(body, client, grantType);
+
+ Long userId = LoginHelper.getUserId();
+ scheduledExecutorService.schedule(() -> {
+ SseMessageDto dto = new SseMessageDto();
+ dto.setMessage("娆㈣繋鐧诲綍RuoYi-Vue-Plus鍚庡彴绠$悊绯荤粺");
+ dto.setUserIds(List.of(userId));
+ SseMessageUtils.publishMessage(dto);
+ }, 5, TimeUnit.SECONDS);
+ return R.ok(loginVo);
}
/**
@@ -92,13 +117,18 @@
* @return 缁撴灉
*/
@GetMapping("/binding/{source}")
- public R<String> authBinding(@PathVariable("source") String source) {
+ public R<String> authBinding(@PathVariable("source") String source,
+ @RequestParam String tenantId, @RequestParam String domain) {
SocialLoginConfigProperties obj = socialProperties.getType().get(source);
if (ObjectUtil.isNull(obj)) {
return R.fail(source + "骞冲彴璐﹀彿鏆備笉鏀寔");
}
AuthRequest authRequest = SocialUtils.getAuthRequest(source, socialProperties);
- String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
+ Map<String, String> map = new HashMap<>();
+ map.put("tenantId", tenantId);
+ map.put("domain", domain);
+ map.put("state", AuthStateUtils.createState());
+ String authorizeUrl = authRequest.authorize(Base64.encode(JsonUtils.toJsonString(map), StandardCharsets.UTF_8));
return R.ok("鎿嶄綔鎴愬姛", authorizeUrl);
}
@@ -109,9 +139,11 @@
* @return 缁撴灉
*/
@PostMapping("/social/callback")
- public R<Void> socialCallback(@RequestBody LoginBody loginBody) {
+ public R<Void> socialCallback(@RequestBody SocialLoginBody loginBody) {
// 鑾峰彇绗笁鏂圭櫥褰曚俊鎭�
- AuthResponse<AuthUser> response = SocialUtils.loginAuth(loginBody, socialProperties);
+ AuthResponse<AuthUser> response = SocialUtils.loginAuth(
+ loginBody.getSource(), loginBody.getSocialCode(),
+ loginBody.getSocialState(), socialProperties);
AuthUser authUserData = response.getData();
// 鍒ゆ柇鎺堟潈鍝嶅簲鏄惁鎴愬姛
if (!response.ok()) {
@@ -146,6 +178,7 @@
/**
* 鐢ㄦ埛娉ㄥ唽
*/
+ @ApiEncrypt
@PostMapping("/register")
public R<Void> register(@Validated @RequestBody RegisterBody user) {
if (!configService.selectRegisterEnabled(user.getTenantId())) {
@@ -162,8 +195,26 @@
*/
@GetMapping("/tenant/list")
public R<LoginTenantVo> tenantList(HttpServletRequest request) throws Exception {
+ // 杩斿洖瀵硅薄
+ LoginTenantVo result = new LoginTenantVo();
+ boolean enable = TenantHelper.isEnable();
+ result.setTenantEnabled(enable);
+ // 濡傛灉鏈紑鍚鎴疯繖鐩存帴杩斿洖
+ if (!enable) {
+ return R.ok(result);
+ }
+
List<SysTenantVo> tenantList = tenantService.queryList(new SysTenantBo());
List<TenantListVo> voList = MapstructUtils.convert(tenantList, TenantListVo.class);
+ try {
+ // 濡傛灉鍙秴绠¤繑鍥炴墍鏈夌鎴�
+ if (LoginHelper.isSuperAdmin()) {
+ result.setVoList(voList);
+ return R.ok(result);
+ }
+ } catch (NotLoginException ignored) {
+ }
+
// 鑾峰彇鍩熷悕
String host;
String referer = request.getHeader("referer");
@@ -175,12 +226,9 @@
}
// 鏍规嵁鍩熷悕杩涜绛涢��
List<TenantListVo> list = StreamUtils.filter(voList, vo ->
- StringUtils.equals(vo.getDomain(), host));
- // 杩斿洖瀵硅薄
- LoginTenantVo vo = new LoginTenantVo();
- vo.setVoList(CollUtil.isNotEmpty(list) ? list : voList);
- vo.setTenantEnabled(TenantHelper.isEnable());
- return R.ok(vo);
+ StringUtils.equals(vo.getDomain(), host));
+ result.setVoList(CollUtil.isNotEmpty(list) ? list : voList);
+ return R.ok(result);
}
}
--
Gitblit v1.9.3