-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
130a617
commit 87df9b8
Showing
10 changed files
with
185 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...sphereex/dbplusengine/authority/distsql/handler/update/AlterDistUserSetAdminExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.sphereex.dbplusengine.authority.distsql.handler.update; | ||
|
||
import com.sphereex.dbplusengine.authority.distsql.handler.converter.AuthorityStatementConverter; | ||
import com.sphereex.dbplusengine.authority.distsql.segment.DistUserSegment; | ||
import com.sphereex.dbplusengine.authority.distsql.statement.user.AlterDistUserSetAdminStatement; | ||
import com.sphereex.dbplusengine.authority.exception.definition.MissingRequiredUserException; | ||
import com.sphereex.dbplusengine.distsql.handler.aware.DistSQLExecutorGranteeAware; | ||
import lombok.Setter; | ||
import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration; | ||
import org.apache.shardingsphere.authority.config.UserConfiguration; | ||
import org.apache.shardingsphere.authority.rule.AuthorityRule; | ||
import org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.global.GlobalRuleDefinitionExecutor; | ||
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; | ||
import org.apache.shardingsphere.infra.metadata.user.Grantee; | ||
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Alter dist user set admin executor. | ||
*/ | ||
@Setter | ||
public final class AlterDistUserSetAdminExecutor implements GlobalRuleDefinitionExecutor<AlterDistUserSetAdminStatement, AuthorityRule>, DistSQLExecutorGranteeAware { | ||
|
||
private AuthorityRule rule; | ||
|
||
private Grantee grantee; | ||
|
||
@Override | ||
public void checkBeforeUpdate(final AlterDistUserSetAdminStatement sqlStatement) { | ||
if (sqlStatement.isAdmin()) { | ||
checkNoAdmins(); | ||
} else { | ||
checkCurrentUserIsAdmin(); | ||
checkTargetUserIsAdmin(sqlStatement); | ||
} | ||
checkUserExist(sqlStatement); | ||
} | ||
|
||
private void checkNoAdmins() { | ||
Collection<UserConfiguration> users = rule.getConfiguration().getUsers(); | ||
ShardingSpherePreconditions.checkState(users.stream().noneMatch(UserConfiguration::isAdmin), | ||
() -> new UnsupportedOperationException("Unsupported when there is already an admin user")); | ||
} | ||
|
||
private void checkCurrentUserIsAdmin() { | ||
Optional<ShardingSphereUser> currentUser = rule.findUser(grantee); | ||
ShardingSpherePreconditions.checkState(currentUser.isPresent() && currentUser.get().isAdmin(), | ||
() -> new UnsupportedOperationException("Unsupported because current user is not admin")); | ||
} | ||
|
||
private void checkTargetUserIsAdmin(final AlterDistUserSetAdminStatement sqlStatement) { | ||
Optional<Grantee> toBeAlteredGrantee = rule.getGrantees().stream().filter(each -> each.accept(new Grantee(sqlStatement.getUser().getUser(), sqlStatement.getUser().getHost()))).findFirst(); | ||
ShardingSpherePreconditions.checkState(toBeAlteredGrantee.isPresent(), () -> new MissingRequiredUserException(Collections.singleton(grantee))); | ||
Optional<ShardingSphereUser> toBeAlteredUser = rule.findUser(toBeAlteredGrantee.get()); | ||
ShardingSpherePreconditions.checkState(toBeAlteredUser.isPresent(), () -> new MissingRequiredUserException(Collections.singleton(grantee))); | ||
ShardingSpherePreconditions.checkState(toBeAlteredUser.get().isAdmin(), | ||
() -> new UnsupportedOperationException("Unsupported because to be altered user is not admin")); | ||
} | ||
|
||
private void checkUserExist(final AlterDistUserSetAdminStatement sqlStatement) { | ||
DistUserSegment userSegment = sqlStatement.getUser(); | ||
Grantee grantee = new Grantee(userSegment.getUser(), userSegment.getHost()); | ||
ShardingSpherePreconditions.checkContains(rule.getGrantees(), grantee, () -> new MissingRequiredUserException(Collections.singleton(grantee))); | ||
} | ||
|
||
@Override | ||
public AuthorityRuleConfiguration buildToBeAlteredRuleConfiguration(final AlterDistUserSetAdminStatement sqlStatement) { | ||
Optional<Grantee> toBeAlteredGrantee = rule.getGrantees().stream().filter(each -> each.accept(new Grantee(sqlStatement.getUser().getUser(), sqlStatement.getUser().getHost()))).findFirst(); | ||
ShardingSpherePreconditions.checkState(toBeAlteredGrantee.isPresent(), () -> new MissingRequiredUserException(Collections.singleton(grantee))); | ||
Optional<ShardingSphereUser> toBeAlteredUser = rule.findUser(toBeAlteredGrantee.get()); | ||
ShardingSpherePreconditions.checkState(toBeAlteredUser.isPresent(), () -> new MissingRequiredUserException(Collections.singleton(grantee))); | ||
AuthorityRuleConfiguration result = AuthorityStatementConverter.createAuthorityRuleConfiguration(rule.getConfiguration()); | ||
result.getUsers().removeIf(each -> new Grantee(each.getUsername(), each.getHostname()).equals(toBeAlteredUser.get().getGrantee())); | ||
result.getUsers().add(new UserConfiguration(toBeAlteredGrantee.get().getUsername(), | ||
toBeAlteredUser.get().getPassword(), toBeAlteredGrantee.get().getHostname(), toBeAlteredUser.get().getAuthenticationMethodName(), sqlStatement.isAdmin())); | ||
return result; | ||
} | ||
|
||
@Override | ||
public Class<AuthorityRule> getRuleClass() { | ||
return AuthorityRule.class; | ||
} | ||
|
||
@Override | ||
public Class<AlterDistUserSetAdminStatement> getType() { | ||
return AlterDistUserSetAdminStatement.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -254,3 +254,11 @@ FROM | |
ALL | ||
: A L L | ||
; | ||
|
||
SET | ||
: S E T | ||
; | ||
|
||
ADMIN | ||
: A D M I N | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...phereex/dbplusengine/authority/distsql/statement/user/AlterDistUserSetAdminStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.sphereex.dbplusengine.authority.distsql.statement.user; | ||
|
||
import com.sphereex.dbplusengine.authority.distsql.segment.DistUserSegment; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.shardingsphere.distsql.statement.rdl.rule.global.GlobalRuleDefinitionStatement; | ||
import org.apache.shardingsphere.sql.parser.statement.core.statement.dcl.DCLStatement; | ||
|
||
/** | ||
* Alter dist user set admin statement. | ||
*/ | ||
@RequiredArgsConstructor | ||
@Getter | ||
public final class AlterDistUserSetAdminStatement extends GlobalRuleDefinitionStatement implements DCLStatement { | ||
|
||
private final DistUserSegment user; | ||
|
||
private final boolean admin; | ||
} |