Skip to content

Commit

Permalink
update regex
Browse files Browse the repository at this point in the history
  • Loading branch information
nhathoang989 committed Oct 17, 2023
1 parent 0e5def7 commit 17caaa2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
49 changes: 32 additions & 17 deletions src/modules/mix.portal/Domain/Services/MixApplicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
using Mix.SignalR.Hubs;
using System.IO.Packaging;
using System.Text.RegularExpressions;
using static NuGet.Packaging.PackagingConstants;

namespace Mix.Portal.Domain.Services
{
public sealed class MixApplicationService : TenantServiceBase, IMixApplicationService
{
static string[] excludeFileNames = { "jquery" };
static string[] excludeFileNames = { "jquery", "index" };
private readonly IQueueService<MessageQueueModel> _queueService;
private readonly IThemeService _themeService;
private readonly MixIdentityService _mixIdentityService;
Expand Down Expand Up @@ -67,16 +68,12 @@ public async Task<MixApplicationViewModel> Install(MixApplicationViewModel app,
{
try
{
var files = MixFileHelper.GetTopFiles(deployUrl, true);
var folders = string.Join('|', MixFileHelper.GetTopDirectories(deployUrl));
var folders = MixFileHelper.GetTopDirectories(deployUrl);
var topFolderPattern = string.Join('|', folders);
templateId = await ReplaceIndex(templateId, name, deployUrl, baseHref);
foreach (var file in files)
{
if (file.Extension == MixFileExtensions.Js || file.Extension == MixFileExtensions.Css)
{
await ReplaceContent(file, folders, deployUrl);
}
}

await ModifyFilesAndFolders(deployUrl, deployUrl, topFolderPattern);

return templateId;
}
catch (Exception ex)
Expand All @@ -85,6 +82,24 @@ public async Task<MixApplicationViewModel> Install(MixApplicationViewModel app,
}
}

private async Task ModifyFilesAndFolders(string deployUrl, string topFolder, string topFolderPattern)
{
var files = MixFileHelper.GetTopFiles(topFolder, true);
var folders = MixFileHelper.GetTopDirectories(topFolder);

foreach (var file in files)
{
if (file.Extension == MixFileExtensions.Js || file.Extension == MixFileExtensions.Css || file.Extension == MixFileExtensions.Html)
{
await ReplaceContent(file, topFolderPattern, deployUrl);
}
}
foreach (var folder in folders)
{
await ModifyFilesAndFolders(deployUrl, $"{topFolder}/{folder}", topFolderPattern);
}
}

private async Task<int?> ReplaceIndex(int? templateId, string name, string deployUrl, string baseHref)
{
try
Expand All @@ -99,10 +114,10 @@ public async Task<MixApplicationViewModel> Install(MixApplicationViewModel app,
}


Regex regex = new("((\\\"|\\'|\\(|\\`){1}(\\.)?(\\/)?(([0-9a-zA-Z\\/\\._-])+).(js|css|svg|png|PNG|gif|GIF|jp[e]?g|JP[E]?G))");
Regex regex = new("((\\\"|\\'|\\(|\\`){1}(\\.)?(\\/)?(([0-9a-zA-Z\\/\\._-])+)\\.{1}(\\w+)(\"|\\'|\\(|\\`){1})");
Regex baseHrefRegex = new("(base href=\"(.+?)\")");
indexFile.Content = indexFile.Content.Replace("[basePath]/", string.Empty);
indexFile.Content = regex.Replace(indexFile.Content, $"$2/{deployUrl}/$5.$7");
indexFile.Content = regex.Replace(indexFile.Content, $"$2/{deployUrl}/$5.$7$2");
indexFile.Content = baseHrefRegex.Replace(indexFile.Content, $"base href=\"{baseHref}\"")
.Replace("[baseRoute]", deployUrl)

Expand Down Expand Up @@ -148,21 +163,21 @@ private Task ReplaceContent(FileModel file, string folders, string deployUrl)
Regex rg = null;
if (string.IsNullOrEmpty(folders))
{
rg = new($"((\\\"|\\'|\\(|\\`){1}(\\.)?(\\/)?(([0-9a-zA-Z\\/\\._-])+).(js|css|svg|png|PNG|gif|GIF|jp[e]?g|JP[E]?G))");
rg = new("((\\\"|\\'|\\(|\\`){1}(\\.)?(\\/)?(([0-9a-zA-Z\\/\\._-])+)\\.{1}(\\w+)(\"|\\'|\\(|\\`){1})");
if (rg.IsMatch(file.Content))
{
file.Content = rg.Replace(file.Content, $"$2/{deployUrl}/$5.$7");
file.Content = rg.Replace(file.Content, $"$2/{deployUrl}/$5.$7$2");
}
}
else
{
rg = new($"((\\\"|\\'|\\(|\\`)(\\.)?(\\/)?({folders})(([0-9a-zA-Z\\/\\._-])+).(js|css|svg|png|PNG|gif|GIF|jp[e]?g|JP[E]?G))");
rg = new($"((\\\"|\\'|\\(|\\`)(\\.)?(\\/)?({folders})(([0-9a-zA-Z\\/\\._-])+)(\\\"|\\'|\\(|\\`))");
if (rg.IsMatch(file.Content))
{
file.Content = rg.Replace(file.Content, $"$2/{deployUrl}/$5$6.$8");
file.Content = rg.Replace(file.Content, $"$2/{deployUrl}/$5$6$2");
}
}

file.Content = file.Content.Replace("[basePath]", $"/{deployUrl}");

MixFileHelper.SaveFile(file);
Expand Down
13 changes: 6 additions & 7 deletions src/platform/mix.service/Services/MixPermissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.EntityFrameworkCore;
using Mix.Constant.Constants;
using Mix.Database.Entities.Account;
using Mix.Database.Entities.Cms;
using Mix.Database.Entities.MixDb;
using Mix.Database.Services;
using Mix.Heart.Exceptions;
Expand Down Expand Up @@ -32,28 +33,26 @@ public async Task Reload()
{
if (!GlobalConfigService.Instance.IsInit)
{
if (_databaseService.DatabaseProvider == Heart.Enums.MixDatabaseProvider.SQLITE)
{
return;
}
UnitOfWorkInfo<MixDbDbContext> uow = new(new MixDbDbContext(_databaseService));
try
{
RoleEndpoints = new Dictionary<string, string[]>();
accountDbContext = new MixCmsAccountContext(_databaseService);
using var cmsDbContext = new MixCmsContext(_databaseService);

var roles = await accountDbContext.MixRoles.ToListAsync();

foreach (var role in roles)
{
var permissionIds = uow.DbContext.MixDatabaseAssociation
var permissionIds = cmsDbContext.MixDatabaseAssociation
.Where(m => m.GuidParentId == role.Id)
.Select(m => m.ChildId);
var endpointIds = uow.DbContext.MixDatabaseAssociation
var endpointIds = cmsDbContext.MixDatabaseAssociation
.Where(m => m.ParentDatabaseName == MixDatabaseNames.SYSTEM_PERMISSION
&& m.ChildDatabaseName == MixDatabaseNames.SYSTEM_PERMISSION_ENDPOINT
&& permissionIds.Contains(m.ParentId))
.Select(m => m.ChildId);
.Select(m => m.ChildId)
.ToList();

// TODO: PermissionEndpoint cannot initial at first time
var endpoints = await uow.DbContext.PermissionEndpoint.Where(
Expand Down

0 comments on commit 17caaa2

Please sign in to comment.