public override async Task <string> RemoveUserAsync(string username)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentException("Username cannot be null or empty", nameof(username));
            }

            IODataClient client = GetODataClient();
            string       logon  = IDIR.Logon(username);

            SystemUser entry = await GetSystemUserByLogon(client, logon);

            if (entry == null)
            {
                _logger.LogInformation("User {Username} was not found in Dynamics, will not perform update", username);
                return(string.Empty);
            }

            if (entry.IsDisabled.HasValue && entry.IsDisabled.Value)
            {
                _logger.LogInformation("User {SystemUser} is already disabled in Dynamics, will not perform update", new { entry.DomainName, entry.IsDisabled, entry.SystemUserId });
                return(string.Empty); // user does not exist, or is already disabled
            }

            await UpdateSystemUserDisableFlag(client, entry.SystemUserId, true);

            return(string.Empty);
        }
        public override async Task <string> RemoveUserAsync(string username, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentException("Username cannot be null or empty", nameof(username));
            }

            IODataClient client = GetODataClient();
            string       logon  = IDIR.Logon(username);

            Logger.Debug("Removing {Username} from project", username);

            SystemUser entry = await client
                               .For <SystemUser>()
                               .Filter(_ => _.DomainName == logon)
                               .Select(_ => _.SystemUserId)
                               .FindEntryAsync(cancellationToken);

            if (entry == null)
            {
                Logger.Information("User {Username} was not found in Dynamics, will not perform update", username);
                return(string.Empty);
            }

            Logger.Information("{@SystemUser} exists ensuring the user is disabled", entry);
            await UpdateSystemUserDisableFlag(client, entry.SystemUserId, user : null, true, cancellationToken);

            return(string.Empty);
        }
        public override async Task <string> AddUserAsync(string username, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentException("Username cannot be null or empty", nameof(username));
            }

            IODataClient client = GetODataClient();
            string       logon  = IDIR.Logon(username);

            Logger.Debug("Adding {Username} to project", username);

            User user = await _userSearchService.SearchAsync(username);

            SystemUser entry = await client
                               .For <SystemUser>()
                               .Filter(_ => _.DomainName == logon)
                               .Select(_ => _.SystemUserId)
                               .FindEntryAsync(cancellationToken);

            if (entry == null)
            {
                Logger.Information("{Username} does not exist, creating a new record", username);

                BusinessUnit rootBusinessUnit = await GetRootBusinessUnit(client, cancellationToken);

                // populate the SystemUser with required attributes
                entry = new SystemUser
                {
                    Firstname              = user.FirstName,
                    Lastname               = user.LastName,
                    DomainName             = IDIR.Logon(username),
                    InternalEMailAddress   = user.Email,
                    BusinessUnit           = rootBusinessUnit,
                    IsDisabled             = false,
                    SharePointEmailAddress = user.UserPrincipalName
                };

                await client
                .For <SystemUser>()
                .Set(entry)
                .InsertEntryAsync(cancellationToken);
            }
            else
            {
                Logger.Information("{@SystemUser} exists ensuring the user is enabled", entry);
                await UpdateSystemUserDisableFlag(client, entry.SystemUserId, user, false, cancellationToken);
            }

            return(string.Empty);
        }
        public override async Task <bool> UserHasAccessAsync(string username)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentException("Username cannot be null or empty", nameof(username));
            }

            IODataClient client = GetODataClient();

            string logon = IDIR.Logon(username);

            var entry = await GetSystemUserByLogon(client, logon);

            return(entry?.IsDisabled != null && !entry.IsDisabled.Value);
        }
        public override async Task <string> AddUserAsync(string username)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentException("Username cannot be null or empty", nameof(username));
            }

            IODataClient client = GetODataClient();
            string       logon  = IDIR.Logon(username);

            SystemUser entry = await GetSystemUserByLogon(client, logon);

            if (entry == null)
            {
                _logger.LogInformation("{Username} does not exist, creating a new record", username);

                User user = await _userSearchService.SearchAsync(username);

                BusinessUnit rootBusinessUnit = await GetRootBusinessUnit(client);

                // populate the SystemUser with required attributes
                entry = new SystemUser
                {
                    Firstname            = user.FirstName,
                    Lastname             = user.LastName,
                    DomainName           = IDIR.Logon(username),
                    InternalEMailAddress = user.Email,
                    BusinessUnit         = rootBusinessUnit,
                    IsDisabled           = false
                };

                entry = await client
                        .For <SystemUser>()
                        .Set(entry)
                        .InsertEntryAsync();
            }
            else if (entry.IsDisabled != null && entry.IsDisabled.Value)
            {
                _logger.LogInformation("{@SystemUser} exists but is disabled, enabling user", entry);
                await UpdateSystemUserDisableFlag(client, entry.SystemUserId, false);
            }
            else
            {
                _logger.LogInformation("{@SystemUser} exists and is already enabled user", entry);
            }

            return(string.Empty);
        }
        public override async Task <bool> UserHasAccessAsync(string username, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentException("Username cannot be null or empty", nameof(username));
            }

            IODataClient client = GetODataClient();

            string logon = IDIR.Logon(username);

            Logger.Debug("Checking {Username} has access to project", username);

            SystemUser entry = await client
                               .For <SystemUser>()
                               .Filter(_ => _.DomainName == logon && _.IsDisabled == false)
                               .Select(_ => _.SystemUserId)
                               .FindEntryAsync(cancellationToken);

            return(entry != null);
        }