001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.hbase.security.access; 020 021import java.io.DataInput; 022import java.io.DataOutput; 023import java.io.IOException; 024 025import org.apache.hadoop.hbase.TableName; 026import org.apache.yetus.audience.InterfaceAudience; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029import org.apache.hadoop.hbase.util.Bytes; 030 031/** 032 * Represents an authorization for access over the given table, column family 033 * plus qualifier, for the given user. 034 */ 035@InterfaceAudience.Private 036public class UserPermission extends TablePermission { 037 private static final Logger LOG = LoggerFactory.getLogger(UserPermission.class); 038 039 private byte[] user; 040 041 /** Nullary constructor for Writable, do not use */ 042 public UserPermission() { 043 super(); 044 } 045 046 /** 047 * Creates a new instance for the given user. 048 * @param user the user 049 * @param assigned the list of allowed actions 050 */ 051 public UserPermission(byte[] user, Action... assigned) { 052 super(null, null, null, assigned); 053 this.user = user; 054 } 055 056 /** 057 * Creates a new instance for the given user, 058 * matching the actions with the given codes. 059 * @param user the user 060 * @param actionCodes the list of allowed action codes 061 */ 062 public UserPermission(byte[] user, byte[] actionCodes) { 063 super(null, null, null, actionCodes); 064 this.user = user; 065 } 066 067 /** 068 * Creates a new instance for the given user. 069 * @param user the user 070 * @param namespace 071 * @param assigned the list of allowed actions 072 */ 073 public UserPermission(byte[] user, String namespace, Action... assigned) { 074 super(namespace, assigned); 075 this.user = user; 076 } 077 078 /** 079 * Creates a new instance for the given user, 080 * matching the actions with the given codes. 081 * @param user the user 082 * @param namespace 083 * @param actionCodes the list of allowed action codes 084 */ 085 public UserPermission(byte[] user, String namespace, byte[] actionCodes) { 086 super(namespace, actionCodes); 087 this.user = user; 088 } 089 090 /** 091 * Creates a new instance for the given user, table and column family. 092 * @param user the user 093 * @param table the table 094 * @param family the family, can be null if action is allowed over the entire 095 * table 096 * @param assigned the list of allowed actions 097 */ 098 public UserPermission(byte[] user, TableName table, byte[] family, 099 Action... assigned) { 100 super(table, family, assigned); 101 this.user = user; 102 } 103 104 /** 105 * Creates a new permission for the given user, table, column family and 106 * column qualifier. 107 * @param user the user 108 * @param table the table 109 * @param family the family, can be null if action is allowed over the entire 110 * table 111 * @param qualifier the column qualifier, can be null if action is allowed 112 * over the entire column family 113 * @param assigned the list of allowed actions 114 */ 115 public UserPermission(byte[] user, TableName table, byte[] family, 116 byte[] qualifier, Action... assigned) { 117 super(table, family, qualifier, assigned); 118 this.user = user; 119 } 120 121 /** 122 * Creates a new instance for the given user, table, column family and 123 * qualifier, matching the actions with the given codes. 124 * @param user the user 125 * @param table the table 126 * @param family the family, can be null if action is allowed over the entire 127 * table 128 * @param qualifier the column qualifier, can be null if action is allowed 129 * over the entire column family 130 * @param actionCodes the list of allowed action codes 131 */ 132 public UserPermission(byte[] user, TableName table, byte[] family, 133 byte[] qualifier, byte[] actionCodes) { 134 super(table, family, qualifier, actionCodes); 135 this.user = user; 136 } 137 138 /** 139 * Creates a new instance for the given user, table, column family and 140 * qualifier, matching the actions with the given codes. 141 * @param user the user 142 * @param perm a TablePermission 143 */ 144 public UserPermission(byte[] user, TablePermission perm) { 145 super(perm.getNamespace(), perm.getTableName(), perm.getFamily(), perm.getQualifier(), 146 perm.actions); 147 this.user = user; 148 } 149 150 public byte[] getUser() { 151 return user; 152 } 153 154 /** 155 * Returns true if this permission describes a global user permission. 156 */ 157 public boolean isGlobal() { 158 return(!hasTable() && !hasNamespace()); 159 } 160 161 @Override 162 public boolean equals(Object obj) { 163 if (!(obj instanceof UserPermission)) { 164 return false; 165 } 166 UserPermission other = (UserPermission)obj; 167 168 if ((Bytes.equals(user, other.getUser()) && 169 super.equals(obj))) { 170 return true; 171 } else { 172 return false; 173 } 174 } 175 176 @Override 177 public int hashCode() { 178 final int prime = 37; 179 int result = super.hashCode(); 180 if (user != null) { 181 result = prime * result + Bytes.hashCode(user); 182 } 183 return result; 184 } 185 186 @Override 187 public String toString() { 188 StringBuilder str = new StringBuilder("UserPermission: ") 189 .append("user=").append(Bytes.toString(user)) 190 .append(", ").append(super.toString()); 191 return str.toString(); 192 } 193 194 @Override 195 public void readFields(DataInput in) throws IOException { 196 super.readFields(in); 197 user = Bytes.readByteArray(in); 198 } 199 200 @Override 201 public void write(DataOutput out) throws IOException { 202 super.write(out); 203 Bytes.writeByteArray(out, user); 204 } 205}