public async Task <IActionResult> DodajPostojecegUcenika(AplicationUser model)
        {
            if (model.Email == null)
            {
                ModelState.AddModelError("Email", "Email je obavezno polje");
            }
            else
            {
                var user = await userManager.FindByEmailAsync(model.Email);

                if (user == null)
                {
                    ModelState.AddModelError("Email", "Niste unijeli ispravan email postojećeg učenika.");
                }
                else
                {
                    var vjerouciteljUserName = HttpContext.User.Identity.Name;
                    var vjeroucitelj         = await userManager.FindByEmailAsync(vjerouciteljUserName);

                    var vjerouciteljId     = vjeroucitelj.Id;
                    var vjerouciteljUcenik = new VjerouciteljUcenik
                    {
                        VjerouciteljId = vjerouciteljId,
                        UcenikId       = user.Id,
                        UserName       = user.UserName
                    };
                    _context.Add(vjerouciteljUcenik);
                    _context.SaveChanges();
                    return(RedirectToAction("ListUsers"));
                }
            }



            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new AplicationUser {
                    UserName = Input.Email, Email = Input.Email, NazivMjesta = Input.Mjesto
                };
                user.AplicationUserId = user.Id;
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    if (user.Email == "*****@*****.**")
                    {
                        if (!await _userManager.IsInRoleAsync(user, "Admin"))
                        {
                            IdentityRole identityRole = new IdentityRole
                            {
                                Name = "Admin"
                            };
                            IdentityResult results = await roleManager.CreateAsync(identityRole);

                            if (results.Succeeded)
                            {
                                {
                                    await _userManager.AddToRoleAsync(user, "Admin");

                                    return(RedirectToAction("ListRole", "Administration"));
                                }
                            }
                        }
                    }
                    else
                    {
                        var username     = HttpContext.User.Identity.Name;
                        var vjeroucitelj = await _userManager.FindByNameAsync(username);

                        if (await _userManager.IsInRoleAsync(vjeroucitelj, "Vjeroucitelj"))
                        {
                            var ucenik = await _userManager.FindByEmailAsync(Input.Email);

                            var vjerouciteljUcenik = new VjerouciteljUcenik
                            {
                                VjerouciteljId = vjeroucitelj.Id,
                                UcenikId       = ucenik.Id,
                                UserName       = ucenik.Email
                            };

                            _context.Add(vjerouciteljUcenik);
                            await _context.SaveChangesAsync();
                        }
                    }



                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }));
                    }
                    else
                    {
                        if (_signInManager.IsSignedIn(User) && User.IsInRole("Admin"))
                        {
                            return(RedirectToAction("ListRole", "Administration"));
                        }
                        else
                        if (_signInManager.IsSignedIn(User) && User.IsInRole("Vjeroucitelj"))
                        {
                            return(RedirectToAction("ListUsers", "Vjeroucitelj"));
                        }
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }