Joseph Parsley
Joseph Parsley

Reputation: 11

Sitefinity setting permissions on dynamic content item not working

I'm trying to create a frontend interface for users to manage the permissions of some content for other users.

The code seems to work fine in theory, it executes without any errors and if I look at the items in the backend permissions view I see the permissions I expect there. However, on the view page for the item in question, the permissions don't seem to update.

I know the permissions work on the view generally because if I update them to what I expect using the interface it behaves as I expect, even though the values seem to be exactly the same.

Here's the code:

private string SetDocumentStage(Guid id, string stage, string role = "") {
    DynamicModuleManager manager = DynamicModuleManager.GetManager(providerName);
    Type itemType = TypeResolutionService.ResolveType(documentTypeName);
    DynamicContent liveItem = manager.GetDataItem(itemType, id);                    

    if(liveItem == null)
    {
        return "Item with ID {" + id.ToString() + "} not found.";
    }

    DynamicContent item = manager.Lifecycle.GetMaster(liveItem) as DynamicContent;                    

    // Before making any changes, restore inheritance so we start with a clean slate.
    manager.RestorePermissionsInheritance(item);                     

    string roleNameForView = stage == "Public" ? "Everyone" : role;

    // The default state is admin, so no further changes needed if it's admin.
    if (stage != "Admin")
    {                        
        // In non-admin stages, we should grant either everyone or the working group view permissions.
        // Then, grant just the working group modify permissions, to represent the elevated permissions they need.
        try
        {
            item.ManagePermissions().ForRole(roleNameForView).Grant().View();
            item.ManagePermissions().ForRole(role).Grant().Modify();
        } catch(Exception e)
        {
            return "Error setting permissions. Most likely the role name provided does not exist.";
        }                        
    }

    manager.SaveChanges();

    return "Successfully changed stage to " + stage + ".";
}

Any assistance anyone can provide would be appreciated.

Upvotes: 1

Views: 51

Answers (1)

Veselin Vasilev
Veselin Vasilev

Reputation: 3793

In addition to manager.SaveChanges() you also need to Publish the item.

ILifecycleDataItem publishedAlbumItem = dynamicModuleManager.Lifecycle.Publish(albumItem);
albumItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published");

dynamicModuleManager.SaveChanges();

source: https://www.progress.com/documentation/sitefinity-cms/example-get-and-set-parent-items

Upvotes: 0

Related Questions