public ActionResult Create(BureauViewModel viewModel)
        {
            try
            {
                //create Bureau
                var userId = User.Identity.GetUserId();
                viewModel.Bureau.CreatedBy = userId;
                var bureau = _payrollBureauBusinessService.CreateBureau(viewModel.Bureau);
                if (!bureau.Succeeded)
                {
                    foreach (var error in bureau.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                    return(View(viewModel));
                }

                //create Bureau user and role
                var user = new ApplicationUser
                {
                    UserName = viewModel.Email,
                    Email    = viewModel.Email,
                };

                var roleId = RoleManager.Roles.FirstOrDefault(r => r.Name == "Bureau").Id;
                user.Roles.Add(new IdentityUserRole {
                    UserId = user.Id, RoleId = roleId
                });
                var result = UserManager.Create(user, "Inland12!");
                if (!result.Succeeded)
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }

                //create AspNetUser Bureau
                _payrollBureauBusinessService.CreateAspNetUserBureau(bureau.Entity.BureauId, user.Id);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
            }
            return(RedirectToAction("Index", "Bureau"));
        }
Ejemplo n.º 2
0
 public ActionResult Create(BureauViewModel viewModel)
 {
     try
     {
         //create Bureau
         var userId = User.Identity.GetUserId();
         viewModel.Bureau.CreatedBy = userId;
         var bureau = _payrollBureauBusinessService.CreateBureau(viewModel.Bureau);
         if (!bureau.Succeeded)
         {
             foreach (var error in bureau.Errors)
             {
                 ModelState.AddModelError("", error);
             }
             return(View(viewModel));
         }
     }
     catch (Exception ex)
     {
         ModelState.AddModelError("", ex);
     }
     return(RedirectToAction("Index", "Bureau"));
 }