Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions CS/BlazorAuth/BlazorAuth.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>aspnet-BlazorAuth-FEB431D8-D991-4F8A-923F-40225A99A530</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DevExpress.AspNetCore.Dashboard" Version="26.1.5" />
<PackageReference Include="DevExpress.Blazor.Dashboard" Version="26.1.5" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="10.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="10.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="10.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.10" />
<PackageReference Include="DevExpress.AspNetCore.Dashboard" Version="26.2.*-*" />
<PackageReference Include="DevExpress.Blazor.Dashboard" Version="26.2.*-*" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="10.0.14" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.14" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="10.0.14" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="10.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.14" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
</ItemGroup>
</Project>
18 changes: 18 additions & 0 deletions CS/BlazorAuth/Code/AuthHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.AspNetCore.Components.Authorization;

namespace BlazorAuth {
public static class AuthHelper {
internal static string? GetUserName(AuthenticationStateProvider provider) {
var stateTask = provider.GetAuthenticationStateAsync();

if (!stateTask.IsCompletedSuccessfully)
throw new InvalidOperationException("Authentication state is not ready.");

var identity = stateTask.GetAwaiter().GetResult().User.Identity;

return identity?.IsAuthenticated == true
? identity.Name?.Split('@')[0]
: null;
}
}
}
10 changes: 5 additions & 5 deletions CS/BlazorAuth/Code/CustomConnectionStringProvider.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Web;
using System.Collections.Generic;

namespace BlazorAuth {
public class CustomConnectionStringProvider : IDataSourceWizardConnectionStringsProvider {
private string userName;
private Dictionary<string, string> connectionStrings = new Dictionary<string, string>();
readonly IDashboardUserProvider dashboardUserProvider;
readonly Dictionary<string, string> connectionStrings = new Dictionary<string, string>();

public CustomConnectionStringProvider(string userName) {
this.userName = userName;
public CustomConnectionStringProvider(IDashboardUserProvider dashboardUserProvider) {
this.dashboardUserProvider = dashboardUserProvider;
connectionStrings.Add("NorthwindConnectionString", @"XpoProvider=SQLite; Data Source=App_Data/nwind.db;");
connectionStrings.Add("CarsXtraSchedulingConnectionString", @"XpoProvider=SQLite;Data Source=App_Data/CarsDB.db;");
}

public Dictionary<string, string> GetConnectionDescriptions() {
var userName = dashboardUserProvider.GetUserName();
var connections = new Dictionary<string, string>();

if (userName == "admin") {
Expand Down
11 changes: 5 additions & 6 deletions CS/BlazorAuth/Code/CustomDBSchemaProvider.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
using DevExpress.DataAccess.Sql;
using DevExpress.Xpo.DB;
using Microsoft.AspNetCore.Http;
using System.Linq;

namespace BlazorAuth {
public class CustomDBSchemaProvider : DBSchemaProviderEx {
private string userName;
private readonly IDashboardUserProvider dashboardUserProvider;

public CustomDBSchemaProvider(string userName) : base() {
this.userName = userName;
public CustomDBSchemaProvider(IDashboardUserProvider dashboardUserProvider) : base() {
this.dashboardUserProvider = dashboardUserProvider;
}

public override DBTable[] GetTables(SqlDataConnection connection, params string[] tableList) {
var result = base.GetTables(connection, tableList);
var userName = dashboardUserProvider.GetUserName();

if (userName == "admin") {
return result;
} else if (userName == "user") {
return result.Where(t => t.Name == "Cars").ToArray();
} else {
return new DBTable[0];
return [];
}
}
}
Expand Down
60 changes: 36 additions & 24 deletions CS/BlazorAuth/Code/CustomDashboardStorage.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,75 @@
using DevExpress.DashboardWeb;
using Microsoft.AspNetCore.Hosting;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Xml.Linq;
using DevExpress.DashboardWeb;
using DevExpress.Security;

namespace BlazorAuth {
public class CustomDashboardStorage : IEditableDashboardStorage {
private string userName;
private string dashboardStorageFolder;
readonly IDashboardUserProvider dashboardUserProvider;
string DashboardStorageFolder { get; }

public CustomDashboardStorage(IWebHostEnvironment hostingEnvironment, string userName) {
this.userName = userName;
this.dashboardStorageFolder = hostingEnvironment.ContentRootFileProvider.GetFileInfo("App_Data/Dashboards").PhysicalPath;
public CustomDashboardStorage(IWebHostEnvironment hostingEnvironment, IDashboardUserProvider dashboardUserProvider) {
this.dashboardUserProvider = dashboardUserProvider;
this.DashboardStorageFolder = hostingEnvironment.ContentRootFileProvider.GetFileInfo("App_Data/Dashboards").PhysicalPath!
?? throw new InvalidOperationException("Dashboard storage dashboardPath is not available.");
}

public IEnumerable<DashboardInfo> GetAvailableDashboardsInfo() {
var userName = dashboardUserProvider.GetUserName();
var dashboardInfos = new List<DashboardInfo>();

var files = Directory.GetFiles(dashboardStorageFolder, "*.xml");
var files = Directory.GetFiles(DashboardStorageFolder, "*.xml");

foreach (var item in files) {
foreach(var item in files) {
var name = Path.GetFileNameWithoutExtension(item);

if (!string.IsNullOrEmpty(userName) && name.EndsWith(userName, System.StringComparison.InvariantCultureIgnoreCase))
if(!string.IsNullOrEmpty(userName) && name.EndsWith(userName, System.StringComparison.InvariantCultureIgnoreCase))
dashboardInfos.Add(new DashboardInfo() { ID = name, Name = name });
}

return dashboardInfos;
}

public XDocument LoadDashboard(string dashboardID) {
if (GetAvailableDashboardsInfo().Any(di => di.Name == dashboardID)) {
var path = Path.Combine(dashboardStorageFolder, dashboardID + ".xml");
var content = File.ReadAllText(path);
if(GetAvailableDashboardsInfo().Any(di => di.Name == dashboardID)) {
var dashboardPath = Path.GetFullPath(Path.Combine(DashboardStorageFolder, dashboardID + ".xml"));
ValidateDashboardPath(dashboardPath);

var content = File.ReadAllText(dashboardPath);
return XDocument.Parse(content);
} else {
throw new System.ApplicationException("You are not authorized to view this dashboard.");
}
}

public string AddDashboard(XDocument dashboard, string dashboardName) {
if (string.IsNullOrEmpty(userName) || userName != "admin")
var userName = dashboardUserProvider.GetUserName();

if(string.IsNullOrEmpty(userName) || userName != "admin")
throw new System.ApplicationException("You are not authorized to add dashboards.");

var path = Path.Combine(dashboardStorageFolder, dashboardName + "_" + userName + ".xml");
var dashboardPath = Path.GetFullPath(Path.Combine(DashboardStorageFolder, dashboardName + "_" + userName + ".xml"));
ValidateDashboardPath(dashboardPath);

File.WriteAllText(path, dashboard.ToString());
File.WriteAllText(dashboardPath, dashboard.ToString());

return Path.GetFileNameWithoutExtension(path);
return Path.GetFileNameWithoutExtension(dashboardPath);
}

public void SaveDashboard(string dashboardID, XDocument dashboard) {
if (string.IsNullOrEmpty(userName) || userName != "admin")
var userName = dashboardUserProvider.GetUserName();

if(string.IsNullOrEmpty(userName) || userName != "admin")
throw new System.ApplicationException("You are not authorized to save dashboards.");

var path = Path.Combine(dashboardStorageFolder, dashboardID + ".xml");
var dashboardPath = Path.GetFullPath(Path.Combine(DashboardStorageFolder, dashboardID + ".xml"));
ValidateDashboardPath(dashboardPath);

File.WriteAllText(dashboardPath, dashboard.ToString());
}

File.WriteAllText(path, dashboard.ToString());
void ValidateDashboardPath(string dashboardPath) {
if(!SafePath.IsRooted(DashboardStorageFolder, dashboardPath))
throw new InvalidOperationException("Invalid dashboard folder dashboardPath");
}
}
}
32 changes: 16 additions & 16 deletions CS/BlazorAuth/Code/CustomDataSourceStorage.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
using DevExpress.DashboardCommon;
using System.Xml.Linq;
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.Json;
using DevExpress.DataAccess.Sql;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace BlazorAuth {
public class CustomDataSourceStorage : IDataSourceStorage {
private string userName;
private Dictionary<string, XDocument> documents = new Dictionary<string, XDocument>();
readonly IDashboardUserProvider dashboardUserProvider;
readonly Dictionary<string, XDocument> documents = new Dictionary<string, XDocument>();

private const string sqlDataSourceId1 = "SQL Data Source (Northwind)";
private const string sqlDataSourceId2 = "SQL Data Source (CarsXtraScheduling)";
private const string jsonDataSourceId = "JSON Data Source";
const string sqlDataSourceId1 = "SQL Data Source (Northwind)";
const string sqlDataSourceId2 = "SQL Data Source (CarsXtraScheduling)";
const string jsonDataSourceId = "JSON Data Source";

public CustomDataSourceStorage(string userName) {
this.userName = userName;
public CustomDataSourceStorage(IDashboardUserProvider dashboardUserProvider) {
this.dashboardUserProvider = dashboardUserProvider;

DashboardSqlDataSource sqlDataSource1 = new DashboardSqlDataSource(sqlDataSourceId1, "NorthwindConnectionString");
SelectQuery query1 = SelectQueryFluentBuilder
Expand Down Expand Up @@ -47,20 +45,22 @@ public CustomDataSourceStorage(string userName) {
}

public XDocument GetDataSource(string dataSourceID) {
if (GetDataSourcesID().Contains(dataSourceID)) {
if(GetDataSourcesID().Contains(dataSourceID)) {
return documents[dataSourceID];
} else {
throw new System.ApplicationException("You are not authorized to use this datasource.");
}
}

public IEnumerable<string> GetDataSourcesID() {
if (userName == "admin") {
var userName = dashboardUserProvider.GetUserName();

if(userName == "admin") {
return documents.Keys;
} else if (userName == "user") {
return new string[] { sqlDataSourceId2 };
} else if(userName == "user") {
return [sqlDataSourceId2];
} else {
return new string[0];
return [];
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions CS/BlazorAuth/Code/DashboardUserProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Components.Authorization;

namespace BlazorAuth {
public interface IDashboardUserProvider {
Task<string?> GetUserNameAsync();
string? GetUserName();
}

public class DashboardUserProvider : IDashboardUserProvider {
readonly AuthenticationStateProvider authProvider;

public DashboardUserProvider(AuthenticationStateProvider provider) {
this.authProvider = provider;
}

public string? GetUserName() {
var stateTask = authProvider.GetAuthenticationStateAsync();
if(!stateTask.IsCompletedSuccessfully)
throw new InvalidOperationException("Authentication state is not ready.");

return ReadName(stateTask.Result);
}

public async Task<string?> GetUserNameAsync() {
var state = await authProvider.GetAuthenticationStateAsync();

return ReadName(state);
}

static string? ReadName(AuthenticationState state) {
var identity = state.User.Identity;
return identity?.IsAuthenticated == true ? identity.Name?.Split('@')[0] : null;
}
}
}
30 changes: 17 additions & 13 deletions CS/BlazorAuth/Code/MultiTenantDashboardConfigurator.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.Web;

namespace BlazorAuth {
public class MultiTenantDashboardConfigurator : DashboardConfigurator {
private string? userName;
public MultiTenantDashboardConfigurator(
IDashboardUserProvider dashboardUserProvider,
IDataSourceWizardConnectionStringsProvider connectionStringsProvider,
IDataSourceStorage dataSourceStorage,
IEditableDashboardStorage dashboardStorage,
IDBSchemaProviderEx dbSchemaProvider) {

public MultiTenantDashboardConfigurator(IWebHostEnvironment hostingEnvironment, IHttpContextAccessor contextAccessor) {
string? identityName = contextAccessor.HttpContext?.User.Identity?.Name;
userName = identityName?.Substring(0, identityName.IndexOf("@"));
SetConnectionStringsProvider(connectionStringsProvider);
SetDataSourceStorage(dataSourceStorage);
SetDashboardStorage(dashboardStorage);
SetDBSchemaProvider(dbSchemaProvider);

SetConnectionStringsProvider(new CustomConnectionStringProvider(userName));
SetDataSourceStorage(new CustomDataSourceStorage(userName));
SetDashboardStorage(new CustomDashboardStorage(hostingEnvironment, userName));
SetDBSchemaProvider(new CustomDBSchemaProvider(userName));
VerifyClientTrustLevel += (s, e) => {
var userName = dashboardUserProvider.GetUserName();

VerifyClientTrustLevel += MultiTenantDashboardConfigurator_VerifyClientTrustLevel;
}
private void MultiTenantDashboardConfigurator_VerifyClientTrustLevel(object sender, VerifyClientTrustLevelEventArgs e) {
if (string.IsNullOrEmpty(userName) || userName == "guest")
e.ClientTrustLevel = ClientTrustLevel.Restricted;
if (string.IsNullOrEmpty(userName) || userName == "guest")
e.ClientTrustLevel = ClientTrustLevel.Restricted;
};
}
}
}
11 changes: 0 additions & 11 deletions CS/BlazorAuth/Controllers/DefaultDashboardController.cs

This file was deleted.

13 changes: 4 additions & 9 deletions CS/BlazorAuth/Pages/Dashboard.razor
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
@page "/dashboard"
@inject AuthenticationStateProvider AuthStateProvider
@inject IDashboardUserProvider DashboardUserProvider

<DxDashboard Endpoint="api/dashboard" style="width: 100%; height: 800px;" @bind-WorkingMode="@workingMode">
</DxDashboard>
<DxDashboard style="width: 100%; height: 800px;" @bind-WorkingMode="@workingMode" />

@code {
private WorkingMode workingMode;

protected override async Task OnInitializedAsync() {
AuthenticationState authState = await AuthStateProvider.GetAuthenticationStateAsync();
if (authState.User.Identity is not null) {
var identityName = authState.User.Identity.Name;
var userName = identityName?.Substring(0, identityName.IndexOf("@"));
var userName = await DashboardUserProvider.GetUserNameAsync();

workingMode = (userName == "admin" || userName == "user") ? WorkingMode.Designer : WorkingMode.ViewerOnly;
}
workingMode = (userName == "admin" || userName == "user") ? WorkingMode.Designer : WorkingMode.ViewerOnly;
}
}
Loading
Loading