Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
GrantRevokeRoleStmt.java
Go to the documentation of this file.
1 // Copyright 2014 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.analysis;
16 
18 import com.cloudera.impala.thrift.TGrantRevokeRoleParams;
19 import com.google.common.base.Preconditions;
20 import com.google.common.base.Strings;
21 import com.google.common.collect.Lists;
22 
26 public class GrantRevokeRoleStmt extends AuthorizationStmt {
27  private final String roleName_;
28  private final String groupName_;
29  private final boolean isGrantStmt_;
30 
31  public GrantRevokeRoleStmt(String roleName, String groupName, boolean isGrantStmt) {
32  Preconditions.checkNotNull(roleName);
33  Preconditions.checkNotNull(groupName);
34  roleName_ = roleName;
35  groupName_ = groupName;
36  isGrantStmt_ = isGrantStmt;
37  }
38 
39  @Override
40  public String toSql() {
41  if (isGrantStmt_) {
42  return String.format("GRANT ROLE %s TO %s", roleName_, groupName_);
43  } else {
44  return String.format("REVOKE ROLE %s FROM %s", roleName_, groupName_);
45  }
46  }
47 
48  public TGrantRevokeRoleParams toThrift() {
49  TGrantRevokeRoleParams params = new TGrantRevokeRoleParams();
50  params.setRole_names(Lists.newArrayList(roleName_));
51  params.setGroup_names(Lists.newArrayList(groupName_));
52  params.setIs_grant(isGrantStmt_);
53  return params;
54  }
55 
56  @Override
57  public void analyze(Analyzer analyzer) throws AnalysisException {
58  super.analyze(analyzer);
59  if (analyzer.getCatalog().getAuthPolicy().getRole(roleName_) == null) {
60  throw new AnalysisException(String.format("Role '%s' does not exist.", roleName_));
61  }
62  if (Strings.isNullOrEmpty(roleName_)) {
63  throw new AnalysisException("Role name in GRANT/REVOKE ROLE cannot be empty.");
64  }
65  if (Strings.isNullOrEmpty(groupName_)) {
66  throw new AnalysisException("Group name in GRANT/REVOKE ROLE cannot be empty.");
67  }
68  }
69 }
GrantRevokeRoleStmt(String roleName, String groupName, boolean isGrantStmt)