Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom 713 posts 954 karma points
    Feb 02, 2010 @ 23:54
    Tom
    0

    Action Handler Test Value Before

    Hi All,

    I'm writing an action handler so when a member is saved they get their roles reassigned if a property has changed..

    what im having trouble with is how to actually check the previous value of the property vs the value to be saved.. is that possible?

    here's my code so far for my actionhandler

     

     

     

     

     

     

     

     

     

     

     

     

     

    public

     

     

     

    MemberActionHelper()

    {

     

    Member.BeforeSave += new Member.SaveEventHandler

    (Member_BeforeSave);

    }

     

    void Member_BeforeSave(Member sender, SaveEventArgs

    e)

    {

    //what id like to do here is check if the memberDomain has changed but am unsure how to do that..

     

    string memberDomainString = sender.getProperty("memberDomain"

    ).Value.ToString();

     

    int

    memberDomainID;

     

    if (int.TryParse(memberDomainString, out

    memberDomainID))

    {

     

    using (var dc = new AdeptDataContext

    ())

    {

     

    var

    domain = dc.MemberDomains.Where(d => d.ID == memberDomainID).SingleOrDefault();

     

    if (domain != null

    )

    {

     

    Roles.RemoveUserFromRoles(sender.LoginName, Roles

    .GetAllRoles());

     

    foreach (var memberDomainToGroupRelationship in

    domain.MemberDomainToMemberGroups)

    {

     

    string

    domainRoleName = memberDomainToGroupRelationship.umbracoNode.text;

     

    if (!Roles

    .IsUserInRole(sender.LoginName, domainRoleName))

    {

     

    Roles

    .AddUserToRole(sender.LoginName, domainRoleName);

    also i was tossing up whether to use an actionhandler or to create a custom membership provider but have found that my override of update user never actually get's hit breakpoint wise..

     

    and here's the custom membership provider but for some reason the code isn't being hit.. i like the idea of using the membership provider it's just i can't seem to get it to call update:

     

     

     

     

     

     

     

     

     

     

     

     

     

    public

     

     

     

    override void UpdateUser(System.Web.Security.MembershipUser

    user)

    {

     

    Member member = Member

    .GetMemberFromEmail(user.Email);

     

    string memberDomainString = member.getProperty("memberDomain").Value as string

    ;

     

    int

    memberDomainID;

     

    if (int.TryParse(memberDomainString, out

    memberDomainID))

    {

     

    using (var dc = new AdeptDataContext

    ())

    {

     

    var

    domain = dc.MemberDomains.Where(d => d.ID == memberDomainID).SingleOrDefault();

     

    if (domain != null

    )

    {

     

    Roles.RemoveUserFromRoles(member.LoginName, Roles

    .GetAllRoles());

     

    foreach (var memberDomainToGroupRelationship in

    domain.MemberDomainToMemberGroups)

    {

     

    string

    domainRoleName = memberDomainToGroupRelationship.umbracoNode.text;

     

    if (!Roles

    .IsUserInRole(member.LoginName, domainRoleName))

    {

     

    Roles

    .AddUserToRole(member.LoginName, domainRoleName);

    }

    }

    }

    }

    }

     

    base

    .UpdateUser(user);

    }

     

    and in web.config

     

     

     

     

     

     

     

     

     

    <!--

     

     

     

    Membership Provider

    -->

    <

     

     

     

    membership defaultProvider="UmbracoMembershipProvider" userIsOnlineTimeWindow="15"

    >

    <

     

     

     

    providers

    >

    <

     

     

     

    clear

    />

    <!--

     

     

     

    <add name="UmbracoMembershipProvider" type="umbraco.providers.members.UmbracoMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="TestType" />

    -->

    <

     

     

     

    add name="UmbracoMembershipProvider" type="ProjectName.Web.CustomMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="Member"

    />

    <

     

     

     

    add name="AspNetSqlMemberShipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="LocalSqlServer"

    />

    <

     

     

     

    add name="UsersMembershipProvider" type="umbraco.providers.UsersMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false"

    />

    </

     

     

     

    providers

    >

    </

     

     

     

    membership

    >

     

     

    any help on either approach would be awesome!

    thanks,

    Tom

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 02, 2010 @ 23:58
    Jan Skovgaard
    0

    Hi Tom

    Unfortunately your code is a bit hard to read. Something's not good with the formatting. Can you please try and post again?

    Then it would be a bit easier to view your code and help you out :-)

    /Jan

  • Tom 713 posts 954 karma points
    Feb 03, 2010 @ 00:13
    Tom
    0

    Hi Jan,

    my apologies!

     

    So let's try again!:

     

    I'm writing an action handler so when a member is saved they get their roles reassigned if a property has changed..

    what im having trouble with is how to actually check the previous value of the property vs the value to be saved.. is that possible?

    here's my code so far for my actionhandler

     

    The Member Action Handler

    public

     

     

    MemberActionHelper()

    {

     

     

    Member.BeforeSave += new Member.SaveEventHandler(Member_BeforeSave);

    }

     

     

     

    void Member_BeforeSave(Member sender, SaveEventArgs e)

    {

     

     

    string memberDomainString = sender.getProperty("memberDomain").Value.ToString();

     

     

    int memberDomainID;

     

     

    if (int.TryParse(memberDomainString, out memberDomainID))

    {

     

     

    using (var dc = new AdeptDataContext())

    {

     

     

    var domain = dc.MemberDomains.Where(d => d.ID == memberDomainID).SingleOrDefault();

     

     

    if (domain != null)

    {

     

     

    Roles.RemoveUserFromRoles(sender.LoginName, Roles.GetAllRoles());

     

     

    foreach (var memberDomainToGroupRelationship in domain.MemberDomainToMemberGroups)

    {

     

     

    string domainRoleName = memberDomainToGroupRelationship.umbracoNode.text;

     

     

    if (!Roles.IsUserInRole(sender.LoginName, domainRoleName))

    {

     

     

    Roles.AddUserToRole(sender.LoginName, domainRoleName);

    }

    }

    }

    }

    }

    }

    also i was tossing up whether to use an actionhandler or to create a custom membership provider but have found that my override of update user never actually get's hit breakpoint wise..

     

    and here's the custom membership provider but for some reason the code isn't being hit.. i like the idea of using the membership provider it's just i can't seem to get it to call update:

    The Membership Provider

    public

     

     

    class AdeptMembershipProvider : UmbracoMembershipProvider

    {

     

     

    public override void UpdateUser(System.Web.Security.MembershipUser user)

    {

     

     

    Member member = Member.GetMemberFromEmail(user.Email);

     

     

    string memberDomainString = member.getProperty("memberDomain").Value as string;

     

     

    int memberDomainID;

     

     

    if (int.TryParse(memberDomainString, out memberDomainID))

    {

     

     

    using (var dc = new AdeptDataContext())

    {

     

     

    var domain = dc.MemberDomains.Where(d => d.ID == memberDomainID).SingleOrDefault();

     

     

    if (domain != null)

    {

     

     

    Roles.RemoveUserFromRoles(member.LoginName, Roles.GetAllRoles());

     

     

    foreach (var memberDomainToGroupRelationship in domain.MemberDomainToMemberGroups)

    {

     

     

    string domainRoleName = memberDomainToGroupRelationship.umbracoNode.text;

     

     

    if (!Roles.IsUserInRole(member.LoginName, domainRoleName))

    {

     

     

    Roles.AddUserToRole(member.LoginName, domainRoleName);

    }

    }

    }

    }

    }

     

     

    base.UpdateUser(user);

    }

    }

    The web.config file:

     

    <!--

     

     

    Membership Provider -->

    <

     

     

    membership defaultProvider="UmbracoMembershipProvider" userIsOnlineTimeWindow="15">

    <

     

     

    providers>

    <

     

     

    clear />

    <!--

     

     

    <add name="UmbracoMembershipProvider" type="umbraco.providers.members.UmbracoMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="TestType" />-->

    <

     

     

    add name="UmbracoMembershipProvider" type="Adept.Web.AdeptMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="Member" />

    <

     

     

    add name="AspNetSqlMemberShipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="LocalSqlServer" />

    <

     

     

    add name="UsersMembershipProvider" type="umbraco.providers.UsersMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" />

    </

     

     

    providers>

    </

     

     

    membership>

  • Tom 713 posts 954 karma points
    Feb 03, 2010 @ 00:16
    Tom
    0

    wierd! im copying and pasting from visual studio in a code block and the formatting is still terrible!

  • Richard Soeteman 4046 posts 12899 karma points MVP 2x
    Feb 03, 2010 @ 08:37
    Richard Soeteman
    0

    Hi Tom,

    To answer your question without looking at the coide... It's not possible to get the previous value of a member. The modified object by the user is used in the Event. Not to plug anything, but in UmbImport people can assign roles also. When a member gets updated I first remove all roles and the reassign the selected roles. Maybe that will also work in your case?

    Cheers,

    Richard

     

Please Sign in or register to post replies

Write your reply to:

Draft