Files
BeWoPlaner/BeWoPlanerMobil/Controllers/LoginController.cs
Lyndon Jetten 3ca4ec5323 Das Gruppenbuchungsformular hat jetzt die Summe der Ziele aller ausgewählten Hilfepläne und die Schnittmenge deren Leistungen und Kategorien.
Das Mehrfachbuchungsformular hat ebenfalls die Schnittmenge der Leistungen und Kategorien von den ausgewählten Hilfeplänen.

Das Caching ist wieder aktiviert. Und lädt pro Post-Aufruf nur noch einmal 13 MB und danach nur noch 300 kb herunter.

Überall konsequent geprüft, ob das Model nicht null ist und falls dem so ist, wird ausgeloggt, damit es zu keinen Fehlern mehr kommt.
2024-06-26 12:29:22 +02:00

234 lines
7.2 KiB
C#

using System;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using BeWoPlanerMobil.Models;
using BeWoPlanerMobil.Service;
using BeWoPlanerMobil.Util;
using BS.Shared;
using BS.Shared.DataContracts;
using HttpCookie = System.Web.HttpCookie;
namespace BeWoPlanerMobil.Controllers
{
public class LoginController : AbstractBaseController
{
private static readonly string GetServerUrl = "https://support.bewoplaner.de/api/getserver.php?k={0}&type=1";
public static string LoginErrorIndex => "LoginErrorIndex";
[HttpPost]
public ActionResult Index()
{
try
{
var url = GetRedirectUrlFromTenant(MobileSessionFacade.Tenant);
if (!string.IsNullOrEmpty(url))
{
return Redirect(url);
}
ViewData[LoginErrorIndex] = string.Empty;
TempData[LoginErrorIndex] = string.Empty;
var isUserLoggedIn = MobileSessionFacade.IsUserLoggedIn();
if(isUserLoggedIn && !Request.Browser.Browser.Equals("InternetExplorer"))
{
var loginState = MobileUtils.GetLoginState(MobileSessionFacade.Tenant);
if(loginState.Message != null && loginState.State > -1)
{
TempData[MobileUtils.TempDataLoginStateKey] = loginState;
if(loginState.State == 0)
{
ViewData[LoginErrorIndex] = loginState.Message;
return Logout();
}
}
Log.Info($"LoginController.Index(): Loggedin User: {MobileSessionFacade.LoggedInUser.LoginName}");
var viewServiceRecords = AbstractModel.HasRightToSeeServiceRecords;//AbstractModel.CheckRight(UserRightType.ServiceRecordView_View);
var viewAll = AbstractModel.CheckRight(UserRightType.ViewAll);
var user = UserService.LoadUser(MobileSessionFacade.LoggedInUser.Oid.Value);
var userGroups = user.UserGroups;
//if(!MobileSessionFacade.LoggedInUser.UserGroups.Any(a => a.Rights.Any(f => f.RightType.Equals(UserRightType.ServiceRecordView_View))) &&
// !MobileSessionFacade.LoggedInUser.UserGroups.Any(a => a.Rights.Any(f => f.RightType.Equals(UserRightType.ViewAll))))
if(!viewServiceRecords && !viewAll)
{
ViewData[LoginErrorIndex] = HttpUtility.HtmlDecode("Sie haben keine ausreichenden Berechtigungen, um die Zeiterfassung anzeigen zu lassen.");
MobileSessionFacade.LogUserOut();
FormsAuthentication.SignOut();
return View("Index");
}
FormsAuthentication.SetAuthCookie(MobileSessionFacade.LoggedInUser.LoginName, true);
ViewData[LoginErrorIndex] = string.Empty;
var keks = new HttpCookie(MobileSessionFacade.CookieName) { Value = MobileSessionFacade.Tenant, Expires = DateTime.Now.AddDays(1) };
HttpContext.Response.SetCookie(keks);
var brauser = Request.Browser;
var brauserInfoDC = new BeWoMobilBrowserInfoDC
{
Type = brauser.Type,
Name = brauser.Browser,
BrowserVersion = brauser.Version,
Platform = brauser.Platform,
Tenant = MobileSessionFacade.Tenant
};
// TODO: Führt manchmal zu Exception
try
{
OperationsService.InsertBeWoMobilBrowserInfo(brauserInfoDC);
}
catch(Exception exception)
{
Log.Error("Fehler beim Speichern der BrowserInfo: " + exception);
}
AbstractModel.TimeOfLastAction = $"{DateTime.Now:yyyy-MM-dd}T{DateTime.Now:HH:mm:ss}";
return RedirectToActionPermanent("Main", "Main");
}
if(isUserLoggedIn && Request.Browser.Browser.Equals("InternetExplorer"))
{
MobileSessionFacade.LogUserOut();
FormsAuthentication.SignOut();
}
var beWoCookie = HttpContext.Response.Cookies[MobileSessionFacade.CookieName];
if(beWoCookie != null)
{
var newCookie = new HttpCookie(MobileSessionFacade.CookieName)
{
Expires = DateTime.Now.AddDays(-1),
SameSite = SameSiteMode.Strict
};
Response.Cookies.Add(newCookie);
}
Log.Info($"LoginController.Index(): Login failed! Tenant = {MobileSessionFacade.Tenant}");
ViewData[LoginErrorIndex] = Request.Browser.Browser.Equals("InternetExplorer") ?
"Dieser Browser ist nicht mit dem mobilen BeWoPlaner kompatibel. Bitte benutzen Sie einen anderen Browser als den Internet Explorer." :
"Die Anmeldung ist fehlgeschlagen. Bitte überprüfen Sie Benutzername und Passwort.";
return View("Index");
}
catch(Exception e)
{
Log.Info($"Exception: " + e.Message + "\n" + e.StackTrace);
if(e.InnerException != null)
{
Log.Info($"\n\nInnerException: " + e.InnerException.Message + "\n" + e.InnerException.StackTrace);
}
return View("Index");
}
}
public ActionResult Index(string tenant, string returnUrl)
{
if(returnUrl != null)
{
return RedirectToAction("Index", new {tenant = MobileSessionFacade.Tenant});
}
var queryString = HttpContext.Request.QueryString;
if(queryString != null && queryString.HasKeys() && queryString.AllKeys.Contains("isSessionTimedOut"))
{
ViewData[IsSessionTimedOut] = "Sie wurden automatisch abgemeldet, da Ihre Session abgelaufen ist.";
}
var errorMessage = TempData[TempDataConstants.ErrorMessageKey];
if(tenant is null)
{
var keks = HttpContext.Request.Cookies.Get(MobileSessionFacade.CookieName);
if(keks != null)
{
tenant = keks.Value;
}
}
if(MobileSessionFacade.IsUserLoggedIn())
{
return RedirectToActionPermanent("Main", "Main");
}
var url = GetRedirectUrlFromTenant(tenant);
if (!string.IsNullOrEmpty(url))
{
return Redirect(url);
}
MobileSessionFacade.Tenant = tenant;
return View("Index");
}
private String GetRedirectUrlFromTenant(String tenant)
{
#if DEBUG
return null;
#endif
if (!String.IsNullOrWhiteSpace(tenant))
{
Log.Info($"Check Tenant " + tenant);
var server = GetServerFromTenant(tenant);
if (!String.IsNullOrEmpty(server) && server.Contains("bewoplaner.de"))
{
if (Request.Url.Host != server)
{
var newurl = String.Format("https://{0}/mobil/login/{1}", server, tenant);
Log.Info($"Wrong Server... Redirect to : " + newurl);
return newurl;
}
}
}
return null;
}
public static string GetServerFromTenant(string tenant)
{
try
{
if(tenant == "demo")
{
return "app1.bewoplaner.de";
}
var url = string.Format(GetServerUrl, tenant);
using(var client = new WebClient())
{
var response = client.UploadValues(url, "POST", new NameValueCollection());
return System.Text.Encoding.ASCII.GetString(response);
}
}
catch(Exception ex)
{
return null;
}
}
}
}