Besciamello (#1)
* Fixed login & auth * Added dynamic breadcrumb * Added DOC * Added Patient * Added Notifications * Added Messages * Refactoring api * Re-writed menu * Removed unused things * Created README
This commit was merged in pull request #1.
This commit is contained in:
committed by
GitHub
parent
191daf8218
commit
1246116804
@@ -1,10 +1,19 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SeniorAssistant.Data;
|
||||
using System.Linq;
|
||||
using System;
|
||||
|
||||
namespace SeniorAssistant.Controllers
|
||||
{
|
||||
public abstract class BaseController : Controller
|
||||
{
|
||||
protected static readonly string MustBeLogged = "Devi essere loggato per vedere/modificare questo dato";
|
||||
protected static readonly string InvalidModel = "Modello non valido";
|
||||
protected static readonly string NoAuthorized = "Non sei autorizzato a vedere questi dati";
|
||||
protected static readonly string Username = "username";
|
||||
protected readonly JsonResponse OkJson = new JsonResponse();
|
||||
|
||||
IDataContextFactory<SeniorDataContext> dbFactory;
|
||||
SeniorDataContext db;
|
||||
|
||||
@@ -20,5 +29,68 @@ namespace SeniorAssistant.Controllers
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
protected bool IsLogged()
|
||||
{
|
||||
return HttpContext.Session.GetString(Username) != null;
|
||||
}
|
||||
|
||||
protected ActionResult Action(Func<ActionResult> success)
|
||||
{
|
||||
return ModelState.IsValid ?
|
||||
success.Invoke() :
|
||||
Json(new JsonResponse()
|
||||
{
|
||||
Success = false,
|
||||
Message = InvalidModel
|
||||
});
|
||||
}
|
||||
|
||||
protected ActionResult LoggedAction(Func<ActionResult> success)
|
||||
{
|
||||
return Action(() =>
|
||||
{
|
||||
return IsLogged() ?
|
||||
success.Invoke() :
|
||||
Json(new JsonResponse()
|
||||
{
|
||||
Success = false,
|
||||
Message = MustBeLogged
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
protected ActionResult LoggedAccessDataOf(string username, Func<ActionResult> success, bool patients = true)
|
||||
{
|
||||
return LoggedAction(() =>
|
||||
{
|
||||
var loggedUser = HttpContext.Session.GetString(Username);
|
||||
var condition = username.Equals(loggedUser);
|
||||
|
||||
condition = condition || (patients && (from patient in Db.Patients
|
||||
where patient.Doctor.Equals(loggedUser) && patient.Username.Equals(username)
|
||||
select patient).ToArray().FirstOrDefault() != null);
|
||||
|
||||
return condition ?
|
||||
success.Invoke() :
|
||||
Json(new JsonResponse()
|
||||
{
|
||||
Success = false,
|
||||
Message = NoAuthorized
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public class JsonResponse
|
||||
{
|
||||
public JsonResponse(bool success=true, string message="")
|
||||
{
|
||||
Success = success;
|
||||
Message = message;
|
||||
}
|
||||
|
||||
public bool Success { get; set; }
|
||||
public string Message { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user