Hello,
AEM: 6.2
I have a use case where on inject properties of a component, my sling model executes a @PostConstruct method to configure a state of content by setting a node property value. For example:
@PostConstruct
protected void setProperty() {
try {
ModifiableValueMap map = this.resource.adaptTo(ModifiableValueMap.class);
(*) map.put("customProperty", "initalStateValue");
resource.getResourceResolver().commit();
} catch (PersistenceException e) {
LOGGER.error(e);
}
}
Now, when i publish a page which uses this AEM component, the anonymous user on the published site gets an error message:
On further analysis: I have found that since "anonymous" user does not have permissions to write on /content nodes, the statement marked above with an (*) sets map object to null.
My Question is: Is there a better implementation pattern to achieve the scenario of configuring the state of content node such that the state of the content can be set through node property only for authors?
Views
Replies
Total Likes
Hi ,
This forum is dedicated for queries related to Adobe DTM. Hence we re-posting your query to AEM Forum
Thanks & Regards
Parit Mittal
Views
Replies
Total Likes
Dear Varun,
Usually, when you do content authoring in AEM, you do set all of the properties such as your "customProperty" via the dialog's[0]. Which automatically send your data to SlingPostServlet[1] that handles saving data to the database.
You normally, should not write data when you generate SlingModel.
In case if you do need to do it, due to your unique and awesome! requirements.
Kindly refer to the following guide[2] on how to allow 'anonymous' to write into your database.
[0] http://www.aemcq5tutorials.com/tutorials/create-dialogs-aem/
[2] http://stackoverflow.com/questions/31350548/resourceresolverfactory-getserviceresourceresolver-throw... (look at accepted answer)
Kindest Regards,
Peter
Views
Replies
Total Likes
The "customProperty" is not part of the dialog and hence not added via authoring interface.
Question: "You normally, should not write data when you generate SlingModel."
What if I require to set/unset a property (such as "customProperty") whenever the page is loaded? In such circumstance, without apply business logic of updating properties via SlingModel, is there any other scheme or design I could follow?
Views
Replies
Total Likes
Dear Varun,
Correct, normally as per SlingModel design goals:
However, it fit's well with your 'customProperty' approach then go for it.
You can add an if check to make sure that you are adding custom property only for Author and get run mode(whether it's Publisher or Author) via the SlingSettingsService[0].
[0]
https://sling.apache.org/documentation/bundles/sling-settings-org-apache-sling-settings.html
Regards,
Peter
Views
Replies
Total Likes