CSLA .NET

From Rockford Lhotka's Expert C# 2005 and VB 2005 Business Objects books

Welcome to CSLA .NET Sign in | Join | Help
in Search

Adding UpdatedBy to Business Objects

Last post 08-05-2008, 6:54 AM by SonOfPirate. 6 replies.
Sort Posts: Previous Next
  •  07-18-2008, 4:39 PM 24799

    Adding UpdatedBy to Business Objects

    All users of my winform SQL Server app connect to the database via a single SQL Server
    userid. Each user is assigned an application userid and role based on their Windows logon which
    is authenticated via LDAP.

    This has been working real well and management approves because I have locked down the
    database to sproc access only for the single SQL Server userid yet authentication is
    achieved against the each user's Windows userid, I don't store passwords in the application database.

    My issue now is how to best populate the updatedby column in the database tables.  I cannot
    use the SQL Server SUSER_NAME() in my sprocs since it returns the database connected user and I need my application user.

    I have read another post where the writer suggested their approach would be a shared function
    on the Identity object which returns the user name and then provides this to the Save method et al?

    Any others care to share how they have done this sort of thing?

    Thanks in advance.

  •  07-18-2008, 4:55 PM 24800 in reply to 24799

    Re: Adding UpdatedBy to Business Objects

    The Csla.ApplicationContext.User contains the current user and identity, so you can just grab this right when saving your object when appropriate. I'm doing this now for our similar auditing requirement.

    Another possibility, from our legacy application, is that we have the capability to store the current user in a temporary table in the database that maintains separate data for each connection. Then you can record the update in your stored procedure or trigger. However, this requires that you maintain the value in the database temporary table. (Easy in old-style application that opens and holds the connection for the duration for the application session, more involved in a web or connection sharing situation)

  •  07-21-2008, 9:01 AM 24825 in reply to 24800

    Re: Adding UpdatedBy to Business Objects

    The Identity carries the information you need and is available on both sides of the DataPortal.

    All you need to do is use it.

    So for a SQL statement that saves a new record you might have something like:

    Public Function Insert(ByVal code As String, ByVal name As String, ByVal userid As Integer) As String

    And when you call the function you pass:

    mBO.code, mBO.name, user.userid

    Joe


      

  •  07-21-2008, 9:11 AM 24827 in reply to 24799

    Re: Adding UpdatedBy to Business Objects

    In the past I have 'overloaded' the workstation id connection string parameter to hold the identity of the user accessing the database.  Take a look at: http://articles.techrepublic.com.com/5100-10878_11-6084879.html for information on the parameters you can add to the connection string.

    Regards,
    Fintan

  •  07-21-2008, 1:27 PM 24846 in reply to 24799

    Re: Adding UpdatedBy to Business Objects

    I use CSLAGen and this is quite simple to do.

    1) Add a function like the following:

    Public Module Helpers

    ' Get the Username of the current user from the CSLA Identity Object
    Public Function GetUserName() As String
       Return Csla.ApplicationContext.User.Identity.Name
    End Function

    End Module

    In the CSLAGen value properties for the business object, just set the default to GetUserName().

    Thanks for all the great feedback everyone!

  •  08-04-2008, 3:02 PM 25212 in reply to 24846

    Re: Adding UpdatedBy to Business Objects

    I have a completely local Windows client app.  The UI, BO, and db are all on my PC.

    Csla.ApplicationContext.User returns a GenericIdentity and Csla.ApplicationContext.User.Name is an empty string.

    I know I am logged on.  What am I missing?  Do I have to set the principal somewhere?

    Jim

  •  08-05-2008, 6:54 AM 25221 in reply to 25212

    Re: Adding UpdatedBy to Business Objects

    You have to manually set the Csla.ApplicationContext.User property to the appropriate Principal object.  This is typically done when the app starts.  With single sign-on (i.e. Windows authentication), you'll just grab to the Threading.CurrentPrincipal and use that.

    Back to the original question...  In the first post, Warren indicated that:

    Each user is assigned an application userid and role based on their Windows login which is authenticated via LDAP.

    This is basically the way we do it.  We have our custom "User" object, which implements IPrincipal, exposes the UserID guid property.  We then set this object to Csla.ApplicationContext.User.  In our business objects, before posting to the database (insert/update), we type check the AppContext.User to make sure it is our User object and, if so, use the UserID property to set the LastModifiedBy value.

    HTH

     

View as RSS news feed in XML

Please contact Magenic for your .NET consulting and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Powered by Community Server, by Telligent Systems