PageRenderTime 31ms CodeModel.GetById 8ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 1ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/security/authorization/Privilege.java

#
Java | 189 lines | 138 code | 30 blank | 21 comment | 25 complexity | 82c2286221b515513a1554f5c39ca5f6 MD5 | raw file
  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, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18
 19package org.apache.hadoop.hive.ql.security.authorization;
 20
 21import java.util.EnumSet;
 22import org.apache.hadoop.hive.ql.parse.HiveParser;
 23
 24/**
 25 * Privilege defines a privilege in Hive. Each privilege has a name and scope associated with it.
 26 * This class contains all of the predefined privileges in Hive.
 27 */
 28public class Privilege {
 29  
 30  public enum PrivilegeType {
 31    ALL,
 32    ALTER_DATA,
 33    ALTER_METADATA,
 34    CREATE,
 35    DROP,
 36    INDEX,
 37    LOCK,
 38    SELECT,
 39    SHOW_DATABASE,
 40    UNKNOWN
 41  }
 42
 43
 44  public static PrivilegeType getPrivTypeByToken(int token) {
 45    switch (token) {
 46    case HiveParser.TOK_PRIV_ALL:
 47      return PrivilegeType.ALL;
 48    case HiveParser.TOK_PRIV_ALTER_DATA:
 49      return PrivilegeType.ALTER_DATA;
 50    case HiveParser.TOK_PRIV_ALTER_METADATA:
 51      return PrivilegeType.ALTER_METADATA;
 52    case HiveParser.TOK_PRIV_CREATE:
 53      return PrivilegeType.CREATE;
 54    case HiveParser.TOK_PRIV_DROP:
 55      return PrivilegeType.DROP;
 56    case HiveParser.TOK_PRIV_INDEX:
 57      return PrivilegeType.INDEX;
 58    case HiveParser.TOK_PRIV_LOCK:
 59      return PrivilegeType.LOCK;
 60    case HiveParser.TOK_PRIV_SELECT:
 61      return PrivilegeType.SELECT;
 62    case HiveParser.TOK_PRIV_SHOW_DATABASE:
 63      return PrivilegeType.SHOW_DATABASE;
 64    default:
 65      return PrivilegeType.UNKNOWN;
 66    }
 67  }
 68
 69  public static PrivilegeType getPrivTypeByName(String privilegeName) {
 70    String canonicalizedName = privilegeName.toLowerCase();
 71    if (canonicalizedName.equals("all")) {
 72      return PrivilegeType.ALL;
 73    } else if (canonicalizedName.equals("update")) {
 74      return PrivilegeType.ALTER_DATA;
 75    } else if (canonicalizedName.equals("alter")) {
 76      return PrivilegeType.ALTER_METADATA;
 77    } else if (canonicalizedName.equals("create")) {
 78      return PrivilegeType.CREATE;
 79    } else if (canonicalizedName.equals("drop")) {
 80      return PrivilegeType.DROP;
 81    } else if (canonicalizedName.equals("index")) {
 82      return PrivilegeType.INDEX;
 83    } else if (canonicalizedName.equals("lock")) {
 84      return PrivilegeType.LOCK;
 85    } else if (canonicalizedName.equals("select")) {
 86      return PrivilegeType.SELECT;
 87    } else if (canonicalizedName.equals("show_database")) {
 88      return PrivilegeType.SHOW_DATABASE;
 89    }
 90
 91    return PrivilegeType.UNKNOWN;
 92  }
 93
 94  private PrivilegeType priv;
 95  
 96  private EnumSet<PrivilegeScope> supportedScopeSet;
 97  
 98  private Privilege(PrivilegeType priv, EnumSet<PrivilegeScope> scopeSet) {
 99    super();
100    this.priv = priv;
101    this.supportedScopeSet = scopeSet;
102  }
103
104  public Privilege(PrivilegeType priv) {
105    super();
106    this.priv = priv;
107    
108  }
109
110  public PrivilegeType getPriv() {
111    return priv;
112  }
113
114  public void setPriv(PrivilegeType priv) {
115    this.priv = priv;
116  }
117  
118  public boolean supportColumnLevel() {
119    return supportedScopeSet != null
120        && supportedScopeSet.contains(PrivilegeScope.COLUMN_LEVEL_SCOPE);
121  }
122
123  public boolean supportDBLevel() {
124    return supportedScopeSet != null
125        && supportedScopeSet.contains(PrivilegeScope.DB_LEVEL_SCOPE);
126  }
127
128  public boolean supportTableLevel() {
129    return supportedScopeSet != null
130        && supportedScopeSet.contains(PrivilegeScope.TABLE_LEVEL_SCOPE);
131  }
132  
133  @Override
134  public String toString() {
135    switch (this.priv) {
136    case ALL:
137      return "All";
138    case ALTER_DATA:
139      return "Update";
140    case ALTER_METADATA:
141      return "Alter";
142    case CREATE:
143      return "Create";
144    case DROP:
145      return "Drop";
146    case INDEX:
147      return "Index";
148    case LOCK:
149      return "Lock";
150    case SELECT:
151      return "Select";
152    case SHOW_DATABASE:
153      return "Show_Database";
154    default:
155      return "Unknown";
156    }
157  }
158
159  public Privilege() {
160  }
161
162  public static Privilege ALL = new Privilege(PrivilegeType.ALL,
163      PrivilegeScope.ALLSCOPE_EXCEPT_COLUMN);
164
165  public static Privilege ALTER_METADATA = new Privilege(PrivilegeType.ALTER_METADATA,
166      PrivilegeScope.ALLSCOPE_EXCEPT_COLUMN);
167
168  public static Privilege ALTER_DATA = new Privilege(PrivilegeType.ALTER_DATA,
169      PrivilegeScope.ALLSCOPE_EXCEPT_COLUMN);
170
171  public static Privilege CREATE = new Privilege(PrivilegeType.CREATE,
172      PrivilegeScope.ALLSCOPE_EXCEPT_COLUMN);
173
174  public static Privilege DROP = new Privilege(PrivilegeType.DROP,
175      PrivilegeScope.ALLSCOPE_EXCEPT_COLUMN);
176
177  public static Privilege INDEX = new Privilege(PrivilegeType.INDEX,
178      PrivilegeScope.ALLSCOPE);
179
180  public static Privilege LOCK = new Privilege(PrivilegeType.LOCK,
181      PrivilegeScope.ALLSCOPE_EXCEPT_COLUMN);
182
183  public static Privilege SELECT = new Privilege(PrivilegeType.SELECT,
184      PrivilegeScope.ALLSCOPE);
185
186  public static Privilege SHOW_DATABASE = new Privilege(PrivilegeType.SHOW_DATABASE,
187      EnumSet.of(PrivilegeScope.USER_LEVEL_SCOPE));
188
189}