Skip to content

Commit

Permalink
Merge pull request #151 from amosproj/feature/delte-user-75
Browse files Browse the repository at this point in the history
Delete user
  • Loading branch information
Faoilthiama authored Jan 16, 2023
2 parents 88093e1 + 5874588 commit 31334a7
Show file tree
Hide file tree
Showing 21 changed files with 1,236 additions and 73 deletions.
4 changes: 3 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"eamodio.gitlens",
"bradlc.vscode-tailwindcss",
"ms-vscode.vscode-typescript",
"ms-playwright.playwright"
"ms-playwright.playwright",
"github.copilot"
]
}
},
Expand All @@ -29,6 +30,7 @@

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "dotnet tool install --global dotnet-ef",
"postStartCommand": "git config core.autocrlf true && git config core.filemode false",
"remoteEnv": {
"PATH": "${containerEnv:PATH}:/home/vscode/.dotnet/tools"
}
Expand Down
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
123 changes: 118 additions & 5 deletions src/deskstar-backend/Deskstar/Controllers/UserController.cs
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/**
* UserController
*
* Version 1.0
*
* 2023-01-13
*
* MIT License
*/
using Deskstar.Core;
using Deskstar.Models;
using Deskstar.Entities;
Expand All @@ -13,17 +22,18 @@ namespace Deskstar.Controllers;
[Produces("application/json")]
public class UserController : ControllerBase
{

private readonly IUserUsecases _userUsecases;
private readonly ILogger<UserController> _logger;
private readonly IAutoMapperConfiguration _autoMapperConfiguration;

public UserController(ILogger<UserController> logger, IUserUsecases userUsecases, IAutoMapperConfiguration autoMapperConfiguration)
public UserController(ILogger<UserController> logger, IUserUsecases userUsecases,
IAutoMapperConfiguration autoMapperConfiguration)
{
_logger = logger;
_userUsecases = userUsecases;
_autoMapperConfiguration = autoMapperConfiguration;
}

/// <summary>
/// Returns all users for a specific company
/// </summary>
Expand Down Expand Up @@ -65,6 +75,7 @@ public IActionResult Get()
return Problem(statusCode: 500);
}
}

/// <summary>
/// Returns user specific information
/// </summary>
Expand Down Expand Up @@ -212,6 +223,7 @@ public IActionResult DeclineUser(string userId)
return Problem(statusCode: 500);
}
}

/// <summary>
/// Update user information
/// </summary>
Expand All @@ -232,14 +244,13 @@ public IActionResult DeclineUser(string userId)
public IActionResult UpdateUser(UserProfileDto userDto)
{
var userId = RequestInteractions.ExtractIdFromRequest(Request);

try
{
var mapper = _autoMapperConfiguration.GetConfiguration().CreateMapper();
var user = mapper.Map<UserProfileDto, User>(userDto);

_userUsecases.UpdateUser(userId, user);

_userUsecases.UpdateUser(user);

return Ok();
}
catch (EntityNotFoundException e)
Expand All @@ -253,4 +264,106 @@ public IActionResult UpdateUser(UserProfileDto userDto)
return Problem(statusCode: 500);
}
}

/// <summary>
/// Update given user information
/// </summary>
/// <returns> empty response </returns>
/// <remarks>
/// Sample request:
/// Post /edit
/// </remarks>
///
/// <response code="200">Empty Response</response>
/// <response code="400">Bad Request</response>
/// <response code="403">Forbid</response>
/// <response code="404">Not Found</response>
/// <response code="500">Internal Server Error</response>
[HttpPost("edit")]
[Authorize(Policy = "Admin")]
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult UpdateGivenUser(UserProfileDto userDto)
{
var adminId = RequestInteractions.ExtractIdFromRequest(Request);
try
{
var mapper = _autoMapperConfiguration.GetConfiguration().CreateMapper();
var user = mapper.Map<UserProfileDto, User>(userDto);
_userUsecases.UpdateUser(adminId,user);
return Ok();
}
catch (ArgumentInvalidException e)
{
_logger.LogError(e, e.Message);
return BadRequest(e.Message);
}
catch (InsufficientPermissionException e)
{
_logger.LogError(e, e.Message);
return Forbid(e.Message);
}
catch (EntityNotFoundException e)
{
_logger.LogError(e, e.Message);
return NotFound(e.Message);
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
return Problem(statusCode: 500);
}
}

/// <summary>
/// Delete given user
/// </summary>
/// <returns> empty response </returns>
/// <remarks>
/// Sample request:
/// Post /delete/{userId}
/// </remarks>
///
/// <response code="200">Empty Response</response>
/// <response code="404">Not Found</response>
/// <response code="500">Internal Server Error</response>
[HttpDelete("delete/{userId}")]
[Authorize(Policy = "Admin")]
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult DeleteGivenUser(string userId)
{
var adminId = RequestInteractions.ExtractIdFromRequest(Request);
try
{
_userUsecases.DeleteUser(adminId, userId);
return Ok();
}
catch (ArgumentInvalidException e)
{
_logger.LogError(e, e.Message);
return BadRequest(e.Message);
}
catch (InsufficientPermissionException e)
{
_logger.LogError(e, e.Message);
return Forbid(e.Message);
}
catch (EntityNotFoundException e)
{
_logger.LogError(e, e.Message);
return NotFound(e.Message);
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
return Problem(statusCode: 500);
}
}
}
2 changes: 2 additions & 0 deletions src/deskstar-backend/Deskstar/Entities/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public User()
public bool IsApproved { get; set; }

public bool IsCompanyAdmin { get; set; }

public bool IsMarkedForDeletion { get; set; }

public virtual Company Company { get; set; } = null!;
public virtual ICollection<Booking> Bookings { get; set; }
Expand Down
39 changes: 27 additions & 12 deletions src/deskstar-backend/Deskstar/Helper/EmailHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,55 @@
*
* MIT License
*/

using System.Net.Mail;

namespace Deskstar.Helper;

public class EmailHelper
{
private static string? _emailPassword;
private static string? _emailHost;
private static int _emailPort;
private static string? _emailPassword;
private static string? _emailHost;
private static int _emailPort;
private static string? _emailUsername;
private static SmtpClient? _smtpClient;

private const string Footer = "Note: this is an automatic email notification. Do not reply to this email. <br/> " +
"<br/> " +
"Regards, <br/> " +
"<br/>" +
"Deskstar Team";

public static bool SendEmail(ILogger logger, string userEmail, string subject, string message)
{
if(_smtpClient==null)
if (_smtpClient == null)
{
if (_emailPassword == null || _emailHost == null || _emailUsername == null)
{
logger.LogError("Email credentials not set");
return false;
return false;
}

_smtpClient = new SmtpClient(_emailHost, _emailPort);
_smtpClient.Credentials = new System.Net.NetworkCredential(_emailUsername, _emailPassword);
_smtpClient.EnableSsl = true;
_smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
_smtpClient.UseDefaultCredentials = false;
}


if (userEmail.Contains("@acme.com"))
{
return true;
}

var mailMessage = new MailMessage();
mailMessage.From = new MailAddress(_emailUsername!);
mailMessage.To.Add(new MailAddress(userEmail));

mailMessage.Subject = subject;
mailMessage.IsBodyHtml = true;
mailMessage.Body = message;
mailMessage.Body = message + Footer;

try
{
_smtpClient.Send(mailMessage);
Expand All @@ -51,26 +65,27 @@ public static bool SendEmail(ILogger logger, string userEmail, string subject, s
{
logger.LogError(ex, ex.Message);
}

return false;
}

public static void SetMailPassword(string? password)
{
_emailPassword = password;
}

public static void SetMailHost(string host)
{
_emailHost = host;
}

public static void SetMailPort(int port)
{
_emailPort = port;
}

public static void SetMailUsername(string username)
{
_emailUsername = username;
}

}
Loading

0 comments on commit 31334a7

Please sign in to comment.