Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FSHApiConventions #375

Merged
merged 2 commits into from
Jan 16, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/Host/Controllers/Catalog/BrandsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Task<string> GenerateRandomAsync(GenerateRandomBrandRequest request)
}

[HttpDelete("delete-random")]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Delete))]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Search))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Search?

Copy link
Contributor Author

@fretje fretje Jan 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that you can't use a method in that attribute that has multiple overloads... and search is one that doesn't have multiple overloads...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point it doesn't really matter as they all have the same attributes... but eventually we will probably want to clean this out and put the right responsecodes on the right kind of methods...

public Task<string> DeleteRandomAsync()
{
return Mediator.Send(new DeleteRandomBrandRequest());
Expand Down
76 changes: 76 additions & 0 deletions src/Host/Controllers/FSHApiConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ public static void Get()
{
}

[ProducesResponseType(200)]
[ProducesResponseType(400, Type = typeof(HttpValidationProblemDetails))]
[ProducesDefaultResponseType(typeof(ErrorResult))]
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)]
public static void Get(
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)]
[ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
object id,
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)]
[ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
object cancellationtoken)
{
}

[ProducesResponseType(200)]
[ProducesResponseType(400, Type = typeof(HttpValidationProblemDetails))]
[ProducesDefaultResponseType(typeof(ErrorResult))]
Expand All @@ -48,6 +62,20 @@ public static void Get()
{
}

[ProducesResponseType(200)]
[ProducesResponseType(400, Type = typeof(HttpValidationProblemDetails))]
[ProducesDefaultResponseType(typeof(ErrorResult))]
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)]
public static void Post(
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)]
[ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
object request,
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)]
[ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
object cancellationToken)
{
}

[ProducesResponseType(200)]
[ProducesResponseType(400, Type = typeof(HttpValidationProblemDetails))]
[ProducesDefaultResponseType(typeof(ErrorResult))]
Expand Down Expand Up @@ -95,6 +123,23 @@ public static void Get()
{
}

[ProducesResponseType(200)]
[ProducesResponseType(400, Type = typeof(HttpValidationProblemDetails))]
[ProducesDefaultResponseType(typeof(ErrorResult))]
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)]
public static void Update(
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)]
[ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
object request,
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)]
[ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
object id,
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)]
[ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
object cancellationToken)
{
}

[ProducesResponseType(200)]
[ProducesResponseType(400, Type = typeof(HttpValidationProblemDetails))]
[ProducesDefaultResponseType(typeof(ErrorResult))]
Expand All @@ -109,6 +154,23 @@ public static void Get()
{
}

[ProducesResponseType(200)]
[ProducesResponseType(400, Type = typeof(HttpValidationProblemDetails))]
[ProducesDefaultResponseType(typeof(ErrorResult))]
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)]
public static void Put(
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)]
[ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
object request,
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)]
[ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
object id,
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)]
[ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
object cancellationToken)
{
}

[ProducesResponseType(200)]
[ProducesResponseType(400, Type = typeof(HttpValidationProblemDetails))]
[ProducesDefaultResponseType(typeof(ErrorResult))]
Expand All @@ -120,6 +182,20 @@ public static void Get()
{
}

[ProducesResponseType(200)]
[ProducesResponseType(400, Type = typeof(HttpValidationProblemDetails))]
[ProducesDefaultResponseType(typeof(ErrorResult))]
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)]
public static void Delete(
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)]
[ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
object id,
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)]
[ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
object cancellationToken)
{
}

[ProducesResponseType(200)]
[ProducesResponseType(400, Type = typeof(HttpValidationProblemDetails))]
[ProducesDefaultResponseType(typeof(ErrorResult))]
Expand Down
6 changes: 3 additions & 3 deletions src/Host/Controllers/Identity/IdentityController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public Task<string> ConfirmPhoneNumberAsync([FromQuery] string userId, [FromQuer
[HttpPost("forgot-password")]
[AllowAnonymous]
[TenantIdHeader]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Post))]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Register))]
public Task<string> ForgotPasswordAsync(ForgotPasswordRequest request)
{
return _identityService.ForgotPasswordAsync(request, OriginFromRequest);
}

[HttpPost("reset-password")]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Post))]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Register))]
public Task<string> ResetPasswordAsync(ResetPasswordRequest request)
{
return _identityService.ResetPasswordAsync(request);
Expand All @@ -69,7 +69,7 @@ public Task<UserDetailsDto> GetProfileDetailsAsync(CancellationToken cancellatio
}

[HttpPut("change-password")]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Put))]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Register))]
public Task ChangePasswordAsync(ChangePasswordRequest model)
{
return _identityService.ChangePasswordAsync(model, _currentUser.GetUserId().ToString());
Expand Down
4 changes: 2 additions & 2 deletions src/Host/Controllers/Identity/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public Task<List<PermissionDto>> GetPermissionsAsync(string id, CancellationToke
}

[HttpPost("{id}/roles")]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Post))]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Register))]
public Task<string> AssignRolesAsync(string id, UserRolesRequest request, CancellationToken cancellationToken)
{
return _userService.AssignRolesAsync(id, request, cancellationToken);
}

[HttpPost("toggle-status")]
[MustHavePermission(FSHPermissions.Users.Edit)]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Post))]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Register))]
public Task ToggleUserStatusAsync(ToggleUserStatusRequest request, CancellationToken cancellationToken)
{
return _userService.ToggleUserStatusAsync(request, cancellationToken);
Expand Down
6 changes: 3 additions & 3 deletions src/Host/Controllers/Multitenancy/TenantsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Task<string> CreateAsync(CreateTenantRequest request)
[HttpPost("upgrade")]
[MustHavePermission(FSHRootPermissions.Tenants.UpgradeSubscription)]
[OpenApiOperation("Upgrade Subscription of Tenant.", "")]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Post))]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Register))]
public Task<string> UpgradeSubscriptionAsync(UpgradeSubscriptionRequest request)
{
return Mediator.Send(request);
Expand All @@ -40,7 +40,7 @@ public Task<string> UpgradeSubscriptionAsync(UpgradeSubscriptionRequest request)
[HttpPost("{tenantId}/deactivate")]
[MustHavePermission(FSHRootPermissions.Tenants.Update)]
[OpenApiOperation("Deactivate Tenant.", "")]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Post))]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Register))]
public Task<string> DeactivateTenantAsync(string tenantId)
{
return Mediator.Send(new DeactivateTenantRequest(tenantId));
Expand All @@ -49,7 +49,7 @@ public Task<string> DeactivateTenantAsync(string tenantId)
[HttpPost("{tenantId}/activate")]
[MustHavePermission(FSHRootPermissions.Tenants.Update)]
[OpenApiOperation("Activate Tenant.", "")]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Post))]
[ApiConventionMethod(typeof(FSHApiConventions), nameof(FSHApiConventions.Register))]
public Task<string> ActivateTenantAsync(string tenantId)
{
return Mediator.Send(new ActivateTenantRequest(tenantId));
Expand Down
10 changes: 5 additions & 5 deletions src/Infrastructure/Identity/TokenService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class TokenService : ITokenService

public async Task<TokenResponse> GetTokenAsync(TokenRequest request, string ipAddress, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(_currentTenant?.Id))
{
throw new UnauthorizedException(_localizer["tenant.invalid"]);
}

var user = await _userManager.FindByEmailAsync(request.Email.Trim().Normalize());
if (user is null)
{
Expand All @@ -56,11 +61,6 @@ public async Task<TokenResponse> GetTokenAsync(TokenRequest request, string ipAd
throw new UnauthorizedException(_localizer["identity.emailnotconfirmed"]);
}

if (string.IsNullOrWhiteSpace(_currentTenant?.Id))
{
throw new UnauthorizedException(_localizer["tenant.invalid"]);
}

if (_currentTenant.Id != MultitenancyConstants.Root.Id)
{
if (!_currentTenant.IsActive)
Expand Down