Exemplo n.º 1
0
        public virtual async Task <IEntityEditModel <T> > Detail([FromService] IDatabaseContext database, [FromService] IAuthenticationProvider authenticationProvider, [FromService] IValueProvider valueProvider, [FromOptions] EntityDomainAuthorizeOption authorizeOption)
        {
            object index = valueProvider.GetRequiredValue("id", Metadata.KeyProperty.ClrType);
            var    auth  = authenticationProvider.GetAuthentication();

            if (authorizeOption == null)
            {
                authorizeOption = EntityDomainAuthorizeOption.Detail;
            }
            authorizeOption.Validate(Metadata, auth);
            var context   = database.GetContext <T>();
            var queryable = context.Query();

            foreach (var propertyMetadata in Metadata.Properties.Where(t => t.CustomType == "Entity"))
            {
                queryable = context.Include(queryable, propertyMetadata.ClrName);
            }
            T entity = await context.GetAsync(queryable, index);

            if (entity == null)
            {
                throw new DomainServiceException(new EntityNotFoundException(typeof(T), index));
            }
            var model = new EntityEditModel <T>(entity);

            model.Properties = authorizeOption.GetProperties(Metadata, auth);
            var e = new EntityModelCreatedEventArgs <T>(model);

            await RaiseAsyncEvent(EntityDetailModelCreatedEvent, e);

            return(model);
        }
Exemplo n.º 2
0
        public void EntityEditModel_delays_changes_at_ViewModel()
        {
            // ARRANGE

            var model = new Entity("entity", new Tag("tag1", new Facet("f", new FacetProperty("p1"))));

            model.SetFacetProperty(model.Tags.Single().Facet.Properties.Single(), 1);

            var viewModel = new EntityViewModel(model, model.Tags.Single().ToViewModel());
            var editModel = new EntityEditModel(viewModel, delegate { }, delegate { });

            // ACT

            editModel.Name = "changed";
            editModel.Tags.Single().Properties.Single().Value = "changed";

            // ASSERT

            Assert.Equal("changed", editModel.Name);
            Assert.Equal("entity", viewModel.Name);
            Assert.Equal("entity", model.Name);

            Assert.Equal("changed", editModel.Tags.Single().Properties.Single().Value);
            Assert.Equal(1, viewModel.Tags.Single().Properties.Single().Value);
            Assert.Equal(1, model.Values[model.Tags.Single().Facet.Properties.Single().Id.ToString()]);
        }
Exemplo n.º 3
0
        public void EntityEditModel_commits_removing_tag_at_ViewModel()
        {
            // ARRANGE

            var model     = new Entity("entity", new Tag("tag1", new Facet("f", new FacetProperty("p1"))));
            var viewModel = new EntityViewModel(model, model.Tags.Single().ToViewModel());

            Entity committed = null;
            var    commitCB  = new Action <Entity>(e => committed = e);

            Entity reverted = null;
            var    revertCB = new Action <Entity>(e => reverted = e);

            var editModel = new EntityEditModel(viewModel, commitCB, revertCB);

            editModel.RemoveTagCommand.Execute(editModel.Tags.Single());

            // ACT

            editModel.CommitCommand.Execute(null);

            // ASSERT

            Assert.Null(reverted);
            Assert.Equal(model, committed);

            Assert.Empty(editModel.Tags);
            Assert.Empty(viewModel.Tags);
            Assert.Empty(model.Tags);
        }
Exemplo n.º 4
0
        public virtual async Task <IEntityEditModel <T> > Create([FromService] IDatabaseContext database, [FromService] IAuthenticationProvider authenticationProvider, [FromOptions] EntityDomainAuthorizeOption authorizeOption)
        {
            var auth = authenticationProvider.GetAuthentication();

            if (authorizeOption == null)
            {
                authorizeOption = EntityDomainAuthorizeOption.Create;
            }
            authorizeOption.Validate(Metadata, auth);
            var context = database.GetContext <T>();
            var item    = context.Create();

            item.OnCreating();
            EntityEditModel <T> model = new EntityEditModel <T>(item);

            model.Properties = authorizeOption.GetProperties(Metadata, auth);
            if (EntityCreateModelCreated != null)
            {
                EntityModelCreatedEventArgs <T> arg = new EntityModelCreatedEventArgs <T>(model);
                await EntityCreateModelCreated(Context, arg);

                model = arg.Model;
            }
            return(model);
        }
Exemplo n.º 5
0
        public void EntityEditModel_reverts_adding_tag_at_ViewModel()
        {
            // ARRANGE

            var tagViewModel = new TagViewModel(new Tag("t2"));

            var model     = new Entity("entity");
            var viewModel = new EntityViewModel(model);

            Entity reverted = null;
            var    revertCB = new Action <Entity>(e => reverted = e);

            var editModel = new EntityEditModel(viewModel, delegate { }, revertCB);

            editModel.AssignTagCommand.Execute(tagViewModel);

            // ACT

            editModel.RollbackCommand.Execute(null);
            editModel.CommitCommand.Execute(null);

            // ASSERT

            Assert.Equal(model, reverted);

            Assert.Empty(editModel.Tags);
            Assert.Empty(viewModel.Tags);
            Assert.Empty(model.Tags);
        }
Exemplo n.º 6
0
        public virtual ActionResult Edit(Guid id)
        {
            if (!EntityQueryable.Editable())
            {
                return(new HttpStatusCodeResult(403));
            }
            if (!User.Identity.IsAuthenticated && !Metadata.AllowAnonymous)
            {
                return(new HttpStatusCodeResult(403));
            }
            if (!Metadata.EditRoles.All(t => User.IsInRole(t)))
            {
                return(new HttpStatusCodeResult(403));
            }
            TEntity item = EntityQueryable.GetEntity(id);

            if (item == null)
            {
                return(new HttpStatusCodeResult(404));
            }
            var model = new EntityEditModel <TEntity>(item);

            model.Properties = Metadata.EditProperties;
            return(View(model));
        }
Exemplo n.º 7
0
        public void EntityEditModel_commits_adding_tag_at_ViewModel()
        {
            // ARRANGE

            var tagViewModel = new TagViewModel(new Tag("t2"));

            var model     = new Entity("entity");
            var viewModel = new EntityViewModel(model);

            Entity committed = null;
            var    commitCB  = new Action <Entity>(e => committed = e);

            Entity reverted = null;
            var    revertCB = new Action <Entity>(e => reverted = e);

            var editModel = new EntityEditModel(viewModel, commitCB, revertCB);

            editModel.AssignTagCommand.Execute(tagViewModel);

            // ACT

            editModel.CommitCommand.Execute(null);

            // ASSERT

            Assert.Equal(model, committed);
            Assert.Null(reverted);

            Assert.Single(editModel.Tags);
            Assert.Single(viewModel.Tags);
            Assert.Single(model.Tags);
        }
Exemplo n.º 8
0
        public virtual ActionResult Edit(string entityName, string key)
        {
            var entity = _admin.GetEntity(entityName);

            if (entity == null)
            {
                return(RedirectToAction("NotFound", new { entityName }));
            }

            var entityRecord = _source.GetEntityRecord(entity, key);

            if (entityRecord == null)
            {
                return(RedirectToAction("Index", "Entities", new { area = "IlaroAdmin", entityName }));
            }

            var model = new EntityEditModel
            {
                Entity           = entity,
                Record           = entityRecord,
                PropertiesGroups = _entityService.PrepareGroups(entityRecord, getKey: false, key: key),
                ConcurrencyCheck = entityRecord.GetConcurrencyCheckValue()
            };

            return(View(model));
        }
Exemplo n.º 9
0
        public void EntityEditModel_reverts_changes_from_ViewModel()
        {
            // ARRANGE

            var model = new Entity("entity", new Tag("tag1", new Facet("f", new FacetProperty("p1"))));

            model.SetFacetProperty(model.Tags.Single().Facet.Properties.Single(), 1);

            var viewModel = new EntityViewModel(model, model.Tags.Single().ToViewModel());

            Entity reverted = null;
            var    revertCB = new Action <Entity>(e => reverted = e);

            var editModel = new EntityEditModel(viewModel, delegate { }, revertCB);

            editModel.Name = "changed";
            editModel.Tags.Single().Properties.Single().Value = "changed";

            // ACT

            editModel.RollbackCommand.Execute(null);
            editModel.CommitCommand.Execute(null);

            // ASSERT

            Assert.Equal(model, reverted);

            Assert.Equal("entity", editModel.Name);
            Assert.Equal("entity", viewModel.Name);
            Assert.Equal("entity", model.Name);

            Assert.Equal(1, editModel.Tags.Single().Properties.Single().Value);
            Assert.Equal(1, viewModel.Tags.Single().Properties.Single().Value);
            Assert.Equal(1, model.Values[model.Tags.Single().Facet.Properties.Single().Id.ToString()]);
        }
Exemplo n.º 10
0
        public void EntityEditModel_commit_notifies()
        {
            // ARRANGE

            var model = new Entity("entity", new Tag("tag1", new Facet("f", new FacetProperty("p1"))));

            model.SetFacetProperty(model.Tags.Single().Facet.Properties.Single(), 1);

            var viewModel = new EntityViewModel(model, model.Tags.Single().ToViewModel());

            var editModel = new EntityEditModel(viewModel, delegate { }, delegate { });

            // ACT

            EditModelCommitted result = null;
            var committedNotification = new Action <EditModelCommitted>(n => result = n);

            Messenger.Default.Register <EditModelCommitted>(this, committedNotification);

            editModel.CommitCommand.Execute(null);

            // ASSERT

            Assert.NotNull(result);
            Assert.Equal(typeof(EntityViewModel), result.ViewModel.GetType());
            Assert.Equal(viewModel, result.TryGetViewModel <EntityViewModel>().Item2);
        }
Exemplo n.º 11
0
        public void EntityEditModel_reverts_removing_tag_from_ViewModel()
        {
            // ARRANGE

            var model = new Entity("entity", new Tag("tag1", new Facet("f", new FacetProperty("p1"))));

            model.SetFacetProperty(model.Tags.Single().Facet.Properties.Single(), 1);

            var viewModel = new EntityViewModel(model, model.Tags.Single().ToViewModel());

            Entity reverted = null;
            var    revertCB = new Action <Entity>(e => reverted = e);

            var editModel = new EntityEditModel(viewModel, delegate { }, revertCB);

            editModel.RemoveTagCommand.Execute(editModel.Tags.Single());

            // ACT

            editModel.RollbackCommand.Execute(null);
            editModel.CommitCommand.Execute(null);

            // ASSERT

            Assert.Equal(model, reverted);

            Assert.Single(editModel.Tags);
            Assert.Single(viewModel.Tags);
            Assert.Single(model.Tags);
        }
Exemplo n.º 12
0
 protected virtual Task <EntityEditModel <TEntity> > GetEditModel(TEntity entity)
 {
     return(Task.Run <EntityEditModel <TEntity> >(() =>
     {
         EntityEditModel <TEntity> model = new EntityEditModel <TEntity>(entity);
         model.Properties = Metadata.EditProperties;
         return model;
     }));
 }
Exemplo n.º 13
0
        public ActionResult Edit(
            string entityName,
            string key,
            FormCollection collection)
        {
            var entity = AdminInitialise.EntitiesTypes
                         .FirstOrDefault(x => x.Name == entityName);

            if (entity == null)
            {
                throw new NoNullAllowedException("entity is null");
            }

            entity.Fill(collection, Request.Files);
            if (_validator.Validate(entity))
            {
                try
                {
                    var result = _entityService.Edit(entity);
                    if (result)
                    {
                        _notificator.Success(IlaroAdminResources.EditSuccess, entity.Verbose.Singular);


                        if (Request["ContinueEdit"] != null)
                        {
                            return(RedirectToAction("Edit", new { entityName, key }));
                        }
                        if (Request["AddNext"] != null)
                        {
                            return(RedirectToAction("Create", new { entityName }));
                        }

                        return(RedirectToAction("Index", "Entities", new { entityName }));
                    }
                    _notificator.Error(IlaroAdminResources.UncaughtError);
                }
                catch (Exception ex)
                {
                    var message = ex.Message;
                    if (ex.InnerException != null)
                    {
                        message += "<br />" + ex.InnerException.Message;
                    }
                    _notificator.Error(message);
                }
            }

            var model = new EntityEditModel
            {
                Entity           = entity,
                PropertiesGroups = _entityService.PrepareGroups(entity, false, key)
            };

            return(View(model));
        }
Exemplo n.º 14
0
        public virtual EntityEditor GetEditor <TEntity>(TEntity entity) where TEntity : class, IEntity, new()
        {
            EntityMetadata metadata = EntityAnalyzer.GetMetadata <TEntity>();

            EntityEditModel <TEntity> model = new EntityEditModel <TEntity>(entity);

            model.Properties = metadata.EditProperties;
            EntityEditor editor = new EntityEditor(this);

            editor.Model = model;
            return(editor);
        }
Exemplo n.º 15
0
        public virtual ActionResult Edit(string entityName, string key)
        {
            var entity = _source.GetEntityWithData(entityName, key);

            if (entity == null)
            {
                return(RedirectToAction("Index", "Entities", new { entityName }));
            }

            var model = new EntityEditModel
            {
                Entity           = entity,
                PropertiesGroups = _entityService.PrepareGroups(entity, false, key)
            };

            return(View(model));
        }
Exemplo n.º 16
0
        /// <summary>
        /// Get edit action model.
        /// </summary>
        /// <param name="id">Entity id.</param>
        /// <returns>Edit model of TEntity.</returns>
        public async Task <EntityEditModel <TEntity> > GetEditModel(Guid id)
        {
            TEntity item = await EntityContext.GetEntityAsync(id);

            if (item == null)
            {
                return(null);
            }
            var model = new EntityEditModel <TEntity>(item);

            model.Properties = Metadata.EditProperties.Where(t => (t.AllowAnonymous || Controller.User.Identity.IsAuthenticated) &&
                                                             (t.EditRoles.Count() == 0 ||
                                                              (t.AuthenticationRequiredMode == AuthenticationRequiredMode.All ?
                                                               t.EditRoles.All(r => Controller.User.IsInRole(r)) :
                                                               t.EditRoles.Any(r => Controller.User.IsInRole(r))))).ToArray();
            return(model);
        }
Exemplo n.º 17
0
        public virtual ActionResult Detail(Guid id)
        {
            if (!Metadata.ViewRoles.All(t => User.IsInRole(t)))
            {
                return(new HttpStatusCodeResult(403));
            }
            TEntity item = EntityQueryable.GetEntity(id);

            if (item == null)
            {
                return(new HttpStatusCodeResult(404));
            }
            var model = new EntityEditModel <TEntity>(item);

            model.Properties = Metadata.DetailProperties;
            return(View(model));
        }
Exemplo n.º 18
0
        public void EntityEditModel_delays_removing_tag_at_ViewModel()
        {
            // ARRANGE

            var model     = new Entity("entity", new Tag("tag1", new Facet("f", new FacetProperty("p1"))));
            var viewModel = new EntityViewModel(model, model.Tags.Single().ToViewModel());
            var editModel = new EntityEditModel(viewModel, delegate { }, delegate { });

            // ACT

            editModel.RemoveTagCommand.Execute(editModel.Tags.Single());

            // ASSERT

            Assert.Empty(editModel.Tags);
            Assert.Single(viewModel.Tags);
            Assert.Single(model.Tags);
        }
Exemplo n.º 19
0
        /// <summary>
        /// Get create action model.
        /// </summary>
        /// <param name="parent">Parent id.</param>
        /// <returns>Edit model with new entity item of TEntity.</returns>
        public async Task <EntityEditModel <TEntity> > GetCreateModel(Guid?parent = null)
        {
            var model = new EntityEditModel <TEntity>(EntityContext.Create());

            model.Item.Index = Guid.Empty;
            model.Properties = Metadata.CreateProperties.Where(t => (t.AllowAnonymous || Controller.User.Identity.IsAuthenticated) &&
                                                               (t.EditRoles.Count() == 0 ||
                                                                (t.AuthenticationRequiredMode == AuthenticationRequiredMode.All ?
                                                                 t.EditRoles.All(r => Controller.User.IsInRole(r)) :
                                                                 t.EditRoles.Any(r => Controller.User.IsInRole(r))))).ToArray();
            if (parent != null && model.Metadata.ParentProperty != null)
            {
                dynamic parentContext = EntityBuilder.GetContext(model.Metadata.ParentProperty.ClrType);
                object  parentObj     = await parentContext.GetEntityAsync(parent.Value);

                model.Metadata.ParentProperty.SetValue(model.Item, parentObj);
            }
            return(model);
        }
Exemplo n.º 20
0
        public void EntityEditModel_rejects_duplicate_Tags()
        {
            // ARRANGE

            var tag1      = new Tag("tag1", new Facet("f", new FacetProperty("p1")));
            var model     = new Entity("entity", tag1);
            var viewModel = new EntityViewModel(model, model.Tags.Single().ToViewModel());
            var editModel = new EntityEditModel(viewModel, delegate { }, delegate { });

            // ACT

            var result = editModel.AssignTagCommand.CanExecute(tag1);

            // ASSERT

            Assert.False(result);
            Assert.Single(editModel.Tags);
            Assert.Single(model.Tags);
        }
Exemplo n.º 21
0
        public void EntityEditModel_delays_adding_tag_at_ViewModel()
        {
            // ARRANGE

            var tagViewModel = new TagViewModel(new Tag("t2"));

            var model     = new Entity("entity");
            var viewModel = new EntityViewModel(model);
            var editModel = new EntityEditModel(viewModel, delegate { }, delegate { });

            // ACT

            editModel.AssignTagCommand.Execute(tagViewModel);

            // ASSERT

            Assert.Single(editModel.Tags);
            Assert.Empty(viewModel.Tags);
            Assert.Empty(model.Tags);
        }
Exemplo n.º 22
0
        public void EntityEditModel_mirrors_ViewModel()
        {
            // ARRANGE

            var model = new Entity("entity", new Tag("tag1", new Facet("f", new FacetProperty("p1"))));

            model.SetFacetProperty(model.Tags.Single().Facet.Properties.Single(), 1);

            var viewModel = new EntityViewModel(model, model.Tags.Single().ToViewModel());

            // ACT

            var editModel = new EntityEditModel(viewModel, delegate { }, delegate { });

            // ASSERT

            Assert.Equal("entity", editModel.Name);
            Assert.Equal("tag1", editModel.Tags.ElementAt(0).ViewModel.Tag.Name);
            Assert.Equal("p1", editModel.Tags.ElementAt(0).Properties.Single().ViewModel.Property.Name);
            Assert.Equal(1, editModel.Tags.ElementAt(0).Properties.Single().Value);
        }
Exemplo n.º 23
0
        public virtual ActionResult Create(Guid?parent = null)
        {
            if (!EntityQueryable.Addable())
            {
                return(new HttpStatusCodeResult(403));
            }
            if (!Metadata.AddRoles.All(t => User.IsInRole(t)))
            {
                return(new HttpStatusCodeResult(403));
            }
            var model = new EntityEditModel <TEntity>(EntityQueryable.Create());

            model.Item.Index = Guid.Empty;
            model.Properties = Metadata.EditProperties;
            if (parent != null && model.Metadata.ParentProperty != null)
            {
                dynamic parentContext = EntityBuilder.GetContext(model.Metadata.ParentProperty.Property.PropertyType);
                object  parentObj     = parentContext.GetEntity(parent.Value);
                model.Metadata.ParentProperty.Property.SetValue(model.Item, parentObj);
            }
            return(View("Edit", model));
        }
Exemplo n.º 24
0
        public ActionResult Edit(
            string entityName,
            string key,
            [Bind(Prefix = "__ConcurrencyCheck")] string concurrencyCheck,
            FormCollection collection)
        {
            var entity = _admin.GetEntity(entityName);

            if (entity == null)
            {
                return(RedirectToAction("NotFound", new { entityName }));
            }

            var concurrencyCheckValue = ConcurrencyCheck.Convert(concurrencyCheck, entity);

            var isSuccess = _entityService.Edit(entity, key, collection, Request.Files, concurrencyCheckValue);

            if (isSuccess)
            {
                _notificator.Success(IlaroAdminResources.EditSuccess, entity.Verbose.Singular);

                return(SaveOrUpdateSucceed(entityName, key));
            }

            var entityRecord = entity.CreateRecord(key, collection, Request.Files);

            var model = new EntityEditModel
            {
                Entity           = entity,
                Record           = entityRecord,
                PropertiesGroups = _entityService.PrepareGroups(entityRecord, getKey: false, key: key),
                ConcurrencyCheck = concurrencyCheckValue
            };

            return(View(model));
        }
Exemplo n.º 25
0
 public EntityModelCreatedEventArgs(EntityEditModel <T> model)
 {
     Model = model;
 }