VMware {code} Community
Sumit_Tyagi
Enthusiast
Enthusiast
Jump to solution

get current user name on vsphere web client

Hi All,

How can we get the current logged in username in vsphere web client. Threads https://communities.vmware.com/message/2297455#2297455 and https://communities.vmware.com/message/2246358#2246358 were useful, but in the threads, code connects to vCenter server, I want to get current logged in user. How can we get UserSession object without calling vimPort.login. Is there any way to get connected vimPort object for currenlty logged in user in vsphere web client.

I need to know the name of current logged-in user for custom access privilages to be given in my plugin.

Any help will be greatly appreciated.

Best Regards,

Sumit Tyagi

Reply
0 Kudos
1 Solution

Accepted Solutions
laurentsd
VMware Employee
VMware Employee
Jump to solution

UserSessionService.getUserSession().userName gives you the user name.

View solution in original post

Reply
0 Kudos
10 Replies
laurentsd
VMware Employee
VMware Employee
Jump to solution

UserSessionService.getUserSession().userName gives you the user name.

Reply
0 Kudos
Sumit_Tyagi
Enthusiast
Enthusiast
Jump to solution

Hi Laurents,

Thank you for quick reply. I am new to VC Plugin development but I didn't knew that it is that much simple. Will try that out. Thank you for your help!

Best Regards,

Sumit Tyagi

Reply
0 Kudos
Sumit_Tyagi
Enthusiast
Enthusiast
Jump to solution

Hi Laurents,

I was trying to send UserSessionService to my own custom service constructor, but it seems that it was creating problems. It was trying to receive UserSessionService object even before any user logged in.

Then I saw that ObjectNavigator service constructor is also having UserSessionService object passed. I saved that UserSessionService in a public class SessionHelper, and exposed static method to access username. My code is working fine now, but just wanted to know if this approach is ok? or is there any better approach as well?

Thanks for your valuable inputs.

Best Regards,

Sumit Tyagi

Reply
0 Kudos
laurentsd
VMware Employee
VMware Employee
Jump to solution

The proper way to use services is to inject them as beans to your constructors, like this in the vsphere-wssdk-provider sample.

It's hard to comment on your code without seeing it but in general static methods should be avoided, they lead to errors in a multi-threaded environment!

   <bean name="VmDataProviderImpl" class="com.vmware.samples.wssdkprovider.VmDataProviderImpl">

      <constructor-arg ref="userSessionService"/>

      <constructor-arg ref="vimObjectReferenceService"/>

      <constructor-arg ref="dataServiceExtensionRegistry"/>

   </bean>

   /**

    *  Constructor used to inject the utility services (see the declaration

    *  in main/resources/spring/bundle-context-osgi.xml)

    *

    * @param userSessionService

    *    Service to access the current session information.

    * @param vimObjectReferenceService

    *    Service to access vSphere object references information.

    * @param registry

    *    Registration interface for data adapters and providers.

    */

   public VmDataProviderImpl(

            UserSessionService userSessionService,

            VimObjectReferenceService vimObjectReferenceService,

            DataServiceExtensionRegistry registry) {

      _userSessionService = userSessionService;

      _vimObjectReferenceService = vimObjectReferenceService;

      registry.registerDataAdapter(this, getProvidedTypeInfos());

   }

then you can do this:

UserSession userSession = _userSessionService.getUserSession();

Sumit_Tyagi
Enthusiast
Enthusiast
Jump to solution

Hi Laurents,

This is what I was trying to do in my custom adaptor. But while installing it gave the error "getUserSession called on an inactive session." And then I was not able to login to the vsphere web client (message to reload page came again and again). I just need the current user name, so I think keeping it at static place should not be an issue. But one thing I learnt from your reply is that I should make my username static accessor method as synchronized.

Thank you for quick reply and valuable suggestions.

Best Regards,

Sumit Tyagi

Reply
0 Kudos
laurentsd
VMware Employee
VMware Employee
Jump to solution

Your code is wrong if you try to call getUserSession inside a service constructor, when your plugin bundle is being deployed.

I am not sure what you are trying to do by calling this too early, you cannot access the user name unless your method is called as part of a service call from the Flex client.

Reply
0 Kudos
erikDoesStorage
Enthusiast
Enthusiast
Jump to solution



I am trying to understand when the user session can be retrieved from the userSessionService.


I am able to successfully access the user session when doing a getData() in my Data Adapter.


However I get a "getUserSession called on an inactive session" when I try to access it when I attempt to access it when doing a redirect using my redirection servlet.   To clarify I have in my plugin.xml file:


 <extension id="com.company.vcenterWebClientui.system.SummaryView">




      <extendedPoint>com.company.vcenterWebClientui.system.summaryViews</extendedPoint>




      <object>




         <name>#{system.summary.view}</name>




         <componentClass className="com.vmware.vsphere.client.htmlbridge.HtmlView">




            <object>




               <root>




                 &lt;url&gt;/vsphere-client/vcenterWebClient-ui/redirect?page=ManagementPage&lt;/url&gt; 


               </root>




            </object>




         </componentClass>




      </object>




   </extension>




This goes to my redirect servlet, where I attempt to access the user session, but get the error message about an inactive session.  


What are the rules/expectations about being able to access the user session?

Reply
0 Kudos
laurentsd
VMware Employee
VMware Employee
Jump to solution

Erik,

In order to have the http session properly authenticated in this case you need to use a URI that ends with .html (this is an artificial requirement imposed by implementation details), for instance  /vsphere-client/vcenterWebClient-ui/redirect.html?page=ManagementPage

Reply
0 Kudos
VaibhavJain
Contributor
Contributor
Jump to solution

Hi Laurent,

I also have a requirement to get all ESXi hosts when my plugin is being deployed, is there any way i can get or establish a UserSession during init or in the constructor of Impl class to get the ESXi hosts or any other required data from vcenter. It may be fine to collect the required data even after the plugin is deployed, only requirement is to collect this info without waiting for user to login to web client. 

As discussed here calling getUserSession() at that stage results in "getUserSession called on an inactive session." error.


We also use vim25 sdk, if there is any way that can be used for this purpose?


Thanks,

Vaibhav 

Reply
0 Kudos
laurentsd
VMware Employee
VMware Employee
Jump to solution

You can't access vCenter data without a valid user session so your requirement is not possible.  There is nothing wrong with lazy initialization of the plugin's java service at the time the first user accesses it (for instance to figure out the location of your back-end based on the plugin's vCenter extension info).  If you are talking about initialization of your back-end to get the list of ESX hosts in the environment you could also do it with your own standalone script or java program with WSSDK (where you provide your admin credentials, etc.)

Reply
0 Kudos