From e4c8fb9825465a253de4a96e1e066c738171bfd2 Mon Sep 17 00:00:00 2001 From: smavros Date: Tue, 4 Jun 2019 20:33:27 +0200 Subject: [PATCH] Renames IsActionAllowed() to ValidateRole() --- common/utilities.go | 5 ++++- routes/user/userEndpoints.go | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/common/utilities.go b/common/utilities.go index b61e874..9ed6aa7 100644 --- a/common/utilities.go +++ b/common/utilities.go @@ -91,7 +91,10 @@ func GetWidgetID(c *gin.Context) (int, error) { } } -func IsActionAllowed(c *gin.Context, model ModelName, action CRUD) error { +func ValidateRole(c *gin.Context, model ModelName, action CRUD) error { + // Extracts and validates the role which is saved in the context for + // executing a specific CRUD operation on a specific model. In case + // of invalid role return an error. // Get user's role from context role, exists := c.Get("user_role") diff --git a/routes/user/userEndpoints.go b/routes/user/userEndpoints.go index e0b5e41..9432742 100644 --- a/routes/user/userEndpoints.go +++ b/routes/user/userEndpoints.go @@ -163,7 +163,7 @@ func getUsers(c *gin.Context) { // @Router /users [post] func addUser(c *gin.Context) { - err := common.IsActionAllowed(c, common.ModelUser, common.Create) + err := common.ValidateRole(c, common.ModelUser, common.Create) if err != nil { c.JSON(http.StatusUnprocessableEntity, fmt.Sprintf("%v", err)) return @@ -251,7 +251,7 @@ func updateUser(c *gin.Context) { // @Router /users/{userID} [get] func getUser(c *gin.Context) { - err := common.IsActionAllowed(c, common.ModelUser, common.Read) + err := common.ValidateRole(c, common.ModelUser, common.Read) if err != nil { c.JSON(http.StatusUnprocessableEntity, fmt.Sprintf("%v", err)) return @@ -286,7 +286,7 @@ func getUser(c *gin.Context) { // @Router /users/{userID} [delete] func deleteUser(c *gin.Context) { - err := common.IsActionAllowed(c, common.ModelUser, common.Delete) + err := common.ValidateRole(c, common.ModelUser, common.Delete) if err != nil { c.JSON(http.StatusUnprocessableEntity, fmt.Sprintf("%v", err)) return