Skip to content

Commit 5048b52

Browse files
committed
Enable users menu if you have Administrator role
1 parent 20051d5 commit 5048b52

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/AzureSphereExplorer/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<Menu DockPanel.Dock="Top">
3939
<MenuItem Header="View">
4040
<MenuItem Header="Reload" Click="menuitemReload_Click"/>
41-
<MenuItem Header="Users..." Click="menuitemUsers_Click"/>
41+
<MenuItem x:Name="menuitemUsers" Header="Users..." Click="menuitemUsers_Click" IsEnabled="False"/>
4242
<MenuItem Header="Error Reports..." Click="menuitemErrorReports_Click"/>
4343
</MenuItem>
4444
<MenuItem Header="Help">

src/AzureSphereExplorer/MainWindow.xaml.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
8787
Tenant = dialog.SelectedTenant;
8888
}
8989

90-
await RefreshAllGrids();
90+
var roles = await Api.GetRolesAsync(Tenant, Api.Username, cancellationTokenSource.Token);
91+
if (roles.Contains("Administrator")) menuitemUsers.IsEnabled = true;
9192

93+
await RefreshAllGrids();
9294
}
9395

9496
private async Task RefreshAllGrids()

src/AzureSpherePublicAPI/AzureSphereAPI.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class AzureSphereAPI
1919
private static readonly Uri AzureSphereApiEndpoint = new Uri("https://prod.core.sphere.azure.net/");
2020

2121
private string AccessToken = string.Empty;
22+
public string Username { get; private set; } = string.Empty;
2223

2324
public async Task AuthenticationAsync(CancellationToken cancellationToken)
2425
{
@@ -33,6 +34,7 @@ public async Task AuthenticationAsync(CancellationToken cancellationToken)
3334
.ExecuteAsync();
3435

3536
AccessToken = authResult.AccessToken;
37+
Username = authResult.Account.Username;
3638
}
3739

3840
public async Task<List<AzureSphereTenant>> GetTenantsAsync(CancellationToken cancellationToken)
@@ -51,6 +53,26 @@ public async Task<List<AzureSphereTenant>> GetTenantsAsync(CancellationToken can
5153
return tenants;
5254
}
5355

56+
public async Task<List<string>> GetRolesAsync(AzureSphereTenant tenant, string username, CancellationToken cancellationToken)
57+
{
58+
var jsonString = await GetAsync($"v2/tenants/{tenant.Id}/users/{username}/role", cancellationToken);
59+
Console.WriteLine("GetRolesAsync()");
60+
Console.WriteLine(jsonString);
61+
var json = JToken.Parse(jsonString);
62+
var jsonRoles = json.Value<JArray>("Roles");
63+
if (jsonRoles.Count != 1) throw new ApplicationException();
64+
if (jsonRoles[0].Value<string>("TenantId") != tenant.Id) throw new ApplicationException();
65+
var jsonRoleNames = jsonRoles[0]["RoleNames"];
66+
67+
var roleNames = new List<string>();
68+
foreach (var jsonRoleName in jsonRoleNames)
69+
{
70+
roleNames.Add(jsonRoleName.Value<string>());
71+
}
72+
73+
return roleNames;
74+
}
75+
5476
public async Task<List<AzureSphereProduct>> GetProductsAsync(AzureSphereTenant tenant, CancellationToken cancellationToken)
5577
{
5678
var jsonString = await GetAsync($"v2/tenants/{tenant.Id}/products", cancellationToken);

0 commit comments

Comments
 (0)