Example #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            RejupoUser user = await _userManager.FindByIdAsync(RejupoUserID);

            if (Input.IsAdmin)
            {
                await _userManager.AddToRoleAsync(user, SD.Admin);
            }
            else
            {
                await _userManager.RemoveFromRoleAsync(user, SD.Admin);
            }
            if (Input.IsUser)
            {
                await _userManager.AddToRoleAsync(user, SD.User);
            }
            else
            {
                await _userManager.RemoveFromRoleAsync(user, SD.User);
            }
            if (Input.IsSuperAdmin)
            {
                await _userManager.AddToRoleAsync(user, SD.SuperAdmin);
            }
            else
            {
                await _userManager.RemoveFromRoleAsync(user, SD.SuperAdmin);
            }

            await LogWriter.WritetoDbAsync(_context, HttpContext.User.Identity.Name,
                                           $"Zmiana ról użytkownika {user.UserName} {SD.SuperAdmin} {Input.IsSuperAdmin} {SD.Admin} {Input.IsAdmin} {SD.User} {Input.IsUser}");

            //_context.Attach(RejupoUser).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RejupoUserExists(RejupoUser.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Example #2
0
        private async Task LoadSharedKeyAndQrCodeUriAsync(RejupoUser user)
        {
            // Load the authenticator key & QR code URI to display on the form
            var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);

            if (string.IsNullOrEmpty(unformattedKey))
            {
                await _userManager.ResetAuthenticatorKeyAsync(user);

                unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
            }

            SharedKey = FormatKey(unformattedKey);

            var email = await _userManager.GetEmailAsync(user);

            AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey);
        }
Example #3
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information during confirmation.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            if (ModelState.IsValid)
            {
                var user = new RejupoUser {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

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

                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            LoginProvider = info.LoginProvider;
            ReturnUrl     = returnUrl;
            return(Page());
        }
Example #4
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new RejupoUser
                {
                    UserName    = Input.Email,
                    Email       = Input.Email,
                    FirstName   = Input.FirstName,
                    LastName    = Input.LastName,
                    Division    = Input.Division,
                    PhoneNumber = Input.Phone
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    // Tworzenie (w razie potrzeby) wpisów do tabeli aspnetroles
                    if (!await _roleManager.RoleExistsAsync(SD.SuperAdmin))
                    {
                        await _roleManager.CreateAsync(new IdentityRole(SD.SuperAdmin));
                    }

                    if (!await _roleManager.RoleExistsAsync(SD.Admin))
                    {
                        await _roleManager.CreateAsync(new IdentityRole(SD.Admin));
                    }

                    if (!await _roleManager.RoleExistsAsync(SD.User))
                    {
                        await _roleManager.CreateAsync(new IdentityRole(SD.User));
                    }


                    // nadanie pierwszemu rejestrującemu się użytkownikowi roli superadmin
                    if (_db.Users.Count() == 1)
                    {
                        await _userManager.AddToRoleAsync(user, SD.SuperAdmin);
                    }

                    else
                    {
                        await _userManager.AddToRoleAsync(user, SD.User);
                    }


                    _logger.LogInformation("User created a new account with password.");
                    await LogWriter.WritetoDbAsync(_db, user.UserName, "Rejestracja użytkownika.");

                    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());
        }