Esempio n. 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)}");
            }
        }