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

11 years ago
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
11 years ago
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
11 years ago
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);
            }
        }
11 years ago
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
  }
}
11 years ago
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.