MCPcopy Create free account
hub / github.com/actiontech/dtle / AuthFilter

Function AuthFilter

api/route.go:220–259  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

218}
219
220func AuthFilter() echo.MiddlewareFunc {
221 return func(next echo.HandlerFunc) echo.HandlerFunc {
222 return func(c echo.Context) error {
223 if inWhiteList(c.Request().URL.Path) {
224 return next(c)
225 }
226 logger := handler.NewLogger().Named("AuthFilter")
227 storeManager, err := common.NewStoreManager([]string{handler.ConsulAddr}, logger)
228 if err != nil {
229 return echo.NewHTTPError(http.StatusForbidden)
230 }
231 user, exists, err := storeManager.GetUser(v2.GetUserName(c))
232 if err != nil || !exists {
233 return echo.NewHTTPError(http.StatusForbidden)
234 }
235
236 // admin has the maximum authority
237 if user.Role == common.DefaultRole {
238 return next(c)
239 }
240 role, exists, err := storeManager.GetRole(user.Tenant, user.Role)
241 if err != nil || !exists {
242 return echo.NewHTTPError(http.StatusForbidden)
243 }
244 authority := make([]models.MenuItem, 0)
245 err = json.Unmarshal([]byte(role.Authority), &authority)
246 if err != nil {
247 return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("check your authority fail : %v", err))
248 }
249 for _, menuItem := range authority {
250 for _, buttonItem := range menuItem.Operations {
251 if c.Request().URL.Path == buttonItem.Uri {
252 return next(c)
253 }
254 }
255 }
256 return echo.NewHTTPError(http.StatusForbidden)
257 }
258 }
259}
260
261var whiteList = []string{
262 "/v2/nodes",

Callers 1

SetupApiServerFunction · 0.85

Calls 7

GetUserMethod · 0.95
GetRoleMethod · 0.95
NewLoggerFunction · 0.92
NewStoreManagerFunction · 0.92
GetUserNameFunction · 0.92
inWhiteListFunction · 0.85
UnmarshalMethod · 0.65

Tested by

no test coverage detected