Exemplo n.º 1
0
        public async Task <IActionResult> Index(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (await _activationInformant.IsRegistered())
            {
                return(RedirectToLocal(returnUrl));
            }

            if (ModelState.IsValid)
            {
                var company = new Company {
                    Name = model.CompanyName, NbEmployees = model.NumberOfEmployee, Mail = model.Email, Address = model.AddressNumber + " " + model.AddressStreet + ", " + model.AddressBox + ", " + model.AddressPostalCode + ", " + model.AddressCity + ", " + model.AddressCountry, Status = false
                };
                var manager = new Employee {
                    UserName = model.UserName, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Wallet = (decimal)0.00, Company = company
                };

                RemoteCall         remoteCaller    = RemoteCall.GetInstance();
                CommWrap <Company> responseCompany = await remoteCaller.RegisterCompany(company);

                if (responseCompany.RequestStatus == 1)
                {
                    company.Id      = responseCompany.Content.Id;
                    company.ChkCode = responseCompany.Content.ChkCode;
                    var result = await _userManager.CreateAsync(manager, model.Password);

                    if (result.Succeeded)
                    {
                        IdentityRole role = new IdentityRole {
                            Name = "Responsable", NormalizedName = "RESPONSABLE"
                        };
                        bool roleExist = await _roleManager.RoleExistsAsync(role.NormalizedName);

                        if (!roleExist)
                        {
                            IdentityResult roleResult = await _roleManager.CreateAsync(role);

                            if (roleResult.Succeeded)
                            {
                                await _userManager.AddToRoleAsync(manager, role.Name);
                            }
                        }
                        else
                        {
                            await _userManager.AddToRoleAsync(manager, role.Name);
                        }

                        return(RedirectToLocal(returnUrl));
                    }
                    AddErrors(result);
                }
                AddErrors("Registration failed on the Snack side");
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }