Ejemplo n.º 1
0
        public bool ValidatePasscode(TakeOwnerShipModel model)
        {
            var config = Catalog.Factory.Resolve <IConfig>();
            var takeOwnerShipPassCode = config["TakeOwnershipPassCode"];

            //return model.PassCode == Guid.Parse(takeOwnerShipPassCode);
            return(model.PassCode == takeOwnerShipPassCode);
        }
Ejemplo n.º 2
0
        public void ValidateOwner(TakeOwnerShipModel model)
        {
            var owners = Roles.GetUsersInRole(DefaultRoles.SuperAdmin);

            if (owners != null && owners.Any())
            {
                throw new ApplicationException("Current application already has an owner");
            }

            //if (model.PassCode == Guid.Empty)
            if (string.IsNullOrEmpty(model.PassCode))
            {
                throw new ApplicationException(
                          "The value should be a GUID. For ex.: e9642097-7d56-49a8-a25e-316beb5feebf");
            }
        }
Ejemplo n.º 3
0
        public ActionResult TakeOwnership(TakeOwnerShipModel model)
        {
            try
            {
                if (!Request.IsAuthenticated)
                {
                    RedirectToAction("Login");
                }

                if (!ModelState.IsValid)
                {
                    // If we got this far, something failed, redisplay form
                    return(View(model));
                }

                _accountBusinessLogic.ValidateOwner(model);
                var validPasscode = _accountBusinessLogic.ValidatePasscode(model);

                if (!validPasscode)
                {
                    throw new ApplicationException("Pass Code is not correct");
                }

                if (TempData.ContainsKey("passcode"))
                {
                    TempData["passcode"] = model.PassCode;
                }
                else
                {
                    TempData.Add("passcode", model.PassCode);
                }
                return(RedirectToAction("Register"));
            }
            catch (Exception ex)
            {
                _log.ErrorFormat(
                    "Current User: {0} - An exception occurred with TakeOwnership Passcode: {1}",
                    User.Identity.Name,
                    ex.Message);
                _applicationAlert.RaiseAlert(ApplicationAlertKind.System, ex.TraceInformation());

                ModelState.AddModelError("Error", ex.Message);
                TempData.Remove("ownershipError");
                TempData["ownershipError"] = ex.Message;
                return(View(model));
            }
        }
Ejemplo n.º 4
0
        public bool AddRoleToUser(TakeOwnerShipModel model, string userName)
        {
            var config = Catalog.Factory.Resolve <IConfig>();
            var takeOwnerShipPassCode = config["TakeOwnershipPassCode"];

            //if (model.PassCode == Guid.Parse(takeOwnerShipPassCode))
            if (!string.IsNullOrEmpty(model.PassCode) && model.PassCode == takeOwnerShipPassCode)
            {
                if (!Roles.RoleExists(DefaultRoles.SuperAdmin))
                {
                    Roles.CreateRole(DefaultRoles.SuperAdmin);
                }
                new UserManager().AddRoleToUser(userName, DefaultRoles.SuperAdmin, Roles.ApplicationName);

                return(true);
            }
            return(false);
        }
Ejemplo n.º 5
0
        public ActionResult Register(RegisterModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //begin
                    var invitationUILogic = new InvitationUILogic();
                    var invitationModel   = invitationUILogic.GetInvitationModelByModelId(model.AuthenticationCode);

                    if (invitationModel == null
                        &&
                        !TenantManager.CurrentTenancy.Equals(
                            Tenants.SuperAdmin, StringComparison.InvariantCultureIgnoreCase))
                    {
                        ModelState.AddModelError("AuthenticationCode", "Code is invalid");
                        ViewBag.ErrorMessage = "Code is invalid";
                        return(View(model));
                    }

                    if (invitationModel != null)
                    {
                        ActionResult redirectToRoute;
                        if (this.RedirectToCorrectTenancy(invitationModel, out redirectToRoute))
                        {
                            return(redirectToRoute);
                        }

                        if (!invitationModel.SentTo.Equals(model.Email, StringComparison.InvariantCultureIgnoreCase))
                        {
                            this.ModelState.AddModelError("Email", "Email do not equals invitation's email");
                            ViewBag.ErrorMessage = "Email do not equals invitation's email";
                            return(View(model));
                        }
                    }

                    //end
                    var invitation = this.Session["Invitation"] as OwnerInvitationModel ?? invitationModel;

                    Server.MapPath(DefaultAvatarLocation);

                    MembershipCreateStatus createStatus;

                    //membership created and log automatically
                    if (CreateMembershipUser(model, invitationUILogic.ModelToEntity(invitation), out createStatus))
                    {
                        Session["Invitation"] = null;
                        object passcode;

                        string code = null;
                        if (TempData.TryGetValue("passcode", out passcode))
                        {
                            code = passcode.ToString();
                        }

                        //if (code != null)
                        if (!string.IsNullOrEmpty(code))
                        {
                            TempData.Remove("passcode");

                            var passCodeModel = new TakeOwnerShipModel {
                                PassCode = code
                            };
                            if (_accountBusinessLogic.AddRoleToUser(passCodeModel, User.Identity.Name))
                            {
                                var user = User as ApplicationUser;

                                if (user != null &&
                                    user.Tenancy.Equals(Tenants.SuperAdmin, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    return(RedirectToAction("Index", "OwnerInvitation"));
                                }

                                ActionResult redirectToRoute;
                                return(this.LoginPostValidations(out redirectToRoute)
                                    ? redirectToRoute
                                    : RedirectAfterLogin(null));
                            }
                        }
                    }

                    this.ModelState.AddModelError(string.Empty, ErrorCodeToString(createStatus));
                }
            }
            catch (Exception ex)
            {
                if (ExceptionHandler.Manage(ex, this, Layer.UILogic))
                {
                    this.ModelState.AddModelError(string.Empty, ex.Message);
                    ViewBag.ErrorMessage = ex.Message;
                }
                else
                {
                    if (ex.InnerException != null)
                    {
                        _log.ErrorFormat("{0} \n Inner Exception: {1}", ex, ex.InnerException);
                    }
                    else
                    {
                        _log.ErrorFormat("An exception occurred with the following message: {0}", ex.Message);
                    }

                    _applicationAlert.RaiseAlert(ApplicationAlertKind.System, ex.TraceInformation());

                    const string errorMessage =
                        "An error occurred while processing your request. Please refresh the page. The error have been logged.";
                    this.ModelState.AddModelError(string.Empty, errorMessage);
                    ViewBag.ErrorMessage = errorMessage;
                }

                return(View(model));
            }

            // If we got this far, something failed, redisplay form
            //return View(model);S
            ActionResult routeToRedirect;

            return(this.LoginPostValidations(out routeToRedirect)
                ? routeToRedirect
                : RedirectAfterLogin(null));
            //return RedirectToAction("Index", "OwnerInvitation");
        }