using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using HeuristicLab.Services.Optimization.Web.Helpers; using HeuristicLab.Services.Optimization.Billing.Interfaces; using System.Web.Security; using HeuristicLab.Services.Optimization.Billing; using HeuristicLab.Services.Optimization.Billing.Business; namespace HeuristicLab.Services.Optimization.Web.Controllers { public class BaseController : Controller { private IOptimizationBilling billing = BillingServiceProvider.Instance; protected ControllerServiceHelper ControllerService { get { var helper = new ControllerServiceHelper(Session["pw"] as string); RedirectToLoginIfNecessary(); return helper; } } protected string UserName { get { MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */); return currentUser.UserName; } } protected bool RedirectToLoginIfNecessary(string destination="%2fOptimization", bool orderNecessary=true) { var pw = Session["pw"] as string; if (pw == null) { Response.Redirect("/Account/Logon?ReturnUrl=" + destination); return true; } if (orderNecessary) return RedirectToCreateOrderIfNecessary(); return false; } protected bool RedirectToCreateOrderIfNecessary() { MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */); var activeOrderOfUser = (from order in billing.GetOrdersByState(Billing.Model.OrderState.Active) where order.User.Name == currentUser.UserName select order).FirstOrDefault(); if (activeOrderOfUser == null) { // create a new order if there is no active order available Response.Redirect("/Order/Overview"); } return false; } } }