Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add middlewares #536

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Add middlewares #536

wants to merge 3 commits into from

Conversation

H1rono
Copy link
Member

@H1rono H1rono commented Apr 20, 2023

close #285

@H1rono H1rono requested a review from ryoha000 April 20, 2023 08:11
Comment on lines 40 to 51
func MiddlewareItemSocial(getItem func(c echo.Context) model.Item) func(next echo.HandlerFunc) echo.HandlerFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
item := getItem(c)
user := c.Get("user").(model.User)
if item.Type != model.PersonalItem && !user.Admin {
return c.NoContent(http.StatusForbidden)
}
return next(c)
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

router.goで詳細なロジックを書きたくないので純粋関数としてrouterパッケージに切り出したいです

@H1rono
Copy link
Member Author

H1rono commented May 18, 2023

io.ReadAllとかないのか

@H1rono
Copy link
Member Author

H1rono commented May 18, 2023

func MiddlewareBodyItemSocial(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
body, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
return next(c)
}
c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(body))
item := model.Item{}
if err = json.Unmarshal(body, &item); err != nil {
return next(c)
}
user := c.Get("user").(model.User)
if item.Type != model.PersonalItem && !user.Admin {
return c.NoContent(http.StatusForbidden)
}
return next(c)
}
}

ここ、 c.Set("item", item) したら ioutil 消せるなと今思ったんですが何か問題ありますかね?

@H1rono
Copy link
Member Author

H1rono commented Jul 4, 2023

↑やっちゃいます

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

権限回りをmiddlewareでやりたい
2 participants