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

Baditsa/ministry admin update fix #867

Merged
merged 3 commits into from
Sep 27, 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class CreateMinistryAdminDto
[DataType(DataType.DateTime)]
public DateTimeOffset CreatingTime { get; set; }

[Required(ErrorMessage = "InstitutionId is required")]
public Guid InstitutionId { get; set; }

public string UserId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using Microsoft.AspNetCore.Mvc.Filters;

using OutOfSchool.Common.Models;
using OutOfSchool.Services.Enums;
using OutOfSchool.WebApi.Common;
using OutOfSchool.WebApi.Enums;
using OutOfSchool.WebApi.Models;

namespace OutOfSchool.WebApi.Controllers;
Expand Down Expand Up @@ -174,7 +176,19 @@ public async Task<ActionResult> Update(MinistryAdminDto ministryAdminDto)

if (userId != ministryAdminDto.Id)
{
return StatusCode(403, "Forbidden to update another user.");
var currentUserRole = GettingUserProperties.GetUserRole(User);
if (currentUserRole == nameof(Role.TechAdmin).ToLower())
{
var updatedMinistryAdmin = await ministryAdminService.GetByIdAsync(ministryAdminDto.Id);
if (updatedMinistryAdmin.AccountStatus == AccountStatus.Accepted)
{
return StatusCode(403, "Forbidden to update accepted user.");
}
}
else
{
return StatusCode(403, "Forbidden to update another user if you haven't techadmin role.");
}
}

return Ok(await ministryAdminService.Update(ministryAdminDto).ConfigureAwait(false));
Expand Down
1 change: 1 addition & 0 deletions OutOfSchool/OutOfSchool.WebApi/Models/MinistryAdminDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace OutOfSchool.WebApi.Models;

public class MinistryAdminDto : BaseUserDto
{
[EnumDataType(typeof(AccountStatus), ErrorMessage = Constants.EnumErrorMessage)]
public AccountStatus AccountStatus { get; set; }

public Guid InstitutionId { get; set; }
Expand Down
10 changes: 9 additions & 1 deletion OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using OutOfSchool.Common.Enums;
using OutOfSchool.Common.Models;
using OutOfSchool.Services.Enums;
using OutOfSchool.WebApi.Enums;
using OutOfSchool.WebApi.Models;
using OutOfSchool.WebApi.Models.Achievement;
using OutOfSchool.WebApi.Models.BlockedProviderParent;
Expand Down Expand Up @@ -381,7 +382,14 @@ public MappingProfile()
.ForMember(dest => dest.MiddleName, opt => opt.MapFrom(src => src.User.MiddleName))
.ForMember(dest => dest.PhoneNumber, opt => opt.MapFrom(src => src.User.PhoneNumber))
.ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.User.Email))
.ForMember(dest => dest.AccountStatus, m => m.Ignore());
.ForMember(
dest => dest.AccountStatus,
opt => opt.MapFrom(src =>
src.User.IsBlocked
? AccountStatus.Blocked
: src.User.LastLogin == DateTimeOffset.MinValue
? AccountStatus.NeverLogged
: AccountStatus.Accepted));

CreateMap<ProviderChangesLogRequest, ChangesLogFilter>()
.ForMember(dest => dest.EntityType, opt => opt.Ignore())
Expand Down