Search in sources :

Example 1 with UserconnectionPK

use of org.orcid.persistence.jpa.entities.UserconnectionPK in project ORCID-Source by ORCID.

the class ProfileEntityManagerImplTest method testDeprecateProfile.

@Test
public void testDeprecateProfile() throws Exception {
    UserconnectionPK pk = new UserconnectionPK();
    pk.setProviderid("providerId");
    pk.setProvideruserid("provideruserid");
    pk.setUserid("4444-4444-4444-4441");
    UserconnectionEntity userConnection = new UserconnectionEntity();
    userConnection.setAccesstoken("blah");
    userConnection.setConnectionSatus(UserConnectionStatus.STARTED);
    userConnection.setDisplayname("blah");
    userConnection.setDateCreated(new Date());
    userConnection.setLastModified(new Date());
    userConnection.setEmail("blah@blah.com");
    userConnection.setOrcid("4444-4444-4444-4441");
    userConnection.setId(pk);
    userConnection.setRank(1);
    userConnectionDao.persist(userConnection);
    ProfileEntity profileEntityToDeprecate = profileEntityCacheManager.retrieve("4444-4444-4444-4441");
    assertNull(profileEntityToDeprecate.getPrimaryRecord());
    boolean result = profileEntityManager.deprecateProfile("4444-4444-4444-4441", "4444-4444-4444-4442");
    assertTrue(result);
    profileEntityToDeprecate = profileEntityCacheManager.retrieve("4444-4444-4444-4441");
    assertNotNull(profileEntityToDeprecate.getPrimaryRecord());
    assertEquals("4444-4444-4444-4442", profileEntityToDeprecate.getPrimaryRecord().getId());
    assertEquals(0, userConnectionDao.findByOrcid("4444-4444-4444-4441").size());
}
Also used : UserconnectionPK(org.orcid.persistence.jpa.entities.UserconnectionPK) UserconnectionEntity(org.orcid.persistence.jpa.entities.UserconnectionEntity) Date(java.util.Date) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 2 with UserconnectionPK

use of org.orcid.persistence.jpa.entities.UserconnectionPK in project ORCID-Source by ORCID.

the class InstitutionalSignInManagerImpl method createUserConnectionAndNotify.

@Override
@Transactional
public void createUserConnectionAndNotify(String idType, String remoteUserId, String displayName, String providerId, String userOrcid, Map<String, String> headers) throws UnsupportedEncodingException {
    UserconnectionEntity userConnectionEntity = userConnectionDao.findByProviderIdAndProviderUserIdAndIdType(remoteUserId, providerId, idType);
    if (userConnectionEntity == null) {
        LOGGER.info("No user connection found for idType={}, remoteUserId={}, displayName={}, providerId={}, userOrcid={}", new Object[] { idType, remoteUserId, displayName, providerId, userOrcid });
        userConnectionEntity = new UserconnectionEntity();
        String randomId = Long.toString(new Random(Calendar.getInstance().getTimeInMillis()).nextLong());
        UserconnectionPK pk = new UserconnectionPK(randomId, providerId, remoteUserId);
        userConnectionEntity.setOrcid(userOrcid);
        userConnectionEntity.setProfileurl(orcidUrlManager.getBaseUriHttp() + "/" + userOrcid);
        userConnectionEntity.setDisplayname(displayName);
        userConnectionEntity.setRank(1);
        userConnectionEntity.setId(pk);
        userConnectionEntity.setLinked(true);
        userConnectionEntity.setLastLogin(new Date());
        userConnectionEntity.setIdType(idType);
        userConnectionEntity.setConnectionSatus(UserConnectionStatus.NOTIFIED);
        userConnectionEntity.setHeadersJson(JsonUtils.convertToJsonString(headers));
        userConnectionDao.persist(userConnectionEntity);
    } else {
        LOGGER.info("Found existing user connection, {}", userConnectionEntity);
    }
    sendNotification(userOrcid, providerId);
}
Also used : Random(java.util.Random) UserconnectionEntity(org.orcid.persistence.jpa.entities.UserconnectionEntity) UserconnectionPK(org.orcid.persistence.jpa.entities.UserconnectionPK) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with UserconnectionPK

use of org.orcid.persistence.jpa.entities.UserconnectionPK in project ORCID-Source by ORCID.

the class SocialController method signinHandler.

@RequestMapping(value = { "/access" }, method = RequestMethod.GET)
public ModelAndView signinHandler(HttpServletRequest request, HttpServletResponse response) {
    SocialType connectionType = socialContext.isSignedIn(request, response);
    if (connectionType != null) {
        Map<String, String> userMap = retrieveUserDetails(connectionType);
        String providerId = connectionType.value();
        String userId = socialContext.getUserId();
        UserconnectionEntity userConnectionEntity = userConnectionManager.findByProviderIdAndProviderUserId(userMap.get("providerUserId"), providerId);
        if (userConnectionEntity != null) {
            if (userConnectionEntity.isLinked()) {
                UserconnectionPK pk = new UserconnectionPK(userId, providerId, userMap.get("providerUserId"));
                userConnectionManager.updateLoginInformation(pk);
                String aCredentials = new StringBuffer(providerId).append(":").append(userMap.get("providerUserId")).toString();
                PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(userConnectionEntity.getOrcid(), aCredentials);
                token.setDetails(new WebAuthenticationDetails(request));
                Authentication authentication = authenticationManager.authenticate(token);
                SecurityContextHolder.getContext().setAuthentication(authentication);
                return new ModelAndView("redirect:" + calculateRedirectUrl(request, response));
            } else {
                ModelAndView mav = new ModelAndView();
                mav.setViewName("social_link_signin");
                mav.addObject("providerId", providerId);
                mav.addObject("accountId", getAccountIdForDisplay(userMap));
                mav.addObject("linkType", "social");
                mav.addObject("emailId", (userMap.get("email") == null) ? "" : userMap.get("email"));
                mav.addObject("firstName", (userMap.get("firstName") == null) ? "" : userMap.get("firstName"));
                mav.addObject("lastName", (userMap.get("lastName") == null) ? "" : userMap.get("lastName"));
                return mav;
            }
        } else {
            throw new UsernameNotFoundException("Could not find an orcid account associated with the email id.");
        }
    } else {
        throw new UsernameNotFoundException("Could not find an orcid account associated with the email id.");
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) Authentication(org.springframework.security.core.Authentication) ModelAndView(org.springframework.web.servlet.ModelAndView) SocialType(org.orcid.frontend.spring.web.social.config.SocialType) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) UserconnectionEntity(org.orcid.persistence.jpa.entities.UserconnectionEntity) UserconnectionPK(org.orcid.persistence.jpa.entities.UserconnectionPK) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

UserconnectionEntity (org.orcid.persistence.jpa.entities.UserconnectionEntity)3 UserconnectionPK (org.orcid.persistence.jpa.entities.UserconnectionPK)3 Date (java.util.Date)2 Random (java.util.Random)1 Test (org.junit.Test)1 SocialType (org.orcid.frontend.spring.web.social.config.SocialType)1 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)1 DBUnitTest (org.orcid.test.DBUnitTest)1 Authentication (org.springframework.security.core.Authentication)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1 WebAuthenticationDetails (org.springframework.security.web.authentication.WebAuthenticationDetails)1 PreAuthenticatedAuthenticationToken (org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken)1 Transactional (org.springframework.transaction.annotation.Transactional)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1