Okay, so I tried to figure this out myself.
ISSUE #1: WHERE DO I PUT THE CODE?First of all, since I don't know a better way to do this, I added all my Added/Modifed code to the BaseEntity in a new method:
/// <summary> /// Sets the Create and Update information /// </summary> protected void SetCreateUpdateInfo() { //Get current logged in user //["UserId"] IPrincipal ipCurrent = Thread.CurrentPrincipal; long lngUserId = (long)AppIdentity.GetUserIdFromPrincipal(ipCurrent);
//Check if this is a new record if (this["AddedBy"] == null || (long)this["AddedBy"] == 0) { this["AddedBy"] = lngUserId; this["DateAdded"] = DateTime.UtcNow; } else { this["ModifiedBy"] = lngUserId; this["DateLastModified"] = DateTime.UtcNow; } }
|
Then I just add a call to this in my various Create routines:
...
aNewObject.SetCreateUpdateInfo(); aNewObject.AddToManager(); ...
|
Perhaps someone has a better idea, but this seems to be functioning according to my requirements for now.
Though this does create a question - where should I put the same call for "Update"?
ISSUE #2: HOW DO I GET THE CURRENTLY LOGGED IN USER? My bigger issue is that I am having a hard time using IPrincipal to get my UserId for the method. This is what I am using in the method above:
... //Get current logged in user //["UserId"]
IPrincipal ipCurrent = Thread.CurrentPrincipal;
long lngUserId = (long)AppIdentity.GetUserIdFromPrincipal(ipCurrent);
...
|
Now, I did update the login manager to attach the logged in user to the Thread:
public IPrincipal Login(ILoginCredential pCredential, PersistenceManager pManager) {
EnterLogin();
AppIdentity identity = GetAppIdentity(pCredential, pManager); String[] roles = GetUserRoles(pManager, identity); IPrincipal principal = new GenericPrincipal(identity, roles);
//Added by HLF // bind the generic principal to the thread Thread.CurrentPrincipal = principal;
ExitingSuccessfully();
return principal; }
|
And if I put a breakpoint in 'SetCreateUpdateInfo()' and check the data, ipCurrent seems to be holding my expected data, but "AppIdentity.GetUserIdFromPrincipal(ipCurrent);" always returns zero. I noticed that the AppIdentity code is buried in "Foundation" and wasn't sure if I was supposed to change something here, or if it should work as expected. I have a class called "User" which has a field called "Id" (linked to the User table in my database also), but I'm not sure how GetUserIdFromPrincipal looks up the corresponding User.Id and returns it. I am not very familair with how Interfaces work, so please excuse any obvious ignorance.
If anyone has an idea of how I can get this to function as expected, I'd appreciate it.
Thanks,
Heather