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