This is an automated email from the ASF dual-hosted git repository.

yx9o pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new cb0dd9995f6 Refactor PrivilegeProvider.build() as single Grantee 
(#33083)
cb0dd9995f6 is described below

commit cb0dd9995f6eea7d4bf223e32206541cbe616f75
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Oct 1 10:42:52 2024 +0800

    Refactor PrivilegeProvider.build() as single Grantee (#33083)
    
    * Refactor PrivilegeProvider.build() as single Grantee
    
    * Refactor PrivilegeProvider.build() as single Grantee
    
    * Refactor PrivilegeProvider.build() as single Grantee
    
    * Refactor PrivilegeProvider.build() as single Grantee
---
 .../authority/spi/PrivilegeProvider.java             | 11 ++++-------
 .../shardingsphere/authority/rule/AuthorityRule.java |  8 ++++++--
 .../authority/fixture/PrivilegeProviderFixture.java  |  9 ++-------
 .../database/DatabasePermittedPrivilegeProvider.java |  4 ++--
 .../DatabasePermittedPrivilegeProviderTest.java      | 20 ++++++--------------
 .../simple/AllPermittedPrivilegeProvider.java        |  6 ++----
 .../simple/AllPermittedPrivilegeProviderTest.java    |  7 ++-----
 7 files changed, 24 insertions(+), 41 deletions(-)

diff --git 
a/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeProvider.java
 
b/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeProvider.java
index 8b4309df5b2..9e6445a64ce 100644
--- 
a/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeProvider.java
+++ 
b/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeProvider.java
@@ -23,9 +23,6 @@ import org.apache.shardingsphere.infra.metadata.user.Grantee;
 import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
 
-import java.util.Collection;
-import java.util.Map;
-
 /**
  * Privilege provider.
  */
@@ -33,11 +30,11 @@ import java.util.Map;
 public interface PrivilegeProvider extends TypedSPI {
     
     /**
-     * Build grantee and privileges map.
+     * Build privileges.
      *
      * @param ruleConfig authority rule configuration
-     * @param grantees grantees
-     * @return grantee and privileges map
+     * @param grantee grantee
+     * @return privileges
      */
-    Map<Grantee, ShardingSpherePrivileges> build(AuthorityRuleConfiguration 
ruleConfig, Collection<Grantee> grantees);
+    ShardingSpherePrivileges build(AuthorityRuleConfiguration ruleConfig, 
Grantee grantee);
 }
diff --git 
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
 
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
index c84933702b8..0dc6a54be17 100644
--- 
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
+++ 
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
@@ -28,6 +28,7 @@ import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Optional;
 import java.util.stream.Collectors;
@@ -49,8 +50,11 @@ public final class AuthorityRule implements GlobalRule {
         configuration = ruleConfig;
         users = ruleConfig.getUsers().stream()
                 .map(each -> new ShardingSphereUser(each.getUsername(), 
each.getPassword(), each.getHostname(), each.getAuthenticationMethodName(), 
each.isAdmin())).collect(Collectors.toList());
-        privileges = TypedSPILoader.getService(PrivilegeProvider.class, 
ruleConfig.getPrivilegeProvider().getType(), 
ruleConfig.getPrivilegeProvider().getProps()).build(ruleConfig,
-                
users.stream().map(ShardingSphereUser::getGrantee).collect(Collectors.toList()));
+        privileges = new HashMap<>();
+        for (ShardingSphereUser each : users) {
+            privileges.put(each.getGrantee(), TypedSPILoader.getService(
+                    PrivilegeProvider.class, 
ruleConfig.getPrivilegeProvider().getType(), 
ruleConfig.getPrivilegeProvider().getProps()).build(ruleConfig, 
each.getGrantee()));
+        }
     }
     
     /**
diff --git 
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/fixture/PrivilegeProviderFixture.java
 
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/fixture/PrivilegeProviderFixture.java
index 12aa04bb1ff..9718ba2d324 100644
--- 
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/fixture/PrivilegeProviderFixture.java
+++ 
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/fixture/PrivilegeProviderFixture.java
@@ -23,19 +23,14 @@ import 
org.apache.shardingsphere.authority.spi.PrivilegeProvider;
 import org.apache.shardingsphere.infra.metadata.user.Grantee;
 import org.mockito.Answers;
 
-import java.util.Collection;
-import java.util.Map;
-import java.util.stream.Collectors;
-
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.withSettings;
 
 public final class PrivilegeProviderFixture implements PrivilegeProvider {
     
     @Override
-    public Map<Grantee, ShardingSpherePrivileges> build(final 
AuthorityRuleConfiguration ruleConfig, final Collection<Grantee> grantees) {
-        ShardingSpherePrivileges privileges = mockPrivileges();
-        return grantees.stream().collect(Collectors.toMap(each -> each, each 
-> privileges));
+    public ShardingSpherePrivileges build(final AuthorityRuleConfiguration 
ruleConfig, final Grantee grantee) {
+        return mockPrivileges();
     }
     
     private ShardingSpherePrivileges mockPrivileges() {
diff --git 
a/kernel/authority/provider/database/src/main/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProvider.java
 
b/kernel/authority/provider/database/src/main/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProvider.java
index 96e9a4c777f..042e23d1bd9 100644
--- 
a/kernel/authority/provider/database/src/main/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProvider.java
+++ 
b/kernel/authority/provider/database/src/main/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProvider.java
@@ -55,9 +55,9 @@ public final class DatabasePermittedPrivilegeProvider 
implements PrivilegeProvid
     }
     
     @Override
-    public Map<Grantee, ShardingSpherePrivileges> build(final 
AuthorityRuleConfiguration ruleConfig, final Collection<Grantee> grantees) {
+    public ShardingSpherePrivileges build(final AuthorityRuleConfiguration 
ruleConfig, final Grantee grantee) {
         Map<ShardingSphereUser, Collection<String>> userDatabasesMappings = 
convertToUserDatabasesMappings();
-        return grantees.stream().collect(Collectors.toMap(each -> each, each 
-> new DatabasePermittedPrivileges(getUserDatabases(each, 
userDatabasesMappings))));
+        return new DatabasePermittedPrivileges(getUserDatabases(grantee, 
userDatabasesMappings));
     }
     
     private Map<ShardingSphereUser, Collection<String>> 
convertToUserDatabasesMappings() {
diff --git 
a/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java
 
b/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java
index 148ecb9d4f1..6d5071a735b 100644
--- 
a/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java
+++ 
b/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.authority.provider.database;
 
 import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
 import org.apache.shardingsphere.authority.config.UserConfiguration;
-import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
 import org.apache.shardingsphere.authority.spi.PrivilegeProvider;
 import 
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
 import org.apache.shardingsphere.infra.metadata.user.Grantee;
@@ -31,12 +30,8 @@ import org.junit.jupiter.api.Test;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Map;
 import java.util.Properties;
-import java.util.stream.Collectors;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -54,15 +49,12 @@ class DatabasePermittedPrivilegeProviderTest {
                 new UserConfiguration("user1", "", "%", null, false),
                 new UserConfiguration("user3", "", "%", null, false));
         AuthorityRuleConfiguration ruleConfig = new 
AuthorityRuleConfiguration(userConfigs, mock(AlgorithmConfiguration.class), 
Collections.emptyMap(), null);
-        Collection<Grantee> grantees = userConfigs.stream().map(each -> new 
Grantee(each.getUsername(), each.getHostname())).collect(Collectors.toList());
-        Map<Grantee, ShardingSpherePrivileges> actual = 
provider.build(ruleConfig, grantees);
-        assertThat(actual.size(), is(4));
-        assertTrue(actual.get(new Grantee("root", 
"localhost")).hasPrivileges("sys_db"));
-        assertTrue(actual.get(new Grantee("user1", 
"127.0.0.1")).hasPrivileges("sys_db"));
-        assertTrue(actual.get(new Grantee("user1", 
"127.0.0.1")).hasPrivileges("foo_db"));
-        assertTrue(actual.get(new Grantee("user1", 
"%")).hasPrivileges("bar_db"));
-        assertFalse(actual.get(new Grantee("user1", 
"%")).hasPrivileges("sys_db"));
-        assertFalse(actual.get(new Grantee("user3", 
"%")).hasPrivileges("sys_db"));
+        assertTrue(provider.build(ruleConfig, new Grantee("root", 
"localhost")).hasPrivileges("sys_db"));
+        assertTrue(provider.build(ruleConfig, new Grantee("user1", 
"127.0.0.1")).hasPrivileges("sys_db"));
+        assertTrue(provider.build(ruleConfig, new Grantee("user1", 
"127.0.0.1")).hasPrivileges("foo_db"));
+        assertTrue(provider.build(ruleConfig, new Grantee("user1", 
"%")).hasPrivileges("bar_db"));
+        assertFalse(provider.build(ruleConfig, new Grantee("user1", 
"%")).hasPrivileges("sys_db"));
+        assertFalse(provider.build(ruleConfig, new Grantee("user3", 
"%")).hasPrivileges("sys_db"));
     }
     
     @Test
diff --git 
a/kernel/authority/provider/simple/src/main/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProvider.java
 
b/kernel/authority/provider/simple/src/main/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProvider.java
index 8ebf64cd1b8..eb770dd0b97 100644
--- 
a/kernel/authority/provider/simple/src/main/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProvider.java
+++ 
b/kernel/authority/provider/simple/src/main/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProvider.java
@@ -24,8 +24,6 @@ import org.apache.shardingsphere.infra.metadata.user.Grantee;
 
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Map;
-import java.util.stream.Collectors;
 
 /**
  * All permitted privilege provider.
@@ -33,8 +31,8 @@ import java.util.stream.Collectors;
 public final class AllPermittedPrivilegeProvider implements PrivilegeProvider {
     
     @Override
-    public Map<Grantee, ShardingSpherePrivileges> build(final 
AuthorityRuleConfiguration ruleConfig, final Collection<Grantee> grantees) {
-        return grantees.stream().collect(Collectors.toMap(each -> each, each 
-> new AllPermittedPrivileges()));
+    public ShardingSpherePrivileges build(final AuthorityRuleConfiguration 
ruleConfig, final Grantee grantee) {
+        return new AllPermittedPrivileges();
     }
     
     @Override
diff --git 
a/kernel/authority/provider/simple/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProviderTest.java
 
b/kernel/authority/provider/simple/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProviderTest.java
index a9b04495e4d..ab9ed6c1405 100644
--- 
a/kernel/authority/provider/simple/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProviderTest.java
+++ 
b/kernel/authority/provider/simple/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProviderTest.java
@@ -31,11 +31,9 @@ import org.junit.jupiter.params.provider.ArgumentsProvider;
 import org.junit.jupiter.params.provider.ArgumentsSource;
 
 import java.util.Collections;
-import java.util.Map;
 import java.util.stream.Stream;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.mockito.Mockito.mock;
 
@@ -46,9 +44,8 @@ class AllPermittedPrivilegeProviderTest {
     void assertBuild(final String name, final PrivilegeProvider provider) {
         AuthorityRuleConfiguration ruleConfig = new AuthorityRuleConfiguration(
                 Collections.singleton(new UserConfiguration("root", "", "%", 
null, false)), mock(AlgorithmConfiguration.class), Collections.emptyMap(), 
null);
-        Map<Grantee, ShardingSpherePrivileges> actual = 
provider.build(ruleConfig, Collections.singleton(new Grantee("root@%")));
-        assertThat(actual.size(), is(1));
-        assertThat(actual.get(new Grantee("root", "%")), 
instanceOf(AllPermittedPrivileges.class));
+        ShardingSpherePrivileges actual = provider.build(ruleConfig, new 
Grantee("root@%"));
+        assertThat(actual, instanceOf(AllPermittedPrivileges.class));
     }
     
     private static final class TestCaseArgumentsProvider implements 
ArgumentsProvider {

Reply via email to