public async Task <UsersModel> GetFilteredUsers(string filter) { UsersModel usersModel = new UsersModel(); var client = GraphClientUtility.GetGraphServiceClient(); var userList = await client.Users.Request().Filter($"startswith(displayName,'{filter}') or startswith(givenName,'{filter}') or startswith(surname,'{filter}') or startswith(mail,'{filter}') or startswith(userPrincipalName,'{filter}')").GetAsync(); if (userList != null) { foreach (var user in userList) { UserModel userModel = GraphClientUtility.ConvertGraphUserToUserModel(user, null); usersModel.Users.Add(userModel); } } return(usersModel); }
public async Task <UsersModel> GetUser() { UsersModel usersModel = new UsersModel(); var client = GraphClientUtility.GetGraphServiceClient(); if (client == null) { return(usersModel); } var userList = await client.Users.Request().GetAsync(); if (userList != null) { foreach (var user in userList) { UserModel userModel = GraphClientUtility.ConvertGraphUserToUserModel(user, null); usersModel.Users.Add(userModel); } } return(usersModel); }
public async Task <UserModel> GetUser(string id) { var userModel = new UserModel(); try { var client = GraphClientUtility.GetGraphServiceClient(); var user = await client.Users[id].Request().GetAsync(); var extensions = await client.Users[id].Extensions.Request().GetAsync(); user.Extensions = extensions; userModel = GraphClientUtility.ConvertGraphUserToUserModel(user, null); userModel.SignInName = user.UserPrincipalName; } catch (Exception ex) { _logger.LogError(ex); } return(userModel); }
private async Task <List <UserModel> > GetMembersForGroup(string groupId) { List <UserModel> retVal = new List <UserModel>(); try { var members = await _graphServiceClient.Groups[groupId].Members.Request().GetAsync(); if (members != null) { foreach (var member in members) { try { if (!(member is User)) { continue; } UserModel userModel = GraphClientUtility.ConvertGraphUserToUserModel((User)member, _logger); retVal.Add(userModel); } catch (Exception ex) { _logger.LogError($"GroupService-GetMembersForGroup: Error adding group owner to the collection for owner {member.Id}"); _logger.LogError(ex); } } } } catch (Exception ex) { _logger.LogError("GroupService-GetMembersForGroup: Exception occured...."); _logger.LogError(ex); throw ex; } return(retVal); }
private async Task <GroupOwnerModel> GetGroupOwnerAsync(string groupId) { GroupOwnerModel retVal = null; try { if (string.IsNullOrEmpty(groupId)) { _logger.LogError("GroupOwnerService-GetGroupOwnerAsync: Group Id cannot be null"); return(retVal); } _logger.LogInfo("GroupOwnerService-GetGroupOwnerAsync: Starting to get group owner from Azure AD B2C"); retVal = new GroupOwnerModel { GroupId = groupId }; var _graphServiceClient = GraphClientUtility.GetGraphServiceClient(); var tasks = new Task[] { Task.Run(async() => { Dictionary <string, string> ownersList = null; var users = await _graphServiceClient.Users.Request().GetAsync(); if (users != null) { ownersList = new Dictionary <string, string>(); foreach (var user in users.Where(x => !string.IsNullOrEmpty(x.UserPrincipalName))) { try { if (!ownersList.ContainsKey(user.Id)) { var value = string.IsNullOrEmpty(user.GivenName + user.Surname) ? user.UserPrincipalName : $"{user.GivenName} {user.Surname}"; ownersList.Add(user.Id, value); } } catch (Exception ex) { _logger.LogError($"GroupOwnerService-GetGroupOwnerMemberAsync: unable to add user in the group owner and member collection for user {user.UserPrincipalName}"); _logger.LogError(ex); } } } if (ownersList != null) { retVal.Owners = new Dictionary <string, string>(); foreach (var aDict in ownersList) { retVal.Owners.Add(aDict.Key, aDict.Value); } } }), Task.Run(async() => { List <UserModel> assignedOwners = new List <UserModel>(); var owners = await _graphServiceClient.Groups[groupId].Owners.Request().GetAsync(); if (owners != null) { foreach (var owner in owners) { try { if (!(owner is User)) { continue; } UserModel userModel = GraphClientUtility.ConvertGraphUserToUserModel((User)owner, _logger); assignedOwners.Add(userModel); } catch (Exception ex) { _logger.LogError($"GroupOwnerService-GetOwnersForGroupGetOwnersForGroup: Error adding group owner to the collection for owner {owner.Id}"); _logger.LogError(ex); } } } if (assignedOwners != null) { retVal.AssignedOwners = assignedOwners.ToList <UserModel>(); } }) }; await Task.WhenAll(tasks); if (retVal != null) { if ((retVal.AssignedOwners != null) && (retVal.Owners != null)) { var userIds = retVal.AssignedOwners.Select(x => x.Id).ToList(); if (retVal.Owners.Any() && userIds.Any()) { var keys = retVal.Owners.Keys.Where(x => userIds.Contains(x)).ToList(); foreach (var key in keys) { retVal.Owners.Remove(key); } } } } _logger.LogInfo("GroupOwnerService-GetGroupOwnerAsync: Completed getting the group owner from Azure AD B2C"); } catch (Exception ex) { _logger.LogError("GroupOwnerService-GetGroupOwnerAsync: Exception occured...."); _logger.LogError(ex); throw ex; } return(retVal); }