Advertisement
Guest User

ProfileController

a guest
Jan 27th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.03 KB | None | 0 0
  1. namespace ExcellentTaste.Controllers
  2. {
  3.     [Authorize(Roles = "Customer,Administrator")]
  4.     public class ProfileController : Controller
  5.     {
  6.         protected readonly UserManager<ApplicationUser> userManager;
  7.  
  8.         protected readonly EmailConfiguration EmailConfiguration;
  9.  
  10.         public ProfileController(
  11.             UserManager<ApplicationUser> userManager,
  12.             IOptions<AppConfiguration> appConfiguration
  13.         )
  14.         {
  15.             this.userManager = userManager;
  16.            
  17.             EmailConfiguration = appConfiguration.Value.Services.Email;
  18.         }
  19.  
  20.         [Authorize(Roles = "Administrator")]
  21.         public async Task<string> DeleteCustomer(string customerID)
  22.         {
  23.             var customer = await userManager.FindByNameAsync(customerID);
  24.  
  25.             var LoggedInUser = userManager.GetUserId(User);
  26.  
  27.             try
  28.             {
  29.                 if (LoggedInUser.ToString() == customer.Id)
  30.                     throw new Exception("Can't Delete the logged in user!");
  31.  
  32.                 await userManager.UpdateSecurityStampAsync(customer);
  33.  
  34.                 var rem = await userManager.DeleteAsync(customer);
  35.  
  36.                 if (!rem.Succeeded)
  37.                     throw new Exception("User has not been deleted. Abort!");
  38.  
  39.                 var sendMail = await SendAccountNotification(customer.Email, customer.CustomerNumber, customer.FullName);
  40.  
  41.                 if (!sendMail)
  42.                     throw new Exception("Email has not been send, something went wrong");
  43.  
  44.             }
  45.             catch (Exception)
  46.             {
  47.                 throw;
  48.             }
  49.  
  50.             return "/beheer";
  51.         }
  52.  
  53.         [Authorize(Roles = "Administrator")]
  54.         public async Task<string> BlockCustomer(string customerID)
  55.         {
  56.             var customer = await userManager.FindByNameAsync(customerID);
  57.  
  58.             var LoggedInUser = userManager.GetUserId(User);
  59.  
  60.             try
  61.             {
  62.                 if (LoggedInUser.ToString() == customer.Id)
  63.                     throw new Exception("Can't Block the logged in user!");
  64.  
  65.                 var setLockoutEnd = await userManager.SetLockoutEndDateAsync(customer, DateTime.MaxValue);
  66.  
  67.                 if (!setLockoutEnd.Succeeded)
  68.                 {
  69.                     throw new Exception("Blocking the user went wrong");
  70.                 }
  71.             }
  72.             catch
  73.             {
  74.                 throw new Exception();
  75.             }
  76.             return "/beheer";
  77.         }
  78.  
  79.         [Authorize(Roles = "Administrator")]
  80.         public async Task<string> UnblockCustomer(string customerID)
  81.         {
  82.             var customer = await userManager.FindByNameAsync(customerID);
  83.  
  84.             var LoggedInUser = userManager.GetUserId(User);
  85.  
  86.             try
  87.             {
  88.                 if (LoggedInUser.ToString() == customer.Id)
  89.                     throw new Exception("Can't Block the logged in user!");
  90.  
  91.                 var setLockoutEnd = await userManager.SetLockoutEndDateAsync(customer, null);
  92.  
  93.  
  94.                 if (!setLockoutEnd.Succeeded)
  95.                 {
  96.                     throw new Exception("Unblocking the user went wrong");
  97.                 }
  98.             }
  99.             catch
  100.             {
  101.                 throw new Exception();
  102.             }
  103.             return "/beheer";
  104.         }
  105.  
  106.  
  107.         #region Helpers
  108.  
  109.         private async Task<bool> SendAccountNotification(string MailTo, string Username, string Name)
  110.         {
  111.             SmtpClient smtpClient = new SmtpClient()
  112.             {
  113.                 Host = EmailConfiguration.Host,
  114.                 Port = EmailConfiguration.Port,
  115.                 UseDefaultCredentials = true,
  116.                 Credentials = new NetworkCredential
  117.                 {
  118.                     UserName = EmailConfiguration.Username,
  119.                     Password = EmailConfiguration.Password
  120.                 },
  121.                 EnableSsl = EmailConfiguration.SSL,
  122.                 Timeout = 20 * 1000, // 20 seconds
  123.             };
  124.  
  125.             MailMessage message = new MailMessage()
  126.             {
  127.                 From = new MailAddress(EmailConfiguration.FromEmail, EmailConfiguration.FromName),
  128.                 Subject = "Uw account is verwijdert",
  129.  
  130.                 IsBodyHtml = true,
  131.  
  132.                 SubjectEncoding = Encoding.UTF8,
  133.  
  134.                 BodyEncoding = Encoding.UTF8,
  135.  
  136.                 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."
  137.             };
  138.  
  139.             message.From = new MailAddress("support@excellent-taste.nl");
  140.  
  141.             try
  142.             {
  143.                 message.To.Add(MailTo);
  144.  
  145.                 await smtpClient.SendMailAsync(message);
  146.                
  147.                 return true;
  148.             }
  149.             catch
  150.             {
  151.                 return false;
  152.             }
  153.             finally
  154.             {
  155.                 smtpClient.Dispose();
  156.                 message.Dispose();
  157.             }
  158.         }
  159.         #endregion
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement