Skip to content

Commit

Permalink
Merge pull request #14 from NRG-Drink/feat/extend-user-search
Browse files Browse the repository at this point in the history
Feat/extend user search
  • Loading branch information
NRG-Drink authored May 23, 2024
2 parents d055c16 + a0a61f9 commit 8c37bf1
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 7 deletions.
61 changes: 60 additions & 1 deletion NRG.CalendarFinder/NRG.CalendarFinder/CalendarFinderService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.ODataErrors;
using NRG.CalendarFinder.MsGraphFactories;

namespace NRG.CalendarFinder;
Expand All @@ -15,7 +16,7 @@ public async Task<User> FindUserAsyncOrThrow(string userIdentifier)
throw new ArgumentException(userIdentifier, nameof(userIdentifier));
}

var user = await _graph.Users[userIdentifier].GetAsync();
var user = await TryFindUser(userIdentifier);

if (user is null || user.Id is null)
{
Expand All @@ -25,6 +26,64 @@ public async Task<User> FindUserAsyncOrThrow(string userIdentifier)
return user;
}

private async Task<User?> TryFindUser(string userIdentifier)
{
try
{
return await GetUserByIdentifier(userIdentifier);
}
catch (ODataError _)
{
var mailUser = await GetUserByMail(userIdentifier);
if (mailUser is not null)
{
return mailUser;
}

var nickname = GetMailNickname(userIdentifier);

var mailNicknameUser = await GetUserByMail(nickname);
if (mailNicknameUser is not null)
{
return mailNicknameUser;
}

var principleUser = await GetUserByPrincipleName(nickname);

return principleUser;
}
}

private static string GetMailNickname(string userIdentifier)
=> userIdentifier
.Split("@")
.FirstOrDefault()
?? userIdentifier;

private async Task<User?> GetUserByPrincipleName(string nickname)
{
var users = await _graph.Users.GetAsync(e =>
{
e.QueryParameters.Filter = $"startsWith(userPrincipleName,'{nickname}')";
});

return users?.Value?.FirstOrDefault();
}

private async Task<User?> GetUserByMail(string nickname)
{
var users = await _graph.Users.GetAsync(e =>
{
e.QueryParameters.Filter = $"startsWith(mail,'{nickname}')";
});

return users?.Value?.FirstOrDefault();
}


private Task<User?> GetUserByIdentifier(string userIdentifier)
=> _graph.Users[userIdentifier].GetAsync();

public async Task<List<Calendar>> FindCalendarsAsyncOrThrow(User user)
{
var calendarResponse = await _graph.Users[user.Id ?? user.Mail].Calendars.GetAsync();
Expand Down
74 changes: 68 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,80 @@
The `CalendarFinder` is an app to find all calendars of a user and some user-data in an easy way without making any `http` requests in ms-graph.

## Usage
In the example below, the calendars for the `UserPrincipalName` 'aula@iseschool.ch are found.
### Query
In this section, the parameters the exe is calles with are listed.
### Result
In this section, the found values are listed.
1. Create a file (like mydata.json) put the `Azure Ad App` Registration and your user-identifiers as shown in the example.
2. Start the app with the input file (step 1) as parameter.
3. The result file (like mydata.result.json) will be opened in `Visual Studio Code` automatically.

### Example
![alt text](image-1.png)
#### Input Data As Json
```json
{
"MsGraphClients": [
{
"TenantId": "adzure_ad_app_tenant_guid",
"ClientId": "adzure_ad_app_client_guid",
"Thumbprint": "certificate_thumbprint",
"Scopes": null
}
],
"UserIdentifier": [
"my_guid_user_identifier",
"my_user_principle_name_identifier",
"my_user_mail_identifier"
]
}
```
#### Start Process (CMD)
```cmd
> NRG.CalendarFinder.exe -f mydata.json
```
#### Result Data As Json
```json
[
{
"Input": {
"UserIdentifier": "my_input_user_identifier"
},
"User": {
"DisplayName": "output_display_name",
"PrincipalName": "output_user_principla_name",
"Mail": "output_user_mail",
"UserId": "output_user_guid",
"CalendarId": "only_or_default_calendar_id"
},
"Calendars": [
{
"Id": "calendar_id",
"Name": "calendar_name",
"Owner": "owner_mail",
"IsDefault": true
}
]
}
{
...
}
]
```

## Needs
- [Appregistration (Azure Ad App)](https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-register-app)
- Necessary Permissions on the App (Azure Ad App)

## Nice To Know
### Need Help In Console To Use The App?
Type `--help` to see the help text and the required and optional arguments.
### Supress File Opening After Finish
To not open the file in `Visual Studio Code` after finish searching, use the command with the argument `-e false` (e is for editor).
### How Are The Users Found?
By default `ms-graph` allows to find users directly by providing the user-id or user-principle-name.
That's how this order came about:
1. user-id / user-principle-name
2. user-mail
3. user-mail-nickname\*
4. user-principle-nickname\*
\**the mail-nickname is the part left of the @ in the mail-address (nickname@provider.com -> nickname)*

## Used Libraries
- [Command Line Parser](https://github.com/commandlineparser/commandline)
- [Ms Graph](https://github.com/microsoftgraph/msgraph-sdk-dotnet)
Expand Down
Binary file removed image-1.png
Binary file not shown.

0 comments on commit 8c37bf1

Please sign in to comment.