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

Tsylia / Disable C(R)UD and Block operations at the blocked providers #1202

Merged
merged 11 commits into from
Aug 25, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public async Task<IActionResult> Login(LoginViewModel model)

if (user != null)
{
if (user.IsBlocked)
if (user.IsBlocked && !user.Role.Equals(Role.Provider.ToString(), StringComparison.InvariantCultureIgnoreCase))
{
logger.LogInformation("User is blocked. Login was failed");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ public async Task<ResponseDto> Block(string providerAdminId, bool isBlocked)
.BlockProviderAdminAsync(providerAdminId, userId, Request.Headers["X-Request-ID"], isBlocked);
}

[HttpPut("{providerId}/{isBlocked}")]
[HasPermission(Permissions.ProviderBlock)]
public async Task<ResponseDto> BlockByProvider(Guid providerId, bool isBlocked)
{
logger.LogDebug($"Received request " +
$"{Request.Headers["X-Request-ID"]}. {path} started. User(id): {userId}");

return await providerAdminService
.BlockProviderAdminsAndDeputiesByProviderAsync(providerId, userId, Request.Headers["X-Request-ID"], isBlocked);
}

[HttpPut("{providerAdminId}")]
[HasPermission(Permissions.ProviderRemove)]
public async Task<ResponseDto> Reinvite(string providerAdminId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ Task<ResponseDto> BlockProviderAdminAsync(
string requestId,
bool isBlocked);

Task<ResponseDto> BlockProviderAdminsAndDeputiesByProviderAsync(
Guid providerId,
string userId,
string requestId,
bool isBlocked);

Task<ResponseDto> ReinviteProviderAdminAsync(
string providerAdminId,
string userId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using OutOfSchool.AuthCommon.Config;
using OutOfSchool.AuthCommon.Config.ExternalUriModels;
using OutOfSchool.AuthCommon.Services.Password;
using OutOfSchool.Common.Enums;
using OutOfSchool.Common.Models;
using OutOfSchool.RazorTemplatesData.Models.Emails;
using OutOfSchool.Services.Enums;
using OutOfSchool.Services.Models;

namespace OutOfSchool.AuthCommon.Services;

Expand Down Expand Up @@ -493,11 +495,52 @@ await providerAdminChangesLogService.SaveChangesLogAsync(providerAdmin, userId,
}
}

public async Task<ResponseDto> BlockProviderAdminsAndDeputiesByProviderAsync(
Guid providerId,
string userId,
string requestId,
bool isBlocked)
{
var mainResponse = new ResponseDto() { IsSuccess = true, HttpStatusCode = HttpStatusCode.OK, Message = string.Empty };

var providerAdmins = await providerAdminRepository
.GetByFilter(x => x.ProviderId == providerId && x.BlockingType != BlockingType.Manually)
.ConfigureAwait(false);

foreach (var providerAdmin in providerAdmins)
{
var response = await BlockProviderAdminAsync(providerAdmin.UserId, userId, requestId, isBlocked);

if (response.IsSuccess)
{
logger.LogInformation(
"ProviderAdmin(id):{providerAdminId} was successfully blocked by User(id): {userId}. Request(id): {requestId}",
providerAdmin.UserId,
userId,
requestId);
}
else
{
mainResponse.IsSuccess = false;
mainResponse.Message = string.Concat(mainResponse.Message, providerAdmin.UserId, " ", response.HttpStatusCode.ToString(), " ");

logger.LogInformation(
"ProviderAdmin(id):{providerAdminId} wasn't blocked by User(id): {userId}. Reason {ResponseHttpStatusCode}. Request(id): {requestId}",
providerAdmin.UserId,
userId,
response.HttpStatusCode,
requestId);
}
}

return mainResponse;
}

public async Task<ResponseDto> ReinviteProviderAdminAsync(
string providerAdminId,
string userId,
IUrlHelper url,
string requestId)
string providerAdminId,
string userId,
IUrlHelper url,
string requestId)
{
var executionStrategy = context.Database.CreateExecutionStrategy();
var result = await executionStrategy.Execute(async () =>
Expand Down
12 changes: 12 additions & 0 deletions OutOfSchool/OutOfSchool.Common/Enums/BlockingType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace OutOfSchool.Common.Enums;

[JsonConverter(typeof(StringEnumConverter))]
public enum BlockingType
{
None,
Manually,
Automatically,
}
5 changes: 4 additions & 1 deletion OutOfSchool/OutOfSchool.DataAccess/Models/ProviderAdmin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using OutOfSchool.Common.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

Expand All @@ -18,6 +19,8 @@ public class ProviderAdmin : IKeyedEntity<(string, Guid)>
// "false" executes further inspection into admins-to-workshops relations
public bool IsDeputy { get; set; }

public BlockingType BlockingType { get; set; }

public virtual List<Workshop> ManagedWorkshops { get; set; }

[NotMapped]
Expand Down
Loading
Loading