org.apache.shiro.biz.authz.permission.BitPermission Maven / Gradle / Ivy
/*
* Copyright (c) 2018 (https://github.com/hiwepy).
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.apache.shiro.biz.authz.permission;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.util.StringUtils;
/**
* 规则
* +资源字符串+权限位+实例ID
*
* 以+开头 中间通过+分割
*
* 权限:
* 0 表示所有权限
* 1 新增 0001
* 2 修改 0010
* 4 删除 0100
* 8 查看 1000
*
* 如 +user+10 表示对资源user拥有修改/查看权限
*
* 不考虑一些异常情况
*
* User: Zhang Kaitao
*
Date: 14-1-26
*
Version: 1.0
*/
public class BitPermission implements Permission {
private String resourceIdentify;
private int permissionBit;
private String instanceId;
public BitPermission(String permissionString) {
String[] array = permissionString.split("\\+");
if(array.length > 1) {
resourceIdentify = array[1];
}
if(!StringUtils.hasLength(resourceIdentify)) {
resourceIdentify = "*";
}
if(array.length > 2) {
permissionBit = Integer.valueOf(array[2]);
}
if(array.length > 3) {
instanceId = array[3];
}
if(!StringUtils.hasLength(instanceId)) {
instanceId = "*";
}
}
@Override
public boolean implies(Permission p) {
if(!(p instanceof BitPermission)) {
return false;
}
BitPermission other = (BitPermission) p;
if(!("*".equals(this.resourceIdentify) || this.resourceIdentify.equals(other.resourceIdentify))) {
return false;
}
if(!(this.permissionBit ==0 || (this.permissionBit & other.permissionBit) != 0)) {
return false;
}
if(!("*".equals(this.instanceId) || this.instanceId.equals(other.instanceId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "BitPermission{" + "resourceIdentify='" + resourceIdentify
+ '\'' + ", permissionBit=" + permissionBit + ", instanceId='"
+ instanceId + '\'' + '}';
}
}