We are developing a workflow-like application. We want to display the same form (with the same ViewModel
and the same View
) for different user roles. We want to make some fields ReadOnly Not Editable based on specific user roles.
The ViewModel
would be like:
public class ServiceViewModel
{
[EditableIfInRoles("Administrator", "Editor")]
public string ServiceId { get; set; }
}
We are planing to create a custom ReadOnlyAttribute
, like:
public class EditableIfInRolesAttribute : EditableAttribute
{
private IEnumerable<string> _allowedRoles;
public EditableIfInRolesAttribute(params string[] allowedRoles) : base(true)
{
_allowedRoles = allowedRoles;
}
/// <summary>
/// Currently we don't know which method to override
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public override bool IsEditable(object value)
{
return _allowedRoles.Any(e => HttpContext.Current.User.IsInRole(e));
}
}
The rendered HTML that we expect for unauthorized user roles is:
<input class="form-control global" id="ServiceID" name="ServiceID" type="text" value="" readonly="readonly">
How to create a custom attribute that inherits either ReadOnlyAttribute
or 'EditableAttribute', since both of them are sealed class?
What method to override? Or is there any other solution?
Thanks.