Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ShowGrantRoleStmt.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.TShowGrantRoleParams;
20 import com.google.common.base.Preconditions;
21 import com.google.common.base.Strings;
22 
26 public class ShowGrantRoleStmt extends AuthorizationStmt {
28  private final String roleName_;
29 
30  // Set/modified during analysis
31  private Role role_;
32 
33  public ShowGrantRoleStmt(String roleName, PrivilegeSpec privilegeSpec) {
34  Preconditions.checkNotNull(roleName);
35  roleName_ = roleName;
36  privilegeSpec_ = privilegeSpec;
37  }
38 
39  public TShowGrantRoleParams toThrift() {
40  TShowGrantRoleParams params = new TShowGrantRoleParams();
41  params.setRole_name(roleName_);
42  params.setRequesting_user(requestingUser_.getShortName());
43  if (privilegeSpec_ != null) {
44  params.setPrivilege(privilegeSpec_.toThrift());
45  params.getPrivilege().setRole_id(role_.getId());
46  }
47  return params;
48  }
49 
50  @Override
51  public String toSql() {
52  StringBuilder sb = new StringBuilder("SHOW GRANT ROLE ");
53  sb.append(roleName_);
54  if (privilegeSpec_ != null) sb.append(" " + privilegeSpec_.toSql());
55  return sb.toString();
56  }
57 
58  @Override
59  public void analyze(Analyzer analyzer) throws AnalysisException {
60  super.analyze(analyzer);
61  if (Strings.isNullOrEmpty(roleName_)) {
62  throw new AnalysisException("Role name in SHOW GRANT ROLE cannot be " +
63  "empty.");
64  }
65  role_ = analyzer.getCatalog().getAuthPolicy().getRole(roleName_);
66  if (role_ == null) {
67  throw new AnalysisException(String.format("Role '%s' does not exist.", roleName_));
68  }
69  if (privilegeSpec_ != null) privilegeSpec_.analyze(analyzer);
70  }
71 
72  public Role getRole() { return role_; }
73 }
ShowGrantRoleStmt(String roleName, PrivilegeSpec privilegeSpec)