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
* @param email unique email address
* @param fullName Full name
* @param password initial account password
* @param accountType TODO
* @param accountType account type
* @return the new user
* @throws NotUniqueUserException user is not unique in the system
* @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");
* 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
private long accountLockoutTime = 5 * 60 * 1000;
/** The user repository. */
@Autowired
private JpaRepository<T, Long> userRepository;
private JpaRepository<T, Long> _repository;
/** The password encoder. */
@Autowired(required = false)
......@@ -79,6 +78,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
@Override
@Transactional
public void afterPropertiesSet() throws Exception {
this._repository = getUserRepository();
try {
loadUserByUsername(BasicUserService.SYSTEM_ADMIN);
} catch (UsernameNotFoundException e) {
......@@ -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
* role and account type {@link AccountType#SYSTEM}
......@@ -150,7 +152,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
*/
@Override
public T getUser(final long id) {
final T user = userRepository.findOne(id);
final T user = _repository.findOne(id);
return deepLoad(user);
}
......@@ -172,7 +174,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
@PreAuthorize("hasRole('ADMINISTRATOR') || principal.id == #user.id")
public T updateUser(T user, final String email, final String fullName) throws NotUniqueUserException, UserException {
// reload
user = userRepository.findOne(user.getId());
user = _repository.findOne(user.getId());
if (!StringUtils.equals(email, user.getEmail()) && getUserByEmail(email) != null) {
throw new NotUniqueUserException("Email address already registered");
......@@ -181,7 +183,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
user.setEmail(email);
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
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR')")
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
*/
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR')")
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 (newRoles.containsAll(user.getRoles()) && user.getRoles().containsAll(newRoles)) {
......@@ -219,7 +220,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
user.getRoles().addAll(newRoles);
user.getRoles().add(getDefaultUserRole());
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
public T changePassword(final T user, final String password) throws PasswordPolicyException {
if (user.getAccountType() == AccountType.LOCAL) {
setPassword(user, password);
return deepLoad(userRepository.save(user));
return deepLoad(_repository.save(user));
} else {
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
* @param password the password
* @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) {
assureGoodPassword(password);
user.setPassword(password == null ? null : passwordEncoder.encode(password));
......@@ -286,7 +287,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
LOG.warn("Unlocking user account for user=" + user.getEmail());
user.setLockedUntil(null);
}
userRepository.save(user);
_repository.save(user);
}
/*
......@@ -305,13 +306,13 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
@Override
@Transactional
public T setAccountType(T user, AccountType accountType) {
T u = userRepository.findOne(user.getId());
T u = _repository.findOne(user.getId());
u.setAccountType(accountType);
if (accountType != AccountType.LOCAL) {
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;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;
......@@ -84,6 +85,11 @@ public class ApplicationConfig {
@Autowired
private TestUserPersistence testUserRepository;
@Override
protected JpaRepository<TestUser, Long> getUserRepository() {
return testUserRepository;
}
@Override
protected TestUser createSystemAdministrator(String username) throws UserException {
TestUser admin = createUser(username, "System Administrator", null, AccountType.SYSTEM);
......
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