using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using ODCWeb.DataAccess.Data; using ODCWeb.Models; using System.Security.Claims; using static Microsoft.EntityFrameworkCore.DbLoggerCategory; using System.Collections.Immutable; using ODCWeb.Utility; using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.EntityFrameworkCore; using Microsoft.CodeAnalysis; namespace ODCWeb.Areas.Admin.Controllers { [Area("Admin")] [Authorize(Roles="Admin")] public class UsersController : Controller { private readonly RoleManager roleManager; private readonly UserManager userManager; private readonly ApplicationDbContext _db; public UsersController(RoleManager roleManager, UserManager userManager, ApplicationDbContext applicationDbContext) { this.roleManager = roleManager; this.userManager = userManager; _db = applicationDbContext; } // GET: UsersController [HttpGet] public IActionResult Index() { var users = userManager.Users; foreach (var person in users) { if (person.SubscriptionDaysLeft > -1) { ApplicationUser obj = _db.ApplicationUsers.FirstOrDefault(c => c.Id == person.Id); obj.SubscriptionStatus = true; _db.Update(obj); _db.SaveChanges(); } else { ApplicationUser obj = _db.ApplicationUsers.FirstOrDefault(c => c.Id == person.Id); obj.SubscriptionStatus = false; _db.Update(obj); _db.SaveChanges(); } } var users2 = userManager.Users; return View(users2); } public IActionResult Approval(string? id) { if (id == null) { return RedirectToAction("Index"); } else { ApplicationUser userFromDb = _db.ApplicationUsers.FirstOrDefault(c => c.Id == id); if (userFromDb.Approved == false) { userFromDb.Approved = true; } else { userFromDb.Approved = false; } _db.Update(userFromDb); _db.SaveChanges(); /* string RoleIdd = _db.UserRoles.Where(a => a.UserId == id).ToString();*/ return View(userFromDb); } } public async Task RoleChange(string? id) { if (id == null) { return RedirectToAction("Index"); } else { ApplicationUser userFromDbb = await userManager.FindByIdAsync(id); if (userFromDbb.RoleName == SD.Role_Customer) { userFromDbb.RoleName = SD.Role_Employee; userFromDbb.RoleId = roleManager.Roles.FirstOrDefault(c => c.Name == SD.Role_Employee).Id; _db.ChangeTracker.Clear(); await userManager.AddToRoleAsync(userFromDbb, SD.Role_Employee); await userManager.RemoveFromRoleAsync(userFromDbb, SD.Role_Customer); } else { userFromDbb.RoleName = SD.Role_Customer; userFromDbb.RoleId = roleManager.Roles.FirstOrDefault(c => c.Name == SD.Role_Customer).Id; await userManager.AddToRoleAsync(userFromDbb, SD.Role_Customer); await userManager.RemoveFromRoleAsync(userFromDbb, SD.Role_Employee); } return View(userFromDbb); } } [HttpGet] public async Task SubExtend(string? id) { ApplicationUser userFromDbbb = await userManager.FindByIdAsync(id); return View(userFromDbbb); } [HttpPost] public IActionResult SubExtend(int days , string? id) { ApplicationUser obj = _db.ApplicationUsers.FirstOrDefault(C => C.Id == id); obj.ExpiryDate = obj.ExpiryDate.AddDays(days); if (obj.SubscriptionDaysLeft > -1) { obj.SubscriptionStatus = true; } else { obj.SubscriptionStatus = false; } obj.SubscriptionDaysLeft = ((int)(obj.ExpiryDate - DateTime.Today).TotalDays); /* _db.Attach(obj); _db.Entry(obj).State = EntityState.Modified;*/ _db.Update(obj); _db.SaveChanges(); return RedirectToAction("Index", "Users"); } public IActionResult Delete_User(string? id) { ApplicationUser obj = _db.ApplicationUsers.FirstOrDefault(u => u.Id == id); if (obj == null) { TempData["error"] = "Something wrong!"; } else { _db.Remove(obj); _db.SaveChanges(); TempData["success"] = "User has been Deleted!"; } return RedirectToAction("Index", "Users"); } [HttpGet] public async Task Edit_User(string? id) { ApplicationUser signInUser = await userManager.GetUserAsync(User); ApplicationUser userFromDbb = await userManager.FindByIdAsync(id); if (userFromDbb != null) { return View(userFromDbb); } if (userFromDbb.Id != signInUser.Id) { TempData["error"] = "Not Allowed!"; return RedirectToAction("Index", "Users"); } else { TempData["error"] = "User not found!"; return RedirectToAction("Index","Users"); } } [HttpPost] [ValidateAntiForgeryToken] public IActionResult Edit_User(ApplicationUser obj) { if (ModelState.IsValid) { if (obj != null) { _db.Update(obj); _db.SaveChanges(); TempData["success"] = "User has been updated!"; } else { TempData["error"] = "User not updated!"; } } return RedirectToAction("Index", "Users"); } // GET: UsersController/Details/5 public ActionResult Details(int id) { return View(); } // GET: UsersController/Create public ActionResult Create() { return View(); } // POST: UsersController/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(IFormCollection collection) { try { return RedirectToAction(nameof(Index)); } catch { return View(); } } // GET: UsersController/Edit/5 public ActionResult Edit(int id) { return View(); } // POST: UsersController/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(int id, IFormCollection collection) { try { return RedirectToAction(nameof(Index)); } catch { return View(); } } // GET: UsersController/Delete/5 public ActionResult Delete(int id) { return View(); } // POST: UsersController/Delete/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Delete(int id, IFormCollection collection) { try { return RedirectToAction(nameof(Index)); } catch { return View(); } } } }