6 Replies Latest reply on Jan 21, 2016 6:40 AM by moesingh

    How to get current user in jive 8

    darshan_patel Novice

      We are migrating from jive 7 to jive 8.

      In jive 7 we can get current user by calling static method getCurrentUser() available in JiveGlobals but in jive 8 this static method is not available.

       

      please guide me how can i get current user in jive 8.

       

      Ryan King

        • Re: How to get current user in jive 8
          darshan_patel Novice

          I found that

           

          JiveApplication.getEffectiveContext().getAuthenticationProvider().getJiveUser()

           

          Correct me if I am wrong..

          • Re: How to get current user in jive 8
            oicfar Beginner

            I prefer this one:

             

            User currentUser = UserPermHelper.getEffectiveUser();
            

             

            It's short

              • Re: How to get current user in jive 8
                Expert

                While the static getEffectiveUser() method on the UserPermHelper class will work, it can cause problems when it comes to unit testing your code.  I find it much easier to use injectable dependencies to execute this kind of call.

                 

                Since PluginTemplateDefinitions are wired by Spring, you have access to any Spring bean defined in the system.  It so happens, Darshan Patel, that the AuthenticationProvider class is a Spring-managed bean, which means you can do the following:

                 

                In your plugin's spring.xml file, add the following property to your PluginTemplateDefinition's Spring bean definition:

                <property name="authenticationProvider" ref="authenticationProvider" />

                 

                Then, in your PluginTemplateDefinition implementation, include the AuthenticationProvider as a property:

                package com.whatever.mypackage;

                 

                ...

                import com.jivesoftware.base.aaa.AuthenticationProvider;

                ...

                     public class MyPluginTemplateDefinition implements PluginTemplateDefinition {

                ...

                     private AuthenticationProvider authenticationProvider;

                ...

                     public void setAuthenticationProvider(AuthenticationProvider authenticationProvider) {

                          this.authenticationProvider = authenticationProvider;

                     }

                ...

                }

                 

                This approach will allow you to adequately unit test your PluginTemplateDefinition class, and mock out the call to the AuthenticationProvider instance using a mock framework like Mockito, which would look something like:

                ...

                @RunWith(MockitoJUnitRunner.class)

                public class MyPluginTemplateDefinitionTest {

                     @InjectMocks private MyPluginTemplateDefinition ptd = new MyPluginTemplateDefinition();

                    

                     @Mock AuthenticationProvider authProvider;

                     @Mock User user;

                 

                     @Before

                     public void setup() {

                          when(authProvider.getJiveUser()).thenReturn(user);

                     }

                ...

                }

                 

                This will cause any call to the getJiveUser() method to return your mocked User instance, which you can then define mocked method implementations for, in the same way you did for AuthenticationProvider.

                 

                Hope this helps!

                2 people found this helpful
              • Re: How to get current user in jive 8
                darshan_patel Novice

                Thanks for quick reply Rafał Kędziorski and Ryan King

                 

                We have developed plugin for gamification in which we have extended PluginTemplateDefinition class to display points on user's profile.

                • Re: How to get current user in jive 8
                  moesingh Novice

                  kind of related fyi - if you want the current user via Javascript, don't use jquery and get it from the avatar, since the varible name is just _jive_current_user.  there are a bunch of other variables there, run chrome debug (f12), go to console and type _jive for the list. e.g. _jive_base_absolute_url will give you the url with context root.