How to determine whether or not a User is within a certain Role? [RESOLVED]

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 anni tempo fa
Hey,

I was wondering how I would go about determining if a user is within a certain role?

All I can think of is
NopContext.Current.User.CustomerRoles


Is there something like
NopContext.Current.User.CustomerRole("Admin");
?

Thanks
13 anni tempo fa
Still having trouble with this.

To make things clear, I just want to prove in and if/else if a user is within a role e.g. "Vendor".

Thanks
13 anni tempo fa
Here's what I've got so far, and it doesn't seem to work. I've checked if the user I'm logged in as has this role, but it's still returning false.

protected virtual bool ValidateVendorSecurity()
        {
            CustomerRole role = CustomerManager.GetCustomerRoleByID(5); // role id 5 = vendor
            Customer customer = CustomerManager.GetCustomerByID(NopContext.Current.User.CustomerID);

            if (NopContext.Current == null ||
                NopContext.Current.User == null ||
                NopContext.Current.User.IsGuest)
            {
                return false;
            }
            else
            {
                return CustomerManager.GetCustomerRolesByCustomerID(customer.CustomerID).Contains(role);
            }
        }
13 anni tempo fa
using NopSolutions.NopCommerce.BusinessLogic.CustomerManagement;



public string getCustomerRole
{
    get
    {
  CustomerRoleCollection customerRoles = new CustomerRoleCollection();
  if (NopContext.Current.User != null)
  {
      customerRoles = CustomerManager.GetCustomerRolesByCustomerID(NopContext.Current.User.CustomerID);
      CustomerRole customerRole = new CustomerRole();
      foreach (CustomerRole custRole in customerRoles)
      {
    if (custRole.CustomerRoleID == 5)
    {
        customerRole = CustomerManager.GetCustomerRoleByID(custRole.CustomerRoleID);
    }
      }
      return customerRole.Name;
  }
  return "";
    }
}


***********************

if (NopContext.Current.User != null)
{
  if (!String.IsNullOrEmpty(getCustomerRole) && getCustomerRole.Equals("vendor"))
  {
      // Do whatever
  }
}
13 anni tempo fa
Just ended up finding a much easier way to do this!

Page.User.IsInRole("Vendor")


Apparently this is in the original header file, which I had modified earlier and removed.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.