diff --git a/controller/park.go b/controller/park.go index e959cd2..1c70ff6 100644 --- a/controller/park.go +++ b/controller/park.go @@ -2,6 +2,7 @@ package controller import ( "electricity_bill_calc/logger" + "electricity_bill_calc/model" "electricity_bill_calc/repository" "electricity_bill_calc/response" "electricity_bill_calc/security" @@ -29,6 +30,24 @@ func InitializeParkHandlers(router *fiber.App) { router.Put("/park/:pid/building/:bid/enabled", security.EnterpriseAuthorize, modifyParkBuildingEnabling) } +// 检查当前用户是否拥有指定园区,在判断完成之后直接产生响应 +func checkParkBelongs(logger *zap.Logger, parkId string, session *model.Session, result *response.Result) (bool, error) { + if session == nil { + logger.Error("用户会话无效。") + return false, result.Unauthorized("用户会话无效。") + } + ok, err := repository.ParkRepository.IsParkBelongs(parkId, session.Uid) + switch { + case err != nil: + logger.Error("无法判断园区是否隶属于当前用户。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err)) + return false, result.Error(http.StatusInternalServerError, err.Error()) + case err == nil && !ok: + logger.Error("用户试图访问不属于自己的园区。", zap.String("park id", parkId), zap.String("user id", session.Uid)) + return false, result.Forbidden("您无权访问该园区。") + } + return true, nil +} + // 列出隶属于当前用户的全部园区 func listParksBelongsToCurrentUser(c *fiber.Ctx) error { result := response.NewResult(c)