Validate one of roles?

Is there a way use declarative validation to check whether user has either of 2 roles?

I would like to define something like the following on a service model:

// Either role is valid for access
[ValidateHasRole([UserRole.ReadData, UserRole.ViewData])]

In the ServiceStack documentation, ValidateHasRole requires ALL roles:

[ValidateHasRole] Protect access to this API to only Users assigned with ALL Roles

so, the folowing would also require BOTH role

[ValidateHasRole(UserRole.ReadData)]
[ValidateHasRole(UserRole.ViewData)]

You would have had to [RequiresAnyRole([UserRole.ReadData,UserRole.ViewData])] Request Filter attribute on your Service class (i.e requires reference to ServiceStack.dll).

I’ve just added support for the impl-free ValidateHasAnyRole attribute which you can add on your DTOs instead:

[ValidateHasAnyRole([UserRole.ReadData,UserRole.ViewData])]

This change is available on v8.9.1+ that’s now available in pre-release packages.

Great, thank you for the information and the enhancement. Much appreciated.

1 Like