[xwiki-users] Getting user name with a Java Component
What's the right way to get the current user from the execution context within a Java component? Taking the example from the guide (http://platform.xwiki.org/xwiki/bin/view/DevGuide/WritingComponents) I thought it would be something like the following, which won't compile for me due to the getLocalUser symbol not being found. import org.xwiki.context.Execution; import org.xwiki.context.ExecutionContext; ... @Inject private Execution execution; @Override public String sayHello() { ExecutionContext context = execution.getContext(); String user = context.getLocalUser(); return "Hello " + user; } I note that in http://nexus.xwiki.org/nexus/service/local/repositories/releases/archive/org... the getLocalUser() method is deprecated in lieu of getUserReference() but I'd really rather deal with a String if at all possible.
Looks like you did not read http://platform.xwiki.org/xwiki/bin/view/DevGuide/WritingComponents#HTheXWik... fully and are mixing ExecutionContext and XWikiContext, ExecutionContext does not have any typed methods, it's just a map basically and most of the time you don't really use it directly but use an API that manipulate the context. 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. On Thu, Nov 27, 2014 at 7:19 AM, Bryn Jeffries <[email protected]> wrote:
What's the right way to get the current user from the execution context within a Java component?
Taking the example from the guide (http://platform.xwiki.org/xwiki/bin/view/DevGuide/WritingComponents) I thought it would be something like the following, which won't compile for me due to the getLocalUser symbol not being found.
import org.xwiki.context.Execution; import org.xwiki.context.ExecutionContext; ... @Inject private Execution execution;
@Override public String sayHello() { ExecutionContext context = execution.getContext();
String user = context.getLocalUser(); return "Hello " + user;
}
I note that in http://nexus.xwiki.org/nexus/service/local/repositories/releases/archive/org... the getLocalUser() method is deprecated in lieu of getUserReference() but I'd really rather deal with a String if at all possible. _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
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/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(). 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
On Nov 28, 2014 12:33 AM, "Bryn Jeffries" <[email protected]> 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/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
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
From: [email protected] [[email protected]]
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. I've actually read through that page several times, but it's only now making sense to me after having given things a try and getting the help on this mailling list. FWIW the problem for a new user is that there is a mix of documentation applying to several versions of XWiki (especially when searching via Google). This is partly the curse of wikis, since they don't go through structured revision control. It's so good that the mailling list is so responsive to put us straight. Thanks, Bryn
From Marius:
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()
I think this is what I need to do. I have a separate database which uses proxy authentication so that queries give different results based on the user (as a form of ACL). So I need the uid for the current user, which is mapped to the wiki alias. Many thanks, Bryn
participants (4)
-
Bryn Jeffries -
Marius Dumitru Florea -
Thomas Mortagne -
vincent@massol.net