If user is Registered he can register again - IsInCustomerRole bugged

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
1. nopCommerce version - 4.10
2. Expected behavior - IsInCustomerRole should return true,if user is already registered
3. Actual behavior - but it return false
4. Steps to reproduce the problem - Register new user, don't logout go to url http://localhost:15536/register ,
write down correct data, and you would be registered again.
5. Any private modifications you made to your nopCommerce , i did some but it doesn't affect the original behavior/logic.


public static bool IsInCustomerRole(...)
{
var result = customer.CustomerRoles
                .FirstOrDefault(cr => (!onlyActiveCustomerRoles || cr.Active) && cr.SystemName == customerRoleSystemName) != null;
}

!onlyActiveCustomerRoles and operator 'or' || is the problem , it would always return false.  
I don't get for what purpose it was made but, if u even set onlyActiveCustomerRoles to false, it would complitly ignore the whole logic
customer.CustomerRoles
                .FirstOrDefault
,
and would return null. Why this is made ?
5 years ago
Seems i'm a bit wrong about problem in the operator

It might be with something in IsInCustomerRole function
if i write this way in

public virtual IActionResult Register(RegisterModel model, string returnUrl, bool captchaValid)
        {
var IsRegistered = _workContext.CurrentCustomer.CustomerRoles
                .FirstOrDefault(cr => cr.Active &&(cr.SystemName == NopCustomerDefaults.RegisteredRoleName) ) != null;
            Debug.WriteLine("model is registered role " + IsRegistered + "crActive "+ crActive);
}


And if i'm already registered IsRegistered  returns true,

but original


public virtual IActionResult Register(RegisterModel model, string returnUrl, bool captchaValid)
        {
if (_workContext.CurrentCustomer.IsRegistered())
            {

}


return false .

maybe because of

 if (customer == null)
                throw new ArgumentNullException(nameof(customer));

            if (string.IsNullOrEmpty(customerRoleSystemName))
                throw new ArgumentNullException(nameof(customerRoleSystemName));


but i don't actually see any throw exception .

Correct me if i'm wrong
5 years ago
[HttpPost]
        [ValidateCaptcha]
        [ValidateHoneypot]
        [PublicAntiForgery]
        //available even when navigation is not allowed
        [CheckAccessPublicStore(true)]
        public virtual IActionResult Register(RegisterModel model, string returnUrl, bool captchaValid)
        {
            //check whether registration is allowed
            if (_customerSettings.UserRegistrationType == UserRegistrationType.Disabled)
                return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.Disabled });

            if (_workContext.CurrentCustomer.IsRegistered())
                    return RedirectToRoute("HomePage");


if you dont want register customer again then put above code. this work.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.