Exemplo n.º 1
0
        public ActionResult Registre(Models.UserRegistreViewModel userRegistreViewModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(View());
            }

            #region Server Side Validation
            bool modelStateFail = false;

            if (string.IsNullOrEmpty(userRegistreViewModel.Name))
            {
                this.ModelState.AddModelError("Name", "Name field is required!");
                modelStateFail = true;
            }

            if (string.IsNullOrEmpty(userRegistreViewModel.Login))
            {
                this.ModelState.AddModelError("Login", "Login field is required!");
                modelStateFail = true;
            }

            if (string.IsNullOrEmpty(userRegistreViewModel.Password))
            {
                this.ModelState.AddModelError("Password", "Password field is required!");
                modelStateFail = true;
            }

            if (string.IsNullOrEmpty(userRegistreViewModel.PasswordConfirm))
            {
                this.ModelState.AddModelError("PasswordConfirm", "Confirm password field is required!");
                modelStateFail = true;
            }

            if (string.IsNullOrEmpty(userRegistreViewModel.Email))
            {
                this.ModelState.AddModelError("Email", "Emailfield is required!");
                modelStateFail = true;
            }

            if (string.IsNullOrEmpty(userRegistreViewModel.EmailConfirm))
            {
                this.ModelState.AddModelError("EmailConfirm", "Confirm email field is required!");
                modelStateFail = true;
            }

            if (!userRegistreViewModel.Email.Equals(userRegistreViewModel.EmailConfirm))
            {
                this.ModelState.AddModelError("EmailConfirm", "Confirm email is different!");
                modelStateFail = true;
            }

            if (!userRegistreViewModel.Password.Equals(userRegistreViewModel.PasswordConfirm))
            {
                this.ModelState.AddModelError("PasswordConfirm", "Confirm password is different!");
                modelStateFail = true;
            }


            if (iServiceUser.AlreadyRegistredEmail(userRegistreViewModel.Email))
            {
                this.ModelState.AddModelError("Email", "Email alread registred!");
                modelStateFail = true;
            }

            if (modelStateFail)
            {
                return(View());
            }
            #endregion



            User user = new Model.Users.User()
            {
                Name     = userRegistreViewModel.Name,
                Login    = userRegistreViewModel.Login,
                Email    = userRegistreViewModel.Email,
                Password = userRegistreViewModel.Password,
                UserType = userRegistreViewModel.UserType
            };


            this.iServiceUser.Insert(user);
            this.iUnitOfWorkAsync.SaveChanges();


            IServiceRole serviceRole = DependencyResolver.Current.GetService <ServiceRole>();
            string       roleName    = "";

            if (user.UserType == Model.Enums.UserType.Supplier)
            {
                roleName = "Supplier";
                IServiceSupplierInfo serviceSupplierInfo = DependencyResolver.Current.GetService <ServiceSupplierInfo>();
                SupplierInfo         SupplierInfo        = new SupplierInfo();
                SupplierInfo.User = user;
                serviceSupplierInfo.Insert(SupplierInfo);
            }
            else if (user.UserType == Model.Enums.UserType.Custumer)
            {
                roleName = "Custumer";
                IServiceCustumerInfo seviceCustumerInfo = DependencyResolver.Current.GetService <ServiceCustumerInfo>();
                CustumerInfo         custumerInfo       = new CustumerInfo();
                custumerInfo.User = user;
                seviceCustumerInfo.Insert(custumerInfo);
            }

            serviceRole.AddUserOnRole(roleName, user);

            this.iUnitOfWorkAsync.SaveChanges();


            FormsAuthentication.SetAuthCookie(user.Email, false);

            ModelState.Remove("Password");



            return(RedirectToAction("Edit", "User", new { id = user.ID }));
            //return View();
        }