Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Anne 8 posts 48 karma points
    Dec 18, 2013 @ 14:17
    Anne
    0

    Creating a pricegroup by code, no permission set

    Hi,

    Im trying to create a pricegroup by code, and i goes fine. But the permission dont get set, so the user cannot see the pricegroup on products unless you change this under security.

    I tried to create a new role also by code, because it looks like the role first gets created when clicking on the security section. But i couldnt add permission to this role.

    Is there any way to add a new pricegroup to all users and add permission so they can see it..

     

    using (var transaction = statelessSession.BeginTransaction())

                    {

                        var priceGroupObject = new PriceGroup();

                        priceGroupObject.Name = PriceListName.Text;

                        priceGroupObject.VATRate = new decimal(0.25);

                        priceGroupObject.Currency = currency;

                        priceGroupObject.CreatedOn = DateTime.UtcNow;

                        priceGroupObject.CreatedBy = "admin";

                        priceGroupObject.ModifiedOn = DateTime.UtcNow;

                        priceGroupObject.ModifiedBy = "admin";

                        statelessSession.Insert(priceGroupObject);

     

    var userservice = ObjectFactory.Instance.Resolve<IUserService>();

                        PriceGroupRole role = new PriceGroupRole();

                         role.PriceGroup = priceGroupObject;

     

                        foreach (var user in userservice.GetAllUsers())

                        {

                            role.AddUser(user); // this does not add permission to the user.

                        }

                        statelessSession.Insert(role);

                        transaction.Commit();

  • Morten Skjoldager 440 posts 1499 karma points
    Dec 23, 2013 @ 09:24
    Morten Skjoldager
    0

    Hello Anne,

    This should be possible. You can try to resolve RoleService and Call GetAllRoles(). I'm not sure it will work for you in the context you're in though. However it will actively create any missing roles there might be.

    If that doesn't work for you, you can find inspiratino in the code here that creates the roles for you (this is related to a pricegroup role):

    It takes a ProductCatalogGroupRole and all the existing roles. You might want to resolve those first anyways. 

    The basics of it is to add the PriceGroupRole to the ProductCatalogGroupRole before saving.

    Hope this helps.

    Regards Morten

    private void EnsurePriceGroupRoles(ProductCatalogGroupRole productCatalogGroupRole, IList<Role> existingRoles)
            {
                foreach (PriceGroup priceGroup in PriceGroupRepository.Select())
                {
                    Role priceGroupRole =
                        existingRoles.FirstOrDefault(
                            x => x.ParentRole == productCatalogGroupRole && typeof(PriceGroupRole) == x.GetType() &&
                                 ((PriceGroupRole)x).PriceGroup == priceGroup);
    
                    if (priceGroupRole == null && !priceGroup.Deleted)
                    {
                        priceGroupRole = new PriceGroupRole(productCatalogGroupRole.ProductCatalogGroup, priceGroup);
                        productCatalogGroupRole.AddRole(priceGroupRole);
                    }
    
                    if (priceGroupRole != null && priceGroup.Deleted)
                    {
                        // Remove role from any parent roles before deleting it to avoid cascade error.
                        var parentRoles = RoleRepository.Select().Where(x => x.Roles.Contains(priceGroupRole));
                        parentRoles.ForEach(x => x.RemoveRole(priceGroupRole));
    
                        priceGroupRole.Delete();
                    }
                }
            }
Please Sign in or register to post replies

Write your reply to:

Draft