Compare commits
No commits in common. "3da4a09a6d8e100699680d1ffa85353704a3b6ea" and "d807ac1998e82f9332f2d0e313a83168f2f9048e" have entirely different histories.
3da4a09a6d
...
d807ac1998
File diff suppressed because it is too large
Load Diff
@ -1,40 +0,0 @@
|
|||||||
using Microsoft.EntityFrameworkCore.Migrations;
|
|
||||||
|
|
||||||
#nullable disable
|
|
||||||
|
|
||||||
namespace Marco.Pms.DataAccess.Migrations
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
public partial class Added_IsSystem_Flag : Migration
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void Up(MigrationBuilder migrationBuilder)
|
|
||||||
{
|
|
||||||
migrationBuilder.AddColumn<bool>(
|
|
||||||
name: "IsSystem",
|
|
||||||
table: "Employees",
|
|
||||||
type: "tinyint(1)",
|
|
||||||
nullable: false,
|
|
||||||
defaultValue: false);
|
|
||||||
|
|
||||||
migrationBuilder.AddColumn<bool>(
|
|
||||||
name: "IsSystem",
|
|
||||||
table: "ApplicationRoles",
|
|
||||||
type: "tinyint(1)",
|
|
||||||
nullable: false,
|
|
||||||
defaultValue: false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void Down(MigrationBuilder migrationBuilder)
|
|
||||||
{
|
|
||||||
migrationBuilder.DropColumn(
|
|
||||||
name: "IsSystem",
|
|
||||||
table: "Employees");
|
|
||||||
|
|
||||||
migrationBuilder.DropColumn(
|
|
||||||
name: "IsSystem",
|
|
||||||
table: "ApplicationRoles");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -342,9 +342,6 @@ namespace Marco.Pms.DataAccess.Migrations
|
|||||||
b.Property<bool>("IsActive")
|
b.Property<bool>("IsActive")
|
||||||
.HasColumnType("tinyint(1)");
|
.HasColumnType("tinyint(1)");
|
||||||
|
|
||||||
b.Property<bool>("IsSystem")
|
|
||||||
.HasColumnType("tinyint(1)");
|
|
||||||
|
|
||||||
b.Property<Guid?>("JobRoleId")
|
b.Property<Guid?>("JobRoleId")
|
||||||
.HasColumnType("char(36)");
|
.HasColumnType("char(36)");
|
||||||
|
|
||||||
@ -471,9 +468,6 @@ namespace Marco.Pms.DataAccess.Migrations
|
|||||||
b.Property<string>("Description")
|
b.Property<string>("Description")
|
||||||
.HasColumnType("longtext");
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
b.Property<bool>("IsSystem")
|
|
||||||
.HasColumnType("tinyint(1)");
|
|
||||||
|
|
||||||
b.Property<string>("Role")
|
b.Property<string>("Role")
|
||||||
.HasColumnType("longtext");
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
|||||||
@ -39,8 +39,6 @@ namespace Marco.Pms.Model.Employees
|
|||||||
|
|
||||||
public bool IsActive { get; set; } = true;
|
public bool IsActive { get; set; } = true;
|
||||||
|
|
||||||
public bool IsSystem { get; set; } = false;
|
|
||||||
|
|
||||||
public Guid RoleId { get; set; }
|
public Guid RoleId { get; set; }
|
||||||
//[ForeignKey(nameof(RoleId))]
|
//[ForeignKey(nameof(RoleId))]
|
||||||
//public EmployeeRole EmployeeRole { get; set; }
|
//public EmployeeRole EmployeeRole { get; set; }
|
||||||
|
|||||||
@ -5,7 +5,6 @@
|
|||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public string? Role { get; set; }
|
public string? Role { get; set; }
|
||||||
public string? Description { get; set; }
|
public string? Description { get; set; }
|
||||||
public bool IsSystem { get; set; } = false;
|
|
||||||
|
|
||||||
public Guid TenantId { get; set; }
|
public Guid TenantId { get; set; }
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,6 @@ namespace Marco.Pms.Model.Mapper
|
|||||||
{
|
{
|
||||||
Id = model.Id,
|
Id = model.Id,
|
||||||
Role = model.Role,
|
Role = model.Role,
|
||||||
IsSystem = model.IsSystem,
|
|
||||||
Description = model.Description,
|
Description = model.Description,
|
||||||
//FeaturePermission = model.FeaturePermissions
|
//FeaturePermission = model.FeaturePermissions
|
||||||
};
|
};
|
||||||
|
|||||||
@ -29,7 +29,6 @@ namespace Marco.Pms.Model.Mapper
|
|||||||
PhoneNumber = model.PhoneNumber,
|
PhoneNumber = model.PhoneNumber,
|
||||||
Photo = model.Photo,
|
Photo = model.Photo,
|
||||||
IsActive = model.IsActive,
|
IsActive = model.IsActive,
|
||||||
IsSystem = model.IsSystem,
|
|
||||||
JoiningDate = model.JoiningDate
|
JoiningDate = model.JoiningDate
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,6 @@
|
|||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public string? Role { get; set; }
|
public string? Role { get; set; }
|
||||||
public string? Description { get; set; }
|
public string? Description { get; set; }
|
||||||
public bool IsSystem { get; set; }
|
|
||||||
public ICollection<FeaturePermissionVM>? FeaturePermission { get; set; }
|
public ICollection<FeaturePermissionVM>? FeaturePermission { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,7 +28,6 @@
|
|||||||
public string? ApplicationUserId { get; set; }
|
public string? ApplicationUserId { get; set; }
|
||||||
|
|
||||||
public Guid? JobRoleId { get; set; }
|
public Guid? JobRoleId { get; set; }
|
||||||
public bool IsSystem { get; set; }
|
|
||||||
public string? JobRole { get; set; }
|
public string? JobRole { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -85,7 +85,50 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
[Route("roles")]
|
||||||
|
public async Task<IActionResult> ManageRoles([FromBody] List<EmployeeRoleDot> employeeRoleDots)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
var errors = ModelState.Values
|
||||||
|
.SelectMany(v => v.Errors)
|
||||||
|
.Select(e => e.ErrorMessage)
|
||||||
|
.ToList();
|
||||||
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
||||||
|
}
|
||||||
|
|
||||||
|
Guid TenantId = GetTenantId();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
foreach (EmployeeRoleDot role in employeeRoleDots)
|
||||||
|
{
|
||||||
|
EmployeeRoleMapping mapping = role.ToEmployeeRoleMappingFromEmployeeRoleDot(TenantId);
|
||||||
|
|
||||||
|
var existingItem = await _context.EmployeeRoleMappings.AsNoTracking().SingleOrDefaultAsync(c => c.Id == mapping.Id);
|
||||||
|
|
||||||
|
if (existingItem == null)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (role.IsEnabled == true)
|
||||||
|
{
|
||||||
|
_context.EmployeeRoleMappings.Add(mapping);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (role.IsEnabled == false)
|
||||||
|
{
|
||||||
|
_context.EmployeeRoleMappings.Remove(existingItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return BadRequest(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400));
|
||||||
|
}
|
||||||
|
return Ok(ApiResponse<object>.SuccessResponse("success", "Roles modified.", 200));
|
||||||
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("list/{projectid?}")]
|
[Route("list/{projectid?}")]
|
||||||
@ -280,75 +323,66 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
public async Task<IActionResult> SuspendEmployee(Guid id)
|
public async Task<IActionResult> SuspendEmployee(Guid id)
|
||||||
{
|
{
|
||||||
Guid tenantId = _userHelper.GetTenantId();
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
var LoggedEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
Employee? employee = await _context.Employees.FirstOrDefaultAsync(e => e.Id == id && e.TenantId == tenantId);
|
||||||
Employee? employee = await _context.Employees.FirstOrDefaultAsync(e => e.Id == id && e.IsActive && e.TenantId == tenantId);
|
|
||||||
if (employee != null)
|
if (employee != null)
|
||||||
{
|
{
|
||||||
if (employee.IsSystem)
|
var assignedToTasks = await _context.TaskMembers.Where(t => t.EmployeeId == employee.Id).ToListAsync();
|
||||||
|
if (assignedToTasks.Count != 0)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Employee with ID {LoggedEmployeeId} tries to suspend system-defined employee with ID {EmployeeId}", LoggedEmployee.Id, employee.Id);
|
List<Guid> taskIds = assignedToTasks.Select(t => t.TaskAllocationId).ToList();
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("System-defined employees cannot be suspended.", "System-defined employees cannot be suspended.", 400));
|
var tasks = await _context.TaskAllocations.Where(t => taskIds.Contains(t.Id)).ToListAsync();
|
||||||
|
|
||||||
|
foreach (var assignedToTask in assignedToTasks)
|
||||||
|
{
|
||||||
|
var task = tasks.Find(t => t.Id == assignedToTask.TaskAllocationId);
|
||||||
|
if (task != null && task.CompletedTask == 0)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Employee with ID {EmployeeId} is currently assigned to any incomplete task", employee.Id);
|
||||||
|
return BadRequest(ApiResponse<object>.ErrorResponse("Employee is currently assigned to any incomplete task", "Employee is currently assigned to any incomplete task", 400));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
var attendance = await _context.Attendes.Where(a => a.EmployeeID == employee.Id && (a.OutTime == null || a.Activity == ATTENDANCE_MARK_TYPE.REQUEST_REGULARIZE)).ToListAsync();
|
||||||
|
if (attendance.Count != 0)
|
||||||
{
|
{
|
||||||
var assignedToTasks = await _context.TaskMembers.Where(t => t.EmployeeId == employee.Id).ToListAsync();
|
_logger.LogWarning("Employee with ID {EmployeeId} have any pending check-out or regularization requests", employee.Id);
|
||||||
if (assignedToTasks.Count != 0)
|
return BadRequest(ApiResponse<object>.ErrorResponse("Employee have any pending check-out or regularization requests", "Employee have any pending check-out or regularization requests", 400));
|
||||||
{
|
|
||||||
List<Guid> taskIds = assignedToTasks.Select(t => t.TaskAllocationId).ToList();
|
|
||||||
var tasks = await _context.TaskAllocations.Where(t => taskIds.Contains(t.Id)).ToListAsync();
|
|
||||||
|
|
||||||
foreach (var assignedToTask in assignedToTasks)
|
|
||||||
{
|
|
||||||
var task = tasks.Find(t => t.Id == assignedToTask.TaskAllocationId);
|
|
||||||
if (task != null && task.CompletedTask == 0)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("Employee with ID {EmployeeId} is currently assigned to any incomplete task", employee.Id);
|
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Employee is currently assigned to any incomplete task", "Employee is currently assigned to any incomplete task", 400));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var attendance = await _context.Attendes.Where(a => a.EmployeeID == employee.Id && (a.OutTime == null || a.Activity == ATTENDANCE_MARK_TYPE.REQUEST_REGULARIZE)).ToListAsync();
|
|
||||||
if (attendance.Count != 0)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("Employee with ID {EmployeeId} have any pending check-out or regularization requests", employee.Id);
|
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Employee have any pending check-out or regularization requests", "Employee have any pending check-out or regularization requests", 400));
|
|
||||||
}
|
|
||||||
employee.IsActive = false;
|
|
||||||
var projectAllocations = await _context.ProjectAllocations.Where(a => a.EmployeeId == employee.Id).ToListAsync();
|
|
||||||
if (projectAllocations.Count != 0)
|
|
||||||
{
|
|
||||||
List<ProjectAllocation> allocations = new List<ProjectAllocation>();
|
|
||||||
foreach (var projectAllocation in projectAllocations)
|
|
||||||
{
|
|
||||||
projectAllocation.ReAllocationDate = DateTime.UtcNow;
|
|
||||||
projectAllocation.IsActive = false;
|
|
||||||
allocations.Add(projectAllocation);
|
|
||||||
}
|
|
||||||
_logger.LogInfo("Employee with ID {EmployeeId} has been removed from all assigned projects.", employee.Id);
|
|
||||||
}
|
|
||||||
var user = await _context.ApplicationUsers.FirstOrDefaultAsync(u => u.Id == employee.ApplicationUserId);
|
|
||||||
if (user != null)
|
|
||||||
{
|
|
||||||
user.IsActive = false;
|
|
||||||
_logger.LogInfo("The application user associated with employee ID {EmployeeId} has been suspended.", employee.Id);
|
|
||||||
|
|
||||||
var refreshTokens = await _context.RefreshTokens.AsNoTracking().Where(t => t.UserId == user.Id).ToListAsync();
|
|
||||||
if (refreshTokens.Count != 0)
|
|
||||||
{
|
|
||||||
_context.RefreshTokens.RemoveRange(refreshTokens);
|
|
||||||
_logger.LogInfo("Refresh tokens associated with employee ID {EmployeeId} has been removed.", employee.Id);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
var roleMapping = await _context.EmployeeRoleMappings.AsNoTracking().Where(r => r.EmployeeId == employee.Id).ToListAsync();
|
|
||||||
if (roleMapping.Count != 0)
|
|
||||||
{
|
|
||||||
_context.EmployeeRoleMappings.RemoveRange(roleMapping);
|
|
||||||
_logger.LogInfo("Application role mapping associated with employee ID {EmployeeId} has been removed.", employee.Id);
|
|
||||||
}
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
_logger.LogInfo("Employee with ID {EmployeId} Deleted successfully", employee.Id);
|
|
||||||
}
|
}
|
||||||
|
employee.IsActive = false;
|
||||||
|
var projectAllocations = await _context.ProjectAllocations.Where(a => a.EmployeeId == employee.Id).ToListAsync();
|
||||||
|
if (projectAllocations.Count != 0)
|
||||||
|
{
|
||||||
|
List<ProjectAllocation> allocations = new List<ProjectAllocation>();
|
||||||
|
foreach (var projectAllocation in projectAllocations)
|
||||||
|
{
|
||||||
|
projectAllocation.ReAllocationDate = DateTime.UtcNow;
|
||||||
|
projectAllocation.IsActive = false;
|
||||||
|
allocations.Add(projectAllocation);
|
||||||
|
}
|
||||||
|
_logger.LogInfo("Employee with ID {EmployeeId} has been removed from all assigned projects.", employee.Id);
|
||||||
|
}
|
||||||
|
var user = await _context.ApplicationUsers.FirstOrDefaultAsync(u => u.Id == employee.ApplicationUserId);
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
user.IsActive = false;
|
||||||
|
_logger.LogInfo("The application user associated with employee ID {EmployeeId} has been suspended.", employee.Id);
|
||||||
|
|
||||||
|
var refreshTokens = await _context.RefreshTokens.AsNoTracking().Where(t => t.UserId == user.Id).ToListAsync();
|
||||||
|
if (refreshTokens.Count != 0)
|
||||||
|
{
|
||||||
|
_context.RefreshTokens.RemoveRange(refreshTokens);
|
||||||
|
_logger.LogInfo("Refresh tokens associated with employee ID {EmployeeId} has been removed.", employee.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
var roleMapping = await _context.EmployeeRoleMappings.AsNoTracking().Where(r => r.EmployeeId == employee.Id).ToListAsync();
|
||||||
|
if (roleMapping.Count != 0)
|
||||||
|
{
|
||||||
|
_context.EmployeeRoleMappings.RemoveRange(roleMapping);
|
||||||
|
_logger.LogInfo("Application role mapping associated with employee ID {EmployeeId} has been removed.", employee.Id);
|
||||||
|
}
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
_logger.LogInfo("Employee with ID {EmployeId} Deleted successfully", employee.Id);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,13 +1,11 @@
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
using Marco.Pms.DataAccess.Data;
|
using Marco.Pms.DataAccess.Data;
|
||||||
using Marco.Pms.Model.Dtos.Employees;
|
|
||||||
using Marco.Pms.Model.Dtos.Roles;
|
using Marco.Pms.Model.Dtos.Roles;
|
||||||
using Marco.Pms.Model.Entitlements;
|
using Marco.Pms.Model.Entitlements;
|
||||||
using Marco.Pms.Model.Mapper;
|
using Marco.Pms.Model.Mapper;
|
||||||
using Marco.Pms.Model.Utilities;
|
using Marco.Pms.Model.Utilities;
|
||||||
using Marco.Pms.Model.ViewModels;
|
using Marco.Pms.Model.ViewModels;
|
||||||
using MarcoBMS.Services.Helpers;
|
using MarcoBMS.Services.Helpers;
|
||||||
using MarcoBMS.Services.Service;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@ -23,16 +21,16 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
private readonly ApplicationDbContext _context;
|
private readonly ApplicationDbContext _context;
|
||||||
private readonly RolesHelper _rolesHelper;
|
private readonly RolesHelper _rolesHelper;
|
||||||
private readonly UserHelper _userHelper;
|
private readonly UserHelper _userHelper;
|
||||||
private readonly UserManager<ApplicationUser> _userManager;
|
|
||||||
private readonly ILoggingService _logger;
|
|
||||||
|
|
||||||
public RolesController(UserManager<ApplicationUser> userManager, ApplicationDbContext context, RolesHelper rolesHelper, UserHelper userHelper, ILoggingService logger)
|
|
||||||
|
private readonly UserManager<ApplicationUser> _userManager;
|
||||||
|
|
||||||
|
public RolesController(UserManager<ApplicationUser> userManager, ApplicationDbContext context, RolesHelper rolesHelper, UserHelper userHelper)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
_userManager = userManager;
|
_userManager = userManager;
|
||||||
_rolesHelper = rolesHelper;
|
_rolesHelper = rolesHelper;
|
||||||
_userHelper = userHelper;
|
_userHelper = userHelper;
|
||||||
_logger = logger;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Guid GetTenantId()
|
private Guid GetTenantId()
|
||||||
@ -139,7 +137,6 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
{
|
{
|
||||||
Id = item.Id,
|
Id = item.Id,
|
||||||
Role = item.Role,
|
Role = item.Role,
|
||||||
IsSystem = item.IsSystem,
|
|
||||||
Description = item.Description,
|
Description = item.Description,
|
||||||
FeaturePermission = []
|
FeaturePermission = []
|
||||||
};
|
};
|
||||||
@ -228,21 +225,17 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
Guid TenantId = GetTenantId();
|
Guid TenantId = GetTenantId();
|
||||||
var LoggedEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
||||||
var existingRole = await _context.ApplicationRoles.AsNoTracking().FirstOrDefaultAsync(r => r.Id == id);
|
|
||||||
if (existingRole != null && existingRole.IsSystem)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("Employee with ID {LoggedEmployeeId} tries to update System-defined application roles {AppcationRoleId}", LoggedEmployee.Id, existingRole.Id);
|
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("System-defined roles cannot be updated", "System-defined roles cannot be updated", 400));
|
|
||||||
}
|
|
||||||
ApplicationRole role = updateRoleDto.ToApplicationRoleFromUpdateDto(TenantId);
|
ApplicationRole role = updateRoleDto.ToApplicationRoleFromUpdateDto(TenantId);
|
||||||
|
|
||||||
if (role.TenantId != TenantId)
|
if (role.TenantId != TenantId)
|
||||||
return Unauthorized(ApiResponse<object>.ErrorResponse("You don't have any authority to update role", "You don't have any authority to update role", 401));
|
return Unauthorized(ApiResponse<object>.ErrorResponse("You don't have any authority to update role", "You don't have any authority to update role", 401));
|
||||||
if (existingRole != null)
|
|
||||||
|
var projectModel = _context.ApplicationRoles.Update(role);
|
||||||
|
|
||||||
|
if (projectModel == null)
|
||||||
{
|
{
|
||||||
_context.ApplicationRoles.Update(role);
|
return NotFound(ApiResponse<object>.ErrorResponse("Project not found", "Project not found", 404));
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool modified = false;
|
bool modified = false;
|
||||||
@ -272,13 +265,7 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var response = new
|
return BadRequest(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400));
|
||||||
{
|
|
||||||
message = ex.Message,
|
|
||||||
detail = ex.StackTrace,
|
|
||||||
statusCode = StatusCodes.Status500InternalServerError
|
|
||||||
};
|
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse(ex.Message, response, 400));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -316,95 +303,11 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
{
|
{
|
||||||
Id = role.Id,
|
Id = role.Id,
|
||||||
Role = role.Role,
|
Role = role.Role,
|
||||||
IsSystem = role.IsSystem,
|
|
||||||
FeaturePermission = featurePermissions
|
FeaturePermission = featurePermissions
|
||||||
};
|
};
|
||||||
|
|
||||||
return Ok(ApiResponse<object>.SuccessResponse(vm, "Roles Perimssions fetched successfully.", 200));
|
return Ok(ApiResponse<object>.SuccessResponse(vm, "Roles Perimssions fetched successfully.", 200));
|
||||||
|
|
||||||
}
|
}
|
||||||
[HttpDelete("{id}")]
|
|
||||||
public async Task<IActionResult> DeleteApplicationRole(Guid id)
|
|
||||||
{
|
|
||||||
Guid tenantId = GetTenantId();
|
|
||||||
var LoggedEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
||||||
var role = await _context.ApplicationRoles.AsNoTracking().FirstOrDefaultAsync(r => r.Id == id && r.TenantId == tenantId);
|
|
||||||
if (role != null)
|
|
||||||
{
|
|
||||||
if (role.IsSystem)
|
|
||||||
{
|
|
||||||
_logger.LogInfo("Employee with ID {LoggedEmployeeId} tries to delete system-defined application role with ID {ApplicationRoleId}", LoggedEmployee.Id, role.Id);
|
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("This role cannot be deleted because it is system-defined.", "This role cannot be deleted because it is system-defined.", 400));
|
|
||||||
}
|
|
||||||
var employeeRoleMapping = await _context.EmployeeRoleMappings.Where(erm => erm.RoleId == role.Id).ToListAsync();
|
|
||||||
if (employeeRoleMapping.Count != 0)
|
|
||||||
{
|
|
||||||
_logger.LogInfo("Employee with ID {LoggedEmployeeId} tries to delete application role with ID {ApplicationRoleId} with is assigned to an employee", LoggedEmployee.Id, role.Id);
|
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("This role cannot be deleted because it is currently assigned to employees.", "This role cannot be deleted because it is currently assigned to employees.", 400));
|
|
||||||
}
|
|
||||||
_context.ApplicationRoles.Remove(role);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
_logger.LogInfo("Employee with ID {LoggedEmployeeId} deleted application role with ID {ApplicationRoleId}", LoggedEmployee.Id, role.Id);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_logger.LogWarning("Application role with ID {ApplicationRoleId} not found in database", id);
|
|
||||||
}
|
|
||||||
return Ok(ApiResponse<object>.SuccessResponse(new { }, "Application role is deleted successfully", 200));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
[HttpPost]
|
|
||||||
[Route("assign-roles")]
|
|
||||||
public async Task<IActionResult> ManageRoles([FromBody] List<EmployeeRoleDot> employeeRoleDots)
|
|
||||||
{
|
|
||||||
if (!ModelState.IsValid)
|
|
||||||
{
|
|
||||||
var errors = ModelState.Values
|
|
||||||
.SelectMany(v => v.Errors)
|
|
||||||
.Select(e => e.ErrorMessage)
|
|
||||||
.ToList();
|
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
|
||||||
}
|
|
||||||
var employeesIds = employeeRoleDots.Select(e => e.EmployeeId).Distinct().ToList();
|
|
||||||
var LoggedEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
||||||
var employees = await _context.Employees.Where(e => employeesIds.Contains(e.Id)).ToListAsync();
|
|
||||||
|
|
||||||
Guid TenantId = GetTenantId();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
foreach (EmployeeRoleDot role in employeeRoleDots)
|
|
||||||
{
|
|
||||||
var employee = employees.Find(e => e.Id == role.EmployeeId && e.IsSystem);
|
|
||||||
if (employee != null)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("Employee with ID {LoggedEmployeeId} tries to assign or remove the application role to System-defined employee with ID {EmployeeId}", LoggedEmployee.Id, employee.Id);
|
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("System-defined employee cannot have application roles assigned or removed.", "System-defined employee cannot have application roles assigned or removed.", 400));
|
|
||||||
}
|
|
||||||
EmployeeRoleMapping mapping = role.ToEmployeeRoleMappingFromEmployeeRoleDot(TenantId);
|
|
||||||
|
|
||||||
var existingItem = await _context.EmployeeRoleMappings.AsNoTracking().SingleOrDefaultAsync(c => c.Id == mapping.Id);
|
|
||||||
|
|
||||||
if (existingItem == null)
|
|
||||||
{
|
|
||||||
if (role.IsEnabled == true)
|
|
||||||
{
|
|
||||||
_context.EmployeeRoleMappings.Add(mapping);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (role.IsEnabled == false)
|
|
||||||
{
|
|
||||||
_context.EmployeeRoleMappings.Remove(existingItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400));
|
|
||||||
}
|
|
||||||
return Ok(ApiResponse<object>.SuccessResponse("success", "Roles modified.", 200));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -63,7 +63,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
{
|
{
|
||||||
Role = "Super User",
|
Role = "Super User",
|
||||||
Description = "Super User",
|
Description = "Super User",
|
||||||
IsSystem = true,
|
|
||||||
TenantId = Guid.Parse("b3466e83-7e11-464c-b93a-daf047838b26")
|
TenantId = Guid.Parse("b3466e83-7e11-464c-b93a-daf047838b26")
|
||||||
};
|
};
|
||||||
if (!await dbContext.ApplicationRoles.Where(a => a.Role == "Super User").AnyAsync())
|
if (!await dbContext.ApplicationRoles.Where(a => a.Role == "Super User").AnyAsync())
|
||||||
@ -94,7 +93,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
PhoneNumber = "9876543210",
|
PhoneNumber = "9876543210",
|
||||||
Photo = null, // GetFileDetails(model.Photo).Result.FileData,
|
Photo = null, // GetFileDetails(model.Photo).Result.FileData,
|
||||||
JobRoleId = jobRole != null ? jobRole.Id : Guid.Empty,
|
JobRoleId = jobRole != null ? jobRole.Id : Guid.Empty,
|
||||||
IsSystem = true,
|
|
||||||
JoiningDate = Convert.ToDateTime("2000-04-20 10:11:17.588000"),
|
JoiningDate = Convert.ToDateTime("2000-04-20 10:11:17.588000"),
|
||||||
};
|
};
|
||||||
if ((!await dbContext.Employees.Where(e => e.FirstName == "Admin").AnyAsync()) && (jobRole != null ? jobRole.Id : Guid.Empty) != Guid.Empty)
|
if ((!await dbContext.Employees.Where(e => e.FirstName == "Admin").AnyAsync()) && (jobRole != null ? jobRole.Id : Guid.Empty) != Guid.Empty)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user