com.smallchill.core.aop.PermissionAop.java Source code

Java tutorial

Introduction

Here is the source code for com.smallchill.core.aop.PermissionAop.java

Source

/**
 * Copyright (c) 2015-2016, Chill Zhuang  (smallchill@163.com).
 *
 * 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 com.smallchill.core.aop;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;

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;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.smallchill.core.annotation.Permission;
import com.smallchill.core.exception.NoPermissionException;
import com.smallchill.core.toolbox.Func;
import com.smallchill.core.toolbox.check.PermissionCheckManager;

/**
 * AOP ??
 */
@Aspect
@Component
public class PermissionAop {

    @Pointcut(value = "@annotation(com.smallchill.core.annotation.Permission)")
    private void cutPermission() {

    }

    @Around("cutPermission()")
    public Object doPermission(ProceedingJoinPoint point) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        MethodSignature ms = (MethodSignature) point.getSignature();
        Method method = ms.getMethod();
        Permission permission = method.getAnnotation(Permission.class);
        Object[] permissions = permission.value();
        if ((permissions.length == 1 && Func.format(permissions[0]).equals("ALL")) || permissions == null
                || permissions.length == 0) {
            //
            boolean result = PermissionCheckManager.checkAll(request);
            if (result) {
                return point.proceed();
            } else {
                throw new NoPermissionException();
            }
        } else {
            //
            boolean result = PermissionCheckManager.check(permissions, request);
            if (result) {
                return point.proceed();
            } else {
                throw new NoPermissionException();
            }
        }

    }

}