Quantcast
Channel: Sitefinity – Falafel Software Blog
Viewing all articles
Browse latest Browse all 73

Accessing Sitefinity Roles in Code

$
0
0

When developing custom solutions in your Sitefinity application, you may need the ability to work with Sitefinity roles. As a developer, you would reach out to the RoleManager that Sitefinity provides, and access the roles from there. However, you may run into an issue where some roles, verified to be a part of your site, are not represented when working with the RoleManager. This post goes into this issue, and shows how to access all Sitefinity roles to work with.

Basic Role Access

Let’s say you want to list all available Sitefinity roles on your web site. The natural idea would be to access the RoleManager, then call GetRoles() (or GetRoleNames() if you only care about the names), iterate the returned collection and be done. On a fresh Sitefinity install (9.1), I created a couple custom roles “CustomRole1” and “CustomRole2”, then proceeded to call the RoleManager to grab my roles and display them:

RoleManager.GetManager().GetRoleNames()

Done, right? Wrong! The roles we end up seeing are the following:

  • Customers (ecommerce)
  • CustomRole1
  • CustomRole2
  • Moderators (comments)
  • Moderators (forums)

See anything missing? That’s right, none of the system roles are there! Here’s the list of Sitefinity roles in the backend of my new site:

Sitefinity Roles

The roles clearly exist (and have to, being built-in functions for Sitefinity), so where are they?

Accessing All Built-In Sitefinity Roles

In actuality, what Sitefinity is doing behind the scenes is storing these built-in roles using a different provider. I won’t get too in depth here about the Sitefinity provider model, but in essence it’s a separate data source (which can use the same database, or something completely different). Sitefinity’s Managers, when retrieving them via the GetManager() call, can be fed a provider name. By default (or if you pass in null or empty string), Sitefinity falls back on the default provider name. Most of the time you don’t have to concern yourself with calling out a specific provider, but in this instance we do.

Modify the above line of code to pass in “AppRoles” as the provider for the RoleManager. This gets you access to the other Sitefinity roles that were not included in the first snippet.

RoleManager.GetManager("AppRoles").GetRoleNames()

“AppRoles” is the provider name in this instance. Now, the list of roles we get is the following:

  • Administrators
  • Anonymous
  • Authenticated
  • Authors
  • BackendUsers
  • Designers
  • Editors
  • Everyone
  • Owner
  • Users

(NOTE: Even roles you can’t see listed in the backend, Anonymous/Authenticated/Owner, are returned here.)

In our example scenario, we want to list all our roles, however, not just one subset or the other. Now that we have access to both, it’s as simple as creating a list, and adding both sets together. We then sort the list alphabetically so that the final list we show is nice and organized.

List<string> allRoleNames = new List<string>();
allRoleNames.AddRange(RoleManager.GetManager().GetRoleNames());
allRoleNames.AddRange(RoleManager.GetManager("AppRoles").GetRoleNames());
allRoleNames = allRoleNames.OrderBy(str => str).ToList();

There’s more you can do with access to all the roles than just listing them. You can better-manage a user’s roles programmatically now! With all of the roles at your disposal, you can have business logic set up in code that can add users to some of Sitefinity’s built-in roles, search for users in a given role, or do anything else your business rules require.

The post Accessing Sitefinity Roles in Code appeared first on Falafel Software Blog.


Viewing all articles
Browse latest Browse all 73

Trending Articles