View Javadoc

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  
19  package org.apache.hadoop.hbase.security.access;
20  
21  import java.io.DataInput;
22  import java.io.DataOutput;
23  import java.io.IOException;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.util.Bytes;
30  
31  /**
32   * Represents an authorization for access over the given table, column family
33   * plus qualifier, for the given user.
34   */
35  @InterfaceAudience.Private
36  public class UserPermission extends TablePermission {
37    private static Log LOG = LogFactory.getLog(UserPermission.class);
38  
39    private byte[] user;
40  
41    /** Nullary constructor for Writable, do not use */
42    public UserPermission() {
43      super();
44    }
45  
46    /**
47     * Creates a new instance for the given user.
48     * @param user the user
49     * @param assigned the list of allowed actions
50     */
51    public UserPermission(byte[] user, Action... assigned) {
52      super(null, null, null, assigned);
53      this.user = user;
54    }
55  
56    /**
57     * Creates a new instance for the given user,
58     * matching the actions with the given codes.
59     * @param user the user
60     * @param actionCodes the list of allowed action codes
61     */
62    public UserPermission(byte[] user, byte[] actionCodes) {
63      super(null, null, null, actionCodes);
64      this.user = user;
65    }
66  
67    /**
68     * Creates a new instance for the given user.
69     * @param user the user
70     * @param namespace
71     * @param assigned the list of allowed actions
72     */
73    public UserPermission(byte[] user, String namespace, Action... assigned) {
74      super(namespace, assigned);
75      this.user = user;
76    }
77  
78    /**
79     * Creates a new instance for the given user,
80     * matching the actions with the given codes.
81     * @param user the user
82     * @param namespace
83     * @param actionCodes the list of allowed action codes
84     */
85    public UserPermission(byte[] user, String namespace, byte[] actionCodes) {
86      super(namespace, actionCodes);
87      this.user = user;
88    }
89  
90    /**
91     * Creates a new instance for the given user, table and column family.
92     * @param user the user
93     * @param table the table
94     * @param family the family, can be null if action is allowed over the entire
95     *   table
96     * @param assigned the list of allowed actions
97     */
98    public UserPermission(byte[] user, TableName table, byte[] family,
99                          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 }