Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
GrantRevokePrivStmt.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 
19 import com.cloudera.impala.thrift.TGrantRevokePrivParams;
20 import com.cloudera.impala.thrift.TPrivilege;
21 import com.google.common.base.Preconditions;
22 import com.google.common.base.Strings;
23 import com.google.common.collect.Lists;
24 
33 public class GrantRevokePrivStmt extends AuthorizationStmt {
35  private final String roleName_;
36  private final boolean isGrantPrivStmt_;
37  private final boolean hasGrantOpt_;
38 
39  // Set/modified during analysis
40  private Role role_;
41 
42  public GrantRevokePrivStmt(String roleName, PrivilegeSpec privilegeSpec,
43  boolean isGrantPrivStmt, boolean hasGrantOpt) {
44  Preconditions.checkNotNull(privilegeSpec);
45  Preconditions.checkNotNull(roleName);
46  privilegeSpec_ = privilegeSpec;
47  roleName_ = roleName;
48  isGrantPrivStmt_ = isGrantPrivStmt;
49  hasGrantOpt_ = hasGrantOpt;
50  }
51 
52  public TGrantRevokePrivParams toThrift() {
53  TGrantRevokePrivParams params = new TGrantRevokePrivParams();
54  params.setRole_name(roleName_);
55  params.setIs_grant(isGrantPrivStmt_);
56  TPrivilege privilege = privilegeSpec_.toThrift();
57  privilege.setRole_id(role_.getId());
58  privilege.setHas_grant_opt(hasGrantOpt_);
59  params.setPrivileges(Lists.newArrayList(privilege));
60  return params;
61  }
62 
63  @Override
64  public String toSql() {
65  StringBuilder sb = new StringBuilder(isGrantPrivStmt_ ? "GRANT " : "REVOKE ");
66  if (!isGrantPrivStmt_ && hasGrantOpt_) sb.append("GRANT OPTION FOR ");
67  sb.append(privilegeSpec_.toSql());
68  sb.append(isGrantPrivStmt_ ? " TO " : " FROM ");
69  sb.append(roleName_);
70  if (isGrantPrivStmt_ && hasGrantOpt_) sb.append(" WITH GRANT OPTION");
71  return sb.toString();
72  }
73 
74  @Override
75  public void analyze(Analyzer analyzer) throws AnalysisException {
76  super.analyze(analyzer);
77  if (Strings.isNullOrEmpty(roleName_)) {
78  throw new AnalysisException("Role name in GRANT/REVOKE privilege cannot be " +
79  "empty.");
80  }
81  role_ = analyzer.getCatalog().getAuthPolicy().getRole(roleName_);
82  if (role_ == null) {
83  throw new AnalysisException(String.format("Role '%s' does not exist.", roleName_));
84  }
85  privilegeSpec_.analyze(analyzer);
86  }
87 }
GrantRevokePrivStmt(String roleName, PrivilegeSpec privilegeSpec, boolean isGrantPrivStmt, boolean hasGrantOpt)