Exemple #1
0
        private async Task ExecuteStoreProcedure(string storedProcedure, GroupMember member, Guid groupId)
        {
            using (var conn = new SqlConnection(_connectionString))
            {
                await conn.OpenAsync();

                using (var cmd = new SqlCommand())
                {
                    cmd.Connection  = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = storedProcedure;
                    cmd.Parameters.AddWithValue("groupId", groupId);
                    cmd.Parameters.AddWithValue("memberId", member.Id);
                    try
                    {
                        await cmd.ExecuteNonQueryAsync();
                    }
                    catch (SqlException e)
                    {
                        switch (e.Number)
                        {
                        case 50001:
                            throw GroupNotFoundException.Create(groupId, e);

                        case 50002:
                            throw MemberNotFoundException.Create(member.Id, e);
                        }
                        throw;
                    }
                }
            }
        }
Exemple #2
0
        public async Task <GroupInfo> GetGroupInfoAsync(Guid groupId)
        {
            GroupInfo groupInfo = null;

            try
            {
                using (DirectoryEntry directoryEntry = new DirectoryEntry($"LDAP://<GUID={groupId}>"))
                {
                    directoryEntry.RefreshCache(new string[] { "displayName" });
                    string displayName = null;
                    if (directoryEntry.Properties["displayName"].Count == 1)
                    {
                        displayName = (string)directoryEntry.Properties["displayName"][0];
                    }
                    groupInfo = new GroupInfo(groupId, displayName, GroupStores.OnPremAd);
                }
            }
            catch (DirectoryServicesCOMException ex)
            {
                if ((uint)ex.HResult == LDAP_NO_SUCH_OBJECT)
                {
                    throw GroupNotFoundException.Create(groupId, ex);
                }
                throw;
            }
            return(await Task.FromResult(groupInfo));
        }
Exemple #3
0
        public async Task GetGroupMembersAsync(GroupMemberCollection memberCollection, Guid groupId)
        {
            await AssureGraphClient();

            try
            {
                var members = await _graphClient.Groups[groupId.ToString()].Members.Request().GetAsync();
                do
                {
                    foreach (var member in members)
                    {
                        if (member is User u)
                        {
                            memberCollection.Add(new GroupMember(Guid.Parse(u.Id), u.UserPrincipalName, GroupMemberTypes.AzureAd));
                        }
                        else
                        {
                            memberCollection.Add(new GroupMember(Guid.Parse(member.Id), member.Id, GroupMemberTypes.AzureAd));
                        }
                    }
                }while (members.NextPageRequest != null && (members = await members.NextPageRequest.GetAsync()).Count > 0);
            }
            catch (ServiceException ex)
            {
                if (IsNotFoundError(ex))
                {
                    throw GroupNotFoundException.Create(groupId, ex);
                }
                throw;
            }
        }
Exemple #4
0
        public async Task GetGroupMembersAsync(GroupMemberCollection memberCollection, Guid groupId)
        {
            string groupDn = GetDistinguishedName(groupId);

            if (groupDn == null)
            {
                throw GroupNotFoundException.Create(groupId);
            }
            string filter = $"(memberOf={groupDn})";

            QueryGroupMembers(memberCollection, filter, null);
            await Task.FromResult(0);
        }
Exemple #5
0
        public async Task <GroupInfo> GetGroupInfoAsync(Guid groupId)
        {
            await AssureGraphClient();

            try
            {
                var group = await _graphClient.Groups[groupId.ToString()].Request().GetAsync();
                return(new GroupInfo(groupId, group.DisplayName, GroupStores.AzureAd));
            }
            catch (ServiceException ex)
            {
                if (IsNotFoundError(ex))
                {
                    throw GroupNotFoundException.Create(groupId, ex);
                }
                throw;
            }
        }
Exemple #6
0
        public async Task GetGroupMembersAsync(GroupMemberCollection memberCollection, Guid groupId)
        {
            using (var conn = new SqlConnection(_connectionString))
            {
                await conn.OpenAsync();

                using (var cmd = new SqlCommand())
                {
                    cmd.Connection  = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "dbo.spOpenEGetGroupMember";
                    cmd.Parameters.AddWithValue("groupId", groupId);
                    SqlDataReader reader = null;
                    try
                    {
                        reader = await cmd.ExecuteReaderAsync();

                        while (await reader.ReadAsync())
                        {
                            memberCollection.Add(new GroupMember(
                                                     reader.GetGuid(1),
                                                     reader.GetString(2),
                                                     GroupMemberTypes.OnPremAd
                                                     ));
                        }
                    }
                    catch (SqlException e)
                    {
                        if (e.Number == 50001)
                        {
                            throw GroupNotFoundException.Create(groupId, e);
                        }
                        throw;
                    }
                    finally
                    {
                        if (reader != null)
                        {
                            reader.Dispose();
                        }
                    }
                }
            }
        }
Exemple #7
0
 private Exception CreateNotFoundException(Guid groupId, Guid? memberId, RemoteException ex)
 {
     Exception exception = null;
     foreach (Match match in guidRegex.Matches(ex.Message))
     {
         Guid? guid = Guid.Parse(match.Groups["guid"].Value);
         if (guid == groupId)
         {
             exception = GroupNotFoundException.Create(groupId, ex);
             break;
         }
         if (guid == memberId)
         {
             exception = MemberNotFoundException.Create(groupId, ex);
             break;
         }
     }
     return exception;
 }
Exemple #8
0
        public async Task <GroupInfo> GetGroupInfoAsync(Guid groupId)
        {
            using (var conn = new SqlConnection(_connectionString))
            {
                await conn.OpenAsync();

                using (var cmd = new SqlCommand())
                {
                    cmd.Connection  = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "dbo.spOpenEGetGroupInfo";
                    cmd.Parameters.AddWithValue("groupId", groupId);
                    SqlDataReader reader = null;
                    try
                    {
                        reader = await cmd.ExecuteReaderAsync();

                        if (await reader.ReadAsync())
                        {
                            return(new GroupInfo(groupId, reader.GetString(1), GroupStores.OpenE));
                        }
                    }
                    catch (SqlException e)
                    {
                        if (e.Number == 50001)
                        {
                            throw GroupNotFoundException.Create(groupId, e);
                        }
                        throw;
                    }
                    finally
                    {
                        if (reader != null)
                        {
                            reader.Dispose();
                        }
                    }
                }
            }
            throw GroupNotFoundException.Create(groupId);
        }
Exemple #9
0
        public async Task RemoveGroupMemberAsync(GroupMember member, Guid groupId)
        {
            if (member.MemberType != GroupMemberTypes.OnPremAd)
            {
                throw new ArgumentException(nameof(member), "Can only remove members of type 'OnPremAd'");
            }
            string groupDn = GetDistinguishedName(groupId);

            if (groupDn == null)
            {
                throw GroupNotFoundException.Create(groupId);
            }
            string memberDn = GetDistinguishedName(member.Id);

            if (memberDn == null)
            {
                throw MemberNotFoundException.Create(member.Id);
            }
            using (DirectoryEntry directoryEntry = GetDirectoryEntry(groupDn))
            {
                try
                {
                    directoryEntry.Properties["member"].Remove(memberDn);
                    directoryEntry.CommitChanges();
                }
                catch (DirectoryServicesCOMException ex)
                {
                    if ((uint)ex.HResult == LDAP_NO_SUCH_OBJECT)
                    {
                        throw GroupNotFoundException.Create(groupId, ex);
                    }
                    throw;
                }
            }
            await Task.FromResult(0);
        }