How to Search for Custom Profile Fields in the User Manager (Non Admins)

How to Search for Custom Profile Fields in the User Manager (Non Admins)

DESCRIPTION

While the other solution “How to Search for Custom Profile Fields in the User Manager (Admins only)” worked for Admin users only, this solution allows non-admin users to perform a custom search in the Sitecore user manage.

WHY DO WE HAVE TO IMPLEMENT TWO DIFFERENT SOLUTIONS?

The Sitecore.Security.Accounts.UserDelegation class  implements the GetManagedUsers() function as shown below:

public virtual IEnumerable GetManagedUsers()
{
  if (this._user.Profile.IsAdministrator)
    return (IEnumerable) UserManager.GetUsers();
  else
    return this.DoGetManagedUsers();
}
 
private IEnumerable DoGetManagedUsers()
{
   return new DomainsWrapper(this.GetManagedDomains()).GetUsers();
}

If the context user has an Admin profile and executes a search the Sitecore calls the UserManager.GetUsers() that we have already customized.

If the context user is not an admin then DoGetManagedUsers() is called. This function returns a subset of the total users based on domains.

It will retrieve users on all domains specified in the Domains.config file located under your App_config/Security.

<domain name="sitecore" ensureAnonymousUser="false" />
  <domain name="extranet" />
  <domain name="default" isDefault="true" />

 

SOLUTION

On Domains.config file we need to specify our class that will override the GetUsersByName() function and also specify the assembly file where this class is declared.
<sc:templates>
    <!--<domain type="Sitecore.Security.Domains.Domain, Sitecore.Kernel">-->
    <domain type="BusynessLayer.Pipelines.UserManagerCustomSearchNonAdmins, BayerSocial.SBL">
      <ensureanonymoususer>true</ensureanonymoususer>
      <locallymanaged>false</locallymanaged>
    </domain>
  </sc:templates>

The solution below is going to allow a non-admin user to perform a custom search in the Sitecore user manage. As explained in the other article the CPF is a Brazilian document but you could search for EMAIL or other fields. By specifying the words “CPF:” the search will be executed.

class UserManagerCustomSearchNonAdmins : Domain
    {
        public override IEnumerable<User> GetUsersByName(int pageIndex, int pageSize, string search, out int total)
        {

            var domain = AccountPrefix.Replace("\\", string.Empty);

            if (search.Contains("cpf"))
            {
                search = search.Trim().Replace("cpf:", "").Replace("*", "");

                //if no CPF typed then return empty (not NULL)
                if (string.IsNullOrEmpty(search)) { total = 0; return new User[] { }; };

                var u = Busynesslayer.UserProfile.Index.CustomProfileIndexManager.GetUserByCpf(search);

                if (u == null) { total = 0; return new User[] { }; };

                //return user found
                var users = new List<User>();

                if (u.Domain.Name == domain) users.Add(u);

                total = users.Count();

                return users;

            }
            else
            {

                MembershipUserCollection usersByName = Membership.FindUsersByName(search, pageIndex, pageSize, out total);

                var list = new List<User>();

                foreach (MembershipUser membershipUser in usersByName)

                    if (membershipUser.UserName.Contains(domain))

                        list.Add(User.FromName(membershipUser.UserName, false));

                total = list.Count();

                return list;

            }
        }
    }

Any questions please feel free to contact me.

Regards

Leave a Comment

%d bloggers like this: