Exemple #1
0
        private static string GetCreatorUserFullName(ICreationAudited entity)
        {
            if (entity.CreatorUserId == null)
            {
                return(null);
            }

            var personService = StaticContext.IocManager.Resolve <IRepository <Person, Guid> >();
            var person        = personService.GetAll().FirstOrDefault(p => p.User != null && p.User.Id == entity.CreatorUserId);

            return(person?.FullName);
        }
Exemple #2
0
        private TEntity CheckICreationAudited <TUserKey>(TEntity entity)
            where TUserKey : struct, IEquatable <TUserKey>
        {
            if (!entity.GetType().IsBaseOn(typeof(ICreationAudited <>)))
            {
                return(entity);
            }

            ICreationAudited <TUserKey> entity1 = (ICreationAudited <TUserKey>)entity;

            entity1.CreatorUserId = _principal?.Identity.GetUesrId <TUserKey>();
            entity1.CreatedTime   = DateTime.Now;
            return((TEntity)entity1);
        }
Exemple #3
0
        public bool IsCreatedUser(long id, ICreationAudited entity)
        {
            if (entity == null)
            {
                return(false);
            }

            if (id == entity.CreatorUserId)
            {
                return(true);
            }

            return(false);
        }
Exemple #4
0
        public bool IsCreatedUser(User user, ICreationAudited entity)
        {
            if (user == null || entity == null)
            {
                return(false);
            }

            if (user.Id == entity.CreatorUserId)
            {
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// 检测并执行<see cref="ICreationAudited{TUserKey}"/>接口的处理
        /// </summary>
        public static TEntity CheckICreationAudited <TEntity, TKey, TUserKey>(this TEntity entity, IPrincipal user)
            where TEntity : IEntity <TKey>
            where TUserKey : struct
        {
            if (!(entity is ICreationAudited <TUserKey>))
            {
                return(entity);
            }

            ICreationAudited <TUserKey> entity1 = (ICreationAudited <TUserKey>)entity;

            entity1.CreatorId   = user.Identity.GetUserId <TUserKey>();
            entity1.CreatedTime = DateTime.Now;
            return((TEntity)entity1);
        }
        /// <summary>
        /// 检测并执行<see cref="ICreationAudited{TUserKey}"/>接口的处理
        /// </summary>
        public static TEntity CheckICreationAudited <TEntity, TKey, TUserKey>(this TEntity entity, IPrincipal user)
            where TEntity : IEntity <TKey>
            where TKey : IEquatable <TKey>
            where TUserKey : struct, IEquatable <TUserKey>
        {
            if (!(entity is ICreationAudited <TUserKey>))
            {
                return(entity);
            }

            ICreationAudited <TUserKey> entity1 = (ICreationAudited <TUserKey>)entity;

            entity1.CreatorId = user.Identity.IsAuthenticated ? (TUserKey?)user.Identity.GetUserId <TUserKey>() : null;
            if (entity1.CreatedTime == default(DateTime))
            {
                entity1.CreatedTime = DateTime.Now;
            }
            return((TEntity)entity1);
        }
Exemple #7
0
 public static void SetCreationAuditProperties(object entityAsObj, long?userId)
 {
     if (!(entityAsObj is IHasCreationTime hasCreationTime))
     {
         return;
     }
     if (hasCreationTime.CreationTime == default)
     {
         hasCreationTime.CreationTime = DateTime.Now;
     }
     if (entityAsObj is ICreationAudited && userId.HasValue)
     {
         ICreationAudited creationAudited = entityAsObj as ICreationAudited;
         if (!creationAudited.CreatorUserId.HasValue)
         {
             creationAudited.CreatorUserId = userId;
         }
     }
 }
        public static void SetCreationAuditProperties(object entityAsObj, int?userId)
        {
            IHasCreationTime hasCreationTime = entityAsObj as IHasCreationTime;

            if (hasCreationTime == null)
            {
                return;
            }
            if (hasCreationTime.CreatedDate == new DateTime())
            {
                hasCreationTime.CreatedDate = DateTime.Now;
            }
            if (!(entityAsObj is ICreationAudited) || !userId.HasValue)
            {
                return;
            }
            ICreationAudited creationAudited = entityAsObj as ICreationAudited;

            if (creationAudited.GetCreatorUserId().HasValue)
            {
                return;
            }
            creationAudited.SetCreatorUserId(userId);
        }
Exemple #9
0
        public override Task <int> SaveChangesAsync(CancellationToken cancellationToken = default)
        {
            foreach (var entry in ChangeTracker.Entries().ToList())
            {
                switch (entry.State)
                {
                case EntityState.Added:
                {
                    IHasCreationTime hasCreation = entry.Entity as IHasCreationTime;
                    if (hasCreation == null)
                    {
                        continue;
                    }
                    hasCreation.CreationTime = DateTime.Now;

                    ICreationAudited creationAudited = entry.Entity as ICreationAudited;
                    if (creationAudited == null)
                    {
                        continue;
                    }
                    if (UserIdentifier != null && UserIdentifier.UserId.HasValue)
                    {
                        creationAudited.CreatorUserId = UserIdentifier.UserId.Value;
                    }
                }
                break;

                case EntityState.Modified:
                {
                    IHasModificationTime hasModification = entry.Entity as IHasModificationTime;
                    if (hasModification == null)
                    {
                        continue;
                    }
                    hasModification.LastModificationTime = DateTime.Now;

                    IModificationAudited modificationAudited = entry.Entity as IModificationAudited;
                    if (modificationAudited == null)
                    {
                        continue;
                    }
                    if (UserIdentifier != null && UserIdentifier.UserId.HasValue)
                    {
                        modificationAudited.LastModifierUserId = UserIdentifier.UserId.Value;
                    }
                }
                break;

                case EntityState.Deleted:
                {
                    IHasDeletionTime hasDeletion = entry.Entity as IHasDeletionTime;
                    if (hasDeletion == null)
                    {
                        continue;
                    }
                    entry.Reload();
                    entry.State = EntityState.Modified;

                    hasDeletion.DeletionTime = DateTime.Now;
                    hasDeletion.IsDeleted    = true;

                    IDeletionAudited deletionAudited = entry.Entity as IDeletionAudited;
                    if (deletionAudited == null)
                    {
                        continue;
                    }
                    if (UserIdentifier != null && UserIdentifier.UserId.HasValue)
                    {
                        deletionAudited.DeleterUserId = UserIdentifier.UserId.Value;
                    }
                }
                break;
                }
            }


            return(base.SaveChangesAsync(cancellationToken));
        }