Base solution for your next web application

Activities of "winson"

ok, I have solved this issue, because I didn't use the zero-module, so the framework can't get the user id from abpsession, after I review the source code, I know just need to override SetCreationAuditProperties this method and can solve my problem....

Yes, I know, I have solved it, thanks!

<cite>klainer: </cite> Please can you post code which solved your issue? Thanks !

Actually, I just created a class and implement the AuthorizeAttribute, this can be an attribute for do the accessright control , this is not related with ABP framework, below are some simple code:

public class AclAttribute : AuthorizeAttribute
    {
       
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }
            
            if (filterContext.HttpContext.Session["User"] == null)
            {
                filterContext.HttpContext.Session["ReturnAction"] = HttpContext.Current.Request.Url.AbsoluteUri;

                HttpContext.Current.Response.Redirect("/Login");
            }

            if (!string.IsNullOrEmpty(this.Roles))
            {
                var currUser = filterContext.HttpContext.Session["User"] as AdminUser;

                if (currUser == null || !AdminUserAppService.HasAccessRights(this.Roles, currUser.Role))
                {
                    filterContext.Result = new RedirectToRouteResult(new
                        RouteValueDictionary(new { controller = "Error", action = "AccessDenied" }));
                }
            }

        }
    }
}

then you can use it in your controller:

[Acl(Roles = UserRole.ADMINISTRATOR)]
 public class AdminUserController : ControllerBase
 {
     //.....
}

I think you can read my reply in this, maybe can help you ?

#922

oh, sorry, I missed somethings, and you also need to rewrite the 'DbContext' class with below method:

protected override void SetCreationAuditProperties(DbEntityEntry entry)
{
       //your code for handle the creation method user id
       base.SetCreationAuditProperties(entry);

            if (entry.Entity is IHasCreationTime)
            {
                entry.Cast<IHasCreationTime>().Entity.CreationTime = Clock.Now;
            }

            if (entry.Entity is ICreationAudited && MPeLifeSession.Instance.UserId.HasValue)
            {
                entry.Cast<ICreationAudited>().Entity.CreatorUserId =YourProjectSession.Instance.UserId;
            }
}

protected override void SetModificationAuditProperties(DbEntityEntry entry)
        {
            base.SetModificationAuditProperties(entry);

            if (entry.Entity is IHasModificationTime)
            {
                entry.Cast<IHasModificationTime>().Entity.LastModificationTime = Clock.Now;
            }

            if (entry.Entity is IModificationAudited && MPeLifeSession.Instance.UserId.HasValue)
            {
                entry.Cast<IModificationAudited>().Entity.LastModifierUserId = YourProjectSession.Instance.UserId;
            }
        }

for the

YourProjectSession.Instance.UserId;

you need to create a class for handle your user session:

public class YourProjectSession : IAbpSession, ISingletonDependency
    {
        /// <summary>
        /// Singleton instance.
        /// </summary>
        public static YourProjectSession Instance { get { return SingletonInstance; } }
        private static readonly YourProjectSession SingletonInstance = new YourProjectSession();

        public long? UserId { get; set; }

        public int? TenantId { get; set; }

        public long? ImpersonatorUserId { get; set; }

        public int? ImpersonatorTenantId { get; set; }

        public MultiTenancySides MultiTenancySide
        {
            get
            {
                throw new NotImplementedException();
            }
        }
    }

hi, I want to do some auditlog for the field values changed with below:

foreach (var entry in ChangeTracker.Entries<FormDataDetail>().Where(a => a.State == EntityState.Modified))
{    
    if (entry.CurrentValues["FieldValue"].ToString() != entry.OriginalValues["FieldValue"].ToString())
    {
       //to do some auditlog
    }
}

but the problem is the OriginalValues always same with the current value, and after I review the ABP source code, I find the below code in update function:

public override TEntity Update(TEntity entity)
        {
            AttachIfNot(entity);
            Context.Entry(entity).State = EntityState.Modified;
            return entity;
        }

I think the problem is

Context.Entry(entity).State = EntityState.Modified;

for the detail, you can take a look this article.

so can we fix this issue?

for now, I have to get the OriginalValues from db again, like below:

var oldValue = entry.GetDatabaseValues().GetValue<string>("FieldValue");

thanks!

oh, that's great! thanks!!

Hi, I just tried this these days, but seems doesn't work, if I didn't call the update method, and the record can't be update, I also try to add the UnitOfWork Attribute but still doesn't work, and support my service should not need to add the UnitOfWork attribute, right? my service class has been inherit the applicationService and IApplicationService

If don't use Update method, I just can use the below code for update the data with unitOfWork:

using (var unitOfWork = _unitOfWorkManager.Begin())
{
     ....
}

oh, sorry, I found the issue, for unitOfWork must be use the virtual method!!

thanks!

Showing 11 to 20 of 25 entries