Ejemplo n.º 1
0
        private void OnBeforeSaving()
        {
            var currentTime = DateTimeOffset.UtcNow;
            var currentUser = _currentUserService.GetCurrentUserName();

            foreach (var item in ChangeTracker.Entries()
                     .Where(e => e.State == EntityState.Added && e.Entity is Audit))
            {
                var entity = item.Entity as Audit;
                entity.CreatedDate  = currentTime;
                entity.CreatedBy    = currentUser;
                entity.ModifiedDate = currentTime;
                entity.ModifiedBy   = currentUser;
            }

            foreach (var item in ChangeTracker.Entries()
                     .Where(predicate: e => e.State == EntityState.Modified && e.Entity is Audit))
            {
                var entity = item.Entity as Audit;
                entity.ModifiedDate = currentTime;
                entity.ModifiedBy   = currentUser;
                item.Property(nameof(entity.CreatedDate)).IsModified = false;
                item.Property(nameof(entity.CreatedBy)).IsModified   = false;
            }
        }
        public async Task <UserResponse> GetCurrentUser()
        {
            var user = await _userManager.FindByNameAsync(_currentUserService.GetCurrentUserName());

            if (user == null)
            {
                throw new RestException(HttpStatusCode.Unauthorized);
            }

            return(new UserResponse
            {
                UserName = user.UserName,
                Token = await _jwtGenerator.CreateToken(user)
            });
        }
Ejemplo n.º 3
0
        public override Task <int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
        {
            foreach (var entry in ChangeTracker.Entries <AuditableEntity>())
            {
                switch (entry.State)
                {
                case EntityState.Added:
                    entry.Entity.CreatedBy
                        = _currentUserService.GetCurrentUserName() ?? "admin";
                    entry.Entity.Created
                        = DateTime.Now;
                    break;

                case EntityState.Modified:
                    entry.Entity.LastModifiedBy
                        = _currentUserService.GetCurrentUserName() ?? "admin";
                    entry.Entity.LastModified
                        = DateTime.Now;
                    break;
                }
            }

            return(base.SaveChangesAsync(cancellationToken));
        }