Exemple #1
0
        public async Task <IActionResult> GetGroupOwners(string id)
        {
            try
            {
                if (string.IsNullOrEmpty(id) || string.IsNullOrWhiteSpace(id))
                {
                    _logger.LogError("GroupOwnersController-GetGroupOwners: Id cannot be empty");
                    return(BadRequest());
                }

                var groupOwnerModel = new GroupOwnerModel
                {
                    GroupId = id
                };

                _logger.LogInfo($"GroupOwnersController-GetGroupOwners: [Started] to get owner(s) detail for group id {id} from Azure AD B2C");

                var client = GraphClientUtility.GetGraphServiceClient();

                if (client == null)
                {
                    _logger.LogError("GroupOwnersController-GetGroupOwners: Unable to create object for graph client");
                    return(NotFound());
                }

                GroupService groupService = new GroupService(_logger, client);
                groupOwnerModel = await groupService.GetGroupOwnerAsync(id);


                _logger.LogInfo($"GroupOwnersController-GetGroup: [Completed] to getting owner(s) detail for group id {id} from Azure AD B2C");
                return(Ok(groupOwnerModel));
            }
            catch (ServiceException ex)
            {
                _logger.LogError("GroupOwnersController-GetGroupOwners: Exception occured....");
                _logger.LogError(ex);

                if (ex.StatusCode == HttpStatusCode.BadRequest)
                {
                    return(BadRequest());
                }
                else
                {
                    return(NotFound());
                }
            }
        }
Exemple #2
0
        public async Task <GroupOwnerModel> GetGroupOwners(string id)
        {
            try
            {
                if (string.IsNullOrEmpty(id) || string.IsNullOrWhiteSpace(id))
                {
                    _logger.LogError("GroupOwnerService-GetGroupOwners: Id cannot be empty");
                    return(null);
                }

                var groupOwnerModel = new GroupOwnerModel
                {
                    GroupId = id
                };

                _logger.LogInfo($"GroupOwnerService-GetGroupOwners: [Started] to get member detail for group id {id} from Azure AD B2C");

                var client = GraphClientUtility.GetGraphServiceClient();

                if (client == null)
                {
                    _logger.LogError("GroupOwnerService-GetGroupOwners: Unable to create object for graph client");
                    return(null);
                }

                groupOwnerModel = await GetGroupOwnerAsync(id);

                _logger.LogInfo($"GroupOwnerService-GetGroupOwners: [Completed] to getting member detail for group id {id} from Azure AD B2C");
                return(groupOwnerModel);
            }
            catch (ServiceException ex)
            {
                _logger.LogError("GroupOwnerService-GetGroupOwners: Exception occured....");
                _logger.LogError(ex);
                throw ex;
            }
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        public async Task <GroupOwnerModel> GetGroupOwnerAsync(string groupId)
        {
            GroupOwnerModel retVal = null;

            try
            {
                if (string.IsNullOrEmpty(groupId))
                {
                    _logger.LogError("GroupService-GetGroupOwnerAsync: Group Id cannot be null");
                    return(retVal);
                }
                _logger.LogInfo("GroupService-GetGroupOwnerAsync: Starting to get group owner from Azure AD B2C");


                retVal = new GroupOwnerModel
                {
                    GroupId = groupId
                };

                var tasks = new Task[]
                {
                    Task.Run(() => {
                        var ownersList = GetNewGroupDefaultOwnerMember();
                        if (ownersList != null)
                        {
                            retVal.Owners = new Dictionary <string, string>();
                            foreach (var aDict in ownersList.Result)
                            {
                                retVal.Owners.Add(aDict.Key, aDict.Value);
                            }
                        }
                    }),
                    Task.Run(() =>
                    {
                        var assignedOwners = GetOwnersForGroup(groupId);
                        if (assignedOwners != null)
                        {
                            retVal.AssignedOwners = assignedOwners.Result.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();
                        retVal.Owners = FilterGroupOwnerMember(userIds, retVal.Owners);
                    }
                }

                _logger.LogInfo("GroupService-GetGroupOwnerAsync: Completed getting the group owner from Azure AD B2C");
            }
            catch (Exception ex)
            {
                _logger.LogError("GroupService-GetGroupOwnerAsync: Exception occured....");
                _logger.LogError(ex);
                throw ex;
            }
            return(retVal);
        }