Monday, September 3, 2012

Filtering users by Domain in UserManager


Often we introduce a new domain in usermanager and would like to filter out the users associated to the new domain. This post will outlines how we go about achieving that.


The following outlines the steps needed to filter out users associated to unwanted domain in UserManager.
Firstly, a new class that inherit from UserManager will need to be created. The class will then override OnLoad method to filter the users. The code to achieve that is as follow
01.public class AdvancedUserManager : UserManager
02. {
03. protected override void OnLoad(EventArgs e)
04. {
05. Assert.ArgumentNotNull(e, "e");
06. base.OnLoad(e);
07. Assert.CanRunApplication("Security/User Manager");
08. var managedUsers = global::Sitecore.Context.User.Delegation.GetManagedUsers().Where(IsAllowedDomain);
09. ComponentArtGridHandler<User>.Manage(Users, new GridSource<User>(managedUsers), RebindRequired);
10. Users.LocalizeGrid();
11. }
12.
13. // Properties
14. private bool RebindRequired
15. {
16. get
17. {
18. return ((!Page.IsPostBack && (Request.QueryString["Cart_Users_Callback"] != "yes")) || (Page.Request.Params["requireRebind"] == "true"));
19. }
20. }
21.
22. private static bool IsAllowedDomain(Account user)
23. {
24. if (user.Domain == null) return false;
25. var domainToCheck = user.Domain.Name.ToLower();
26. var excludedDomain = global::Sitecore.Configuration.Settings.GetSetting("ExcludedDomainInUserManager");
27. if (string.IsNullOrEmpty(excludedDomain)) return true;
28. var domainArray = excludedDomain.Split("|".ToCharArray());
29. return domainArray.Select(domain => domain.ToLower().Equals(domainToCheck)).All(isExcludedDomain => !isExcludedDomain);
30. }
31. }
After the class has been created, we need to modify the UserManager.aspx to point to the new namespace. The path to the UserManager will be as follow
\sitecore\shell\Applications\Security\UserManager\UserManager.aspx
Overwrite the default
1.<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserManager.aspx.cs" Inherits="Sitecore.Shell.Applications.Security.UserManager.UserManager" %>
Lastly, we need to create a Sitecore Setting in config file name "ExcludedDomainInUserManager" to determine which Domain to filter.
That's it! You should now have filtered user manager. :)