1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Access information about user beeing deleted?

Access information about user beeing deleted?

9 posts, 3 answered
  1. Qezlot
    Qezlot avatar
    10 posts
    Registered:
    21 Apr 2010
    27 May 2010
    Link to this post
    Hi!
    I need to write to a log in an external DB when users inside Sitefinity gets created, updated and deleted. I've hooked up some event handlers in the Sitefinity/Admin/CmsAdmin/Users.aspx.cs code to listen to the events:
    * CreatingUser
    * CreatedUser
    * UpdatingUser
    * UpdatedUser
    * DeletingUser
    * DeletedUser

    It all seams to work fine for the events when the user gets created or updated, but in the event DeletingUser, when I check the manageUsers.UserName it returns blank. I thought this event was triggered before the user was deleted and that I still could access the information about that specific user?

    Some code:
    // Handles log when a user is deleted
    void manageUsers_DeletingUser(object source, ModifingActionCancelEventArgs e)
    {
        LogHandler.WriteToLog("Deleted user:'" + manageUsers.UserName 
            +
    "', by:'" + Profile.UserName + "'");
    }


    In the event CreatedUser there is a problem reading some information about the newly created user. I can see that FirstName and LastName were saved correctly inside the Sitefinity table Telerik_users, but when I try to access this information to save to the external log they too seams to return blank...

    A snippet of code (somewhat simplified):

    // Trying to access information through the ProfileBase object
    ProfileBase userProfile = ProfileBase.Create(userName);
    // And through the Membership object
    MembershipUser user = Membership.GetUser(userName);
      
    // A connection to the external DB through LINQ-to-SQL
    DBDataContext db = new DBDataContext()

     

    mUser newUser = new mUser();

    newUser.Email = user.Email;

    newUser.FirstName = (string)userProfile.GetPropertyValue("FirstName");
    newUser.LastName = (string)userProfile.GetPropertyValue("LastName");
    newUser.Username = userName;
    db.mUsers.InsertOnSubmit(newUser);
    db.SubmitChanges();

     

    The FirstName and LastName is blank in the external DB...

    Help is appreciated! :-)
    // Qezlot

     

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    28 May 2010
    Link to this post
    Hi Qezlot,

    There is no problem to access the Membership and Profile data inside DeletingUser. Most probably something is going wrong with your persistence class or your are not passing the values correctly. I prepared a short video for your continence that shows the Profile and Memebership data of the user that is being deleted.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
    Answered
  3. Qezlot
    Qezlot avatar
    10 posts
    Registered:
    21 Apr 2010
    01 Jun 2010
    Link to this post
    Hi Ivan Dimitrov!

    Thank you for your answer and the snippet of video. What I saw in the video was that you were using the parameter "e" that comes with the DeletingUser-event, I didn't look inside that variable to find that it was actually holding the Username of the affected user beeing deleted. Thank you!

    The problem now is that the other events; CreatingUser, CreatedUser, UpdatingUser, UpdatedUser and DeletedUser does NOT seam to hold the Username as a property of those event's "e" variable?

    So how do I get hold of the Username when inside the CreatingUser-event? I need to run some checks of the username against my external database before allowing it to be created inside the Sitefinity database. If the checks fail I need to stop Sitefinity from creating the user, I could throw an exception that will display inside the Sitefinity adminstration page, but I guess there is a neater way to display errors to the administrator? (And to instead use the Cancel-property to stop the user from ever beeing created?).

    Kind regards
    Qezlot
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    01 Jun 2010
    Link to this post
    Hello Qezlot,

    You can get the username of the user that is being created as shown below

    void manageUsers_CreatingUser(object source, ActionCancelEventArgs e)
     {
         Telerik.Security.WebControls.ManageUsers.EditContainer editContainer = manageUsers.GetCurrentContainer() as Telerik.Security.WebControls.ManageUsers.EditContainer;
         if (editContainer != null)
         {
             string un = editContainer.UserNameTextBox.Text;
         }
     }

    You can call e.Cancel = true; and the user will not be created.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
    Answered
  5. Qezlot
    Qezlot avatar
    10 posts
    Registered:
    21 Apr 2010
    02 Jun 2010
    Link to this post

    Thank you!

    To recap: To get information about a user beeing managed in the Sitefinity web page: [web site]\Sitefinity\Admin\CmsAdmin\Users.aspx.cs I first need to hook up event handlers for the events triggered inside the manageUsers object:

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        manageUsers.ModeChanged += new EventHandler(manageUsers_ModeChanged);
        manageUsers.CreatedRole += new RoleCreatedEventHandler(manageUsers_CreatedRole);
      
        // Event handlers to handle user management
        manageUsers.CreatingUser += new ActionCancelEventHandler(manageUsers_CreatingUser);
        manageUsers.CreatedUser += new EventHandler(manageUsers_CreatedUser);
        manageUsers.UpdatingUser += new ActionCancelEventHandler(manageUsers_UpdatingUser);
        manageUsers.UpdatedUser += new EventHandler(manageUsers_UpdatedUser);
        manageUsers.DeletingUser += new ModifingActionCancelEventHandler(manageUsers_DeletingUser);
        manageUsers.DeletedUser += new DeletedUserEventHandler(manageUsers_DeletedUser);
    }

    And then I use different objects to access the UserName, the Membership data and the Profile data for the user currently beeing administrated.

    Inside manageUsers_CreatingUser I need to access the EditContainer to access the UserNameTextBox:
    void manageUsers_CreatingUser(object source, ActionCancelEventArgs e)
    {
        ManageUsers.EditContainer editContainer 
            = manageUsers.GetCurrentContainer() as ManageUsers.EditContainer;
        if (editContainer != null)
        {
            string userName = editContainer.UserNameTextBox.Text; 
               // ...
        }
    }

    And inside the manageUsers_DeletingUser I need to use the ModifingActionCancelEventArgs parameter "e" to access the Username.

    Then I can use code similar to:
    MembershipUser user = Membership.GetUser(userName);
    ProfileBase userProfile = ProfileBase.Create(userName);
    to access the different parts of information about this user...

    BUT, in the event CreatedUser I'm trying to use the above objects to access that user's FirstName and LastName with code like:
    using (DBDataContext db = new DBDataContext())
    {
        MembershipUser user = Membership.GetUser(userName);
        ProfileBase userProfile = ProfileBase.Create(userName);
      
        // Using LINQ-to-SQL for DB access
        mUser newUser = new mUser();
        newUser.Email = user.Email;
        newUser.FirstName = (string)userProfile.GetPropertyValue("FirstName");
        newUser.LastName = (string)userProfile.GetPropertyValue("LastName");
      
        newUser.Username = userName;
      
        db.mUsers.InsertOnSubmit(newUser);
        db.SubmitChanges();
        result = newUser.UserID;
    }

    But the FirstName and LastName properties are set to blank, even though they are set inside the Sitefinity database Telerik_UserMetaData table. Why?

    How do I access the Membership and MetaInfo of the User in the different events?

    Kind regards
    // Qezlot
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    02 Jun 2010
    Link to this post
    Hi Qezlot,

    Here is how to get the value of FirstName and LastName properties.

    void manageUsers_CreatedUser(object sender, EventArgs e)
    {
        Telerik.Security.WebControls.ManageUsers.EditContainer eContainer = manageUsers.GetCurrentContainer() as Telerik.Security.WebControls.ManageUsers.EditContainer;
        if (eContainer != null)
        {
            ManageProfile p = eContainer.FindControl("manageProfile") as ManageProfile;
            if (p != null)
            {
                var fnTB = p.Controls[0].FindControl("FirstName") as TextBox;
                if (fnTB != null)
                {
                    // get the text here
                }
                var lnTB = p.Controls[0].FindControl("LastName") as TextBox;
                if (lnTB != null)
                {
                    //get the text of here
                }
            }
        }
     
    }


    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
    Answered
  7. Qezlot
    Qezlot avatar
    10 posts
    Registered:
    21 Apr 2010
    02 Jun 2010
    Link to this post
    Thanks a million - that worked like a charm! :-)

    But - isn't that a bit complex, having to find the actual text boxes with the information? I could imagine having to use this in the CreatingUser-event because the user isn't actually created yet, but to have to use it in the CreatedUser-event, shouldn't these pieces of information be visible through the user's Profile or Membership-objects already?

    Is there information about these events, the objects and properties inside the development manual?

    Well, the important thing is that the code works, hoping to be able to finnish the current project now. Thank you very much for your quick answers!

    Sincerely yours,
    Qezlot
    Sweden
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    02 Jun 2010
    Link to this post
    Hi Qezlot,

    ManageUsers and ManageProfile controls have modes. Depending on the operation you are performing we load the mode. Each mode has container of controls. Basically you could get the container and search for controls inside it or using recursion to check the controls collection. The problem is that some of the events are fired before the base code has been executed and you get null values.

    The events are documented only as an API reference.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  9. Qezlot
    Qezlot avatar
    10 posts
    Registered:
    21 Apr 2010
    02 Jun 2010
    Link to this post
    Ok, I understand.

    Thank you very much for your help! :-)
    Kind regards,
    Qezlot
Register for webinar
9 posts, 3 answered