예제 #1
0
        public void HandleEvent(NewActiveDirectoryUserEventData eventData)
        {
            lock (_newUserLock)
            {
                try
                {
                    string message       = eventData.Entry.Message;
                    string principalName = GetUserPrincipalName(message);
                    if (principalName.IsNullOrEmpty())
                    {
                        Logger.Warn("Could not determine the User Principal Name from the event. The current Event will be discarded.");
                        return;
                    }

                    Logger.Info($"User Principal Name extracted from the Event as {principalName}");

                    LicenseUser user = _activeDirectoryManager.GetUserByPrincipalName(principalName);
                    if (user == null)
                    {
                        Logger.Warn($"Unable to locate the User Principal Name {principalName} in Active Directory. The current Event will be discarded.");
                        return;
                    }

                    var remoteUser = _portalService.GetUserById(user.Id);
                    if (remoteUser.Count != 1)
                    {
                        var newUser = LicenseUser.Create(
                            user,
                            _settingManager.GetSettingValue <int>(AppSettingNames.ManagedSupportId),
                            _settingManager.GetSettingValue <int>(AppSettingNames.AutotaskAccountId)
                            );

                        AsyncHelper.RunSync(() => _portalService.AddUserAsync(newUser));
                        Logger.Info($"Created: {newUser}");
                        Logger.Debug($"{JsonConvert.SerializeObject(newUser, Formatting.Indented)}");
                        return;
                    }

                    remoteUser[0].UpdateValues(user);
                    AsyncHelper.RunSync(() => _portalService.UpdateUserAsync(remoteUser[0]));

                    Logger.Info($"Updated:  {remoteUser}");
                    Logger.Debug($"{JsonConvert.SerializeObject(remoteUser, Formatting.Indented)}");
                }
                catch (Exception ex)
                {
                    Logger.Error("There was an error while processing the new user.");
                    Logger.Error(ex.Message, ex);
                    Logger.Debug(ex.Message, ex);
                }
            }
        }