Skip to content

Commit

Permalink
Workarounds for issues with Postgres DB provider using async DbContex…
Browse files Browse the repository at this point in the history
…t calls.
  • Loading branch information
bitbound committed Apr 17, 2021
1 parent f317613 commit d40d05a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
11 changes: 5 additions & 6 deletions Server/Pages/ManageOrganization.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@
<h3 class="mb-3">Manage Organization</h3>


@if (User.IsAdministrator)
@if (User?.IsAdministrator == true)
{

<div class="row">
<div class="col-sm-8">
@* Organization ID *@
<div class="form-group mw">
<label class="mb-1">Organization ID</label>
<input readonly class="form-control" value="@_organization.ID" />
<input readonly class="form-control" value="@_organization?.ID" />
</div>
<div class="form-group mw">
<label class="mb-1">Relay Code</label>
<button class="btn btn-sm btn-secondary ml-2" @onclick="ShowRelayCodeHelp">
<span id="relayCodeHelp" class="oi oi-question-mark"></span>
</button>
<br />
<input readonly class="form-control" value="@_organization.RelayCode" />
<input readonly class="form-control" value="@_organization?.RelayCode" />
</div>
</div>
</div>
Expand All @@ -35,7 +34,7 @@

<div class="input-group">
<input @onchange="OrganizationNameChanged"
value="@_organization.OrganizationName"
value="@_organization?.OrganizationName"
maxlength="25"
placeholder="Enter a name for your organization"
class="form-control"
Expand All @@ -53,7 +52,7 @@
<span id="relayCodeHelp" class="oi oi-question-mark"></span>
</button>
<br />
<input type="checkbox" checked="@_organization.IsDefaultOrganization" @onchange="DefaultOrgCheckChanged" />
<input type="checkbox" checked="@_organization?.IsDefaultOrganization" @onchange="DefaultOrgCheckChanged" />
</div>
}
</div>
Expand Down
13 changes: 4 additions & 9 deletions Server/Pages/UserOptions.razor
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,13 @@


@code {
private RemotelyUser _user;
private RemotelyUserOptions _options;
private string _alertMessage;

protected override async Task OnInitializedAsync()
protected override void OnInitialized()
{
var authState = await AuthProvider.GetAuthenticationStateAsync();
_user = await UserManager.GetUserAsync(authState.User);
_options = _user.UserOptions;


await base.OnInitializedAsync();
_options = User.UserOptions;
base.OnInitialized();
}

private Task HandleValidSubmit()
Expand All @@ -124,7 +119,7 @@
_options.CommandModeShortcutWinPS = "/" + _options.CommandModeShortcutWinPS;
}

DataService.UpdateUserOptions(_user.UserName, _options);
DataService.UpdateUserOptions(User.UserName, _options);

_alertMessage = "Options saved";

Expand Down
2 changes: 1 addition & 1 deletion Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.13" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.2" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.2" />
</ItemGroup>

Expand Down
10 changes: 6 additions & 4 deletions Server/Services/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public interface IAuthService
public class AuthService : IAuthService
{
private readonly AuthenticationStateProvider _authProvider;
private readonly UserManager<RemotelyUser> _userManager;
private readonly IDataService _dataService;

public AuthService(
AuthenticationStateProvider authProvider,
UserManager<RemotelyUser> userManager)
IDataService dataService)
{
_authProvider = authProvider;
_userManager = userManager;
_dataService = dataService;
}

public ClaimsPrincipal Principal => _authProvider.GetAuthenticationStateAsync()
Expand All @@ -37,6 +37,8 @@ public class AuthService : IAuthService

public bool IsAuthenticated => Principal?.Identity?.IsAuthenticated ?? false;

public RemotelyUser User => Principal is not null ? _userManager.GetUserAsync(Principal).ToResult() : null;
public RemotelyUser User => Principal is not null ?
_dataService.GetUserByNameWithOrg(Principal.Identity.Name) :
null;
}
}
15 changes: 14 additions & 1 deletion Server/Services/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public interface IDataService

int GetTotalDevices();

Task<RemotelyUser> GetUserAsync(string username);

RemotelyUser GetUserByID(string userID);

RemotelyUser GetUserByNameWithOrg(string userName);
Expand Down Expand Up @@ -1499,9 +1501,20 @@ public int GetTotalDevices()
return dbContext.Devices.Count();
}

public Task<RemotelyUser> GetUserAsync(string username)
{
if (string.IsNullOrWhiteSpace(username))
{
return null;
}
using var dbContext = _dbFactory.CreateDbContext();

return dbContext.Users.FirstOrDefaultAsync(x => x.UserName == username);
}

public RemotelyUser GetUserByID(string userID)
{
if (userID == null)
if (string.IsNullOrWhiteSpace(userID))
{
return null;
}
Expand Down

0 comments on commit d40d05a

Please sign in to comment.