View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Authority rule.
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       * Get authenticator type.
51       *
52       * @param user user
53       * @return authenticator type
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       * Find user.
67       *
68       * @param grantee grantee user
69       * @return found user
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       * Find privileges.
78       *
79       * @param grantee grantee
80       * @return found privileges
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  }