On Nov 28, 2014 12:33 AM, "Bryn Jeffries" <bryn.jeffr...@sydney.edu.au> 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? 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/DocumentAccessBridge.html 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 > users@xwiki.org > http://lists.xwiki.org/mailman/listinfo/users _______________________________________________ users mailing list users@xwiki.org http://lists.xwiki.org/mailman/listinfo/users