示例#1
0
        public async Task ComputeUsers(PerformContext performContext, int managedSupportId)
        {
            Logger.Info(performContext, "Getting the users from the Portal...");
            var remoteUsers = PortalService.GetAllUsers().ToArray();

            Logger.Info(performContext, "Getting the users from Active Directory...");
            var adUsers = _activeDirectoryManager.GetAllUsers().ToList();

            foreach (var adUser in adUsers)
            {
                performContext?.Cancel();

                var remoteUser = remoteUsers.FirstOrDefault(ru => ru.Id == adUser.Id);
                if (remoteUser == null)
                {
                    var newUser = LicenseUser.Create(
                        adUser,
                        managedSupportId,
                        AuthService.GetAccount());

                    await PortalService.AddUserAsync(newUser);

                    performContext?.WriteSuccessLine($"+ {newUser}");
                    Logger.Info($"Created: {newUser}");
                    Logger.Debug($"{JsonConvert.SerializeObject(newUser, Formatting.Indented)}");

                    continue;
                }

                remoteUser.UpdateValues(adUser);
                await PortalService.UpdateUserAsync(remoteUser);

                performContext?.WriteSuccessLine($"^ {remoteUser}");
                Logger.Info($"Updated: {remoteUser}");
                Logger.Debug($"{JsonConvert.SerializeObject(remoteUser, Formatting.Indented)}");
            }

            var staleUsers = remoteUsers.Except(adUsers, _licenseUserEqualityComparer).ToArray();

            foreach (var staleUser in staleUsers)
            {
                performContext?.Cancel();

                if (staleUser.IsDeleted)
                {
                    continue;
                }

                await PortalService.DeleteUserAsync(staleUser);

                performContext?.WriteWarnLine($"- {staleUser}");
                Logger.Info($"Delete: {staleUser}");
                Logger.Debug($"{JsonConvert.SerializeObject(staleUser, Formatting.Indented)}");
            }
        }
示例#2
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);
                }
            }
        }