Esempio n. 1
0
        private async Task CreateUserRoles(IServiceProvider serviceProvider)
        {
            RoleManager <IdentityRole> roleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >();
            StartSpelerContext         connection  = serviceProvider.GetRequiredService <StartSpelerContext>();

            IdentityResult adminResult, spelerResult;

            bool adminCheck, spelerCheck;


            //toekennen admin rol naar de hoofdgebruiker
            // IdentityUser user = connection.Users.FirstOrDefault(u => u.Email == "*****@*****.**");
            List <CustomUser> Admininstrators = connection.Users.Include(x => x.Persoon).Where(x => x.Persoon.IsAdmin == true || x.Persoon.Email == "*****@*****.**").ToList();

            if (Admininstrators.Count <= 0)
            {  // admin rol toevoegen
                adminCheck = await roleManager.RoleExistsAsync("Admin");

                if (!adminCheck)
                {
                    //rol creëren en seeden naar de database
                    adminResult = await roleManager.CreateAsync(new IdentityRole("Admin"));
                }

                foreach (CustomUser user in Admininstrators)
                {
                    DbSet <IdentityUserRole <string> > roles = connection.UserRoles;
                    IdentityRole adminrole = connection.Roles.FirstOrDefault(r => r.Name == "Admin");
                    if (adminrole != null)
                    {
                        if (!roles.Any(ur => ur.UserId == user.Id && ur.RoleId == adminrole.Id))
                        {
                            roles.Add(new IdentityUserRole <string>()
                            {
                                UserId = user.Id, RoleId = adminrole.Id
                            });
                            connection.SaveChanges();
                        }
                    }
                }
            }
            spelerCheck = await roleManager.RoleExistsAsync("Speler");

            if (!spelerCheck)
            {
                //rol creëren en seeden naar de database
                spelerResult = await roleManager.CreateAsync(new IdentityRole("Speler"));

                connection.SaveChanges();
            }
        }
Esempio n. 2
0
        public ActionResult SpelerIsAdmin(EditPersoonViewModel viewModel, int PersoonID)
        {
            viewModel.Persoon         = _context.Personen.Include(x => x.CustomUser).FirstOrDefault(x => x.Persoon_ID == PersoonID);
            viewModel.Persoon.IsAdmin = !viewModel.Persoon.IsAdmin;
            if (viewModel.Persoon.IsAdmin == true)
            {
                viewModel.Persoon.RolDuiding = "Administrator";

                DbSet <IdentityUserRole <string> > roles = _context.UserRoles;
                IdentityRole userrole = _context.Roles.FirstOrDefault(r => r.Name == "Speler");
                if (userrole != null)
                {
                    if (!roles.Any(ur => ur.UserId == viewModel.Persoon.CustomUser.Id && ur.RoleId == userrole.Id))
                    {
                        roles.Add(new IdentityUserRole <string>()
                        {
                            UserId = viewModel.Persoon.CustomUser.Id, RoleId = userrole.Id
                        });
                        _context.SaveChanges();
                    }
                }
            }
            if (viewModel.Persoon.IsAdmin == false)
            {
                viewModel.Persoon.RolDuiding = "Speler";
                DbSet <IdentityUserRole <string> > roles = _context.UserRoles;
                IdentityRole userrole = _context.Roles.FirstOrDefault(r => r.Name == "Speler");
                if (userrole != null)
                {
                    if (!roles.Any(ur => ur.UserId == viewModel.Persoon.CustomUser.Id && ur.RoleId == userrole.Id))
                    {
                        roles.Add(new IdentityUserRole <string>()
                        {
                            UserId = viewModel.Persoon.CustomUser.Id, RoleId = userrole.Id
                        });
                        _context.SaveChanges();
                    }
                }
            }
            return(View("Edit", viewModel));
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new CustomUser {
                    Persoon = new Persoon
                    {
                        Voornaam        = Input.Voornaam,
                        Achternaam      = Input.Achternaam,
                        Geboortedatum   = Input.Geboortedatum,
                        Username        = Input.Username,
                        Email           = Input.Email,
                        Wachtwoord      = Input.Password,
                        AangemaaktDatum = DateTime.Now.Date,
                        IsActief        = true,
                        IsAdmin         = false,
                        Drankkaarten    = new List <Drankkaart>()
                    },
                    UserName = Input.Username,
                    Email    = Input.Email
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                user.Persoon.UserID = user.Id;

                _context.SaveChanges();
                if (user.Persoon.IsAdmin == true)
                {
                    DbSet <IdentityUserRole <string> > roles = _context.UserRoles;
                    IdentityRole userrole = _context.Roles.FirstOrDefault(r => r.Name == "Admin");
                    if (userrole != null)
                    {
                        if (!roles.Any(ur => ur.UserId == user.Id && ur.RoleId == userrole.Id))
                        {
                            roles.Add(new IdentityUserRole <string>()
                            {
                                UserId = user.Id, RoleId = userrole.Id
                            });
                            _context.SaveChanges();
                        }
                    }
                }
                else
                {
                    DbSet <IdentityUserRole <string> > roles = _context.UserRoles;
                    IdentityRole userrole = _context.Roles.FirstOrDefault(r => r.Name == "Speler");
                    if (userrole != null)
                    {
                        if (!roles.Any(ur => ur.UserId == user.Id && ur.RoleId == userrole.Id))
                        {
                            roles.Add(new IdentityUserRole <string>()
                            {
                                UserId = user.Id, RoleId = userrole.Id
                            });
                            _context.SaveChanges();
                        }
                    }
                }



                if (result.Succeeded)
                {
                    _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
                    {
                        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());
        }