On 28 Nov 2014 at 07:00:26, Marius Dumitru Florea ([email protected](mailto:[email protected])) wrote:
On Nov 28, 2014 12:33 AM, "Bryn Jeffries" wrote:
I wrote:
What's the right way to get the current user from the execution context within a Java component?
Thomas Mortagne replied:
For what you need the simplest in a component is usually to use org.xwiki.bridge.DocumentAccessBridge component (from xwiki-platform-bridge module) until a proper user manager api is introduced.
OK, so I should not use ExecutionContext at all?
You’re right, our goal is to have all context information (including the user) be put in the Execution Context. However, currently, we’re in a transition phase, trying to move from the old XWikiContext to the new ExecutionContext and thus a lot of information is currently still held in the XWikiContext. See the details here: http://platform.xwiki.org/xwiki/bin/view/DevGuide/WritingComponents#HTheXWik... Thanks -Vincent
It looks the the approach you're suggesting is:
1) Inject a DocumentAccessBridge instead of an Execution, e.g.: @Inject private DocumentAccessBridge bridge;
2) Get a reference to the current user (According to
http://maven.xwiki.org/site/docs/xwiki-javadoc-5.0.x/org/xwiki/bridge/Docume... which might be out of date but I can't find a more recent API doc)
DocumentReference userDoc = bridge.getCurrentUserReference();
3) Presumably the document reference captures the full location of the user's profile page. The DocumentReference API doesn't appear to have a page name accessor, so presumably to extract the user name I need to pull it out of userDoc.toString().
What information do you need precisely? The user alias (what the user uses to log in)? Or the user pretty name?
The user alias is the name of the user profile document.
bridge.getCurrentUserReference().getName()
For the pretty name you need to get the value of the first_name and last_name properties from the user profile document for which you have the reference: the user reference.
bridge.getProperty...
Hope this helps, Marius
Is this right? Conceptually I find this less intuitive than using a
context object, so it's a pity if that's been deprecated.
Thanks,
Bryn
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users