|
| 1 | +using Belgrade.SqlClient; |
| 2 | +using Microsoft.AspNetCore.Authentication; |
| 3 | +using Microsoft.AspNetCore.Authentication.Cookies; |
| 4 | +using Microsoft.AspNetCore.Authorization; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Data; |
| 10 | +using System.Security.Claims; |
| 11 | +using System.Threading.Tasks; |
| 12 | + |
| 13 | +namespace wwi_app.Controllers |
| 14 | +{ |
| 15 | + public class FrontEndController : Controller |
| 16 | + { |
| 17 | + private readonly ICommand queryService; |
| 18 | + private readonly ILogger _logger; |
| 19 | + |
| 20 | + public FrontEndController(ICommand queryService, ILogger<FrontEndController> logger) |
| 21 | + { |
| 22 | + this.queryService = queryService; |
| 23 | + this._logger = logger; |
| 24 | + } |
| 25 | + |
| 26 | + [ResponseCache(Duration = 60)] |
| 27 | + public IActionResult Index() { return View(); } |
| 28 | + |
| 29 | + [ResponseCache(Duration = 60)] |
| 30 | + public IActionResult Offers() { return View(); } |
| 31 | + |
| 32 | + [ResponseCache(Duration = 60)] |
| 33 | + public IActionResult Contact() { return View(); } |
| 34 | + |
| 35 | + [Authorize] |
| 36 | + public IActionResult BuyingGroups() { return View(); } |
| 37 | + |
| 38 | + [Authorize] |
| 39 | + public IActionResult Cities() { return View(); } |
| 40 | + |
| 41 | + [Authorize] |
| 42 | + public IActionResult Colors() { return View(); } |
| 43 | + |
| 44 | + [Authorize] |
| 45 | + public IActionResult Countries() { return View(); } |
| 46 | + |
| 47 | + [Authorize] |
| 48 | + public IActionResult CustomerCategories() { return View(); } |
| 49 | + |
| 50 | + [Authorize] |
| 51 | + public IActionResult Customers() { return View(); } |
| 52 | + |
| 53 | + [Authorize] |
| 54 | + public IActionResult CustomerTransactions() { return View(); } |
| 55 | + |
| 56 | + [Authorize] |
| 57 | + public IActionResult Dashboard() { return View(); } |
| 58 | + |
| 59 | + [Authorize] |
| 60 | + public IActionResult Deals() { return View(); } |
| 61 | + |
| 62 | + [Authorize] |
| 63 | + public IActionResult DeliveryMethods() { return View(); } |
| 64 | + |
| 65 | + [Authorize] |
| 66 | + public IActionResult Invoices() { return View(); } |
| 67 | + |
| 68 | + [Authorize] |
| 69 | + public IActionResult PackageTypes() { return View(); } |
| 70 | + |
| 71 | + [Authorize] |
| 72 | + public IActionResult PaymentMethods() { return View(); } |
| 73 | + |
| 74 | + [Authorize] |
| 75 | + public IActionResult PurchaseOrders() { return View(); } |
| 76 | + |
| 77 | + [Authorize] |
| 78 | + public IActionResult SalesOrders() { return View(); } |
| 79 | + |
| 80 | + [Authorize] |
| 81 | + public IActionResult StateProvinces() { return View(); } |
| 82 | + |
| 83 | + [Authorize] |
| 84 | + public IActionResult StockGroups() { return View(); } |
| 85 | + |
| 86 | + [Authorize] |
| 87 | + public IActionResult StockItems() { return View(); } |
| 88 | + |
| 89 | + [Authorize] |
| 90 | + public IActionResult SupplierCategories() { return View(); } |
| 91 | + |
| 92 | + [Authorize] |
| 93 | + public IActionResult Suppliers() { return View(); } |
| 94 | + |
| 95 | + [Authorize] |
| 96 | + public IActionResult SupplierTransactions() { return View(); } |
| 97 | + |
| 98 | + [Authorize] |
| 99 | + public IActionResult TransactionTypes() { return View(); } |
| 100 | + |
| 101 | + public async Task<IActionResult> Login(string username, string password) |
| 102 | + { |
| 103 | + if(string.IsNullOrEmpty(username)) |
| 104 | + { |
| 105 | + return Redirect("~/Index"); |
| 106 | + } |
| 107 | + |
| 108 | + bool isValidUser = false; |
| 109 | + var claims = new List<Claim>() { new Claim(ClaimTypes.Email, username) }; |
| 110 | + |
| 111 | + await queryService |
| 112 | + .Sql("EXEC WebApi.Login @LogonName, @Password") |
| 113 | + .Param("LogonName", DbType.String, username, 256) |
| 114 | + .Param("Password", DbType.String, password, 256) |
| 115 | + .OnError(e => _logger.LogError(e, "Cannot login user:" + username)) |
| 116 | + .Map(r => { |
| 117 | + isValidUser = true; |
| 118 | + claims.Add(new Claim(ClaimTypes.Sid, Convert.ToString(r["PersonID"]))); |
| 119 | + claims.Add(new Claim(ClaimTypes.Name, Convert.ToString(r["PreferredName"]))); |
| 120 | + if (Convert.ToBoolean(r["IsSalesperson"])) |
| 121 | + claims.Add(new Claim(ClaimTypes.Role, "Salesperson")); |
| 122 | + if (Convert.ToBoolean(r["IsEmployee"])) |
| 123 | + claims.Add(new Claim(ClaimTypes.Role, "Employee")); |
| 124 | + if (r["Territory"] != null) |
| 125 | + claims.Add(new Claim("Territory", r["Territory"].ToString())); |
| 126 | + } |
| 127 | + ); |
| 128 | + |
| 129 | + if (isValidUser) |
| 130 | + { |
| 131 | + var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); |
| 132 | + await HttpContext.SignInAsync(new ClaimsPrincipal(claimsIdentity)); |
| 133 | + return Redirect("~/Dashboard"); |
| 134 | + } else |
| 135 | + { |
| 136 | + _logger.LogWarning("Cannot login user: " + username); |
| 137 | + } |
| 138 | + return Redirect("~/Index"); |
| 139 | + } |
| 140 | + |
| 141 | + public async Task<IActionResult> SignOut() |
| 142 | + { |
| 143 | + await HttpContext.SignOutAsync(); |
| 144 | + return Redirect("~/Index"); |
| 145 | + } |
| 146 | + |
| 147 | + public async Task Search(string name, string tag, double? minPrice, double? maxPrice, int? stockItemGroup, int top) |
| 148 | + { |
| 149 | + await queryService |
| 150 | + .Sql("EXEC WebApi.SearchForStockItems @Name, @Tag, @MinPrice, @MaxPrice, @StockGroupID, @MaximumRowsToReturn") |
| 151 | + .Param("Name", DbType.String, name, 100) |
| 152 | + .Param("Tag", DbType.String, tag, 100) |
| 153 | + .Param("MinPrice", DbType.Decimal, minPrice) |
| 154 | + .Param("MaxPrice", DbType.Decimal, maxPrice) |
| 155 | + .Param("StockGroupID", DbType.Int32, stockItemGroup) |
| 156 | + .Param("MaximumRowsToReturn", DbType.Int32, 20) |
| 157 | + .Stream(Response.Body, "{\"value\":[]}"); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments