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.
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();
}
}
}
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();
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
is working on a reply...