This commit is contained in:
2019-01-29 20:22:57 +01:00
8 changed files with 71 additions and 103 deletions

View File

@@ -1,5 +1,7 @@
using LinqToDB;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SeniorAssistant.Models;
using System.Linq;
namespace SeniorAssistant.Controllers
@@ -12,9 +14,7 @@ namespace SeniorAssistant.Controllers
[Route("Index")]
public IActionResult Login()
{
if (IsLogged())
return View("Profile");
return View();
return CheckUnAuthorized("Login");
}
[Route("Heartbeat")]
@@ -44,39 +44,33 @@ namespace SeniorAssistant.Controllers
[Route("User/{User}")]
public IActionResult SingleUser(string user)
{
var u = (from us in Db.Users
where us.Username.Equals(user)
select us).FirstOrDefault();
return CheckAuthorized("User", u);
return CheckAuthorized("User", GetUser(user));
}
[Route("Message/{User}")]
public IActionResult Message(string user)
{
return CheckAuthorized("Message", user);
return CheckAuthorized("Message", GetUser(user));
}
[Route("Profile")]
public IActionResult Profile()
{
return CheckAuthorized("Profile");
string username = HttpContext.Session.GetString(Username);
return CheckAuthorized("Profile", GetUser(username));
}
[Route("Register")]
public IActionResult Register()
{
if (IsLogged())
return View("Profile");
return View();
return CheckUnAuthorized("Register");
}
[Route("Forgot")]
public IActionResult Forgot(string username = "")
{
if (IsLogged())
return View("Profile");
var forgot = Db.Forgot.Where(f => f.Username.Equals(username)).FirstOrDefault();
return View(forgot);
return CheckUnAuthorized("Forgot", forgot);
}
protected IActionResult CheckAuthorized(string view, object model = null)
@@ -88,5 +82,24 @@ namespace SeniorAssistant.Controllers
}
return View(view, model);
}
protected IActionResult CheckUnAuthorized(string view, object model = null)
{
if (IsLogged())
{
view = "Profile";
model = GetUser(HttpContext.Session.GetString(Username));
}
return View(view, model);
}
private User GetUser(string username)
{
return Db.Users
.LoadWith(u => u.Doc)
.LoadWith(u => u.Pat)
.Where(u => u.Username.Equals(username))
.FirstOrDefault();
}
}
}