package middleware

import (
	"github.com/gin-gonic/gin"
	"hongze/hongze_yb/controller/response"
	"hongze/hongze_yb/services/company"
	"hongze/hongze_yb/services/user"
)

// CheckChartAuth 图库权限验证
func CheckChartAuth() gin.HandlerFunc {

	return func(c *gin.Context) {

		userInfo := user.GetInfoByClaims(c)

		ok, permissionCheckInfo, err := company.CheckUserChartPermission(userInfo.CompanyID, int(userInfo.UserID))
		if err != nil {
			response.FailMsg("图库权限验证失败", "图库权限验证失败" + err.Error(), c)
			c.Abort()
			return
		}
		if !ok {
			response.AuthError(permissionCheckInfo, "暂无权限", c)
			c.Abort()
			return
		}

		c.Next()
	}
}