People Sync: Custom User Processor - akumina/AkuminaTraining GitHub Wiki

Applies to

Akumina Foundation 3.4.0.0 and later

Download

You can download the code referenced in this article here

Overview

By default the People Directory Sync pulls all users from the data source. We can filter the users retrieved by the People Directory Sync by creating a Custom User Processor

Creating a User Processor

A Custom User Processor takes the form of a Class that implements the IPeopleDirectoryUserProcessor interface.

using Akumina.PeopleSync;
using Akumina.Logging;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DepartmentCheck
{
    public class DepartmentCheckUserProcessService : IPeopleDirectoryUserProcessor
    {
        private Dictionary<string, string> _options;

        public void Initialize(Dictionary<string, string> processorOptions)
        {
            _options = processorOptions;
        }

        public PeopleDirectoryFetchResult Execute(PeopleDirectoryFetchResult fetchedUsers)
        {
            try
            {
                //remove users without given name and surname
                //remove users with disabled accounts
                fetchedUsers.Users =
                    fetchedUsers.Users.Where(user =>
                        !string.IsNullOrEmpty(user.GetValueForProperty("GivenName")) &&
                        !string.IsNullOrEmpty(user.GetValueForProperty("Surname")) &&
                        !string.IsNullOrEmpty(user.GetValueForProperty("Department")) &&
                        (user.GetValueForProperty("AccountEnabled") != "" ? bool.Parse(user.GetValueForProperty("AccountEnabled")) : true)
                    ).ToList();

                fetchedUsers.UserCount = fetchedUsers.Users.Count;
            }
            catch (Exception e)
            {
                fetchedUsers.IsError = true;
                fetchedUsers.Message = $"\nError processing Users. {e.Message} {e.InnerException?.Message ?? ""}";

                TraceEvents.Log.Error(fetchedUsers.Message);
            }

            return fetchedUsers;
        }
    }
}

IPeopleDirectoryUserProcessor

Extending the IPeopleDirectoryUserProcessor interface is required for the Custom UserProcessor to filter users for the People Directory Sync. It requires the following methods

void Initialize(Dictionary<string, string> fetcherOptions);
PeopleDirectoryFetchResult Execute(PeopleDirectoryFetchResult fetchedUsers);

Initialize()

Any additional options for User Processing are set here

Execute(PeopleDirectoryFetchResult fetchedUsers)

Within the Execute method the UserProcessor receives the user list that was retrieved by the PeopleFetcher. Within this method we can process the list of users and filter out ones we do not want to sync to the People Directory. For example, the processing we do in the above section prevents blank, disabled users from being synced to the People Directory by removing a user if it meets all of the qualifications below:

  • Their Given Name is blank
  • Their Surname is blank
  • Their Department value is blank
  • Their account is disabled

Walkthrough

We will use the class above to add a basic User Processor that prevents blank, disabled users from being synced to the People Directory by removing a user if it meets all of the qualifications below:

  • Their Given Name is blank
  • Their Surname is blank
  • Their Department value is blank
  • Their account is disabled

Creating your User Processor dll

Create a new class library project called DepartmentCheck within Visual Studio. Within your new project, add a new C# class called DepartmentCheckUserProcessService.cs and paste the c# code snippet from the section above. Ensure your project has the Akumina.PeopleSync and Akumina.Logging references.

Save and build your solution. Open your solution in File Explorer and Navigate to your Debug folder at ...\DepartmentCheck\bin\Debug. You will see your DepartmentCheck.dll file.

Locate the your People Sync Console App. This will be installed in the same environment as your AppManager Hosted files, which can be found either in File Explorer if you are hosting locally or on a VM, or in an FTP manager if you are hosting on Azure. Within your People Sync Console App directory, paste your DepartmentCheck.dll.

Editing the unity.interchange.config file

Locate your unity.interchange.config file within the root of your PeopleSync files. Edit it within a text editor and make the following edits:

Insert the following after <assembly name="Akumina.PeopleSync" />. The name of this tag corresponds to the name of the UserProcessor dll.

<assembly name="DepartmentCheck" />

Insert the following after <namespace name="Akumina.PeopleSync"/>. The name of this tag corresponds to the name of the UserProcessor dll.

<namespace name="DepartmentCheck" />

Next, locate the following line

<registertype="IPeopleDirectoryUserProcessor"mapTo="PeopleDirectoryUserProcessorService" />

And change it to

<register type="IPeopleDirectoryUserProcessor" mapTo="DepartmentCheckUserProcessService" />

This maps the IPeopleDirectoryUserProcessor to the name of your User Processor class.

After all of your edits the unity.interchange.config should resemble the one shown below:

<unity.interchange>
  <assembly name="Akumina.PeopleSync" />
  <assembly name="DepartmentCheck" />
  <namespace name="Akumina.PeopleSync"/>
  <namespace name="DepartmentCheck" />
  <container>
    <register type="IPeopleDirectoryUserFetcher" mapTo="PeopleDirectoryUserFetcherServiceADRest" />
    <register type="IPeopleDirectoryUserProcessor" mapTo="DepartmentCheckUserProcessService" />
    <register type="IPeopleDirectoryUserStorageHandler" mapTo="PeopleDirectoryStorageHandlerServiceAD" />
  </container>
</unity.interchange>

Synchronizing Users with your new UserProcessor

Within your PeopleSync Console App, manually kick off a People Sync by double clicking Akumina.PeopleSync.exe. You can set the sync up as a scheduled task afterwards. Details on doing so can be found here

Download

You can download the code referenced in this article here

Additional Articles

⚠️ **GitHub.com Fallback** ⚠️