API Questions on Kentico API.
Version 6.x > API > Scheduled Task Permissions View modes: 
User avatar
Member
Member
CC - 5/16/2012 5:02:43 PM
   
Scheduled Task Permissions
I have a scheduled task that needs additional permissions. How can I modify the permissions the scheduled task runs with?

The permission the task needs is to create a new Product in the eCommerce module.

I have a document type P that is configured as a product and is set to create a new Product record automatically. This works as expected when I create a new P node through CMSDesk. It does not work when the scheduled task runs. The P node is created, but the associated product is not.

Thanks

User avatar
Kentico Support
Kentico Support
kentico_radekm2 - 5/17/2012 7:30:50 AM
   
RE:Scheduled Task Permissions
Hello.

Could you please provide complete code you are using? I guess the problem in permissions is related to the custom code you are using, not schedules task itself. Thank you.

Best Regards,
Radek Macalik

User avatar
Member
Member
CC - 5/18/2012 2:11:41 PM
   
RE:Scheduled Task Permissions
The code in the scheduled task is very straightforward. All of this code works as expected:
// productImport contains values to set on document
TreeNode newNode = TreeNode.New(classname);
newNode.SetValue("value1", productImport.Value1);
newNode.SetValue("value2", productImport.Value2);
// etc
newNode.DocumentCulture = CultureHelper.DefaultCulture.Name;
DocumentHelper.InsertDocument(newNode, parentNodeID, new TreeProvider());

All of that works, and my new node is created with the correct values. Then:

bool? success = document.CreateDefaultSKU();

This always returns false, and in the Event Log I get the following:

Event code: CREATEDEFAULTSKU
Description: Message: User is not authorized for creating products.
Stack Trace:


I get the same error if I configure the document type to always create an e-commerce Product when a new document is created.

As Administrator, I can go through CMSDesk and create a new document, and it will correctly create the related Product. Also, for the nodes created by the scheduled task, as Administrator I can edit them and create Products.

It is only when using the API through a scheduled task that the creation of the Product fails.

User avatar
Kentico Support
Kentico Support
kentico_radekm2 - 5/21/2012 1:21:46 AM
   
RE:Scheduled Task Permissions
Hello.

My first guess is that the problem is in TreeProvider class. You are using empty constructor. But, there is overloaded method with UserInfo object as parameter. I would recommend you use this TreeProvider constructor with some user, who is grant with proper permissions, or the administrator account directly? Thank you.

Best Regards,
Radek Macalik

User avatar
Member
Member
CC - 5/21/2012 4:27:01 PM
   
RE:Scheduled Task Permissions
Thank you, that worked.

It would be nice if this were a property on the scheduled task, so that I could specify which user the scheduled task should run as and therefore not have to code it in. It appears that, by default, scheduled tasks run as a public anonymous user.

User avatar
Kentico Support
Kentico Support
kentico_radekm2 - 5/22/2012 1:16:59 AM
   
RE:Scheduled Task Permissions
Hello.

I understand your point of view. However, there could be a problem with permissions inconsistency. Imagine your example. Someone has permission to run the task, but does not have permissions to execute its logic itself. There could be some confusion then.

However, you can always suggest a feature at http://www.kentico.com/Support/Suggest-feature. Thank you.

Best Regards,
Radek Macalik