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.util.Objects;
022
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * UserPermission consists of a user name and a permission.
027 * Permission can be one of [Global, Namespace, Table] permission.
028 */
029@InterfaceAudience.Public
030public class UserPermission {
031
032  private String user;
033  private Permission permission;
034
035  /**
036   * Construct a user permission given permission.
037   * @param user user name
038   * @param permission one of [Global, Namespace, Table] permission
039   */
040  public UserPermission(String user, Permission permission) {
041    this.user = user;
042    this.permission = permission;
043  }
044
045  /**
046   * Get this permission access scope.
047   * @return access scope
048   */
049  public Permission.Scope getAccessScope() {
050    return permission.getAccessScope();
051  }
052
053  public String getUser() {
054    return user;
055  }
056
057  public Permission getPermission() {
058    return permission;
059  }
060
061  public boolean equalsExceptActions(Object obj) {
062    if (!(obj instanceof UserPermission)) {
063      return false;
064    }
065    UserPermission other = (UserPermission) obj;
066    return user.equals(other.user) && permission.equalsExceptActions(other.permission);
067  }
068
069  @Override
070  public boolean equals(Object obj) {
071    if (!(obj instanceof UserPermission)) {
072      return false;
073    }
074    UserPermission other = (UserPermission) obj;
075    return user.equals(other.user) && permission.equals(other.permission);
076  }
077
078  @Override
079  public int hashCode() {
080    final int prime = 37;
081    int result = permission.hashCode();
082    if (user != null) {
083      result = prime * result + Objects.hashCode(user);
084    }
085    return result;
086  }
087
088  @Override
089  public String toString() {
090    StringBuilder str = new StringBuilder("UserPermission: ")
091        .append("user=").append(user)
092        .append(", ").append(permission.toString());
093    return str.toString();
094  }
095}