2

I posted this question here too: https://community.sitecore.net/developers/f/8/t/3681

Hi all,

I am trying to modify the User Manager to search by email. I am using Sitecore 7.1. I updated the UserProvider pipeline to check for emails using RegEx. If the search term is an email, then members are retrieved by email, not user name:

using Sitecore;
using Sitecore.Common;
using Sitecore.Configuration;
using Sitecore.Diagnostics;
using Sitecore.Security.Accounts;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web.Security;

namespace Configuration.SharedCNA.Pipelines
{
public class CustomUserProvider : UserProvider
{
    public override IFilterable<User> GetUsers()
    {
        var users = new Filterable<User>(GetAllUsers, GetAllUsers, GetUserCount, GetUsersByName, GetUsersByNameCount);
        return users;
    }

    protected override IEnumerable<User> GetUsersByName(int pageIndex, int pageSize, string userNameToMatch)
    {
        var users = FindUsers(pageIndex, pageSize, userNameToMatch);
        return users;
    }

    private int GetUsersByNameCount(string userNameToMatch)
    {
        int totalRecords;
        string userWithNoWildcard = StringUtil.RemovePostfix(Settings.Authentication.VirtualMembershipWildcard,
            StringUtil.RemovePrefix(Settings.Authentication.VirtualMembershipWildcard, userNameToMatch));
        if (Regex.IsMatch(userWithNoWildcard,
            @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"))
            Membership.FindUsersByEmail(userWithNoWildcard, 0, 1, out totalRecords);
        else
            Membership.FindUsersByName(userNameToMatch, 0, 1, out totalRecords);

        return totalRecords;
    }

    protected IEnumerable<User> FindUsers(int pageIndex, int pageSize, string userNameToMatch)
    {
        Assert.ArgumentNotNull(userNameToMatch, "userNameToMatch");
        IEnumerable<User> users;
        string userWithNoWildcard = StringUtil.RemovePostfix(Settings.Authentication.VirtualMembershipWildcard,
            StringUtil.RemovePrefix(Settings.Authentication.VirtualMembershipWildcard, userNameToMatch));

        if (Regex.IsMatch(userWithNoWildcard,
            @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"))
            users = FindByEmail(pageIndex, pageSize, userWithNoWildcard);
        else
            users = FindByUsername(pageIndex, pageSize, userNameToMatch);
        return users;
    }

    protected IEnumerable<User> FindByUsername(int pageIndex, int pageSize, string userNameToMatch)
    {
        Assert.ArgumentNotNull(userNameToMatch, "userNameToMatch");
        int total;
        return
            new Enumerable<User>(
                () => Membership.FindUsersByName(userNameToMatch, pageIndex, pageSize, out total).GetEnumerator(),
                o => User.FromName(((MembershipUser)o).UserName, false));
    }

    protected IEnumerable<User> FindByEmail(int pageIndex, int pageSize, string userNameToMatch)
    {
        Assert.ArgumentNotNull(userNameToMatch, "userNameToMatch");
        int total;
        return
            new Enumerable<User>(
                () => Membership.FindUsersByEmail(userNameToMatch, pageIndex, pageSize, out total).GetEnumerator(),
                o => User.FromName(((MembershipUser)o).UserName, false));
    }
}
}

I attached by instance to the debugger and tried to search for a test user. I named that account "test" and set the email to "test.test@ttttt". After I clicked search, the breakpoint in my code was hit, and I saw that it returned the user! However, the interface showed nothing at all! I tried a different scenario where I created another account with the user name and the email set to "test.test@ttttt", attached the debugger again, and again searched by the email. In the code, I saw that two users where returned, but the interface only displayed the account with the user name set to "test.test@ttttt". Any ideas what could be causing this issue? I tried to investigate the UserManager.aspx file, along with the the UserManager and UserProvider in the Sitecore.Kernel and Sitecore.Client dlls, but unfortunately I couldn't figure out the problem.

Thanks!

Albraa

Albraa
  • 337
  • 1
  • 4
  • 11
  • Oh one more thing I forgot to mention, while debugging, I noticed that the GetUsersByName and GetUsersByNameCount functions execute (and return the correct users) AFTER the GetUsers function finishes executing and returns. – Albraa Aug 17 '16 at 19:44
  • are you using an admin user when you search? – Vlad Iobagiu Aug 17 '16 at 19:46
  • Yes, I am also aware that search is handled differently for admin and non admin users. In this case, I am logged in as the Admin user. – Albraa Aug 17 '16 at 19:51
  • https://sitecorecontextitem.wordpress.com/2014/11/10/search-by-email-in-the-sitecore-user-manager/ can you check this solution? – Vlad Iobagiu Aug 17 '16 at 19:57
  • Haha this was actually the first thing I looked at! Thanks anyway Sitecore Climber! – Albraa Aug 17 '16 at 20:03

0 Answers0