File tree Expand file tree Collapse file tree
framework/framework_base/src/main/java/com/github/cadecode/uniboot/framework/base/plugin/handler Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .github .cadecode .uniboot .framework .base .plugin .handler ;
2+
3+ import cn .hutool .core .util .ObjUtil ;
4+ import com .github .cadecode .uniboot .common .plugin .mybatis .annotation .UseDataScope ;
5+ import com .github .cadecode .uniboot .common .plugin .mybatis .config .MybatisConfig ;
6+ import com .github .cadecode .uniboot .common .plugin .mybatis .handler .DataScopeResolver ;
7+ import com .github .cadecode .uniboot .framework .base .security .model .SysUserDetails ;
8+ import com .github .cadecode .uniboot .framework .base .util .SecurityUtil ;
9+ import org .springframework .boot .autoconfigure .AutoConfigureBefore ;
10+ import org .springframework .stereotype .Component ;
11+
12+ import java .util .List ;
13+ import java .util .Objects ;
14+ import java .util .stream .Collectors ;
15+
16+ /**
17+ * 基于用户角色的数据权限范围解析器
18+ *
19+ * @author Cade Li
20+ * @since 2024/5/23
21+ */
22+ @ AutoConfigureBefore (MybatisConfig .class )
23+ @ Component (UseDataScope .DEFAULT_RESOLVER_NAME )
24+ public class UserRoleDataScopeResolver implements DataScopeResolver {
25+
26+ public static final String SCOPE_ROLE_PREFIX = "DATA:" ;
27+
28+ @ Override
29+ public List <Object > getScopes () {
30+ SysUserDetails userDetails = SecurityUtil .getUserDetails (null );
31+ if (Objects .isNull (userDetails )) {
32+ return null ;
33+ }
34+ if (ObjUtil .isEmpty (userDetails .getRoles ())) {
35+ return null ;
36+ }
37+ return userDetails .getRoles ()
38+ .stream ()
39+ .filter (o -> Objects .nonNull (o ) && o .startsWith (SCOPE_ROLE_PREFIX ))
40+ .map (o -> (Object ) o )
41+ .collect (Collectors .toList ());
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments