Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

enhancement: better change role validation (#483)

Guilherme Oenningf0a075c491fe
diff --git a/app/actions/user.go b/app/actions/user.go
index 846883416..d5d49665f 100644
--- a/app/actions/user.go
+++ b/app/actions/user.go
@@ -30,17 +30,22 @@ func (input *ChangeUserRole) IsAuthorized(user *models.User, services *app.Servi
func (input *ChangeUserRole) Validate(user *models.User, services *app.Services) *validate.Result {
result := validate.Success()
if input.Model.Role < models.RoleVisitor || input.Model.Role > models.RoleAdministrator {
- result.AddFieldFailure("role", "Invalid role")
+ return validate.Error(app.ErrNotFound)
}
+
+ if user.ID == input.Model.UserID {
+ result.AddFieldFailure("userId", "It is not allowed to change your own Role.")
+ }
+
target, err := services.Users.GetByID(input.Model.UserID)
if err != nil {
if errors.Cause(err) == app.ErrNotFound {
- result.AddFieldFailure("userId", "User not found")
+ result.AddFieldFailure("userId", "User not found.")
} else {
return validate.Error(err)
}
} else if target.Tenant.ID != user.Tenant.ID {
- result.AddFieldFailure("userId", "User not found")
+ result.AddFieldFailure("userId", "User not found.")
}
return result
}
diff --git a/app/actions/user_test.go b/app/actions/user_test.go
index aa8e6f35b..761682051 100644
--- a/app/actions/user_test.go
+++ b/app/actions/user_test.go
@@ -3,6 +3,7 @@ package actions_test
import (
"testing"
+ "github.com/getfider/fider/app"
"github.com/getfider/fider/app/actions"
"github.com/getfider/fider/app/models"
. "github.com/getfider/fider/app/pkg/assert"
@@ -49,7 +50,7 @@ func TestChangeUserRole_InvalidRole(t *testing.T) {
action := actions.ChangeUserRole{Model: &models.ChangeUserRole{UserID: targetUser.ID, Role: 4}}
action.IsAuthorized(currentUser, nil)
result := action.Validate(currentUser, services)
- ExpectFailed(result, "role")
+ Expect(result.Err).Equals(app.ErrNotFound)
}
func TestChangeUserRole_InvalidUser(t *testing.T) {
@@ -85,3 +86,22 @@ func TestChangeUserRole_InvalidUser_Tenant(t *testing.T) {
result := action.Validate(currentUser, services)
ExpectFailed(result, "userId")
}
+
+func TestChangeUserRole_CurrentUser(t *testing.T) {
+ RegisterT(t)
+
+ currentUser := &models.User{
+ Tenant: &models.Tenant{ID: 2},
+ Role: models.RoleAdministrator,
+ }
+ services.Users.Register(currentUser)
+
+ action := actions.ChangeUserRole{Model: &models.ChangeUserRole{UserID: currentUser.ID, Role: models.RoleVisitor}}
+ action.IsAuthorized(currentUser, nil)
+ result := action.Validate(currentUser, services)
+ ExpectFailed(result, "userId")
+
+ user, err := services.Users.GetByID(currentUser.ID)
+ Expect(err).IsNil()
+ Expect(user.Role).Equals(models.RoleAdministrator)
+}