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.constant.AuthorityOrder;
23 import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
24 import org.apache.shardingsphere.authority.spi.PrivilegeProvider;
25 import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
26 import org.apache.shardingsphere.infra.metadata.user.Grantee;
27 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
28 import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
29 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
30
31 import java.util.Collection;
32 import java.util.LinkedHashMap;
33 import java.util.Map;
34 import java.util.Optional;
35 import java.util.stream.Collectors;
36
37
38
39
40 public final class AuthorityRule implements GlobalRule {
41
42 @Getter
43 private final AuthorityRuleConfiguration configuration;
44
45 private final Map<ShardingSphereUser, ShardingSpherePrivileges> privileges;
46
47 public AuthorityRule(final AuthorityRuleConfiguration ruleConfig) {
48 configuration = ruleConfig;
49 Collection<ShardingSphereUser> users = ruleConfig.getUsers().stream()
50 .map(each -> new ShardingSphereUser(each.getUsername(), each.getPassword(), each.getHostname(), each.getAuthenticationMethodName(), each.isAdmin())).collect(Collectors.toList());
51 privileges = users.stream().collect(Collectors.toMap(each -> each,
52 each -> TypedSPILoader.getService(PrivilegeProvider.class, ruleConfig.getPrivilegeProvider().getType(), ruleConfig.getPrivilegeProvider().getProps())
53 .build(ruleConfig, each.getGrantee()),
54 (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
55 }
56
57
58
59
60
61
62
63 public String getAuthenticatorType(final ShardingSphereUser user) {
64 if (configuration.getAuthenticators().containsKey(user.getAuthenticationMethodName())) {
65 return configuration.getAuthenticators().get(user.getAuthenticationMethodName()).getType();
66 }
67 if (configuration.getAuthenticators().containsKey(configuration.getDefaultAuthenticator())) {
68 return configuration.getAuthenticators().get(configuration.getDefaultAuthenticator()).getType();
69 }
70 return "";
71 }
72
73
74
75
76
77
78 public Collection<Grantee> getGrantees() {
79 return privileges.keySet().stream().map(ShardingSphereUser::getGrantee).collect(Collectors.toList());
80 }
81
82
83
84
85
86
87
88 @HighFrequencyInvocation
89 public Optional<ShardingSphereUser> findUser(final Grantee grantee) {
90 for (ShardingSphereUser each : privileges.keySet()) {
91 if (each.getGrantee().accept(grantee)) {
92 return Optional.of(each);
93 }
94 }
95 return Optional.empty();
96 }
97
98
99
100
101
102
103
104 @HighFrequencyInvocation
105 public Optional<ShardingSpherePrivileges> findPrivileges(final Grantee grantee) {
106 for (ShardingSphereUser each : privileges.keySet()) {
107 if (each.getGrantee().accept(grantee)) {
108 return Optional.of(each).map(privileges::get);
109 }
110 }
111 return Optional.empty();
112 }
113
114 @Override
115 public int getOrder() {
116 return AuthorityOrder.ORDER;
117 }
118 }