1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.authority.rule;
19
20 import lombok.Getter;
21 import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
22 import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
23 import org.apache.shardingsphere.authority.spi.PrivilegeProvider;
24 import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
25 import org.apache.shardingsphere.infra.metadata.user.Grantee;
26 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
27 import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
28 import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
29 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
30
31 import java.util.Map;
32 import java.util.Optional;
33
34
35
36
37 public final class AuthorityRule implements GlobalRule {
38
39 @Getter
40 private final AuthorityRuleConfiguration configuration;
41
42 private final Map<Grantee, ShardingSpherePrivileges> privileges;
43
44 public AuthorityRule(final AuthorityRuleConfiguration ruleConfig) {
45 configuration = ruleConfig;
46 privileges = TypedSPILoader.getService(PrivilegeProvider.class, ruleConfig.getPrivilegeProvider().getType(), ruleConfig.getPrivilegeProvider().getProps()).build(ruleConfig);
47 }
48
49
50
51
52
53
54
55 public String getAuthenticatorType(final ShardingSphereUser user) {
56 if (configuration.getAuthenticators().containsKey(user.getAuthenticationMethodName())) {
57 return configuration.getAuthenticators().get(user.getAuthenticationMethodName()).getType();
58 }
59 if (configuration.getAuthenticators().containsKey(configuration.getDefaultAuthenticator())) {
60 return configuration.getAuthenticators().get(configuration.getDefaultAuthenticator()).getType();
61 }
62 return "";
63 }
64
65
66
67
68
69
70
71 @HighFrequencyInvocation
72 public Optional<ShardingSphereUser> findUser(final Grantee grantee) {
73 return configuration.getUsers().stream().filter(each -> each.getGrantee().accept(grantee)).findFirst();
74 }
75
76
77
78
79
80
81
82 @HighFrequencyInvocation
83 public Optional<ShardingSpherePrivileges> findPrivileges(final Grantee grantee) {
84 return privileges.keySet().stream().filter(each -> each.accept(grantee)).findFirst().map(privileges::get);
85 }
86
87 @Override
88 public RuleAttributes getAttributes() {
89 return new RuleAttributes();
90 }
91 }