示例#1
0
        public async Task <ActionResult> Create(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                if (User.IsInRole("Admin"))
                {
                    var result = await UserManager.CreateAsync(user, model.Password);

                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        UserManager.AddToRole(user.Id, "Worker");

                        // Aby uzyskać więcej informacji o sposobie włączania potwierdzania konta i resetowaniu hasła, odwiedź stronę https://go.microsoft.com/fwlink/?LinkID=320771
                        // Wyślij wiadomość e-mail z tym łączem
                        // 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, "Potwierdź konto", "Potwierdź konto, klikając <a href=\"" + callbackUrl + "\">tutaj</a>");
                        Profile profile = new Profile
                        {
                            Email     = model.Email,
                            FirstName = model.FirstName,
                            LastName  = model.LastName,
                            PESEL     = model.PESEL,
                        };

                        db.Profiles.Add(profile);
                        db.SaveChanges();

                        SendMail(profile.Email, "Rejestracja", "Witamy na pokładzie ");

                        return(RedirectToAction("Index", "BankAccounts"));
                    }
                    AddErrors(result);
                }
                else if (User.IsInRole("Worker"))
                {
                    if (db.Profiles.Any(p => p.PESEL == model.PESEL))
                    {
                        ModelState.AddModelError("PESEL", "Widnieje już taki numer PESEL w bazie");
                    }

                    if (db.Profiles.Any(p => p.Email == model.Email))
                    {
                        ModelState.AddModelError("Email", "Adres email jest już zajęty");
                    }

                    var result = await UserManager.CreateAsync(user, model.Password);

                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        UserManager.AddToRole(user.Id, "User");

                        // Aby uzyskać więcej informacji o sposobie włączania potwierdzania konta i resetowaniu hasła, odwiedź stronę https://go.microsoft.com/fwlink/?LinkID=320771
                        // Wyślij wiadomość e-mail z tym łączem
                        // 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, "Potwierdź konto", "Potwierdź konto, klikając <a href=\"" + callbackUrl + "\">tutaj</a>");
                        Profile profile = new Profile
                        {
                            Email        = model.Email,
                            FirstName    = model.FirstName,
                            LastName     = model.LastName,
                            PESEL        = model.PESEL,
                            BankAccounts = new List <BankAccount>()
                        };

                        BankAccount bankAccount = new BankAccount()
                        {
                            Balance           = 0.0m,
                            AvailableFounds   = 0.0m,
                            Lock              = 0.0m,
                            BankAccountNumber = BankAccountsController.NewBankAcocuntNumber(),
                            CreationDate      = DateTime.Today,
                            BankAccountTypeID = model.BankAccountTypeID,
                            CurrencyID        = db.Currencies.Single(c => c.Code == "PLN").ID
                        };

                        db.BankAccounts.Add(bankAccount);
                        profile.BankAccounts.Add(bankAccount);
                        db.Profiles.Add(profile);
                        db.SaveChanges();

                        SendMail(profile.Email, "Rejestracja", "Witamy w naszym banku ");

                        return(RedirectToAction("Index", "Home"));
                    }
                    AddErrors(result);
                }
            }

            // Dotarcie do tego miejsca wskazuje, że wystąpił błąd, wyświetl ponownie formularz
            ViewBag.BankAccountTypes = db.BankAccountTypes.ToList();
            return(View(model));
        }
示例#2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (db.Profiles.Any(p => p.PESEL == model.PESEL))
            {
                ModelState.AddModelError("PESEL", "Widnieje już taki numer PESEL w bazie");
            }

            if (db.Profiles.Any(p => p.Email == model.Email))
            {
                ModelState.AddModelError("Email", "Adres email jest już zajęty");
            }

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    UserManager.AddToRole(user.Id, "User");

                    Profile profile = new Profile
                    {
                        Email        = model.Email,
                        FirstName    = model.FirstName,
                        LastName     = model.LastName,
                        PESEL        = model.PESEL,
                        BankAccounts = new List <BankAccount>()
                    };

                    BankAccount bankAccount = new BankAccount()
                    {
                        Balance           = 0.0m,
                        AvailableFounds   = 0.0m,
                        Lock              = 0.0m,
                        BankAccountNumber = BankAccountsController.NewBankAcocuntNumber(),
                        CreationDate      = DateTime.Today,
                        BankAccountTypeID = model.BankAccountTypeID,
                        CurrencyID        = db.Currencies.Single(c => c.Code == "PLN").ID
                    };

                    PaymentCard paymentCard = new PaymentCard
                    {
                        PaymentCardNumber = PaymentCardsController.NewPaymentCardNumber(),
                        Code       = new Random().Next(0, 9999).ToString("D4"),
                        Blocked    = false,
                        SecureCard = true,
                    };

                    db.BankAccounts.Add(bankAccount);
                    profile.BankAccounts.Add(bankAccount);
                    paymentCard.BankAccount = bankAccount;
                    db.Profiles.Add(profile);
                    db.PaymentCards.Add(paymentCard);
                    db.SaveChanges();

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            ViewBag.BankAccountTypes = db.BankAccountTypes.ToList();
            return(View(model));
        }