{"action":"closed","issue":{"url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/4024","repository_url":"https://api.github.com/repos/jhipster/generator-jhipster","labels_url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/4024/labels{/name}","comments_url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/4024/comments","events_url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/4024/events","html_url":"https://github.com/jhipster/generator-jhipster/issues/4024","id":172852464,"number":4024,"title":"Login social: If a twitter account doesn't have an e-mail, JHipster sets the wrong user_id","user":{"login":"jodevan","id":8137280,"avatar_url":"https://avatars.githubusercontent.com/u/8137280?v=3","gravatar_id":"","url":"https://api.github.com/users/jodevan","html_url":"https://github.com/jodevan","followers_url":"https://api.github.com/users/jodevan/followers","following_url":"https://api.github.com/users/jodevan/following{/other_user}","gists_url":"https://api.github.com/users/jodevan/gists{/gist_id}","starred_url":"https://api.github.com/users/jodevan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jodevan/subscriptions","organizations_url":"https://api.github.com/users/jodevan/orgs","repos_url":"https://api.github.com/users/jodevan/repos","events_url":"https://api.github.com/users/jodevan/events{/privacy}","received_events_url":"https://api.github.com/users/jodevan/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-08-24T02:46:10Z","updated_at":"2016-08-26T07:21:28Z","closed_at":"2016-08-26T07:21:28Z","body":"[yo-rc.json.zip](https://github.com/jhipster/generator-jhipster/files/433891/yo-rc.json.zip)\r\n\r\n##### **Overview of the issue**\r\n\r\nIf a twitter account doesn't have an e-mail, JHipster sets the wrong user_id in the jhi_social_user_connection table. This user_id belongs to the wrong user.\r\n\r\n##### **Motivation for or Use Case**\r\n\r\nSetting the user_id that belongs to another user is a really bad thing :-)\r\n\r\n##### **Reproduce the error**\r\n\r\n1. Have an existing non-social user in the database;\r\n2. Login with a twitter user that doesn't have an e-mail set;\r\n3. Once logged in, you'll see \"You are logged in as user xxx.\", where xxx is a pre-existing user.\r\n\r\n##### **Related issues**\r\n\r\nNot applicable\r\n\r\n##### **Suggest a Fix**\r\n\r\nThe problem lays in the method createUserIfNotExist(UserProfile userProfile, String langKey, TipoUsuario tipoUsuario, String providerId), in class SocialService:\r\n\r\n```java\r\nprivate User createUserIfNotExist(UserProfile userProfile, String langKey, TipoUsuario tipoUsuario, String providerId) {\r\n    String email = userProfile.getEmail();\r\n    String userName = userProfile.getUsername();\r\n    if (!StringUtils.isBlank(userName)) {\r\n        userName = userName.toLowerCase(Locale.ENGLISH);\r\n    }\r\n    if (StringUtils.isBlank(email) && StringUtils.isBlank(userName)) {\r\n        log.error(\"Cannot create social user because email and login are null\");\r\n        throw new IllegalArgumentException(\"Email and login cannot be null\");\r\n    }\r\n    if (StringUtils.isBlank(email) && userRepository.findOneByLogin(userName).isPresent()) {\r\n        log.error(\"Cannot create social user because email is null and login already exist, login -> {}\", userName);\r\n        throw new IllegalArgumentException(\"Email cannot be null with an existing login\");\r\n    }\r\n\r\n    // **********************************************************************************************************\r\n    // If email is null or blank, this method will bring some arbitrary user from the database\r\n    // **********************************************************************************************************\r\n    Optional<User> user = userRepository.findOneByEmail(email);\r\n    if (user.isPresent()) {\r\n        log.info(\"User already exist associate the connection to this account\");\r\n        return user.get();\r\n    }\r\n// ...\r\n}\r\n```\r\n\r\nSuggested fix: just test if the email address is not empty:\r\n```java\r\nif (!StringUtils.isBlank(email)) {\r\n    Optional<User> user = userRepository.findOneByEmail(email);\r\n    if (user.isPresent()) {\r\n        log.info(\"User already exist associate the connection to this account\");\r\n        return user.get();\r\n    }\r\n}\r\n```\r\n\r\n##### **JHipster Version(s)**\r\n\r\n3.4.2\r\n\r\n##### **JHipster configuration, a `.yo-rc.json` file generated in the root folder**\r\n\r\n{\r\n  \"generator-jhipster\": {\r\n    \"jhipsterVersion\": \"3.4.2\",\r\n    \"baseName\": \"tc\",\r\n    \"packageName\": \"com.tc\",\r\n    \"packageFolder\": \"com/tc\",\r\n    \"serverPort\": \"8080\",\r\n    \"authenticationType\": \"session\",\r\n    \"hibernateCache\": \"hazelcast\",\r\n    \"clusteredHttpSession\": \"hazelcast\",\r\n    \"websocket\": \"spring-websocket\",\r\n    \"databaseType\": \"sql\",\r\n    \"devDatabaseType\": \"mysql\",\r\n    \"prodDatabaseType\": \"mysql\",\r\n    \"searchEngine\": \"no\",\r\n    \"buildTool\": \"maven\",\r\n    \"enableSocialSignIn\": true,\r\n    \"rememberMeKey\": \"3f987da705ca593beefbc9c6ef079575463b6d01\",\r\n    \"useSass\": false,\r\n    \"applicationType\": \"monolith\",\r\n    \"testFrameworks\": [\r\n      \"protractor\"\r\n    ],\r\n    \"jhiPrefix\": \"jhi\",\r\n    \"enableTranslation\": false\r\n  }\r\n}\r\n\r\n##### **Entity configuration(s) `entityName.json` files generated in the `.jhipster` directory**\r\n\r\nNot applicable\r\n\r\n##### **Browsers and Operating System**\r\n\r\nNot applicable\r\n"}}
{"actor":{"display_login":"deepu105"}}