Пример #1
0
        public async Task <JsonResult> OnPostCreateOrEditAsync(string id, RolesViewModel role)
        {
            if (ModelState.IsValid && role.Name != "SuperAdmin" && role.Name != "Basic")
            {
                if (string.IsNullOrEmpty(id))
                {
                    await _roleManager.CreateAsync(new IdentityRole(role.Name));

                    Notify.AddSuccessToastMessage("Role Created");
                }
                else
                {
                    var existingRole = await _roleManager.FindByIdAsync(id);

                    existingRole.Name           = role.Name;
                    existingRole.NormalizedName = role.Name.ToUpper();
                    await _roleManager.UpdateAsync(existingRole);

                    Notify.AddSuccessToastMessage("Role Updated");
                }

                var roles = await _roleManager.Roles.ToListAsync();

                Roles = Mapper.Map <IEnumerable <RolesViewModel> >(roles);
                var html = await Renderer.RenderPartialToStringAsync("_ViewAllRoles", Roles);

                return(new JsonResult(new { isValid = true, html = html }));
            }
            else
            {
                var html = await Renderer.RenderPartialToStringAsync <RolesViewModel>("_CreateOrEdit", role);

                return(new JsonResult(new { isValid = false, html = html }));
            }
        }
Пример #2
0
        public async Task <IActionResult> OnPostUpdateAsync()
        {
            var role = await _roleManager.FindByIdAsync(RoleId);

            //Remove all Claims First
            var claims = await _roleManager.GetClaimsAsync(role);

            foreach (var claim in claims)
            {
                await _roleManager.RemoveClaimAsync(role, claim);
            }
            var selectedClaims = RoleClaims.Where(a => a.Selected).ToList();

            foreach (var claim in selectedClaims)
            {
                await _roleManager.AddPermissionClaim(role, claim.Value);
            }
            var user = await _userManager.GetUserAsync(User);

            await _signInManager.RefreshSignInAsync(user);

            Notify.AddSuccessToastMessage($"Updated Claims / Permissions for Role '{role.Name}'");
            return(RedirectToPage("/roles", new { area = "Admin" }));
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var userName = Input.Email;
                if (IsValidEmail(Input.Email))
                {
                    var userCheck = await _userManager.FindByEmailAsync(Input.Email);

                    if (userCheck != null)
                    {
                        userName = userCheck.UserName;
                    }
                }
                var user = await _userManager.FindByNameAsync(userName);

                if (user != null)
                {
                    if (!user.IsActive)
                    {
                        return(RedirectToPage("./Deactivated"));
                    }
                    else if (!user.EmailConfirmed)
                    {
                        Notify.AddErrorToastMessage("Email Not Confirmed.");
                        ModelState.AddModelError(string.Empty, "Email Not Confirmed.");
                        return(Page());
                    }
                    else
                    {
                        var result = await _signInManager.PasswordSignInAsync(userName, Input.Password, Input.RememberMe, lockoutOnFailure : false);

                        if (result.Succeeded)
                        {
                            _logger.LogInformation("User logged in.");
                            Notify.AddSuccessToastMessage($"Logged in as {userName}.");
                            return(LocalRedirect(returnUrl));
                        }
                        if (result.RequiresTwoFactor)
                        {
                            return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe }));
                        }
                        if (result.IsLockedOut)
                        {
                            Notify.AddWarningToastMessage("User account locked out.");
                            _logger.LogWarning("User account locked out.");
                            return(RedirectToPage("./Lockout"));
                        }
                        else
                        {
                            Notify.AddErrorToastMessage("Invalid login attempt.");
                            ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                            return(Page());
                        }
                    }
                }
                else
                {
                    Notify.AddErrorToastMessage("Email / Username Not Found.");
                    ModelState.AddModelError(string.Empty, "Email / Username Not Found.");
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
Пример #4
0
        public async Task <JsonResult> OnPostCreateOrEditAsync(int id, ProductViewModel product)
        {
            if (ModelState.IsValid)
            {
                if (Request.Form.Files.Count > 0)
                {
                    IFormFile file = Request.Form.Files.FirstOrDefault();
                    using (var dataStream = new MemoryStream())
                    {
                        await file.CopyToAsync(dataStream);

                        product.Image = dataStream.ToArray();
                    }
                }

                if (id == 0)
                {
                    User.HasRequiredClaims(new List <string> {
                        MasterPermissions.Create, ProductPermissions.Create
                    });
                    var createProductCommand = Mapper.Map <CreateProductCommand>(product);
                    var result = await Mediator.Send(createProductCommand);

                    if (result.Succeeded)
                    {
                        Notify.AddSuccessToastMessage($"Product Created.");
                    }
                }
                else
                {
                    User.HasRequiredClaims(new List <string> {
                        MasterPermissions.Update, ProductPermissions.Update
                    });
                    if (product.Image == null)
                    {
                        var oldProduct = await Mediator.Send(new GetProductByIdQuery { Id = id });

                        product.Image = oldProduct.Data.Image;
                    }
                    var updateProductCommand = Mapper.Map <UpdateProductCommand>(product);
                    var result = await Mediator.Send(updateProductCommand);

                    if (result.Succeeded)
                    {
                        Notify.AddSuccessToastMessage($"Product Updated.");
                    }
                }
                var response = await Mediator.Send(new GetAllProductsQuery());

                if (response.Succeeded)
                {
                    var data = response.Data;
                    Products = Mapper.Map <IEnumerable <ProductViewModel> >(data);
                }
                var html = await Renderer.RenderPartialToStringAsync("_ViewAll", Products);

                return(new JsonResult(new { isValid = true, html = html }));
            }
            else
            {
                var html = await Renderer.RenderPartialToStringAsync <ProductViewModel>("_CreateOrEdit", product);

                return(new JsonResult(new { isValid = false, html = html }));
            }
        }
        public async Task <JsonResult> OnPostCreateOrEditAsync(int id, ProductCategoryViewModel product)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (id == 0)
                    {
                        User.HasRequiredClaims(new List <string> {
                            MasterPermissions.Create, ProductCategoryPermissions.Create
                        });
                        var createProductCommand = Mapper.Map <CreateProductCategoryCommand>(product);
                        var result = await Mediator.Send(createProductCommand);

                        if (result.Succeeded)
                        {
                            Notify.AddSuccessToastMessage($"Product Created.");
                        }
                    }
                    else
                    {
                        User.HasRequiredClaims(new List <string> {
                            MasterPermissions.Update, ProductCategoryPermissions.Update
                        });
                        var updateProductCommand = Mapper.Map <UpdateProductCategoryCommand>(product);

                        try
                        {
                            var result = await Mediator.Send(updateProductCommand);

                            if (result.Succeeded)
                            {
                                Notify.AddSuccessToastMessage($"Product Updated.");
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.LogInformation(ex.Message);
                            throw;
                        }
                    }
                    var response = await Mediator.Send(new GetAllProductCategoriesQuery());

                    if (response.Succeeded)
                    {
                        var data = response.Data;
                        ProductCategories = Mapper.Map <IEnumerable <ProductCategoryViewModel> >(data);
                    }
                    var html = await Renderer.RenderPartialToStringAsync("_ViewAll", ProductCategories);

                    return(new JsonResult(new { isValid = true, html = html }));
                }
                catch (Exception ex)
                {
                    Notify.AddErrorToastMessage(ex.Message);
                    throw;
                }
            }
            else
            {
                var html = await Renderer.RenderPartialToStringAsync <ProductCategoryViewModel>("_CreateOrEdit", product);

                return(new JsonResult(new { isValid = false, html = html }));
            }
        }
Пример #6
0
        public async Task <JsonResult> OnPostCreateOrEditAsync(int id, ProductViewModel product)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (Request.Form.Files.Count > 0)
                    {
                        IFormFile file = Request.Form.Files.FirstOrDefault();
                        product.Image = "";// file.OptimizeImageSize(700, 700);
                    }

                    if (id == 0)
                    {
                        User.HasRequiredClaims(new List <string> {
                            MasterPermissions.Create, ProductPermissions.Create
                        });
                        var createProductCommand = Mapper.Map <CreateProductCommand>(product);
                        var result = await Mediator.Send(createProductCommand);

                        if (result.Succeeded)
                        {
                            Notify.AddSuccessToastMessage($"Product Created.");
                        }
                    }
                    else
                    {
                        User.HasRequiredClaims(new List <string> {
                            MasterPermissions.Update, ProductPermissions.Update
                        });
                        if (product.Image == null)
                        {
                            var oldProduct = await Mediator.Send(new GetProductByIdQuery { Id = id });

                            product.Image = oldProduct.Data.Image;
                        }
                        var updateProductCommand = Mapper.Map <UpdateProductCommand>(product);

                        try
                        {
                            var result = await Mediator.Send(updateProductCommand);

                            if (result.Succeeded)
                            {
                                Notify.AddSuccessToastMessage($"Product Updated.");
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.LogInformation(ex.Message);
                            throw;
                        }
                    }
                    var response = await Mediator.Send(new GetAllProductsQuery());

                    if (response.Succeeded)
                    {
                        var data = response.Data;
                        Products = Mapper.Map <IEnumerable <ProductViewModel> >(data);
                    }
                    var html = await Renderer.RenderPartialToStringAsync("_ViewAll", Products);

                    return(new JsonResult(new { isValid = true, html = html }));
                }
                catch (Exception ex)
                {
                    Notify.AddErrorToastMessage(ex.Message);
                    throw;
                }
            }
            else
            {
                var html = await Renderer.RenderPartialToStringAsync <ProductViewModel>("_CreateOrEdit", product);

                return(new JsonResult(new { isValid = false, html = html }));
            }
        }