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();
}
}
}
I think you can read my reply in this, maybe can help you ?
<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
{
//.....
}
Yes, I know, I have solved it, thanks!
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....
anyone can help?
thanks!!!!
I have my own user table, just want to use the ABP Authorization feature and session, but don't want to implement the module-zero project, can I do that?
thanks!
Yes, after I used the Repository and it's working, below is my entity, there is no any especially:
public class FormLayout : Entity, IModificationAudited, ICreationAudited
{
public virtual bool Active { get; set; }
[Required]
public virtual int CateTabId { get; set; }
[Required]
public virtual string Name { get; set; }
[Required]
public virtual string Value { get; set; }
public DateTime CreationTime { get; set; }
public DateTime? LastModificationTime { get; set; }
public long? LastModifierUserId { get; set; }
public long? CreatorUserId { get; set; }
[ForeignKey("CreatorUserId")]
public virtual AdminUser CreatorUser { get; set; }
[ForeignKey("LastModifierUserId")]
public virtual AdminUser LastModifierUser { get; set; }
[ForeignKey("CateTabId")]
public virtual CategoryTab CateTab { get; set; }
}
I inherit IModificationAudited and ICreationAudited interface and create CreatorUserId and LastModifierUserId fields, but when I insert a record, I have set the value to these fields like below:
var item = new Item
{
Active = true,
Name = input.Name,
Value = input.Value,
CreationTime = DateTime.Now,
LastModificationTime = DateTime.Now,
CreatorUserId = 1, //TODO: for testing
LastModifierUserId = 1
};
_itemRepository.Insert(item);
but this just can save the LastModifierUserId value, and the CreatorUserId is null, if the LastModifierUserId is null, when I do the update, I also can't update it:
var item = _itemRepository.Get(input.Id);
item.Name = input.Name;
item.Value = input.Value;
item.LastModificationTime = DateTime.Now;
item.LastModifierUserId = 1; //TODO: for testing
_itemRepository.Update(item);
after that, the LastModifierUserId is also null.
I have created the ForeignKey for these fields :
public long? LastModifierUserId { get; set; }
public long? CreatorUserId { get; set; }
[ForeignKey("CreatorUserId")]
public virtual AdminUser CreatorUser { get; set; }
[ForeignKey("LastModifierUserId")]
public virtual AdminUser LastModifierUser { get; set; }
I found that if I use a repository to inject the app service, and it's working:
private readonly IFormLayoutRepository _formLayoutRepository;
public FormLayoutAppService(IFormLayoutRepository formLayoutRepository)
{
_formLayoutRepository = formLayoutRepository;
}
but why it must be use Repository? I have no other custom method to use, so if I create the Repository for each table, these Repositories are empty, and I have to create an interface and implements it, but I just want to use the simple generic methods:
IRepository<MyModel>