@@ -217,7 +217,79 @@ public Result<ValidationResult> validateToken(@RequestParam String token) {
217217 return Result .success (new ValidationResult (false , null ));
218218 }
219219
220+ /**
221+ * 设置当前组织
222+ *
223+ * @param tenantId the tenantId
224+ * @return result
225+ */
226+ @ Operation (summary = "设置当前组织" , description = "设置当前组织" ,
227+ parameters = {
228+ @ Parameter (name = "tenantId" , description = "组织id" )
229+ }, responses = {
230+ @ ApiResponse (responseCode = "200" , description = "返回信息" ,
231+ content = @ Content (mediaType = "application/json" ,
232+ schema = @ Schema (implementation = App .class ))),
233+ @ ApiResponse (responseCode = "400" , description = "请求失败" )
234+ })
235+
236+ @ SystemControllerLog (description = "设置当前组织" )
237+ @ GetMapping ("/user/tenant" )
238+ public Result <SSOTicket > setTenant (@ RequestParam Integer tenantId ) {
239+ int userIdInt ;
240+ String userId = loginUserContext .getLoginUserId ();
241+ try {
242+ userIdInt = Integer .parseInt (userId );
243+ } catch (NumberFormatException e ) {
244+ return Result .failed (ExceptionEnum .CM342 );
245+ }
246+ List <Tenant > tenants = authUsersUnitsRolesMapper .queryAllTenantByUserId (userIdInt );
220247
248+ if (tenantId == null ) {
249+ return Result .failed (ExceptionEnum .CM320 );
250+ }
251+
252+ if (tenants == null || tenants .isEmpty ()) {
253+ return Result .failed (ExceptionEnum .CM337 );
254+ }
255+ List <Tenant > tenantList = new ArrayList <>();
256+ boolean found = false ;
257+ for (Tenant tenant : tenants ) {
258+ if (tenant .getId ().equals (tenantId .toString ())) {
259+ tenant .setIsInUse (true );
260+ found = true ;
261+ } else {
262+ tenant .setIsInUse (false );
263+ }
264+
265+ tenantList .add (tenant );
266+ }
267+
268+ if (!found ) {
269+ return Result .failed (ExceptionEnum .CM341 );
270+ }
271+ //存储当前组织到LoginUserContext
272+ UserInfo currentUser = DefaultLoginUserContext .getCurrentUser ();
273+ currentUser .setTenants (tenantList );
274+ DefaultLoginUserContext .setCurrentUser (currentUser );
275+
276+ // 通过 RequestContextHolder 获取请求
277+ HttpServletRequest request = ((ServletRequestAttributes ) RequestContextHolder .currentRequestAttributes ())
278+ .getRequest ();
279+ String authHeader = request .getHeader ("Authorization" );
280+ String headerToken = jwtUtil .getTokenFromRequest (authHeader );
281+ if (headerToken == null || headerToken .isEmpty ()) {
282+ return Result .failed (ExceptionEnum .CM336 );
283+ }
284+
285+ // 创建SSO票据
286+ SSOTicket ticket = new SSOTicket ();
287+ ticket .setToken (headerToken );
288+ ticket .setUsername (DefaultLoginUserContext .getCurrentUser ().getUsername ());
289+ ticket .setExpireTime (System .currentTimeMillis () + 3600000 );
290+
291+ return Result .success (ticket );
292+ }
221293 private boolean authenticate (String salt , String password , String userPassword ) throws Exception {
222294 return SM3PasswordUtil .verifyPassword (password , userPassword , salt );
223295 }
0 commit comments