This is what i would do.
- Create a media library for each 'company', put static site files in it
- Add a company role and give it access to it's media library
- Add user to this role.
Next, we need to check for permissions, any file that is retrieved through the generated media library url (ex '/getmedia/feaf-eaf3-f3q-3aceacea') will automatically check, however for static sites most references are relative and you don't want to go through and update all that.
So, i would create a custom StaticFile OnPrepareResponse to check for the media library and check security:
1. on Startup, when you do app.UseStaticFiles, adjust code to be about this:
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
if (!ctx.Context.User.Identity.IsAuthenticated)
{
// respond HTTP 401 Unauthorized.
ctx.Context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
}
// Cache user and role ID retrieval
var user = CMS.Membership.UserInfoProvider.GetUserInfo(ctx.Context.User.Identity.Name);
// get Roles
var roleIDs = CMS.Membership.UserRoleInfoProvider.GetUserRoles()
.WhereEquals(nameof(CMS.Membership.UserRoleInfo.UserID), User.UserID)
.Select(x => x.RoleID);
// Access denied, All users Authenticated
string where = "";
where += " (LibraryAccess < 5000000) AND ((LibraryAccess < 1000000) OR (LibraryAccess > 999999 AND LibraryAccess < 2000000)";
// Authorized roles
where += String.Format(" OR (LibraryAccess > 1999999 AND LibraryAccess < 3000000) AND ((SELECT Count(RoleID) FROM Media_LibraryRolePermission WHERE Media_LibraryRolePermission.LibraryID = LibraryID AND PermissionID = (SELECT TOP 1 PermissionID FROM CMS_Permission WHERE PermissionName = 'LibraryAccess') {0}) > 0))", roleIDs);
// Get library with permission check
string MediaFileName = ctx.File.PhysicalPath.Trim('/').Split('/')[0];
// Cache this check, user to media library
if(CMS.MediaLibrary.MediaLibraryInfoProvider.GetMediaLibraries()
.WhereEquals(nameof(CMS.MediaLibrary.MediaLibraryInfo.LibraryName), MediaFileName)
.Where(where)
.FirstOrDefault() == null)
{
// respond HTTP 401 Unauthorized.
ctx.Context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
}
}
});
I haven't tested this, and you SHOULD cache these methods or extrapolate this logic into an interface or some sort of helper function, but in essence this should check to make sure the current user has access to the requested file's media library (gotten through the path).