package com.sysu.admin.support.token;

import com.xiesx.fastboot.core.token.handle.GoTokenAuthentication;
import com.sysu.admin.support.system.role_permission.RolePermission;
import com.sysu.admin.support.system.role_permission.RolePermissionService;
import com.sysu.admin.support.system.user_role.UserRole;
import com.sysu.admin.support.system.user_role.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class GoTokenAuthenticationImpl implements GoTokenAuthentication {
    @Autowired
    @Lazy
    UserRoleService userRoleService;

    @Autowired
    @Lazy
    RolePermissionService rolePermissionService;

    @Override
    public void authenticate(Long userId, String permission){
        // 权限列表
        List<UserRole> userRoles = userRoleService.getRoleByUsers(userId);
        for (UserRole ur : userRoles) {
            // 查询角色权限
            List<RolePermission> rolePermissions = rolePermissionService.getListByRole(ur.getPk().roles());
            for(RolePermission rolePermission : rolePermissions){
                if(permission.equals(rolePermission.getPermission())){
                    return;
                }
            }
        }
        throw new RuntimeException("没有访问权限！permission:"+permission );
    }
}
