|
167 | 167 | import java.util.Collections; |
168 | 168 | import java.util.List; |
169 | 169 | import java.util.Objects; |
| 170 | +import java.util.StringJoiner; |
170 | 171 | import java.util.function.Supplier; |
171 | 172 | import java.util.stream.Collectors; |
172 | 173 |
|
@@ -668,31 +669,12 @@ public TSStatus visitAuthor(AuthorStatement statement, TreeAccessCheckContext co |
668 | 669 | auditObject)) { |
669 | 670 | return RpcUtils.SUCCESS_STATUS; |
670 | 671 | } |
671 | | - for (String s : statement.getPrivilegeList()) { |
672 | | - PrivilegeType privilegeType = PrivilegeType.valueOf(s.toUpperCase()); |
673 | | - if (privilegeType.isSystemPrivilege()) { |
674 | | - if (!checkHasGlobalAuth(context, privilegeType, auditObject, true)) { |
675 | | - return AuthorityChecker.getTSStatus( |
676 | | - false, |
677 | | - "Has no permission to execute " |
678 | | - + authorType |
679 | | - + ", please ensure you have these privileges and the grant option is TRUE when granted)"); |
680 | | - } |
681 | | - } else if (privilegeType.isPathPrivilege()) { |
682 | | - if (!AuthorityChecker.checkPathPermissionGrantOption( |
683 | | - context.getUsername(), privilegeType, statement.getNodeNameList())) { |
684 | | - return AuthorityChecker.getTSStatus( |
685 | | - false, |
686 | | - "Has no permission to execute " |
687 | | - + authorType |
688 | | - + ", please ensure you have these privileges and the grant option is TRUE when granted)"); |
689 | | - } |
690 | | - } else { |
691 | | - return AuthorityChecker.getTSStatus( |
692 | | - false, "Not support Relation statement in tree sql_dialect"); |
693 | | - } |
694 | | - } |
695 | | - return RpcUtils.SUCCESS_STATUS; |
| 672 | + return checkPermissionsWithGrantOption( |
| 673 | + context, |
| 674 | + Arrays.stream(statement.getPrivilegeList()) |
| 675 | + .map(PrivilegeType::valueOf) |
| 676 | + .collect(Collectors.toList()), |
| 677 | + statement.getNodeNameList()); |
696 | 678 | default: |
697 | 679 | throw new IllegalArgumentException("Unknown authorType: " + authorType); |
698 | 680 | } |
@@ -1997,6 +1979,58 @@ protected boolean checkHasGlobalAuth( |
1997 | 1979 | return result; |
1998 | 1980 | } |
1999 | 1981 |
|
| 1982 | + protected TSStatus checkPermissionsWithGrantOption( |
| 1983 | + IAuditEntity auditEntity, List<PrivilegeType> privilegeList, List<PartialPath> paths) { |
| 1984 | + Supplier<String> supplier = |
| 1985 | + () -> { |
| 1986 | + StringJoiner joiner = new StringJoiner(" "); |
| 1987 | + if (paths != null) { |
| 1988 | + paths.forEach(path -> joiner.add(path.getFullPath())); |
| 1989 | + } |
| 1990 | + return joiner.toString(); |
| 1991 | + }; |
| 1992 | + auditEntity.setPrivilegeTypes(privilegeList); |
| 1993 | + if (AuthorityChecker.SUPER_USER.equals(auditEntity.getUsername())) { |
| 1994 | + recordObjectAuthenticationAuditLog(auditEntity.setResult(true), supplier); |
| 1995 | + return SUCCEED; |
| 1996 | + } |
| 1997 | + TSStatus status = SUCCEED; |
| 1998 | + for (PrivilegeType privilegeType : privilegeList) { |
| 1999 | + if (privilegeType.isSystemPrivilege()) { |
| 2000 | + if (!AuthorityChecker.checkSystemPermissionGrantOption( |
| 2001 | + auditEntity.getUsername(), privilegeType)) { |
| 2002 | + status = |
| 2003 | + AuthorityChecker.getTSStatus( |
| 2004 | + false, |
| 2005 | + "Has no permission to execute " |
| 2006 | + + privilegeType |
| 2007 | + + ", please ensure you have these privileges and the grant option is TRUE when granted)"); |
| 2008 | + break; |
| 2009 | + } |
| 2010 | + } else if (privilegeType.isPathPrivilege()) { |
| 2011 | + if (!AuthorityChecker.checkPathPermissionGrantOption( |
| 2012 | + auditEntity.getUsername(), privilegeType, paths)) { |
| 2013 | + status = |
| 2014 | + AuthorityChecker.getTSStatus( |
| 2015 | + false, |
| 2016 | + "Has no permission to execute " |
| 2017 | + + privilegeType |
| 2018 | + + ", please ensure you have these privileges and the grant option is TRUE when granted)"); |
| 2019 | + break; |
| 2020 | + } |
| 2021 | + } else { |
| 2022 | + status = |
| 2023 | + AuthorityChecker.getTSStatus( |
| 2024 | + false, "Not support Relation statement in tree sql_dialect"); |
| 2025 | + break; |
| 2026 | + } |
| 2027 | + } |
| 2028 | + recordObjectAuthenticationAuditLog( |
| 2029 | + auditEntity.setResult(status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()), |
| 2030 | + supplier); |
| 2031 | + return status; |
| 2032 | + } |
| 2033 | + |
2000 | 2034 | protected TSStatus checkWriteOnReadOnlyPath(IAuditEntity auditEntity, PartialPath path) { |
2001 | 2035 | if (includeByAuditTreeDB(path) |
2002 | 2036 | && !AuthorityChecker.INTERNAL_AUDIT_USER.equals(path.getFullPath())) { |
|
0 commit comments