public async Task <IActionResult> GenericAttributeInsert(GenericAttributeModel model, string entityName, int entityId)
        {
            model.Key   = model.Key.TrimSafe();
            model.Value = model.Value.TrimSafe() ?? string.Empty;

            if (ModelState.IsValid)
            {
                var storeId = Services.StoreContext.CurrentStore.Id;
                var(readPermission, updatePermission) = GetGenericAttributesInfos(entityName);
                if (updatePermission.HasValue() && !Services.Permissions.Authorize(updatePermission))
                {
                    NotifyError(await Services.Permissions.GetUnauthorizedMessageAsync(updatePermission));
                    return(Json(new { success = false }));
                }
                else
                {
                    var attr = await _db.GenericAttributes
                               .Where(x => x.EntityId == entityId && x.KeyGroup == entityName && x.Key == model.Key && x.StoreId == storeId)
                               .FirstOrDefaultAsync();

                    if (attr == null)
                    {
                        _db.GenericAttributes.Add(new GenericAttribute
                        {
                            StoreId  = storeId,
                            KeyGroup = entityName,
                            EntityId = entityId,
                            Key      = model.Key,
                            Value    = model.Value
                        });
                        await _db.SaveChangesAsync();

                        return(Json(new { success = true }));
                    }
                    else
                    {
                        NotifyWarning(T("Admin.Common.GenericAttributes.NameAlreadyExists", model.Key));
                        return(Json(new { success = false }));
                    }
                }
            }
            else
            {
                var modelStateErrorMessages = ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage);
                NotifyError(modelStateErrorMessages.FirstOrDefault());
                return(Json(new { success = false }));
            }
        }
Exemplo n.º 2
0
        public ActionResult GenericAttributeUpdate(GenericAttributeModel model, GridCommand command)
        {
            model.Key   = model.Key.TrimSafe();
            model.Value = model.Value.TrimSafe();

            if (ModelState.IsValid)
            {
                var storeId = _services.StoreContext.CurrentStore.Id;
                var infos   = GetGenericAttributesInfos(model.EntityName);

                if (infos.UpdatePermission.HasValue() && !Services.Permissions.Authorize(infos.UpdatePermission))
                {
                    NotifyError(Services.Permissions.GetUnauthorizedMessage(infos.UpdatePermission));
                }
                else
                {
                    var attr = _genericAttributeService.GetAttributeById(model.Id);

                    // If the key changed, ensure it isn't being used by another attribute.
                    if (!attr.Key.IsCaseInsensitiveEqual(model.Key))
                    {
                        var attr2 = _genericAttributeService.GetAttributesForEntity(model.EntityId, model.EntityName)
                                    .Where(x => x.StoreId == storeId && x.Key.Equals(model.Key, StringComparison.InvariantCultureIgnoreCase))
                                    .FirstOrDefault();

                        if (attr2 != null && attr2.Id != attr.Id)
                        {
                            NotifyWarning(T("Admin.Common.GenericAttributes.NameAlreadyExists", model.Key));
                            return(GenericAttributesSelect(model.EntityName, model.EntityId, command));
                        }
                    }

                    attr.Key   = model.Key;
                    attr.Value = model.Value;

                    _genericAttributeService.UpdateAttribute(attr);
                }
            }
            else
            {
                var modelStateErrorMessages = ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage);
                NotifyError(modelStateErrorMessages.FirstOrDefault());
            }

            return(GenericAttributesSelect(model.EntityName, model.EntityId, command));
        }
Exemplo n.º 3
0
        public ActionResult GenericAttributeAdd(GenericAttributeModel model, GridCommand command)
        {
            model.Key   = model.Key.TrimSafe();
            model.Value = model.Value.TrimSafe() ?? string.Empty;

            if (ModelState.IsValid)
            {
                var storeId = _services.StoreContext.CurrentStore.Id;
                var infos   = GetGenericAttributesInfos(model.EntityName);
                if (infos.UpdatePermission.HasValue() && !Services.Permissions.Authorize(infos.UpdatePermission))
                {
                    NotifyError(Services.Permissions.GetUnauthorizedMessage(infos.UpdatePermission));
                }
                else
                {
                    var attr = _genericAttributeService.GetAttribute <string>(model.EntityName, model.EntityId, model.Key, storeId);
                    if (attr == null)
                    {
                        _genericAttributeService.InsertAttribute(new GenericAttribute
                        {
                            StoreId  = storeId,
                            KeyGroup = model.EntityName,
                            EntityId = model.EntityId,
                            Key      = model.Key,
                            Value    = model.Value
                        });
                    }
                    else
                    {
                        NotifyWarning(T("Admin.Common.GenericAttributes.NameAlreadyExists", model.Key));
                    }
                }
            }
            else
            {
                var modelStateErrorMessages = ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage);
                return(Content(modelStateErrorMessages.FirstOrDefault()));
            }

            return(GenericAttributesSelect(model.EntityName, model.EntityId, command));
        }
Exemplo n.º 4
0
        public ActionResult GenericAttributeUpdate(GenericAttributeModel model, GridCommand command)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel))
            {
                return(AccessDeniedView());
            }

            model.Key   = model.Key.TrimSafe();
            model.Value = model.Value.TrimSafe();

            if (!ModelState.IsValid)
            {
                // display the first model error
                var modelStateErrorMessages = this.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage);
                return(Content(modelStateErrorMessages.FirstOrDefault()));
            }

            var storeId = _storeContext.CurrentStore.Id;

            var attr = _genericAttributeService.GetAttributeById(model.Id);

            // if the key changed, ensure it isn't being used by another attribute
            if (!attr.Key.IsCaseInsensitiveEqual(model.Key))
            {
                var attr2 = _genericAttributeService.GetAttributesForEntity(model.EntityId, model.EntityName)
                            .Where(x => x.StoreId == storeId && x.Key.Equals(model.Key, StringComparison.InvariantCultureIgnoreCase))
                            .FirstOrDefault();
                if (attr2 != null && attr2.Id != attr.Id)
                {
                    return(Content(string.Format(_localizationService.GetResource("Admin.Common.GenericAttributes.NameAlreadyExists"), model.Key)));
                }
            }

            attr.Key   = model.Key;
            attr.Value = model.Value;

            _genericAttributeService.UpdateAttribute(attr);

            return(GenericAttributesSelect(model.EntityName, model.EntityId, command));
        }
Exemplo n.º 5
0
        public ActionResult GenericAttributeAdd(GenericAttributeModel model, GridCommand command)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel))
            {
                return(AccessDeniedView());
            }

            model.Key   = model.Key.TrimSafe();
            model.Value = model.Value.TrimSafe();

            if (!ModelState.IsValid)
            {
                // display the first model error
                var modelStateErrorMessages = this.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage);
                return(Content(modelStateErrorMessages.FirstOrDefault()));
            }

            var storeId = _storeContext.CurrentStore.Id;

            var attr = _genericAttributeService.GetAttribute <string>(model.EntityName, model.EntityId, model.Key, storeId);

            if (attr == null)
            {
                var ga = new GenericAttribute
                {
                    StoreId  = storeId,
                    KeyGroup = model.EntityName,
                    EntityId = model.EntityId,
                    Key      = model.Key,
                    Value    = model.Value
                };
                _genericAttributeService.InsertAttribute(ga);
            }
            else
            {
                return(Content(string.Format(_localizationService.GetResource("Admin.Common.GenericAttributes.NameAlreadyExists"), model.Key)));
            }

            return(GenericAttributesSelect(model.EntityName, model.EntityId, command));
        }