Users & Roles

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
What's the easiest way to assign a role dynamically (ie: via c# code)?
13 years ago
*bump*
13 years ago
Look at CustomerManager.AddCustomerToRole() method (NopSolutions.NopCommerce.BusinessLogic.CustomerManagement namespace)
13 years ago
Thank you kind sir, for answering my question, and for releasing this app.

For anybody else who may have this question in the future...

If you want to see the source, in the Solution Explorer, it's:

Libraries
Nop.BusinessLogic
Customer
CustomerManager.cs
13 years ago
Can you please help me  Mr. a.m?

When I try to add a user to staff role vi aadmin area,I got  object instance not set to instance of object.
when debugging :


        /// <summary>
        /// Adds a customer to role
        /// </summary>
        /// <param name="customerId">Customer identifier</param>
        /// <param name="customerRoleId">Customer role identifier</param>
        public static void AddCustomerToRole(int customerId, int customerRoleId)
        {
            var customer = GetCustomerById(customerId);
            if (customer == null)
                return;

            var customerRole = GetCustomerRoleById(customerRoleId);
            if (customerRole == null)
                return;

            var context = ObjectContextHelper.CurrentObjectContext;
            if (!context.IsAttached(customer))
                context.Customers.Attach(customer);
            if (!context.IsAttached(customerRole))
                context.CustomerRoles.Attach(customerRole);

            customer.NpCustomerRoles.Add(customerRole);<<<<<<<<<<<<<NpCustomerRoles is null
            context.SaveChanges();
        }

Am I missing something?
13 years ago
Add the following code before "customer.NpCustomerRoles.Add(customerRole)":
//ensure that navigation property is loaded
if (customer.NpCustomerRoles == null)
_context.LoadProperty(customer, c => c.NpCustomerRoles);
13 years ago
Thanks a lot a.m.

The code works perfectly ,I'd like to tell you that I used the same code in function
public static void RemoveCustomerFromRole(int customerId, int customerRoleId) as I faced the same error.
Now I can add,remove roles to customers perfectly.
13 years ago
Thanks a lot for your reply
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.