Developer forum

Forum » Development » NotificationSubscriber on "DWN_STANDARD_USER_OnExtranetLogin" values from customfields

NotificationSubscriber on "DWN_STANDARD_USER_OnExtranetLogin" values from customfields

Marie Louise Veigert
Reply

Hi,

I need to get some customfield values on user login event ([Subscribe("DWN_STANDARD_USER_OnExtranetLogin")]), because I need to check a field upon login - whether the user should be required reset password or not.
Ive made a custom field, but it's value is null, when I try to access it.

This is some of the code:

[Subscribe("DWN_STANDARD_USER_OnExtranetLogin")]
    public class HandleLoginRouteNotification : NotificationSubscriber, IComparable<NotificationSubscriber>
    {
        public override void OnNotify(string notification, NotificationArgs args)
        {
            if (args != null)
            {
                Dynamicweb.Notifications.Standard.User.OnExtranetLoginArgs myargs = (Dynamicweb.Notifications.Standard.User.OnExtranetLoginArgs)args;
                var argsObject = myargs.User.CustomFieldValues.Where(x => x.CustomField.SystemName == "AccessUser_ResetPasswordOnLogin").ToList();
                if (argsObject != null && argsObject.Any())
                {
                    var resetPasswordValue = argsObject.FirstOrDefault().Value;
                    var requireResetPassword = false;
                    //need to get the field value from user - value on CustomFieldValue is null/empty..?
                    if (requireResetPassword)
                    {
                        string recoveryPasswordPageLink = "/english/sign-in?LoginAction=Recovery";
                    }
                }
            } else
            {
                //arg is null
                var s = "";
            }
}
}


Do anyone have trick to get it? Can't find anything in documentation :)

BR Marie Louise


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

argsObject.FirstOrDefault().Value returns an object that you first need to cast to a boolean.

Here is a quick mock - not tested at all:

[Subscribe(Dynamicweb.Notifications.Standard.User.OnExtranetLogin)]
    public class HandleLoginRouteNotification : NotificationSubscriber, IComparable<NotificationSubscriber>
    {
        public override void OnNotify(string notification, NotificationArgs args)
        {
            if (args != null)
            {
                Dynamicweb.Notifications.Standard.User.OnExtranetLoginArgs myargs = (Dynamicweb.Notifications.Standard.User.OnExtranetLoginArgs)args;
                
                Security.UserManagement.Common.CustomFields.CustomFieldValue customFieldValue = myargs.User.CustomFieldValues.FirstOrDefault(x => x.CustomField.SystemName == "AccessUser_ResetPasswordOnLogin");
                if (customFieldValue != null)
                {
                    var resetPasswordValue = Dynamicweb.Core.Converter.ToBoolean(customFieldValue.Value);
                    //need to get the field value from user - value on CustomFieldValue is null/empty..?
                    if (resetPasswordValue)
                    {
                        string recoveryPasswordPageLink = "/english/sign-in?LoginAction=Recovery";
                    }
                }
            }
            else
            {
                //arg is null
                var s = "";
            }
        }
    }
Votes for this answer: 1
 
Marie Louise Veigert
Reply

Figured the user needs to be saved again to have the values visible even though its a bullet point selector.
Could this have default value in the database instead on just being null?
On the user before save again, it appears as having the first value as default.

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

My code above will return false also for null values and only true once the checkbox is set.

If you want it to be default to true, you have to set that your self.

You can delete the field using SQL and create it again using the SQL firehose like this:

ALTER TABLE AccessUser
ADD AccessUser_ResetPasswordOnLogin boolean default true

Then default value should be true for all new and existing records.

Votes for this answer: 1
 
Marie Louise Veigert
Reply

Thanks for the input Nicolai - I will try these options.

 

You must be logged in to post in the forum