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

Use the middleware to implement the same logic #15

Open
YiNNx opened this issue Apr 11, 2023 · 0 comments
Open

Use the middleware to implement the same logic #15

YiNNx opened this issue Apr 11, 2023 · 0 comments

Comments

@YiNNx
Copy link
Collaborator

YiNNx commented Apr 11, 2023

func checkAuthorization(c *gin.Context) (string, bool) {
username := c.Param("username")
t, _ := c.Get("userdata")
userData := t.(map[string]string)
// get user id
m := model.GetModel()
u, err := m.FindUserByUsername(username)
if err != nil {
response.Error(c, http.StatusBadRequest, "User does not exist. ", err.Error())
return "", false
}
id := strconv.Itoa(int(u.ID))
if userData["role"] != "admin" && userData["id"] != id {
response.Error(c, http.StatusUnauthorized, "Insufficient permission. ")
return "", false
}
return id, true
}

This function exactly implements the function of middleware. Maybe refactor it into a middleware function is a good idea :D

@YiNNx YiNNx changed the title Add a check auth middleware to implement the same function Use the middleware to implement the same logic Apr 11, 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

No branches or pull requests

1 participant