Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ExcellentTaste.Controllers
- {
- [Authorize(Roles = "Customer,Administrator")]
- public class ProfileController : Controller
- {
- protected readonly UserManager<ApplicationUser> userManager;
- protected readonly EmailConfiguration EmailConfiguration;
- public ProfileController(
- UserManager<ApplicationUser> userManager,
- IOptions<AppConfiguration> appConfiguration
- )
- {
- this.userManager = userManager;
- EmailConfiguration = appConfiguration.Value.Services.Email;
- }
- [Authorize(Roles = "Administrator")]
- public async Task<string> DeleteCustomer(string customerID)
- {
- var customer = await userManager.FindByNameAsync(customerID);
- var LoggedInUser = userManager.GetUserId(User);
- try
- {
- if (LoggedInUser.ToString() == customer.Id)
- throw new Exception("Can't Delete the logged in user!");
- await userManager.UpdateSecurityStampAsync(customer);
- var rem = await userManager.DeleteAsync(customer);
- if (!rem.Succeeded)
- throw new Exception("User has not been deleted. Abort!");
- var sendMail = await SendAccountNotification(customer.Email, customer.CustomerNumber, customer.FullName);
- if (!sendMail)
- throw new Exception("Email has not been send, something went wrong");
- }
- catch (Exception)
- {
- throw;
- }
- return "/beheer";
- }
- [Authorize(Roles = "Administrator")]
- public async Task<string> BlockCustomer(string customerID)
- {
- var customer = await userManager.FindByNameAsync(customerID);
- var LoggedInUser = userManager.GetUserId(User);
- try
- {
- if (LoggedInUser.ToString() == customer.Id)
- throw new Exception("Can't Block the logged in user!");
- var setLockoutEnd = await userManager.SetLockoutEndDateAsync(customer, DateTime.MaxValue);
- if (!setLockoutEnd.Succeeded)
- {
- throw new Exception("Blocking the user went wrong");
- }
- }
- catch
- {
- throw new Exception();
- }
- return "/beheer";
- }
- [Authorize(Roles = "Administrator")]
- public async Task<string> UnblockCustomer(string customerID)
- {
- var customer = await userManager.FindByNameAsync(customerID);
- var LoggedInUser = userManager.GetUserId(User);
- try
- {
- if (LoggedInUser.ToString() == customer.Id)
- throw new Exception("Can't Block the logged in user!");
- var setLockoutEnd = await userManager.SetLockoutEndDateAsync(customer, null);
- if (!setLockoutEnd.Succeeded)
- {
- throw new Exception("Unblocking the user went wrong");
- }
- }
- catch
- {
- throw new Exception();
- }
- return "/beheer";
- }
- #region Helpers
- private async Task<bool> SendAccountNotification(string MailTo, string Username, string Name)
- {
- SmtpClient smtpClient = new SmtpClient()
- {
- Host = EmailConfiguration.Host,
- Port = EmailConfiguration.Port,
- UseDefaultCredentials = true,
- Credentials = new NetworkCredential
- {
- UserName = EmailConfiguration.Username,
- Password = EmailConfiguration.Password
- },
- EnableSsl = EmailConfiguration.SSL,
- Timeout = 20 * 1000, // 20 seconds
- };
- MailMessage message = new MailMessage()
- {
- From = new MailAddress(EmailConfiguration.FromEmail, EmailConfiguration.FromName),
- Subject = "Uw account is verwijdert",
- IsBodyHtml = true,
- SubjectEncoding = Encoding.UTF8,
- BodyEncoding = Encoding.UTF8,
- Body = $"Beste {Name}, <br> Uw account is verwijdert door de beheerder van Excellent Taste. <br> voor meer informatie kunt u contact opnemen met Excellent Taste."
- };
- message.From = new MailAddress("support@excellent-taste.nl");
- try
- {
- message.To.Add(MailTo);
- await smtpClient.SendMailAsync(message);
- return true;
- }
- catch
- {
- return false;
- }
- finally
- {
- smtpClient.Dispose();
- message.Dispose();
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement