Commit 0c2e5832 authored by Matija Obreza's avatar Matija Obreza

Set user#loginType to GOOGLE when Google+ sign-in is first used

parent 195332f4
/*
* Copyright 2016 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.server.model.impl;
public enum LoginType {
......
......@@ -82,7 +82,7 @@ public class User extends BusinessModel {
private String name;
@Enumerated(EnumType.STRING)
@Column(name = "loginType")
@Column(name = "loginType", length=10, nullable=false, columnDefinition="VARCHAR(10) DEFAULT 'PASSWORD'")
private LoginType loginType;
// validation
......
......@@ -92,4 +92,6 @@ public interface UserService {
List<User> autocompleteUser(String email);
User setLoginType(User user, LoginType loginType);
}
......@@ -67,8 +67,12 @@ import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {
private static final Log LOG = LogFactory.getLog(UserServiceImpl.class);
/// A non-password used for system and Google accounts
private static final String THIS_IS_NOT_A_PASSWORD = "THIS-IS-NOT-A-PASSWORD";
@Autowired
private UserPersistence userPersistence;
......@@ -225,9 +229,8 @@ public class UserServiceImpl implements UserService {
@Transactional(readOnly = false, rollbackFor = NotUniqueUserException.class)
public void addUser(User user) throws UserException, PasswordPolicyException {
try {
if (user.isSystemAccount() || user.getPassword() == null
|| user.getLoginType() == LoginType.GOOGLE) {
user.setPassword("THIS-IS-NOT-A-PASSWORD");
if (user.isSystemAccount() || user.getPassword() == null || user.getLoginType() == LoginType.GOOGLE) {
user.setPassword(THIS_IS_NOT_A_PASSWORD);
} else {
setPassword(user, user.getPassword());
}
......@@ -270,7 +273,7 @@ public class UserServiceImpl implements UserService {
LOG.warn("Invalid email provided: " + email);
throw new UserException("Invalid email provided: " + email);
}
if (! StringUtils.equals(email, user.getEmail()) && userPersistence.findByEmail(email) != null) {
if (!StringUtils.equals(email, user.getEmail()) && userPersistence.findByEmail(email) != null) {
throw new NotUniqueUserException(new Throwable(), email);
}
......@@ -345,14 +348,13 @@ public class UserServiceImpl implements UserService {
*/
private void setPassword(User user, String rawPassword) throws PasswordPolicyException {
if (user.getLoginType() == LoginType.GOOGLE) {
user.setPassword("THIS-IS-NOT-A-PASSWORD");
user.setPassword(THIS_IS_NOT_A_PASSWORD);
return;
} else {
passwordPolicy.assureGoodPassword(rawPassword);
// encrypt password
user.setPassword(passwordEncoder.encode(rawPassword));
}
passwordPolicy.assureGoodPassword(rawPassword);
// encrypt password
user.setPassword(passwordEncoder.encode(rawPassword));
}
@Override
......@@ -530,4 +532,17 @@ public class UserServiceImpl implements UserService {
return Collections.emptyList();
return userPersistence.autocompleteByEmail(email + "%", new PageRequest(0, 10, new Sort("email")));
}
@Override
@Transactional
public User setLoginType(User user, LoginType loginType) {
User u = userPersistence.findOne(user.getId());
u.setLoginType(loginType);
if (loginType == LoginType.GOOGLE) {
user.setPassword(THIS_IS_NOT_A_PASSWORD);
}
return userPersistence.save(u);
}
}
......@@ -30,6 +30,7 @@ import org.genesys2.server.servlet.util.GoogleOAuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
......@@ -80,8 +81,17 @@ public class GoogleSocialController extends BaseController {
final Google google = new GoogleTemplate(accessToken);
final Person userInfo = google.plusOperations().getGoogleProfile();
if (!userService.exists(userInfo.getAccountEmail())) {
final User user = userService.createAccount(userInfo.getAccountEmail(), null, userInfo.getDisplayName(), LoginType.GOOGLE);
User user = null;
try {
user = userService.getUserByEmail(userInfo.getAccountEmail());
if (user.getLoginType() == LoginType.PASSWORD) {
// account exists, change to {@link LoginType#GOOGLE}
_logger.info("Changing account type to LoginType#GOOGLE");
userService.setLoginType(user, LoginType.GOOGLE);
}
} catch (UsernameNotFoundException e) {
_logger.info("Username not found, creating new Google account");
user = userService.createAccount(userInfo.getAccountEmail(), null, userInfo.getDisplayName(), LoginType.GOOGLE);
userService.userEmailValidated(user.getUuid());
}
......
......@@ -16,18 +16,18 @@
<div class="form-horizontal">
<div class="form-group">
<label for="password" class="col-lg-2 control-label"><spring:message code="user.full-name" /></label>
<div class="col-lg-5"><c:out value="${user.name}" /></div>
<div class="col-lg-5 form-control-static"><c:out value="${user.name}" /></div>
</div>
<security:authorize access="hasRole('ADMINISTRATOR') || (isAuthenticated() && principal.user.id == #user.id)">
<div class="form-group">
<label for="password" class="col-lg-2 control-label"><spring:message code="user.email" /></label>
<div class="col-lg-5"><c:out value="${user.email}" /></div>
<div class="col-lg-5 form-control-static"><c:out value="${user.email}" /></div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label"><spring:message code="user.account-status" /></label>
<div class="col-lg-5">
<div class="col-lg-5 form-control-static">
<c:if test="${user.systemAccount}">SYSTEM</c:if>
<c:if test="${not user.enabled}"><spring:message code="user.account-disabled" /></c:if>
<c:if test="${user.accountLocked}"><spring:message code="user.account-locked-until" />
......@@ -38,12 +38,12 @@
<div class="form-group">
<label class="col-lg-2 control-label"><spring:message code="user.login-type" /></label>
<div class="col-lg-5"><c:out value="${user.loginType}" /></div>
<div class="col-lg-5 form-control-static"><c:out value="${user.loginType}" /></div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label"><spring:message code="user.roles" /></label>
<div class="col-lg-10">
<div class="col-lg-10 form-control-static">
<c:forEach items="${user.roles}" var="role">
<div><c:out value="${role}" /></div>
</c:forEach>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment