Ejemplo n.º 1
0
        public IActionResult AdminPanel(AdminPanelPages pageID, int?itemID, string path, IFormFile uploadedFile)
        {
            AccessLevelConfiguration accessLevelConfiguration = HttpContext.RequestServices.GetService <AccessLevelConfiguration>();

            HttpContext.Items["AccessLevelConfiguration"] = accessLevelConfiguration;
            User user = SecurityFunctions.CheckCookies(db, HttpContext);

            if (!SecurityFunctions.HasAccessTo(pageID, user, HttpContext))
            {
                return(RedirectToAction(nameof(AdminPanel)));
            }

            HttpContext.Items["User"]            = user;
            HttpContext.Items["LogLocalization"] = localization;

            switch (pageID)
            {
            case AdminPanelPages.AddProductImage:
                ImagesManagementFunctions.UploadProductImageToServer(db, uploadedFile, itemID, HttpContext, out bool successfullyUploadedImage);
                if (successfullyUploadedImage)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(415));
                }

            case AdminPanelPages.UploadFile:
                FileManagerManagementFunctions.UploadFileToServer(db, path, uploadedFile, HttpContext, out bool successfullyUploadedFile);
                if (successfullyUploadedFile)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(415));
                }

            default:
                return(RedirectToAction(nameof(AdminPanel)));
            }
        }
Ejemplo n.º 2
0
        public IActionResult AdminPanel(Model model)
        {
            // Проверяем кукисы пользователя на наличие информации о предыдущем входе
            // Если кукисы некорректны, либо вышло время возможного бездействия, то
            // отправляем пользователя на логин форму
            User user = SecurityFunctions.CheckCookies(db, HttpContext);

            if (user == null)
            {
                return(LoginForm());
            }
            // Создаем объект для проверки уровней доступа и передаем его в контейнер Items
            // Объект AccessLevelConfiguration используется в методе HasAccessTo(...)
            // Передача в контейнер обязательна, т.к. эти методы вызываются не только внутри контроллера,
            // но и внутри представлений
            AccessLevelConfiguration accessLevelConfiguration = HttpContext.RequestServices.GetService <AccessLevelConfiguration>();

            HttpContext.Items["AccessLevelConfiguration"] = accessLevelConfiguration;
            if (!SecurityFunctions.HasAccessTo(AdminPanelPages.MainPage, user, HttpContext))
            {
                return(LoginForm());
            }
            if (model.PageId.HasValue && model.PageId.Value == AdminPanelPages.UserActions)
            {
                if (!SecurityFunctions.HasAccessTo(AdminPanelPages.GetUserLog, user, HttpContext))
                {
                    model.PageId = AdminPanelPages.MainPage;
                }
            }
            else
            {
                if (!SecurityFunctions.HasAccessTo(model.PageId, user, HttpContext))
                {
                    model.PageId = AdminPanelPages.MainPage;
                }
            }

            HttpContext.Items["User"] = user;

            switch (model.PageId)
            {
            case AdminPanelPages.Pages:
                return(Pages());

            case AdminPanelPages.AddPage:
                return(AddPage());

            case AdminPanelPages.EditPage:
                return(EditPage(model.itemID));

            case AdminPanelPages.Categories:
                return(Categories());

            case AdminPanelPages.AddCategory:
                return(AddCategory());

            case AdminPanelPages.EditCategory:
                return(EditCategory(model.itemID));

            case AdminPanelPages.CategoryProducts:
                return(CategoryProducts(model.itemID));

            case AdminPanelPages.AddProduct:
                return(AddProduct(model.itemID));

            case AdminPanelPages.EditProduct:
                return(EditProduct(model.itemID));

            case AdminPanelPages.ProductImages:
                return(ProductImages(model.itemID));

            case AdminPanelPages.Redirections:
                return(Redirections());

            case AdminPanelPages.Templates:
                return(Templates());

            case AdminPanelPages.AddTemplate:
                return(AddTemplate());

            case AdminPanelPages.EditTemplate:
                return(EditTemplate(model.itemID));

            case AdminPanelPages.Chunks:
                return(Chunks());

            case AdminPanelPages.AddChunk:
                return(AddChunk());

            case AdminPanelPages.EditChunk:
                return(EditChunk(model.itemID));

            case AdminPanelPages.FileManager:
                return(FileManager(model.Path));

            case AdminPanelPages.EditStyle:
                return(EditCssFile(model.Path));

            case AdminPanelPages.EditScript:
                return(EditScriptFile(model.Path));

            case AdminPanelPages.Users:
                return(Users());

            case AdminPanelPages.UserActions:
                return(UserActions(model.itemID));

            case AdminPanelPages.UserTypes:
                return(UserTypes());

            case AdminPanelPages.SynonymsForStrings:
                return(SynonymsForStrings());

            case AdminPanelPages.UserProfile:
                return(UserProfile());

            case AdminPanelPages.Settings:
                return(Settings());

            case AdminPanelPages.GetVisitors:
                return(GetVisitors());

            case AdminPanelPages.GetVisitorActions:
                return(GetVisitorActions(model.itemID));

            case AdminPanelPages.GetUserLog:
                return(GetUserLog(model.itemID, model.CurrentLogDate, HttpContext));

            default:
                return(MainPage());
            }
        }
Ejemplo n.º 3
0
        public IActionResult AdminPanel(AdminPanelPages pageID, int?itemID, int?imageID, string path)
        {
            AccessLevelConfiguration accessLevelConfiguration = HttpContext.RequestServices.GetService <AccessLevelConfiguration>();

            HttpContext.Items["AccessLevelConfiguration"] = accessLevelConfiguration;
            User user = SecurityFunctions.CheckCookies(db, HttpContext);

            if (pageID == AdminPanelPages.Exit)
            {
                DatabaseInteraction.Exit(db, user, HttpContext, out int exitStatusCode);
                return(StatusCode(exitStatusCode));
            }
            if (!SecurityFunctions.HasAccessTo(pageID, user, HttpContext))
            {
                return(RedirectToAction(nameof(AdminPanel)));
            }

            HttpContext.Items["User"]            = user;
            HttpContext.Items["LogLocalization"] = localization;

            switch (pageID)
            {
            case AdminPanelPages.DeletePage:
                DatabaseInteraction.DeletePage(db, PageType.Usual, itemID, HttpContext, out bool pageDeleted);
                if (pageDeleted)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(404));
                }

            case AdminPanelPages.DeleteCategory:
                DatabaseInteraction.DeletePage(db, PageType.Category, itemID, HttpContext, out bool categoryDeleted);
                if (categoryDeleted)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(404));
                }

            case AdminPanelPages.DeleteProduct:
                DatabaseInteraction.DeleteProduct(db, itemID, HttpContext, out bool productDeleted);
                if (productDeleted)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(404));
                }

            case AdminPanelPages.DeleteProductImage:
                ImagesManagementFunctions.DeleteProductImage(db, itemID, imageID, HttpContext, out bool productImageDeleted);
                if (productImageDeleted)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(404));
                }

            case AdminPanelPages.DeleteRedirection:
                DatabaseInteraction.DeleteRedirection(db, itemID, HttpContext, out bool redirectionDeleted);
                if (redirectionDeleted)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(404));
                }

            case AdminPanelPages.DeleteTemplate:
                DatabaseInteraction.DeleteTemplate(db, itemID, HttpContext, out bool templateDeleted);
                if (templateDeleted)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(404));
                }

            case AdminPanelPages.DeleteChunk:
                DatabaseInteraction.DeleteChunk(db, itemID, HttpContext, out bool chunkDeleted);
                if (chunkDeleted)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(404));
                }

            case AdminPanelPages.DeleteFileOrFolder:
                FileManagerManagementFunctions.DeleteFileOrFolder(db, path, HttpContext, out string redirectPath);
                if (redirectPath == null)
                {
                    return(StatusCode(404));
                }
                else
                {
                    string redirectUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{HttpContext.Request.Path}?pageID={(int)AdminPanelPages.FileManager}&path={redirectPath}";
                    HttpContext.Response.Headers.Add("location", redirectUrl);
                    return(StatusCode(200));
                }

            case AdminPanelPages.DeleteUser:
                DatabaseInteraction.DeleteUser(db, itemID, HttpContext, out int userDeletionStatusCode);
                return(StatusCode(userDeletionStatusCode));

            case AdminPanelPages.DeleteUserType:
                DatabaseInteraction.DeleteUserType(db, itemID, HttpContext, out bool userTypeDeleted);
                if (userTypeDeleted)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(404));
                }

            case AdminPanelPages.DeleteSynonymForString:
                DatabaseInteraction.DeleteSynonymForString(db, itemID, HttpContext, out bool synonymForStringDeleted);
                if (synonymForStringDeleted)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(404));
                }

            default:
                return(RedirectToAction(nameof(AdminPanel)));
            }
        }
Ejemplo n.º 4
0
        public IActionResult AdminPanel(Model model, LoginFormModel loginFormModel)
        {
            HttpContext.Items["LogLocalization"] = localization;
            User user = null;

            if (model.PageId == AdminPanelPages.LoginForm)
            {
                if (SecurityFunctions.IsValidLoginFormData(db, loginFormModel, HttpContext, out user))
                {
                    DatabaseInteraction.AddConnectedUser(db, user, HttpContext);
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(401));
                }
            }

            AccessLevelConfiguration accessLevelConfiguration = HttpContext.RequestServices.GetService <AccessLevelConfiguration>();

            HttpContext.Items["AccessLevelConfiguration"] = accessLevelConfiguration;
            user = SecurityFunctions.CheckCookies(db, HttpContext);
            if (!SecurityFunctions.HasAccessTo(model.PageId, user, HttpContext))
            {
                return(RedirectToAction(nameof(AdminPanel)));
            }

            HttpContext.Items["User"] = user;

            switch (model.PageId)
            {
            case AdminPanelPages.AddPage:
                model.PageModel.PageType = PagesManagement.PageType.Usual;
                DatabaseInteraction.AddPage(db, model.PageModel, HttpContext, out bool pageAdded);
                if (pageAdded)
                {
                    string createdPageUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{HttpContext.Request.Path}?pageID={(int)AdminPanelPages.EditPage}&itemID={model.PageModel.ID}";
                    HttpContext.Response.Headers.Add("location", createdPageUrl);
                    return(StatusCode(201));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.EditPage:
                model.PageType = PagesManagement.PageType.Usual;
                DatabaseInteraction.EditPage(db, model, HttpContext, out bool pageEdited);
                if (pageEdited)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.AddCategory:
                model.PageModel.PageType = PagesManagement.PageType.Category;
                DatabaseInteraction.AddPage(db, model.PageModel, HttpContext, out bool categoryAdded);
                if (categoryAdded)
                {
                    string createdCategoryUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{HttpContext.Request.Path}?pageID={(int)AdminPanelPages.EditCategory}&itemID={model.PageModel.ID}";
                    HttpContext.Response.Headers.Add("location", createdCategoryUrl);
                    return(StatusCode(201));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.EditCategory:
                model.PageType = PagesManagement.PageType.Category;
                DatabaseInteraction.EditPage(db, model, HttpContext, out bool categoryEdited);
                if (categoryEdited)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.AddProduct:
                DatabaseInteraction.AddProduct(db, model.PageModel, model.itemID, HttpContext, out bool productAdded);
                if (productAdded)
                {
                    string createdProductUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{HttpContext.Request.Path}?pageID={(int)AdminPanelPages.EditProduct}&itemID={model.PageModel.ID}";
                    HttpContext.Response.Headers.Add("location", createdProductUrl);
                    return(StatusCode(201));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.EditProduct:
                DatabaseInteraction.EditProduct(db, model.PageModel, model.itemID, HttpContext, out bool productEdited);
                if (productEdited)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.AddRedirection:
                DatabaseInteraction.AddRedirection(db, model.RedirectionModel, HttpContext, out bool redirectionAdded);
                if (redirectionAdded)
                {
                    return(StatusCode(201));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.EditRedirection:
                DatabaseInteraction.EditRedirection(db, model.itemID, model.RedirectionModel, HttpContext, out bool redirectionEdited);
                if (redirectionEdited)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.AddTemplate:
                DatabaseInteraction.AddTemplate(db, model.TemplateModel, HttpContext, out bool templateAdded);
                if (templateAdded)
                {
                    string createdTemplateUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{HttpContext.Request.Path}?pageID={(int)AdminPanelPages.EditTemplate}&itemID={model.TemplateModel.ID}";
                    HttpContext.Response.Headers.Add("location", createdTemplateUrl);
                    return(StatusCode(201));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.EditTemplate:
                DatabaseInteraction.EditTemplate(db, model.itemID, model.TemplateModel, HttpContext, out bool templateEdited);
                if (templateEdited)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.AddChunk:
                DatabaseInteraction.AddChunk(db, model.TemplateModel, HttpContext, out bool chunkAdded);
                if (chunkAdded)
                {
                    string createdChunkUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{HttpContext.Request.Path}?pageID={(int)AdminPanelPages.EditChunk}&itemID={model.TemplateModel.ID}";
                    HttpContext.Response.Headers.Add("location", createdChunkUrl);
                    return(StatusCode(201));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.EditChunk:
                DatabaseInteraction.EditChunk(db, model.itemID, model.TemplateModel, HttpContext, out bool chunkEdited);
                if (chunkEdited)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.CreateFolder:
                FileManagerManagementFunctions.CreateFolder(db, model.Path, model.Name, HttpContext, out bool folderCreated);
                if (folderCreated)
                {
                    return(StatusCode(201));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.CreateStyle:
                FileManagerManagementFunctions.CreateCssFile(db, model.Path, model.Name, HttpContext, out bool styleFileCreated);
                if (styleFileCreated)
                {
                    return(StatusCode(201));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.CreateScript:
                FileManagerManagementFunctions.CreateScriptFile(db, model.Path, model.Name, HttpContext, out bool scriptFileCreated);
                if (scriptFileCreated)
                {
                    return(StatusCode(201));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.EditStyle:
                FileManagerManagementFunctions.EditCssFile(db, model.Path, model.StyleModel, HttpContext, out string editedStylePath, out bool cssFileEdited);
                if (cssFileEdited)
                {
                    string editedCssFileUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{HttpContext.Request.Path}?pageID={(int)AdminPanelPages.EditStyle}&path={editedStylePath}";
                    HttpContext.Response.Headers.Add("location", editedCssFileUrl);
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.EditScript:
                FileManagerManagementFunctions.EditScriptFile(db, model.Path, model.StyleModel, HttpContext, out string editedScriptPath, out bool scriptFileEdited);
                if (scriptFileEdited)
                {
                    string editedScriptFileUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{HttpContext.Request.Path}?pageID={(int)AdminPanelPages.EditScript}&path={editedScriptPath}";
                    HttpContext.Response.Headers.Add("location", editedScriptFileUrl);
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.AddUser:
                DatabaseInteraction.AddUser(db, model.UserModel, HttpContext, out int userAdditionStatusCode);
                return(StatusCode(userAdditionStatusCode));

            case AdminPanelPages.EditUser:
                DatabaseInteraction.EditUser(db, model.itemID, model.UserModel, HttpContext, out int userEditionStatusCode);
                return(StatusCode(userEditionStatusCode));

            case AdminPanelPages.AddUserType:
                DatabaseInteraction.AddUserType(db, model.UserTypeModel, HttpContext, out bool userTypeAdded);
                if (userTypeAdded)
                {
                    return(StatusCode(201));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.EditUserType:
                DatabaseInteraction.EditUserType(db, model.itemID, model.UserTypeModel, HttpContext, out bool userTypeEdited);
                if (userTypeEdited)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.AddSynonymForString:
                DatabaseInteraction.AddSynonymForString(db, model.SynonymForStringModel, HttpContext, out bool synonymForStringAdded);
                if (synonymForStringAdded)
                {
                    return(StatusCode(201));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.EditSynonymForString:
                DatabaseInteraction.EditSynonymForString(db, model.itemID, model.SynonymForStringModel, HttpContext, out bool synonymForStringEdited);
                if (synonymForStringEdited)
                {
                    return(StatusCode(200));
                }
                else
                {
                    return(StatusCode(422));
                }

            case AdminPanelPages.EditUserData:
                DatabaseInteraction.EditUserData(db, model.UserModel, HttpContext, out int userDataEditionStatusCode);
                return(StatusCode(userDataEditionStatusCode));

            case AdminPanelPages.EditSettings:
                return(EditSettings(db, model.SettingsModel, HttpContext));

            default:
                return(RedirectToAction(nameof(AdminPanel)));
            }
        }