Spaces:
Sleeping
Sleeping
File size: 6,731 Bytes
c2908d2 a19fd25 af069e3 a19fd25 af069e3 a19fd25 af069e3 a19fd25 af069e3 a19fd25 af069e3 a19fd25 7ecaafb a19fd25 7ecaafb a19fd25 c2908d2 a19fd25 7ecaafb a19fd25 af069e3 a19fd25 af069e3 a19fd25 7ecaafb a19fd25 7ecaafb a19fd25 7ecaafb a19fd25 7ecaafb a19fd25 7ecaafb a19fd25 c2908d2 8ded86b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using OilShopManagement.Authorization;
using OilShopManagement.Models;
using System.ComponentModel.DataAnnotations;
using System.Security.Claims;
namespace OilShopManagement.Controllers;
public class AccountController : Controller
{
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
public AccountController(SignInManager<ApplicationUser> signInManager, UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
{
_signInManager = signInManager;
_userManager = userManager;
_roleManager = roleManager;
}
[AllowAnonymous]
public IActionResult Login(string? returnUrl = null)
{
if (User.Identity?.IsAuthenticated == true)
return RedirectToAction("Index", "Dashboard");
ViewBag.ReturnUrl = returnUrl;
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string? returnUrl = null)
{
if (!ModelState.IsValid) return View(model);
var user = await _userManager.FindByEmailAsync(model.Email);
if (user == null || !user.IsActive)
{
ModelState.AddModelError("", "البريد الإلكتروني أو كلمة المرور غير صحيحة");
return View(model);
}
if (await _userManager.IsLockedOutAsync(user))
{
ModelState.AddModelError("", "تم تعطيل الحساب مؤقتاً بسبب محاولات دخول متكررة. حاول بعد 15 دقيقة.");
return View(model);
}
var result = await _signInManager.PasswordSignInAsync(user, model.Password, model.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
user.LastLoginAt = DateTime.UtcNow;
await _userManager.UpdateAsync(user);
if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
return Redirect(returnUrl);
return RedirectToAction("Index", "Dashboard");
}
if (result.IsLockedOut)
{
ModelState.AddModelError("", "تم تعطيل الحساب مؤقتاً بسبب محاولات دخول متكررة. حاول بعد 15 دقيقة.");
return View(model);
}
ModelState.AddModelError("", "البريد الإلكتروني أو كلمة المرور غير صحيحة");
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Logout()
{
await _signInManager.SignOutAsync();
return RedirectToAction(nameof(Login));
}
[AllowAnonymous]
public IActionResult Register()
{
if (User.Identity?.IsAuthenticated == true)
return RedirectToAction("Index", "Dashboard");
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model)
{
if (!ModelState.IsValid) return View(model);
if (await _userManager.FindByEmailAsync(model.Email) != null)
{
ModelState.AddModelError("Email", "البريد الإلكتروني مستخدم بالفعل");
return View(model);
}
var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email,
FullName = model.FullName,
EmailConfirmed = true,
IsActive = true,
CreatedAt = DateTime.UtcNow
};
var result = await _userManager.CreateAsync(user, model.Password);
if (!result.Succeeded)
{
foreach (var error in result.Errors)
ModelState.AddModelError("", error.Description);
return View(model);
}
if (!await _roleManager.RoleExistsAsync("Employee"))
await _roleManager.CreateAsync(new IdentityRole("Employee"));
await _userManager.AddToRoleAsync(user, "Employee");
foreach (var perm in Permissions.EmployeeDefaults)
await _userManager.AddClaimAsync(user, new Claim("Permission", perm));
await _signInManager.SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Dashboard");
}
[AllowAnonymous]
public IActionResult SetLanguage(string culture, string returnUrl = "/")
{
Response.Cookies.Append(
"language",
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1), IsEssential = true }
);
return LocalRedirect(Url.IsLocalUrl(returnUrl) ? returnUrl : "/");
}
}
public class RegisterViewModel
{
[Required(ErrorMessage = "الاسم مطلوب")]
[StringLength(100)]
[Display(Name = "الاسم الكامل")]
public string FullName { get; set; } = string.Empty;
[Required(ErrorMessage = "البريد الإلكتروني مطلوب")]
[EmailAddress]
[Display(Name = "البريد الإلكتروني")]
public string Email { get; set; } = string.Empty;
[Required(ErrorMessage = "كلمة المرور مطلوبة")]
[DataType(DataType.Password)]
[StringLength(100, MinimumLength = 8, ErrorMessage = "كلمة المرور يجب أن تكون 8 أحرف على الأقل")]
[Display(Name = "كلمة المرور")]
public string Password { get; set; } = string.Empty;
[Required(ErrorMessage = "تأكيد كلمة المرور مطلوب")]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "كلمتا المرور غير متطابقتين")]
[Display(Name = "تأكيد كلمة المرور")]
public string ConfirmPassword { get; set; } = string.Empty;
}
public class LoginViewModel
{
[Required(ErrorMessage = "البريد الإلكتروني مطلوب")]
[EmailAddress]
[Display(Name = "البريد الإلكتروني")]
public string Email { get; set; } = string.Empty;
[Required(ErrorMessage = "كلمة المرور مطلوبة")]
[DataType(DataType.Password)]
[Display(Name = "كلمة المرور")]
public string Password { get; set; } = string.Empty;
[Display(Name = "تذكرني")]
public bool RememberMe { get; set; }
}
|