public async Task <GroupModel> GetGroup(string id) { try { var groupModel = new GroupModel(); if (string.IsNullOrEmpty(id) || string.IsNullOrWhiteSpace(id)) { _logger.LogError("GroupService-GetGroup: Id cannot be empty"); return(null); } _logger.LogInfo($"GroupService-GetGroup: [Started] to get detail for group id {id} from Azure AD B2C"); var client = GraphClientUtility.GetGraphServiceClient(); if (client == null) { _logger.LogError("GroupService-GetGroup: Unable to create object for graph client"); return(null); } var group = await client.Groups[id].Request().GetAsync(); if (group != null) { _logger.LogInfo($"GroupService-GetGroup: Fetched the group detail for id {id} from Azure AD B2C"); groupModel = GraphClientUtility.ConvertGraphGroupToGroupModel(group, _logger); var members = await client.Groups[id].Members.Request().Select(e => new { e.Id }).GetAsync(); if (members != null) { groupModel.NoOfMembers = members.Count(); } var owners = await client.Groups[id].Owners.Request().Select(e => new { e.Id }).GetAsync(); if (owners != null) { groupModel.NoOfOwners = owners.Count(); } } _logger.LogInfo($"GroupService-GetGroup: [Completed] to get detail for group id {id} from Azure AD B2C"); return(groupModel); } catch (ServiceException ex) { _logger.LogError("GroupService-GetGroup: Exception occured...."); _logger.LogError(ex); throw ex; } }
public async Task <List <GroupModel> > GetGroupDetails() { List <GroupModel> retVal = new List <GroupModel>(); try { _logger.LogInfo("GroupService-GetGroups: Getting group detail from Azure AD B2C"); var _graphServiceClient = GraphClientUtility.GetGraphServiceClient(); var groupDetails = await _graphServiceClient.Groups.Request().GetAsync(); if (groupDetails != null) { foreach (var groupItem in groupDetails) { try { var groupModel = GraphClientUtility.ConvertGraphGroupToGroupModel(groupItem, _logger); if (groupItem.GroupTypes != null) { if (groupItem.GroupTypes.Any()) { groupModel.GroupType = string.Join('|', groupItem.GroupTypes); } } retVal.Add(groupModel); } catch (Exception ex) { _logger.LogError($"GroupService-GetGroups: failed to add group for group name {groupItem.DisplayName}"); _logger.LogError(ex); } } retVal = retVal.OrderBy(x => x.DisplayName).ToList(); } _logger.LogInfo("GroupService-GetGroups: Completed getting the group detail from Azure AD B2C"); } catch (Exception ex) { _logger.LogError("GroupService-GetGroups: Exception occured...."); _logger.LogError(ex); throw ex; } return(retVal); }
public async Task <GroupModel> CreateGroup(GroupModel groupModel) { try { _logger.LogInfo("GroupService-CreateGroup: [Started] creation of group in Azure AD B2C"); if (groupModel == null) { _logger.LogError("GroupService-CreateGroup: Group Model cannot be null..."); return(null); } var client = GraphClientUtility.GetGraphServiceClient(); if (client == null) { _logger.LogError("GroupService-CreateGroup: Unable to create object for graph client "); return(null); } var group = BuildGroup(groupModel); var newGroup = await client.Groups.Request().AddAsync(group); var newGroupModel = GraphClientUtility.ConvertGraphGroupToGroupModel(newGroup, _logger); if (newGroupModel != null) { var groupId = newGroup.Id; if (!string.IsNullOrEmpty(groupId)) { #region Assign Members if (groupModel.OwnerSelected.Any()) { _logger.LogInfo($"GroupService-CreateGroup: assigning {groupModel.OwnerSelected.Count()} owner(s) for group {newGroupModel.DisplayName} in Azure AD B2C"); var groupOwnerAssign = new GroupOwnerAssignModel { GroupId = groupId, SelectedOwners = groupModel.OwnerSelected }; await _groupOwnerService.AddGroupOwners(groupOwnerAssign); _logger.LogInfo($"GroupService-CreateGroup: assigned {groupModel.OwnerSelected.Count()} owner(s) for group {newGroupModel.DisplayName} in Azure AD B2C"); } if (groupModel.MemberSelected.Any()) { _logger.LogInfo($"GroupService-CreateGroup: assigning {groupModel.MemberSelected.Count()} member(s) for group {newGroupModel.DisplayName} in Azure AD B2C"); var groupMemberAssign = new GroupMemberAssignModel { GroupId = groupId, SelectedMembers = groupModel.MemberSelected }; await _groupMemberService.AddGroupMembers(groupMemberAssign); _logger.LogInfo($"GroupService-CreateGroup: assigned {groupModel.MemberSelected.Count()} member(s) for group {newGroupModel.DisplayName} in Azure AD B2C"); } #endregion } } _logger.LogInfo("GroupService-CreateGroup: [Completed] creation of group in Azure AD B2C"); return(newGroupModel); } catch (Exception ex) { _logger.LogError("GroupService-CreateGroup: Exception occured...."); _logger.LogError(ex); throw ex; } }