package vn.azteam.tpf.security.authz;

import org.casbin.jcasbin.main.Enforcer;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import vn.azteam.tpf.security.apiKey.ApiKeyProvider;
import vn.azteam.tpf.service.TBPermissionQueryService;
import vn.azteam.tpf.service.UserService;

public class AuthzConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    private final UserService userService;
    private final TBPermissionQueryService tbPermissionQueryService;
    private final Enforcer enforcer;
    private final ApiKeyProvider apiKeyProvider;
    public AuthzConfigurer(UserService userService, TBPermissionQueryService tbPermissionQueryService, Enforcer enforcer, ApiKeyProvider apiKeyProvider) {
        this.userService = userService;
        this.tbPermissionQueryService = tbPermissionQueryService;
        this.enforcer = enforcer;
        this.apiKeyProvider = apiKeyProvider;
    }

    @Override
    public void configure(HttpSecurity http) {
        AuthzFilter customFilter = new AuthzFilter(userService, tbPermissionQueryService, enforcer, apiKeyProvider);
        http.addFilterBefore(customFilter, UsernamePasswordAuthenticationFilter.class);
    }
}
