Exemplo n.º 1
0
        public ActionResult Create()
        {
            var model = new CC.Web.Models.RegisterModel();

            model.LoadData(this.db, this.Permissions);
            return(View(model));
        }
Exemplo n.º 2
0
        public ActionResult Create(CC.Web.Models.RegisterModel model)
        {
            //revalidate model with permissions set
            model.Permissions = this.Permissions;
            ModelState.Clear();
            TryValidateModel(model);

            if (ModelState.IsValid)
            {
                //create the user
                var user = CC.Data.User.CreateUser(model.UserName, model.Password);

                //update entity
                model.ApplyValuesTo(user, db);

                //validate entity
                TryValidateModel(user);

                if (ModelState.IsValid)
                {
                    //add user
                    db.Users.AddObject(user);

                    //and detach agencygroups
                    if (user.AgencyGroups.Any())
                    {
                        foreach (var ser in user.AgencyGroups)
                        {
                            db.ObjectStateManager.GetObjectStateEntry(ser).ChangeState(EntityState.Unchanged);
                        }
                    }

                    // Attempt to register the user
                    var createStatus = MembershipCreateStatus.Success;
                    try
                    {
                        db.SaveChanges();
                        log.Debug(string.Format("Create User Success: user's username {0}, membershipUser's username {1}",
                                                user.UserName, user.MembershipUser.LoweredUserName));
                    }
                    catch
                    {
                        createStatus = MembershipCreateStatus.ProviderError;
                    }



                    if (createStatus == MembershipCreateStatus.Success)
                    {
                        return(this.RedirectToAction("Index"));
                    }
                    else
                    {
                        ModelState.AddModelError("", CC.Web.Controllers.AccountController.ErrorCodeToString(createStatus));
                    }
                }
            }


            model.LoadData(this.db, this.Permissions);
            return(View(model));
        }