コード例 #1
0
 private static void SetAddress(Address address, RegisterCollaboratorViewModel collaboratorViewModel)
 {
     collaboratorViewModel.Street     = address.Street;
     collaboratorViewModel.Square     = address.Square;
     collaboratorViewModel.PostalCode = address.PostalCode;
     collaboratorViewModel.State      = address.State;
     collaboratorViewModel.City       = address.City;
 }
コード例 #2
0
 private static Address GetAddress(RegisterCollaboratorViewModel registerModel)
 {
     return(new Address
     {
         City = registerModel.City,
         PostalCode = registerModel.PostalCode,
         Square = registerModel.Square,
         State = registerModel.State,
         Street = registerModel.Street
     });
 }
コード例 #3
0
        public async Task <ActionResult> Register(RegisterCollaboratorViewModel model)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser user   = model.ToApplicationUser(UserType.Collaborator);
                IdentityResult  result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    Collaborator    collaborator = model.ToCollaborator(user.PasswordHash);
                    ApplicationUser localUser    = User.Identity.GetApplicationUser();
                    LocalManager    localManager = BusinessManager.Instance.LocalManagers.FindAll().SingleOrDefault(l => localUser.Email == l.Email);

                    try
                    {
                        collaborator.Parking = BusinessManager.Instance.Parkings.FindAll().SingleOrDefault(p => p.LocalManager.CPF == localManager.CPF);
                        BusinessManager.Instance.Collaborators.Add(collaborator);
                    }
                    catch (UniqueKeyViolationException ex)
                    {
                        ModelState["CPF"].Errors.Add(ex.Message);
                    }

                    // Enviar um email com este link
                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);

                    await UserManager.SendEmailAsync(user.Id, "Confirmar sua conta Smart Parking System", "Olá, " +
                                                     model.FirstName + "!<br/> Para começar a utilizar sua nova conta Smart Parking System,<br/> clique <a href=\"" +
                                                     callbackUrl + "\">aqui</a>");

                    ViewBag.EmailSent = model.Email;

                    return(RedirectToAction("Index", "LocalAdmin"));
                }
                else
                {
                    var emailErrors = result.Errors.Where(e => e.Contains("email"));

                    if (emailErrors.Count() > 0)
                    {
                        foreach (var error in emailErrors)
                        {
                            ModelState["Email"].Errors.Add(error);
                        }
                    }
                }
            }

            // Se chegamos até aqui e houver alguma falha, exiba novamente o formulário
            return(View(model));
        }
コード例 #4
0
 public static ApplicationUser ToApplicationUser(this RegisterCollaboratorViewModel model, UserType userType)
 {
     return(new ApplicationUser
     {
         FirstName = model.FirstName,
         LastName = model.LastName,
         UserName = model.Email,
         Email = model.Email,
         PhoneNumber = model.PhoneNumber,
         UserType = userType
     });
 }
コード例 #5
0
        public static RegisterCollaboratorViewModel ToRegisterCollaboratorViewModel(this Collaborator collaborator)
        {
            var collaboratorViewModel = new RegisterCollaboratorViewModel
            {
                CPF         = collaborator.CPF,
                Email       = collaborator.Email,
                FirstName   = collaborator.FirstName,
                LastName    = collaborator.LastName,
                Number      = collaborator.StreetNumber.ToString(),
                PhoneNumber = collaborator.Telephone,
                RG          = collaborator.RG,
                Salary      = collaborator.Salary.ToString()
            };

            SetAddress(collaborator.Address, collaboratorViewModel);

            return(collaboratorViewModel);
        }
コード例 #6
0
        public static Collaborator ToCollaborator(this RegisterCollaboratorViewModel model, string passwordHash)
        {
            var salary = decimal.Parse(model.Salary, NumberStyles.Currency, new CultureInfo("pt-br"));

            return(new Collaborator
            {
                Address = GetAddress(model),
                StreetNumber = int.Parse(model.Number),
                CPF = model.CPF,
                Email = model.Email,
                FirstName = model.FirstName,
                LastName = model.LastName,
                Password = passwordHash,
                RG = model.RG,
                Salary = salary,
                Telephone = model.PhoneNumber,
                Complement = model.Complement
            });
        }