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;
024import java.util.Objects;
025
026import org.apache.hadoop.hbase.NamespaceDescriptor;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * Represents an authorization for access for the given namespace.
032 */
033@InterfaceAudience.Public
034public class NamespacePermission extends Permission {
035
036  private String namespace = NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR;
037
038  /**
039   * Construct a namespace permission.
040   * @param namespace namespace's name
041   * @param assigned assigned actions
042   */
043  NamespacePermission(String namespace, Action... assigned) {
044    super(assigned);
045    this.namespace = namespace;
046    this.scope = Scope.NAMESPACE;
047  }
048
049  public String getNamespace() {
050    return namespace;
051  }
052
053  /**
054   * check if given action is granted in given namespace.
055   * @param namespace namespace's name
056   * @param action action to be checked
057   * @return true if granted, false otherwise
058   */
059  public boolean implies(String namespace, Action action) {
060    return namespace.equals(this.namespace) && implies(action);
061  }
062
063  @Override
064  public boolean equalsExceptActions(Object obj) {
065    if (!(obj instanceof NamespacePermission)) {
066      return false;
067    }
068    NamespacePermission gp = (NamespacePermission) obj;
069    return namespace.equals(gp.namespace);
070  }
071
072  @Override
073  public int hashCode() {
074    return Objects.hash(namespace) + super.hashCode();
075  }
076
077  @Override
078  public boolean equals(Object obj) {
079    return equalsExceptActions(obj) && super.equals(obj);
080  }
081
082  @Override
083  public String toString() {
084    return "[NamespacePermission: " + rawExpression() + "]";
085  }
086
087  @Override
088  protected String rawExpression() {
089    StringBuilder raw = new StringBuilder("namespace=").append(namespace).append(", ");
090    return raw.toString() + super.rawExpression();
091  }
092
093  @Override
094  public void readFields(DataInput in) throws IOException {
095    super.readFields(in);
096    namespace = Bytes.toString(Bytes.readByteArray(in));
097  }
098
099  @Override
100  public void write(DataOutput out) throws IOException {
101    super.write(out);
102    Bytes.writeByteArray(out, Bytes.toBytes(namespace));
103  }
104}