|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.confignode.manager.pipe.extractor; |
| 21 | + |
| 22 | +import org.apache.iotdb.commons.auth.entity.PrivilegeType; |
| 23 | +import org.apache.iotdb.confignode.consensus.request.ConfigPhysicalPlan; |
| 24 | +import org.apache.iotdb.confignode.consensus.request.ConfigPhysicalPlanType; |
| 25 | +import org.apache.iotdb.confignode.consensus.request.ConfigPhysicalPlanVisitor; |
| 26 | +import org.apache.iotdb.confignode.consensus.request.write.auth.AuthorRelationalPlan; |
| 27 | +import org.apache.iotdb.confignode.consensus.request.write.auth.AuthorTreePlan; |
| 28 | + |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.Optional; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +public class PipeConfigPhysicalPlanTableScopeParseVisitor |
| 35 | + extends ConfigPhysicalPlanVisitor<Optional<ConfigPhysicalPlan>, Void> { |
| 36 | + @Override |
| 37 | + public Optional<ConfigPhysicalPlan> visitPlan(final ConfigPhysicalPlan plan, final Void context) { |
| 38 | + return Optional.of(plan); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public Optional<ConfigPhysicalPlan> visitRGrantUserAll( |
| 43 | + final AuthorRelationalPlan plan, final Void context) { |
| 44 | + return visitTableAuthorPlan(plan, ConfigPhysicalPlanType.GrantUser); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Optional<ConfigPhysicalPlan> visitRGrantRoleAll( |
| 49 | + final AuthorRelationalPlan plan, final Void context) { |
| 50 | + return visitTableAuthorPlan(plan, ConfigPhysicalPlanType.GrantRole); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Optional<ConfigPhysicalPlan> visitRRevokeUserAll( |
| 55 | + final AuthorRelationalPlan plan, final Void context) { |
| 56 | + return visitTableAuthorPlan(plan, ConfigPhysicalPlanType.RevokeUser); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Optional<ConfigPhysicalPlan> visitRRevokeRoleAll( |
| 61 | + final AuthorRelationalPlan plan, final Void context) { |
| 62 | + return visitTableAuthorPlan(plan, ConfigPhysicalPlanType.RevokeRole); |
| 63 | + } |
| 64 | + |
| 65 | + private Optional<ConfigPhysicalPlan> visitTableAuthorPlan( |
| 66 | + final AuthorRelationalPlan authorRelationalPlan, final ConfigPhysicalPlanType type) { |
| 67 | + final Set<Integer> permissions = |
| 68 | + authorRelationalPlan.getPermissions().stream() |
| 69 | + .filter(permission -> PrivilegeType.values()[permission].forRelationalSys()) |
| 70 | + .collect(Collectors.toSet()); |
| 71 | + return !permissions.isEmpty() |
| 72 | + ? Optional.of( |
| 73 | + new AuthorTreePlan( |
| 74 | + type, |
| 75 | + authorRelationalPlan.getUserName(), |
| 76 | + authorRelationalPlan.getRoleName(), |
| 77 | + authorRelationalPlan.getPassword(), |
| 78 | + authorRelationalPlan.getNewPassword(), |
| 79 | + permissions, |
| 80 | + authorRelationalPlan.getGrantOpt(), |
| 81 | + Collections.emptyList())) |
| 82 | + : Optional.empty(); |
| 83 | + } |
| 84 | +} |
0 commit comments