Search This Blog

Wednesday, March 25, 2015

Determining whether a security user belongs to a certain role in code becomes a bit easier

As you probably know, we already have a built-in solution for checking this condition within criteria - IsCurrentUserInRole criteria function (learn more from my blog...).
Doing the same in C# or VB.NET code is also possible with LINQ or a simple 'foreach' (through the SecuritySystem singleton), it would still be great to have a more straightforward and built-in method for this common task, especially for newbies.

Good news is that starting with version 14.2.7, this task can be done more easily. Let me quote the provided solution details from this Support Center thread:

Now, you can determine whether a user belongs to a certain role in code using the IsUserInRole extension method. This method is available for both ISecurityUserWithRoles (new Security System) and IUserWithRoles (legacy Security System) user types.

using DevExpress.ExpressApp.Security; // ... public class MyController : ViewController { // ... protected override void OnActivated() { base.OnActivated(); ISecurityUserWithRoles currentUser = (ISecurityUserWithRoles)SecuritySystem.CurrentUser; restrictedAction.Enabled["ForAdminsOnly"] = currentUser.IsUserInRole("Administrators"); } // ... }
Do not forget to add the using directive for the DevExpress.ExpressApp.Security namespace when using the IsUserInRole extension method.

No comments:

Post a Comment