Example #1
0
        public IActionResult CreateCategory(MenuItem model)
        {
            if (ModelState.IsValid)
            {
                var fileForm = Request.Form.Files.Any() ? Request.Form.Files[0] : null;
                if (fileForm != null)
                {
                    var file = _pictureService.InsertPicture(fileForm);

                    _pictureService.GetPictureUrl(file.Id);

                    model.UrlIcone = _pictureService.GetPictureUrl(file.Id);
                }
                model.ActionName = new TranslitMethods.Translitter().Translit(model.Name, TranslitMethods.TranslitType.Gost)
                                   .Replace(' ', '_');
                _menuService.InsertItem(model);
                _permissionService.InsertPermissionRecord(_securityModelFactory.CreatePermissionRecordForMenu(model));

                var itemViewModel = new MenuItemViewModel()
                {
                    Id       = model.Id,
                    Name     = model.Name,
                    ToolTip  = model.ToolTip,
                    UrlIcon  = model.UrlIcone,
                    ParentId = model.ParentId,
                    Index    = model.Index
                };

                return(Json(JsonConvert.SerializeObject(itemViewModel)));
            }
            return(Json(null));
        }
Example #2
0
        public IActionResult Create([FromForm] MenuItem model)
        {
#if RELEASE
            var user = _userService.GetUserBySystemName(User);
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageMenu, user))
            {
                return(AccessDeniedView());
            }
#endif
            if (ModelState.IsValid)
            {
                var fileForm = Request.Form.Files.Any() ? Request.Form.Files[0] : null;

                if (fileForm != null)
                {
                    var file = _uploadService.InsertPicture(fileForm);
                    _uploadService.GetPictureUrl(file.Id);

                    model.UrlIcone = _uploadService.GetPictureUrl(file.Id);
                }

                var entity = model.Id != null?_menuService.Get(model.Id) : null;

                model.ActionName = new TranslitMethods.Translitter().Translit(model.Name, TranslitMethods.TranslitType.Gost)
                                   .Replace(' ', '_');
                if (entity != null)
                {
                    var actionName = entity.ActionName;//сораняем значение ActionName перед изменением.
                    //вносим в запись измения, соответственно модели
                    entity.Name       = model.Name;
                    entity.IsActive   = model.IsActive;
                    entity.ParentId   = model.ParentId;
                    entity.ToolTip    = model.ToolTip;
                    entity.ActionName = model.ActionName;

                    if (!String.IsNullOrWhiteSpace(model.UrlIcone))
                    {
                        entity.UrlIcone = model.UrlIcone;
                    }

                    try
                    {
                        var permission = _permissionService.GetPermissionRecordBySystemName(actionName);//ищем Permission по старому названию объекта
                        permission.Name       = model.Name;
                        permission.SystemName = model.ActionName;
                        _permissionService.UpdatePermissionRecord(permission);
                        _menuService.UpdateItem(entity);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Не удалось обновить объект прав доступа " + ex.Message);
                        ModelState.AddModelError("", "Не удалось обновить Permission, обратитесь к Администратору для исправления данной проблемы");
                        ViewBag.MenuId = _menuService.GetAll().Select(s => new SelectListItem {
                            Text = s.Name, Value = s.Id
                        }).ToList();
                        return(View(model));
                    }
                }
                else
                {
                    _menuService.InsertItem(model);
                    var permission = _securityModelFactory.CreatePermissionRecordForMenu(model);
                    _permissionService.InsertPermissionRecord(permission);
                }

                return(RedirectToAction("Index", "Home"));
            }
            ViewBag.MenuId = _menuService.GetAll().Select(s => new SelectListItem {
                Text = s.Name, Value = s.Id
            }).ToList();
            return(View(model));
        }