public override async Task <DetailedUser> Handle(Request request, CancellationToken cancellationToken) { try { using CancellationTokenSource cts = CreateCancellationTokenSource(cancellationToken); if (request is null) { throw new ArgumentNullException(nameof(request)); } Logger.LogDebug("Searching for user {Username}", request.Username); var user = await _userSearchService.SearchAsync(request.Username); if (user == null) { Logger.LogDebug("Lookup for {Username} returned null, returning null", request.Username); return(null); } Logger.LogDebug("Looking for project membership for user {Username}", user.UserName); var projects = await _userManagementService.GetProjectsForUserAsync(user, cts.Token); DetailedUser result = new DetailedUser(user, projects); return(result); } catch (Exception e) { Logger.LogError(e, "Error lookup user"); throw; } }
public override async Task <ProjectAccess> Handle(Request request, CancellationToken cancellationToken) { var project = _projects.SingleOrDefault(_ => _.Id == request.ProjectId); if (project == null) { Logger.LogInformation("Project {ProjectId} not found", request.ProjectId); throw new ProjectNotFoundException(request.ProjectId); } var user = await _userSearchService.SearchAsync(request.Username); if (user == null) { Logger.LogInformation("User {Username} not found", request.Username); throw new UserNotFoundException(request.Username); } using CancellationTokenSource cts = CreateCancellationTokenSource(cancellationToken); var access = await _userManagementService.RemoveUserFromProjectAsync(user, project, cts.Token); return(new ProjectAccess { Id = project.Id, Name = project.Name, Users = new List <UserAccess> { new UserAccess { Username = user.UserName, Access = access } } }); }
public async Task <User> Handle(Request request, CancellationToken cancellationToken) { if (request is null) { throw new ArgumentNullException(nameof(request)); } _logger.LogDebug("Searching for user {Username}", request.Username); var user = await _userSearchService.SearchAsync(request.Username); return(user); }
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 <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 async Task <DetailedUser> Handle(Request request, CancellationToken cancellationToken) { if (request is null) { throw new ArgumentNullException(nameof(request)); } _logger.LogDebug("Searching for user {Username}", request.Username); var user = await _userSearchService.SearchAsync(request.Username); if (user == null) { _logger.LogDebug("Lookup for {Username} returned null, returning null", request.Username); return(null); } _logger.LogDebug("Looking for project membership for user {Username}", user.UserName); var projects = await _userManagementService.GetProjectsForUserAsync(user); DetailedUser result = new DetailedUser(user, projects); return(result); }
public override async Task <string> AddUserAsync(string username) { if (string.IsNullOrEmpty(username)) { throw new ArgumentException("Username cannot be null or empty", nameof(username)); } var user = await _userSearchService.SearchAsync(username); if (string.IsNullOrEmpty(user?.UserPrincipalName)) { _logger.LogInformation("Cannot locate UPN for for {Username}, cannot add users access", username); return($"Unable to locate user's User Principal Name (UPN)"); } ISharePointClient restClient = await GetSharePointRestClientForUpdate(); GetSharePointWebVerboseResponse web = await restClient.GetWebAsync(); if (string.IsNullOrEmpty(web?.Data?.Title)) { _logger.LogWarning("Cannot get site group name for {Project} on resource {ResourceType}", Project.Name, ProjectResource.Type); return($"Unable to get SharePoint site group name"); } // we always add users to '<site-group> Members' var siteGroupTitle = web.Data.Title + " Members"; GetSiteGroupsVerboseResponse siteGroups = await restClient.GetSiteGroupsByTitleAsync(siteGroupTitle); if (siteGroups.Data.Results.Count == 0) { _logger.LogInformation("Cannot find site group {@SiteGroup}", new SiteGroup { Title = siteGroupTitle }); return($"SharePoint site group '{siteGroupTitle}' not found"); } var siteGroup = siteGroups.Data.Results[0]; _logger.LogInformation("Adding {Username} to site collection {@SiteGroup} for {Project} on resource {ResourceType}", username, siteGroup, Project.Name, ProjectResource.Type); string logonName = Constants.LoginNamePrefix + user.UserPrincipalName; try { await restClient.AddUserToGroupAsync(siteGroup.Id, new User { LoginName = logonName }); return(string.Empty); } catch (ApiException e) { var errorResponse = await e.GetContentAsAsync <SharePointErrorResponse>(); _logger.LogWarning(e, "Error adding user to Sharepoint group {@Error}", errorResponse); return($"Error occuring adding user to SharePoint site group '{siteGroupTitle}'"); } }