public ActionResult Index(LoginModel model) { if (string.IsNullOrEmpty(model.UserName)) ModelState.AddModelError("UserName not filled", Resources.Common.UserNameNotFilled); else { Regex regEx = new Regex("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"); if (!regEx.IsMatch(model.UserName)) ModelState.AddModelError("", Resources.Administration.EmailFormatNotValid); } if (string.IsNullOrEmpty(model.Password)) ModelState.AddModelError("Password not filled", Resources.Common.PasswordNotFilled); //verify if all information is completed if (ModelState.IsValid) { //validate the user if (AuthorizationBusiness.Instance.LogOn(model.UserName, model.Password)) { string[] rolesForUser = Roles.GetRolesForUser(model.UserName); //if its ok, set the login FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); //get the list of business applications by of the user List<BusinessApplicationByUser> businessAplicationsByUser = AuthorizationBusiness.GetBusinessApplicationsByUser(model.UserName); var businessAplicationsByUserTemp = new List<BusinessApplicationByUser>(businessAplicationsByUser.AsEnumerable()); foreach (var businessApp in businessAplicationsByUser) { BusinessApplicationByUser app = businessApp; var rolesByApp = rolesForUser.Where(role => role.Contains("_" + app.Prefix)).ToList(); if (rolesByApp.Any(role => role.Contains("ApplicationAdministrator")) && rolesByApp.Count == 1) { businessAplicationsByUserTemp.Remove(businessApp); } } businessAplicationsByUser = businessAplicationsByUserTemp; SetBusinessApplication(model, businessAplicationsByUser); //add in a session this list Session.Add("BusinessAplicationsByUser", businessAplicationsByUser); //if the user is GlobalAdministrator or ApplicationAdministrator, the system will display catalogue administation screen by default. //otherwise the system will display service order screen. if (Roles.IsUserInRole(model.UserName, "GlobalAdministrator") || rolesForUser.Any(role => role.Contains("ApplicationAdministrator"))) { //redirect to business screen if (Roles.IsUserInRole(model.UserName, "GlobalAdministrator") || (rolesForUser.Any(role => role.Contains("ApplicationAdministrator")) && (rolesForUser.Count(role => role.Contains("ApplicationAdministrator")) == rolesForUser.Count()))) return RedirectToAction("Index", "Catalogue"); else return RedirectToAction("Index", "ServiceOrder"); } else { //redirect to service order screen return RedirectToAction("Index", "ServiceOrder"); } } else { //if the user is not valid add an error message ModelState.AddModelError("Login Error", Resources.Common.WrongLogin); } } return View(model); }
/// <summary> /// Set business applications /// </summary> /// <param name="model">Login model</param> /// <param name="businessAplicationsByUser">List of business applications</param> /// <returns></returns> private BusinessApplicationByUser SetBusinessApplication(LoginModel model, List<BusinessApplicationByUser> businessAplicationsByUser) { //Set the default application BusinessApplicationByUser applicationByUser = new BusinessApplicationByUser(); UserProfile profile = UserProfile.GetUserProfile(model.UserName); if (string.IsNullOrEmpty(profile.ApplicationDefault)) { applicationByUser = businessAplicationsByUser.FirstOrDefault(); } else { Guid businessAppId = new Guid(profile.ApplicationDefault); if (businessAplicationsByUser.Select(data => data.Id).Contains(businessAppId)) applicationByUser = businessAplicationsByUser.FirstOrDefault(data => data.Id == businessAppId); else applicationByUser = businessAplicationsByUser.FirstOrDefault(); } if (applicationByUser != null) { Session.Add("BusinessAplicationId", applicationByUser.Id); Session.Add("LanguageAplication", applicationByUser.LanguageCode); Session.Add("objDefaultBusinessApp", applicationByUser); } return applicationByUser; }