using NHibernate; using NHibernate.Criterion; using System; using System.CodeDom.Compiler; using System.Diagnostics; using System.Linq; using System.Linq.Expressions; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web.Mvc; using T4MVC; using TFiN.Domain.Entities.Producten; using TFiN.Domain.Entities.Users; using TFiN.Domain.Mvc; using TFiN.Mvc; using TFiN.WebApplication.Areas.Admin.Models; using TFiN.WebApplication.Metron; namespace TFiN.WebApplication.Areas.Admin.Controllers { [TFiN.Domain.Mvc.Authorize(Roles = UserRoles.Admin)] public class GebruikersController : MetronController { [GeneratedCode("T4MVC", "2.0"), System.Diagnostics.DebuggerNonUserCode] public class ActionNamesClass { public readonly string Index = "Index"; } [GeneratedCode("T4MVC", "2.0"), System.Diagnostics.DebuggerNonUserCode] public class ActionNameConstants { public const string Index = "Index"; } [GeneratedCode("T4MVC", "2.0"), System.Diagnostics.DebuggerNonUserCode] public class ActionParamsClass_Index { public readonly string model = "model"; } [GeneratedCode("T4MVC", "2.0"), System.Diagnostics.DebuggerNonUserCode] public class ViewsClass { public class _ViewNamesClass { public readonly string Index = "Index"; } private static readonly GebruikersController.ViewsClass._ViewNamesClass s_ViewNames = new GebruikersController.ViewsClass._ViewNamesClass(); public readonly string Index = "~/Areas/Admin/Views/Gebruikers/Index.cshtml"; public GebruikersController.ViewsClass._ViewNamesClass ViewNames { get { return GebruikersController.ViewsClass.s_ViewNames; } } } [GeneratedCode("T4MVC", "2.0")] public const string NameConst = "Gebruikers"; [GeneratedCode("T4MVC", "2.0")] public readonly string Area = "Admin"; [GeneratedCode("T4MVC", "2.0")] public readonly string Name = "Gebruikers"; private static readonly GebruikersController.ActionNamesClass s_actions = new GebruikersController.ActionNamesClass(); private static readonly GebruikersController.ActionParamsClass_Index s_params_Index = new GebruikersController.ActionParamsClass_Index(); private static readonly GebruikersController.ViewsClass s_views = new GebruikersController.ViewsClass(); [GeneratedCode("T4MVC", "2.0"), System.Diagnostics.DebuggerNonUserCode] public GebruikersController Actions { get { return MVC.Admin.Gebruikers; } } [GeneratedCode("T4MVC", "2.0"), System.Diagnostics.DebuggerNonUserCode] public GebruikersController.ActionNamesClass ActionNames { get { return GebruikersController.s_actions; } } [GeneratedCode("T4MVC", "2.0"), System.Diagnostics.DebuggerNonUserCode] public GebruikersController.ActionParamsClass_Index IndexParams { get { return GebruikersController.s_params_Index; } } [GeneratedCode("T4MVC", "2.0"), System.Diagnostics.DebuggerNonUserCode] public GebruikersController.ViewsClass Views { get { return GebruikersController.s_views; } } [GeneratedCode("T4MVC", "2.0"), System.Diagnostics.DebuggerNonUserCode] public GebruikersController() { } [GeneratedCode("T4MVC", "2.0"), System.Diagnostics.DebuggerNonUserCode] protected GebruikersController(Dummy d) { } [GeneratedCode("T4MVC", "2.0"), System.Diagnostics.DebuggerNonUserCode] protected System.Web.Mvc.RedirectToRouteResult RedirectToAction(System.Web.Mvc.ActionResult result) { IT4MVCActionResult callInfo = result.GetT4MVCResult(); return base.RedirectToRoute(callInfo.RouteValueDictionary); } [GeneratedCode("T4MVC", "2.0"), System.Diagnostics.DebuggerNonUserCode] protected System.Web.Mvc.RedirectToRouteResult RedirectToAction(Task taskResult) { return this.RedirectToAction(taskResult.Result); } [GeneratedCode("T4MVC", "2.0"), System.Diagnostics.DebuggerNonUserCode] protected System.Web.Mvc.RedirectToRouteResult RedirectToActionPermanent(System.Web.Mvc.ActionResult result) { IT4MVCActionResult callInfo = result.GetT4MVCResult(); return base.RedirectToRoutePermanent(callInfo.RouteValueDictionary); } [GeneratedCode("T4MVC", "2.0"), System.Diagnostics.DebuggerNonUserCode] protected System.Web.Mvc.RedirectToRouteResult RedirectToActionPermanent(Task taskResult) { return this.RedirectToActionPermanent(taskResult.Result); } [System.Web.Mvc.HttpGet, Transaction] public virtual System.Web.Mvc.ActionResult Index() { base.Metron.Model = this.Query(new GebruikersViewModel()); base.Metron.DataDictionary.Add("gebruikers", base.Url.Action(MVC.Admin.Gebruikers.Index())); base.Metron.AddOptions(new UserRoles[] { UserRoles.Adviseur, UserRoles.Ambassadeur, UserRoles.Consument, UserRoles.Admin, UserRoles.DocumentOntvanger, UserRoles.ServiceProvider, UserRoles.Pilot, UserRoles.Duitsland, UserRoles.Financieel }); base.Metron.AddOptions(new UserServices[] { UserServices.Basis, UserServices.Professional, UserServices.Leads }); return base.View(); } [System.Web.Mvc.HttpPost, ValidateHeaderAntiForgeryToken, Transaction] public virtual System.Web.Mvc.ActionResult Index(GebruikersViewModel model) { return base.Mson(this.Query(model), System.Web.Mvc.JsonRequestBehavior.DenyGet); } private GebruikersViewModel Query(GebruikersViewModel model) { User ua = null; UserProfile upa = null; IQueryOver profileQuery = base.Db.QueryOver(() => upa).Fetch((UserProfile p) => p.User).Eager.Fetch((UserProfile p) => p.User.Oorsprong).Eager; IQueryOver userQuery = profileQuery.JoinAlias((UserProfile p) => p.User, () => ua).Where(() => (int)ua.Status == 2); if (model.Roles.HasValue) { userQuery = userQuery.And(NHibernate.Criterion.Expression.Sql("Roles & ? <> 0", (int)model.Roles.Value, NHibernateUtil.Int32)); } if (model.Services.HasValue) { userQuery = userQuery.And(NHibernate.Criterion.Expression.Sql("Services & ? <> 0", (int)model.Services.Value, NHibernateUtil.Int32)); } if (!string.IsNullOrEmpty(model.Filter)) { model.Filter = model.Filter.Trim(); string iban = model.Filter.Replace(" ", "").ToUpperInvariant(); System.Guid id; if (System.Guid.TryParse(model.Filter, out id)) { userQuery = userQuery.Where(() => ua.Id == id); } else if (Regex.IsMatch(iban, "^NL\\d{2}[A-Z]{4}\\d{10}$")) { userQuery = userQuery.WithSubquery.WhereExists(( from m in QueryOver.Of() where m.User.Id == ua.Id where m.IBAN == iban select m).Select(new Expression>[] { (Machtiging m) => (object)m.Id })); } else if (Regex.IsMatch(model.Filter, "^D\\d{6}$")) { userQuery = userQuery.WithSubquery.WhereExists(( from m in QueryOver.Of() where m.User.Id == ua.Id where m.Debiteurnummer == int.Parse(model.Filter.Substring(1)) select m).Select(new Expression>[] { (Machtiging m) => (object)m.Id })); } else if (Regex.IsMatch(model.Filter, "^F\\d{8}$")) { userQuery = userQuery.WithSubquery.WhereExists(( from m in ( from f in QueryOver.Of() where f.Factuurnummer == (long)int.Parse(model.Filter.Substring(1)) select f).JoinQueryOver((Factuur f) => f.Machtiging) where m.User.Id == ua.Id select m).Select(new Expression>[] { (Factuur m) => (object)m.Id })); } else if (Regex.IsMatch(model.Filter, "^H\\d{6}$")) { profileQuery = profileQuery.Where(() => upa.HdnVerzenderNummer == (int?)int.Parse(model.Filter.Substring(1))); } else { string[] terms = model.Filter.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries); Disjunction disjunction = Restrictions.Disjunction(); foreach (string term in from t in terms.Take(5) where t.Length >= 2 select t) { string like = string.Format("%{0}%", term); disjunction.Add(Restrictions.Like(Projections.Property(() => ua.Voornaam), like)); disjunction.Add(Restrictions.Like(Projections.Property(() => ua.Achternaam), like)); disjunction.Add(Restrictions.Like(Projections.Property(() => ua.Email), like)); disjunction.Add(Restrictions.Like(Projections.Property(() => upa.Bedrijfsnaam), like)); disjunction.Add(Restrictions.Like(Projections.Property(() => upa.Telefoon), like)); disjunction.Add(Restrictions.Like(Projections.Property(() => upa.VestigingsAdres.Straat), like)); disjunction.Add(Restrictions.Like(Projections.Property(() => upa.VestigingsAdres.Plaats), like)); if (Regex.IsMatch(term, "\\d{4}")) { disjunction.Add(Restrictions.Like(Projections.Property(() => upa.VestigingsAdres.Postcode), like)); } int intValue; if (term.Length >= 7 && int.TryParse(term, out intValue)) { disjunction.Add(Restrictions.Like(Projections.Property(() => (object)upa.AfmNummer), intValue)); } } profileQuery = profileQuery.Where(disjunction); } } model.Gebruikers = from p in profileQuery.OrderBy(() => (object)ua.LastActivity).Desc.Take(100).List() select new GebruikersViewModel.Gebruiker { Url = base.Url.RouteUrlId("Admin_Gebruiker_Overzicht", p.User.Id), Email = p.User.Email, Naam = p.User.Name, Kantoor = p.Bedrijfsnaam, Telefoon = p.Telefoon, Oorsprong = p.User.Name }; return model; } } }