chore: init bj_power monorepo (dashboard/mes/wms/wms_client/workstation), clear subproject git metadata and align naming to folder names
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/common/httpx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/collection"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
type HttpRequest struct {
|
||||
Path string `json:"path"`
|
||||
Method string `json:"method"`
|
||||
}
|
||||
|
||||
func NewHttpRequest(path, method string) HttpRequest {
|
||||
return HttpRequest{Path: path, Method: strings.ToUpper(method)}
|
||||
}
|
||||
|
||||
var ignorePermissions = collection.NewSet()
|
||||
|
||||
func init() {
|
||||
ignorePermissions.Add([]any{
|
||||
NewHttpRequest("/sse", "GET"),
|
||||
NewHttpRequest("/api/v1/roles", "POST"),
|
||||
NewHttpRequest("/api/v1/equipments", "POST"),
|
||||
NewHttpRequest("/api/v1/login", "POST"),
|
||||
NewHttpRequest("/api/v1/userinfo", "GET"),
|
||||
NewHttpRequest("/api/v1/refreshToken", "POST"),
|
||||
}...)
|
||||
}
|
||||
|
||||
func CheckPermission(svcCtx *svc.ServiceContext) rest.Middleware {
|
||||
return func(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
method := strings.ToUpper(r.Method)
|
||||
reqPath := r.URL.Path
|
||||
|
||||
if ignorePermissions.Contains(NewHttpRequest(reqPath, method)) {
|
||||
next(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
userId := xhttp.GetUidFromCtx(r.Context())
|
||||
|
||||
logx.WithContext(r.Context()).Debugf("check permission [%s %s]", method, reqPath)
|
||||
|
||||
u, err := svcCtx.EntClient.User.Get(r.Context(), int(userId))
|
||||
if ent.IsNotFound(err) {
|
||||
httpx.ErrorCtx(r.Context(), w, errorx.WithStack(err, errorx.UserNotFound))
|
||||
return
|
||||
} else if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, errorx.WithStack(err, errorx.DbError))
|
||||
return
|
||||
}
|
||||
|
||||
if u.RoleId == 1 {
|
||||
next(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := svcCtx.DB.QueryContext(r.Context(), `select id, path, method from api
|
||||
left join permission_apis as pa on pa.api_id = api.id
|
||||
left join role_permissions as rp on rp.permission_id = pa.permission_id
|
||||
where rp.role_id = $1`, u.RoleId)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, errorx.WithStack(err, errorx.DbError))
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var apis []ent.Api
|
||||
for rows.Next() {
|
||||
var api ent.Api
|
||||
err = rows.Scan(&api.ID, &api.Path, &api.Method)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, errorx.WithStack(err, errorx.DbError))
|
||||
return
|
||||
}
|
||||
apis = append(apis, api)
|
||||
}
|
||||
|
||||
for _, api := range apis {
|
||||
if strings.EqualFold(api.Method, method) || api.Method == "*" {
|
||||
if match, _ := path.Match(api.Path, reqPath); match {
|
||||
next(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
http.Error(w, "access forbidden", 403)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
// WriteAudit 邂。逅�錘蜿ー蜀呎桃菴懷ョ。隶。荳ュ髣エ莉カ縲?//
|
||||
// 邊セ邂迚?MES 荳榊�菴ソ逕ィ莠倶サカ諤サ郤ソ�悟�謫堺ス懷ョ。隶。證らスョ荳コ遨コ謫堺ス懶シ磯丈シ��会シ�
|
||||
// 蜷守サュ螯る怙謫堺ス懈律蠢怜庄謾ケ荳コ逶エ謗・蜀吝コ薙?func WriteAudit(svcCtx *svc.ServiceContext) rest.Middleware {
|
||||
return func(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
next(w, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user