public IEnumerable <LicenseUser> GetAllUsers()
        {
            using (var principalContext = new PrincipalContext(ContextType.Domain))
            {
                using (var userPrincipal = new UserPrincipal(principalContext))
                {
                    using (var principalSearcher = new PrincipalSearcher(userPrincipal))
                    {
                        using (PrincipalSearchResult <Principal> results = principalSearcher.FindAll())
                        {
                            foreach (Principal principal in results)
                            {
                                if (!principal.Guid.HasValue)
                                {
                                    continue;
                                }

                                LicenseUser localUser = GetUserById(principal.Guid.Value);
                                if (localUser == null)
                                {
                                    continue;
                                }

                                yield return(localUser);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
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)}");
            }
        }
Exemplo n.º 3
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);
                }
            }
        }
        public List <LicenseUserGroup> GetGroupMembers(Guid groupId)
        {
            using (var principalContext = new PrincipalContext(ContextType.Domain))
            {
                GroupPrincipal group = GetGroupPrincipal(principalContext, groupId);

                var licenseGroupUsers = new List <LicenseUserGroup>();

                try
                {
                    using (PrincipalSearchResult <Principal> members = group.GetMembers())
                    {
                        if (!members.Any())
                        {
                            return(licenseGroupUsers);
                        }

                        foreach (Principal principal in members)
                        {
                            UserPrincipal user;
                            try
                            {
                                user = principal.Validate();
                            }
                            catch (Exception ex)
                            {
                                Logger.Error(ex.Message);
                                continue;
                            }

                            if (!user.Guid.HasValue)
                            {
                                continue;
                            }

                            LicenseUser localUser = GetUserById(user.Guid.Value);
                            if (localUser == null)
                            {
                                continue;
                            }

                            licenseGroupUsers.Add(new LicenseUserGroup
                            {
                                GroupId = groupId,
                                UserId  = localUser.Id
                            });
                        }

                        return(licenseGroupUsers);
                    }
                }
                catch (COMException ex)
                {
                    Logger.Warn($"There was a problem getting the members of group: {groupId}");
                    Logger.Error(ex.Message, ex);
                    return(licenseGroupUsers);
                }
                catch (AuthenticationException ex)
                {
                    Logger.Warn($"There was a problem getting the members of group: {groupId}");
                    Logger.Error(ex.Message, ex);
                    return(licenseGroupUsers);
                }
                catch (PrincipalOperationException ex)
                {
                    Logger.Error($"Group: {group.Name} has some invalid members. This will need to be manually corrected in Active Directory.");
                    Logger.Debug(ex.Message, ex);
                    return(licenseGroupUsers);
                }
            }
        }
Exemplo n.º 5
0
 public async Task UpdateUserAsync(LicenseUser user)
 {
     _context.UpdateObject(user);
     await SaveChangesAsync();
 }
Exemplo n.º 6
0
 public async Task AddUserAsync(LicenseUser user)
 {
     _context.AddToUsers(user);
     await SaveChangesAsync();
 }