Skip to content

Commit

Permalink
Fixed SignalR
Browse files Browse the repository at this point in the history
  • Loading branch information
AlenGeoAlex committed Jan 20, 2024
1 parent 8f003da commit 19b6022
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 61 deletions.
1 change: 0 additions & 1 deletion Sharecode.Backend.Api/Sharecode.Backend.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.0" />
<PackageReference Include="Quartz.Extensions.Hosting" Version="3.8.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Expand Down
4 changes: 3 additions & 1 deletion Sharecode.Backend.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"Microsoft": "Error",
"System": "Error",
"Microsoft.AspNetCore": "Error",
"Microsoft.AspNetCore.Authentication" : "Debug"
"Microsoft.AspNetCore.Authentication" : "Debug",
"Microsoft.AspNetCore.SignalR": "Debug",
"Microsoft.AspNetCore.Http.Connections": "Debug"
}
},
"WriteTo": [
Expand Down
130 changes: 73 additions & 57 deletions Sharecode.Backend.Domain/Helper/SnippetAccessPermission.cs
Original file line number Diff line number Diff line change
@@ -1,77 +1,93 @@
using Sharecode.Backend.Domain.Dto.Snippet;
using Sharecode.Backend.Domain.Entity.Snippet;
using Sharecode.Backend.Domain.Enums;

namespace Sharecode.Backend.Domain.Helper;

public class SnippetAccessPermission(Guid snippetId, Guid accessorId, bool read, bool write, bool manage, bool snippetPublic)
namespace Sharecode.Backend.Domain.Helper
{
public class SnippetAccessPermission
{
public Guid SnippetId { get; set; }
public Guid AccessorId { get; set; }
private bool Read { get; set; }
private bool Write { get; set; }
private bool Manage { get; set; }
public bool IsPublicSnippet { get; set;}
public bool IsPrivateSnippet => !IsPublicSnippet;

public static SnippetAccessPermission NoPermission(Guid snippetId, Guid accessorId) =>
new SnippetAccessPermission(snippetId, accessorId, false, false, false, true);

public static SnippetAccessPermission Error =>
new SnippetAccessPermission(Guid.Empty, Guid.Empty, false, false, false, true);

public Guid SnippetId { get; } = snippetId;
public Guid AccessorId { get; } = accessorId;
private bool Read { get; } = read || write || manage;
private bool Write { get; } = write || manage;
private bool Manage { get; } = manage;
// Constructor
public SnippetAccessPermission(Guid snippetId, Guid accessorId, bool read, bool write, bool manage, bool snippetPublic)
{
SnippetId = snippetId;
AccessorId = accessorId;
Read = read || write || manage;
Write = write || manage;
Manage = manage;
IsPublicSnippet = snippetPublic;
}

public bool IsPublicSnippet => snippetPublic;
public bool IsPrivateSnippet => !snippetPublic;
public SnippetAccessPermission()
{

}

public bool Any()
{
return Any(SnippetAccess.Read, SnippetAccess.Write, SnippetAccess.Manage);
}

public bool Any(params SnippetAccess[] accesses)
{
foreach (var access in accesses)
// Factory method for creating SnippetAccessPermission with no permissions
public static SnippetAccessPermission NoPermission(Guid snippetId, Guid accessorId) =>
new SnippetAccessPermission(snippetId, accessorId, false, false, false, true);

// Factory method for creating SnippetAccessPermission with an error state
public static SnippetAccessPermission Error =>
new SnippetAccessPermission(Guid.Empty, Guid.Empty, false, false, false, true);

public bool Any()
{
return Any(SnippetAccess.Read, SnippetAccess.Write, SnippetAccess.Manage);
}

public bool Any(params SnippetAccess[] accesses)
{
switch (access)
foreach (var access in accesses)
{
case SnippetAccess.Read when Read:
case SnippetAccess.Write when Write:
case SnippetAccess.Manage when Manage:
return true;
switch (access)
{
case SnippetAccess.Read when Read:
case SnippetAccess.Write when Write:
case SnippetAccess.Manage when Manage:
return true;
}
}
}

return false;
}
return false;
}

public bool All()
{
return All(SnippetAccess.Read, SnippetAccess.Write, SnippetAccess.Manage);
}
public bool All()
{
return All(SnippetAccess.Read, SnippetAccess.Write, SnippetAccess.Manage);
}

public bool All(params SnippetAccess[] accesses)
{
foreach (var access in accesses)
public bool All(params SnippetAccess[] accesses)
{
switch (access)
foreach (var access in accesses)
{
case SnippetAccess.Read when !Read:
case SnippetAccess.Write when !Write:
case SnippetAccess.Manage when !Manage:
return false;
switch (access)
{
case SnippetAccess.Read when !Read:
case SnippetAccess.Write when !Write:
case SnippetAccess.Manage when !Manage:
return false;
}
}
}

return true;
}
return true;
}

public SnippetAccessControlDto ToControlModel()
{
return new SnippetAccessControlDto()
public SnippetAccessControlDto ToControlModel()
{
UserId = AccessorId,
Manage = manage,
Read = read,
Write = write
};
return new SnippetAccessControlDto()
{
UserId = AccessorId,
Manage = Manage,
Read = Read,
Write = Write
};
}
}
}
}
6 changes: 4 additions & 2 deletions Sharecode.Backend.Infrastructure/Service/SnippetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public async Task<SnippetAccessPermission> GetSnippetAccess(Guid snippetId, Guid
var permission = await dapperContext.QueryFirstOrDefaultAsync<SnippetAccessPermission>(SnippetUserSqlQueries.GetSnippetAccess, param, commandTimeout: 1000);
if(permission == null)
permission = SnippetAccessPermission.NoPermission(snippetId, requestedUser);


permission.SnippetId = snippetId;
permission.AccessorId = requestedUser;
return permission;
}
catch (Exception e)
Expand Down Expand Up @@ -145,5 +147,5 @@ public Task<long> GetUserSnippetUsageAsync(Guid userId, CancellationToken token
internal sealed class SnippetUserSqlQueries
{
public static string GetSnippetAccess =>
$"SELECT \"Read\", \"Write\", \"Manage\" FROM get_permission_of_snippet(@snippetId, @requestedUser, @checkAdminPermission)";
$"SELECT \"Read\", \"Write\", \"Manage\", \"Public\" FROM get_permission_of_snippet(@snippetId, @requestedUser, @checkAdminPermission)";
}

0 comments on commit 19b6022

Please sign in to comment.