forked from wanderer/pwt-0x01-ng
154 lines
3.5 KiB
C#
154 lines
3.5 KiB
C#
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using pwt_0x01_ng.Models.Database;
|
|
using pwt_0x01_ng.Models.Identity;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
namespace pwt_0x01_ng.Areas.Admin.Controllers
|
|
{
|
|
[Area("Admin")]
|
|
[Authorize(Roles = nameof(Roles.Admin))]
|
|
public class UsersController : Controller
|
|
{
|
|
private readonly DBContext _context;
|
|
|
|
public UsersController(DBContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: Admin/Users
|
|
public async Task<IActionResult> Index()
|
|
{
|
|
return View(await _context.Users.ToListAsync());
|
|
}
|
|
|
|
// GET: Admin/Users/Details/5
|
|
public async Task<IActionResult> Details(int? id)
|
|
{
|
|
if (id == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var user = await _context.Users
|
|
.FirstOrDefaultAsync(m => m.Id == id);
|
|
if (user == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return View(user);
|
|
}
|
|
|
|
// GET: Admin/Users/Create
|
|
public IActionResult Create()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
// POST: Admin/Users/Create
|
|
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
|
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Create([Bind("Name,LastName,Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] User user)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
_context.Add(user);
|
|
await _context.SaveChangesAsync();
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
return View(user);
|
|
}
|
|
|
|
// GET: Admin/Users/Edit/5
|
|
public async Task<IActionResult> Edit(int? id)
|
|
{
|
|
if (id == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var user = await _context.Users.FindAsync(id);
|
|
if (user == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return View(user);
|
|
}
|
|
|
|
// POST: Admin/Users/Edit/5
|
|
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
|
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Edit(int id, [Bind("Name,LastName,Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] User user)
|
|
{
|
|
if (id != user.Id)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
_context.Update(user);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!UserExists(user.Id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
return View(user);
|
|
}
|
|
|
|
// GET: Admin/Users/Delete/5
|
|
public async Task<IActionResult> Delete(int? id)
|
|
{
|
|
if (id == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var user = await _context.Users
|
|
.FirstOrDefaultAsync(m => m.Id == id);
|
|
if (user == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return View(user);
|
|
}
|
|
|
|
// POST: Admin/Users/Delete/5
|
|
[HttpPost, ActionName("Delete")]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> DeleteConfirmed(int id)
|
|
{
|
|
var user = await _context.Users.FindAsync(id);
|
|
_context.Users.Remove(user);
|
|
await _context.SaveChangesAsync();
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
|
|
private bool UserExists(int id)
|
|
{
|
|
return _context.Users.Any(e => e.Id == id);
|
|
}
|
|
}
|
|
}
|