public static IEnumerable <MicrosoftGraphUser> FilterUsers(this IMicrosoftGraphClient client, MicrosoftObjectFilterOptions options) { if (!string.IsNullOrEmpty(options.Id)) { try { MicrosoftGraphUser user = client.Users.GetUser(options.Id); if (user != null) { return(new List <MicrosoftGraphUser> { user }); } } catch { /* The group does not exist, ignore the exception */ } } else if (!string.IsNullOrEmpty(options.UPN) || !string.IsNullOrEmpty(options.Mail)) { IList <MicrosoftGraphUser> result = null; try { string upnOrMail = options.UPN ?? options.Mail; var odataQuery = new ODataQuery <MicrosoftGraphUser>(); if (!string.IsNullOrEmpty(options.UPN)) { odataQuery.SetFilter(u => u.UserPrincipalName == upnOrMail); } else { odataQuery.SetFilter(u => u.Mail == upnOrMail); } result = client.Users.ListUser(filter: FormatFilterString(odataQuery)).Value; } catch { /* The user does not exist, ignore the exception. */ } if (result != null) { return(result.Select(u => u)); } } else { ODataQuery <MicrosoftGraphUser> odataQuery = null; if (!string.IsNullOrEmpty(options.SearchString) && options.SearchString.EndsWith("*")) { options.SearchString = options.SearchString.TrimEnd('*'); odataQuery = new ODataQuery <MicrosoftGraphUser>(u => u.DisplayName.StartsWith(options.SearchString)); } else { odataQuery = new ODataQuery <MicrosoftGraphUser>(u => u.DisplayName == options.SearchString); } return(client.Users.ListUser(filter: FormatFilterString(odataQuery)).Value); } return(new List <MicrosoftGraphUser>()); }
public static PSADUser ToPSADUser(this MicrosoftGraphUser user) { return(new PSADUser() { DisplayName = user.DisplayName, Id = user.Id, DeletionTimestamp = user.DeletedDateTime, UserPrincipalName = user.UserPrincipalName, Type = user.UserType ?? "User", UsageLocation = user.UsageLocation, GivenName = user.GivenName, Surname = user.Surname, AccountEnabled = user.AccountEnabled, MailNickname = user.MailNickname, Mail = user.Mail, ImmutableId = user.OnPremisesImmutableId, AdditionalProperties = user.AdditionalProperties }); }
/// <summary> /// Update entity in users /// </summary> /// <remarks> /// Represents an Azure Active Directory user object. /// </remarks> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='userId'> /// key: id of user /// </param> /// <param name='body'> /// New property values /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> public static async Task UpdateUserAsync(this IUsersOperations operations, string userId, MicrosoftGraphUser body, CancellationToken cancellationToken = default(CancellationToken)) { (await operations.UpdateUserWithHttpMessagesAsync(userId, body, null, cancellationToken).ConfigureAwait(false)).Dispose(); }
/// <summary> /// Update entity in users /// </summary> /// <remarks> /// Represents an Azure Active Directory user object. /// </remarks> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='userId'> /// key: id of user /// </param> /// <param name='body'> /// New property values /// </param> public static void UpdateUser(this IUsersOperations operations, string userId, MicrosoftGraphUser body) { operations.UpdateUserAsync(userId, body).GetAwaiter().GetResult(); }
/// <summary> /// Add new entity to users /// </summary> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='body'> /// New entity /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> public static async Task <MicrosoftGraphUser> CreateUserAsync(this IUsersOperations operations, MicrosoftGraphUser body, CancellationToken cancellationToken = default(CancellationToken)) { using (var _result = await operations.CreateUserWithHttpMessagesAsync(body, null, cancellationToken).ConfigureAwait(false)) { return(_result.Body); } }
/// <summary> /// Add new entity to users /// </summary> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='body'> /// New entity /// </param> public static MicrosoftGraphUser CreateUser(this IUsersOperations operations, MicrosoftGraphUser body) { return(operations.CreateUserAsync(body).GetAwaiter().GetResult()); }
public IEnumerable <PSADUser> FilterUsers(ADObjectFilterOptions options, int first = int.MaxValue, int skip = 0) { if (!string.IsNullOrEmpty(options.Id)) { MicrosoftGraphUser user = null; try { user = GraphClient.Users.GetUser(Normalize(options.Id)); } catch { /* The user does not exist, ignore the exception. */ } if (user != null) { return(new List <PSADUser> { user.ToPSADUser() }); } } else if (!string.IsNullOrEmpty(options.UPN) || !string.IsNullOrEmpty(options.Mail)) { IList <MicrosoftGraphUser> result = null; try { string upnOrMail = Normalize(options.UPN) ?? Normalize(options.Mail); var odataQuery = new Rest.Azure.OData.ODataQuery <MicrosoftGraphUser>(); if (!string.IsNullOrEmpty(options.UPN)) { odataQuery.SetFilter(u => u.UserPrincipalName == upnOrMail); } else { odataQuery.SetFilter(u => u.Mail == upnOrMail); } result = GraphClient.Users.ListUser( "eventual", filter: OdataHelper.GetFilterString(odataQuery) ).Value; } catch { /* The user does not exist, ignore the exception. */ } if (result != null) { return(result.Select(u => u.ToPSADUser())); } } else { Rest.Azure.OData.ODataQuery <MicrosoftGraphUser> odataQuery = null; if (!string.IsNullOrEmpty(options.SearchString) && options.SearchString.EndsWith("*")) { options.SearchString = options.SearchString.TrimEnd('*'); odataQuery = new Rest.Azure.OData.ODataQuery <MicrosoftGraphUser>(u => u.DisplayName != null && u.DisplayName.StartsWith(options.SearchString)); } else { odataQuery = new Rest.Azure.OData.ODataQuery <MicrosoftGraphUser>(u => u.DisplayName == options.SearchString); } return(GraphClient.Users.ListUser( consistencyLevel: "eventual", filter: OdataHelper.GetFilterString(odataQuery) ).Value.Select(u => u.ToPSADUser())); } return(new List <PSADUser>()); }