PermissionAop.java 2.3 KB
Newer Older
Z
zhuangqian 已提交
1
/**
Z
update  
zhuangqian 已提交
2
 * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com).
Z
zhuangqian 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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.
 */
S
smallchill 已提交
16
package org.springblade.core.aop;
Z
zhuangqian 已提交
17 18 19 20 21 22

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
S
smallchill 已提交
23 24 25 26
import org.springblade.core.annotation.Permission;
import org.springblade.core.exception.NoPermissionException;
import org.springblade.core.toolbox.Func;
import org.springblade.core.toolbox.check.PermissionCheckManager;
27 28 29
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
Z
zhuangqian 已提交
30 31 32

/**
 * AOP 权限自定义检查
33
 * @author zhuangqian
Z
zhuangqian 已提交
34 35 36 37 38
 */
@Aspect
@Component
public class PermissionAop {

S
smallchill 已提交
39
	@Pointcut(value = "@annotation(org.springblade.core.annotation.Permission)")
Z
zhuangqian 已提交
40 41 42 43 44 45 46 47 48 49
	private void cutPermission() {

	}

	@Around("cutPermission()")
	public Object doPermission(ProceedingJoinPoint point) throws Throwable {
		MethodSignature ms = (MethodSignature) point.getSignature();
		Method method = ms.getMethod();
		Permission permission = method.getAnnotation(Permission.class);
		Object[] permissions = permission.value();
50 51 52 53 54 55
		boolean flag = (permissions.length == 1 && Func.toStr(permissions[0]).equals("ALL"))
                || permissions == null || permissions.length == 0;
		if (flag) {
            /**
             * 检查全体角色
             */
Z
zhuangqian 已提交
56
			boolean result = PermissionCheckManager.checkAll();
Z
zhuangqian 已提交
57 58 59 60 61 62
			if (result) {
				return point.proceed();
			} else {
				throw new NoPermissionException();
			}
		} else {
63 64 65
            /**
             * 检查指定角色
             */
Z
zhuangqian 已提交
66
			boolean result = PermissionCheckManager.check(permissions);
Z
zhuangqian 已提交
67 68 69 70 71 72 73 74 75 76
			if (result) {
				return point.proceed();
			} else {
				throw new NoPermissionException();
			}
		}

	}

}