Commit b74449ff authored by Matija Obreza's avatar Matija Obreza

User repository can't be @Autowired

parent 9b2ccaf4
...@@ -59,7 +59,7 @@ public interface BasicUserService<R extends GrantedAuthority, T extends BasicUse ...@@ -59,7 +59,7 @@ public interface BasicUserService<R extends GrantedAuthority, T extends BasicUse
* @param email unique email address * @param email unique email address
* @param fullName Full name * @param fullName Full name
* @param password initial account password * @param password initial account password
* @param accountType TODO * @param accountType account type
* @return the new user * @return the new user
* @throws NotUniqueUserException user is not unique in the system * @throws NotUniqueUserException user is not unique in the system
* @throws PasswordPolicyException password violates current password policy * @throws PasswordPolicyException password violates current password policy
......
/* /*
* Copyright 2017 Global Crop Diversity Trust * Copyright 2018 Global Crop Diversity Trust
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -62,8 +62,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -62,8 +62,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
private long accountLockoutTime = 5 * 60 * 1000; private long accountLockoutTime = 5 * 60 * 1000;
/** The user repository. */ /** The user repository. */
@Autowired private JpaRepository<T, Long> _repository;
private JpaRepository<T, Long> userRepository;
/** The password encoder. */ /** The password encoder. */
@Autowired(required = false) @Autowired(required = false)
...@@ -79,6 +78,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -79,6 +78,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
@Override @Override
@Transactional @Transactional
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
this._repository = getUserRepository();
try { try {
loadUserByUsername(BasicUserService.SYSTEM_ADMIN); loadUserByUsername(BasicUserService.SYSTEM_ADMIN);
} catch (UsernameNotFoundException e) { } catch (UsernameNotFoundException e) {
...@@ -95,6 +95,8 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -95,6 +95,8 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
} }
} }
protected abstract JpaRepository<T, Long> getUserRepository();
/** /**
* Implementations must create a user with specified username with ADMINISTRATOR * Implementations must create a user with specified username with ADMINISTRATOR
* role and account type {@link AccountType#SYSTEM} * role and account type {@link AccountType#SYSTEM}
...@@ -150,7 +152,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -150,7 +152,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
*/ */
@Override @Override
public T getUser(final long id) { public T getUser(final long id) {
final T user = userRepository.findOne(id); final T user = _repository.findOne(id);
return deepLoad(user); return deepLoad(user);
} }
...@@ -172,7 +174,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -172,7 +174,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
@PreAuthorize("hasRole('ADMINISTRATOR') || principal.id == #user.id") @PreAuthorize("hasRole('ADMINISTRATOR') || principal.id == #user.id")
public T updateUser(T user, final String email, final String fullName) throws NotUniqueUserException, UserException { public T updateUser(T user, final String email, final String fullName) throws NotUniqueUserException, UserException {
// reload // reload
user = userRepository.findOne(user.getId()); user = _repository.findOne(user.getId());
if (!StringUtils.equals(email, user.getEmail()) && getUserByEmail(email) != null) { if (!StringUtils.equals(email, user.getEmail()) && getUserByEmail(email) != null) {
throw new NotUniqueUserException("Email address already registered"); throw new NotUniqueUserException("Email address already registered");
...@@ -181,7 +183,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -181,7 +183,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
user.setEmail(email); user.setEmail(email);
user.setFullName(fullName); user.setFullName(fullName);
return deepLoad(userRepository.save(user)); return deepLoad(_repository.save(user));
} }
/* /*
...@@ -194,7 +196,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -194,7 +196,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
@Transactional @Transactional
@PreAuthorize("hasRole('ADMINISTRATOR')") @PreAuthorize("hasRole('ADMINISTRATOR')")
public void deleteUser(final T user) { public void deleteUser(final T user) {
userRepository.delete(user); _repository.delete(user);
} }
/* /*
...@@ -205,9 +207,8 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -205,9 +207,8 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
*/ */
@Override @Override
@Transactional @Transactional
@PreAuthorize("hasRole('ADMINISTRATOR')")
public T setRoles(T user, final Set<R> newRoles) { public T setRoles(T user, final Set<R> newRoles) {
user = userRepository.findOne(user.getId()); user = _repository.findOne(user.getId());
// If roles match, do nothing // If roles match, do nothing
if (newRoles.containsAll(user.getRoles()) && user.getRoles().containsAll(newRoles)) { if (newRoles.containsAll(user.getRoles()) && user.getRoles().containsAll(newRoles)) {
...@@ -219,7 +220,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -219,7 +220,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
user.getRoles().addAll(newRoles); user.getRoles().addAll(newRoles);
user.getRoles().add(getDefaultUserRole()); user.getRoles().add(getDefaultUserRole());
LOG.info("Setting roles for user {} to {}", user.getEmail(), user.getRoles()); LOG.info("Setting roles for user {} to {}", user.getEmail(), user.getRoles());
return deepLoad(userRepository.save(user)); return deepLoad(_repository.save(user));
} }
/* /*
...@@ -233,7 +234,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -233,7 +234,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
public T changePassword(final T user, final String password) throws PasswordPolicyException { public T changePassword(final T user, final String password) throws PasswordPolicyException {
if (user.getAccountType() == AccountType.LOCAL) { if (user.getAccountType() == AccountType.LOCAL) {
setPassword(user, password); setPassword(user, password);
return deepLoad(userRepository.save(user)); return deepLoad(_repository.save(user));
} else { } else {
throw new PasswordPolicyException("Password can be set only for LOCAL account types"); throw new PasswordPolicyException("Password can be set only for LOCAL account types");
} }
...@@ -246,7 +247,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -246,7 +247,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
* @param password the password * @param password the password
* @throws PasswordPolicyException the password policy exception * @throws PasswordPolicyException the password policy exception
*/ */
protected void setPassword(final T user, final String password) throws PasswordPolicyException { protected final void setPassword(final T user, final String password) throws PasswordPolicyException {
if (user.getAccountType() == AccountType.LOCAL) { if (user.getAccountType() == AccountType.LOCAL) {
assureGoodPassword(password); assureGoodPassword(password);
user.setPassword(password == null ? null : passwordEncoder.encode(password)); user.setPassword(password == null ? null : passwordEncoder.encode(password));
...@@ -286,7 +287,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -286,7 +287,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
LOG.warn("Unlocking user account for user=" + user.getEmail()); LOG.warn("Unlocking user account for user=" + user.getEmail());
user.setLockedUntil(null); user.setLockedUntil(null);
} }
userRepository.save(user); _repository.save(user);
} }
/* /*
...@@ -305,13 +306,13 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -305,13 +306,13 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
@Override @Override
@Transactional @Transactional
public T setAccountType(T user, AccountType accountType) { public T setAccountType(T user, AccountType accountType) {
T u = userRepository.findOne(user.getId()); T u = _repository.findOne(user.getId());
u.setAccountType(accountType); u.setAccountType(accountType);
if (accountType != AccountType.LOCAL) { if (accountType != AccountType.LOCAL) {
user.setPassword(THIS_IS_NOT_A_PASSWORD); user.setPassword(THIS_IS_NOT_A_PASSWORD);
} }
return userRepository.save(u); return _repository.save(u);
} }
} }
...@@ -42,6 +42,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -42,6 +42,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -83,6 +84,11 @@ public class ApplicationConfig { ...@@ -83,6 +84,11 @@ public class ApplicationConfig {
@Autowired @Autowired
private TestUserPersistence testUserRepository; private TestUserPersistence testUserRepository;
@Override
protected JpaRepository<TestUser, Long> getUserRepository() {
return testUserRepository;
}
@Override @Override
protected TestUser createSystemAdministrator(String username) throws UserException { protected TestUser createSystemAdministrator(String username) throws UserException {
......
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