package beego

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/plugins/cors"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego/middleware"
	_ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego/routers"
)

func init() {
	beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
		AllowOrigins:     []string{"*"},
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Authorization", "Access-Control-Allow-Origin"},
		ExposeHeaders:    []string{"Content-Length", "Access-Control-Allow-Origin"},
		AllowCredentials: true,
	}))

	beego.InsertFilter("/auth/profile", beego.BeforeExec, middleware.CheckAuthorization)
	beego.InsertFilter("/user/*", beego.BeforeExec, middleware.InspectRoleAccess(""))
	beego.InsertFilter("/role/*", beego.BeforeExec, middleware.InspectRoleAccess(""))
	beego.InsertFilter("/project_module/*", beego.BeforeExec, middleware.InspectRoleAccess(""))
	beego.InsertFilter("/project_module_version/*", beego.BeforeExec, middleware.InspectRoleAccess("/project_module/*"))
	beego.InsertFilter("/project_module_files/*", beego.BeforeExec, middleware.InspectRoleAccess("/project_module/*"))
	beego.InsertFilter("/rbac/*", beego.BeforeExec, middleware.InspectRoleAccess("/role/*"))
}