Commit b7171112 by Jan Hrabal

b

parent 6aaa7772
package com.jh.boot.security; package com.jh.boot.security;
import java.util.Locale;
import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.AuthenticationException;
import com.jh.boot.security.model.AppUser;
/** /**
...@@ -25,10 +29,10 @@ public interface AuthService { ...@@ -25,10 +29,10 @@ public interface AuthService {
/** /**
* Register. * Register.
* * @param locale TODO
* @param signup the signup * @param signup the signup
*/ */
void register(String login, String password) throws AuthenticationException; void register(String login, String password, Locale locale) throws AuthenticationException;
/** /**
...@@ -48,5 +52,12 @@ public interface AuthService { ...@@ -48,5 +52,12 @@ public interface AuthService {
*/ */
void resetPassword(String login, String token, String newPassword) throws AuthenticationException; void resetPassword(String login, String token, String newPassword) throws AuthenticationException;
/**
* TODO
*
* @param login
*/
void delete(AppUser login);
} }
...@@ -5,9 +5,20 @@ import com.jh.boot.security.model.ResetPasswordToken; ...@@ -5,9 +5,20 @@ import com.jh.boot.security.model.ResetPasswordToken;
public interface AuthServiceListener { public interface AuthServiceListener {
default void registerUser(AppUser user) {
void registerUser(AppUser user); }
void generateResetToken(AppUser user, ResetPasswordToken token); default void initializeUser(AppUser user) {
}
default void generateResetToken(AppUser user, ResetPasswordToken token) {
}
default void deleteUser(AppUser user) {
}
} }
...@@ -113,7 +113,7 @@ public class AuthApiController { ...@@ -113,7 +113,7 @@ public class AuthApiController {
* @return the response entity * @return the response entity
*/ */
@RequestMapping(path = "/auth/signup", method = RequestMethod.POST) @RequestMapping(path = "/auth/signup", method = RequestMethod.POST)
public @ResponseBody LoginResponse signup(@RequestBody Signup signup) { public @ResponseBody LoginResponse signup(@RequestBody Signup signup, Locale locale) {
if (!signupEnabled) { if (!signupEnabled) {
throw new NotFoundException(); throw new NotFoundException();
} }
...@@ -145,11 +145,12 @@ public class AuthApiController { ...@@ -145,11 +145,12 @@ public class AuthApiController {
} }
try { try {
authService.register(signup.getLogin(), signup.getPassword()); authService.register(signup.getLogin(), signup.getPassword(), locale);
} catch (BadCredentialsException e) { } catch (BadCredentialsException e) {
errors.add(ErrorMessage.withCode(e.getMessage())); errors.add(ErrorMessage.withCode(e.getMessage()));
} catch (Exception e) { } catch (Exception e) {
errors.add(ErrorMessage.withMessage(e.getMessage())); errors.add(ErrorMessage.withMessage(e.getMessage()));
e.printStackTrace();
} }
if (errors.isEmpty()) { if (errors.isEmpty()) {
...@@ -227,11 +228,16 @@ public class AuthApiController { ...@@ -227,11 +228,16 @@ public class AuthApiController {
@DeleteMapping("/auth/user") @DeleteMapping("/auth/user")
public ResponseEntity<Void> deleteUser() { public ResponseEntity<Void> deleteUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); Authentication auth = SecurityContextHolder.getContext().getAuthentication();
// if (auth instanceof AppUserAuthentication) {
// return ((AppUserAuthentication) auth).getUser();
// }
//TODO other auth types? AppUser user = null;
if (auth instanceof AppUserAuthentication) {
user = ((AppUserAuthentication) auth).getUser();
}
if (user == null) {
return ResponseEntity.notFound().build();
}
authService.delete(user);
return ResponseEntity.accepted().build(); return ResponseEntity.accepted().build();
} }
......
...@@ -17,7 +17,7 @@ public class AppUserRepository extends AbstractHibernateRepository { ...@@ -17,7 +17,7 @@ public class AppUserRepository extends AbstractHibernateRepository {
if (!StringUtils.hasText(username)) { if (!StringUtils.hasText(username)) {
return null; return null;
} }
Query q = entityManager.createQuery("select au from AppUser au where lower(au.email) = :email"); Query q = entityManager.createQuery("select au from AppUser au where lower(au.email) = :email and (au.deleted is null or au.deleted = false)");
q.setParameter("email", username.trim().toLowerCase()); q.setParameter("email", username.trim().toLowerCase());
return singleResult(q); return singleResult(q);
} }
...@@ -27,7 +27,7 @@ public class AppUserRepository extends AbstractHibernateRepository { ...@@ -27,7 +27,7 @@ public class AppUserRepository extends AbstractHibernateRepository {
if (!StringUtils.hasText(username)) { if (!StringUtils.hasText(username)) {
return null; return null;
} }
Query q = entityManager.createQuery("select au from AppUser au left join fetch au.roles where lower(au.email) = :email"); Query q = entityManager.createQuery("select au from AppUser au left join fetch au.roles where lower(au.email) = :email and (au.deleted is null or au.deleted = false)");
q.setParameter("email", username.trim().toLowerCase()); q.setParameter("email", username.trim().toLowerCase());
AppUser user = singleResult(q); AppUser user = singleResult(q);
if (user == null) { if (user == null) {
......
...@@ -3,6 +3,7 @@ package com.jh.boot.security.service; ...@@ -3,6 +3,7 @@ package com.jh.boot.security.service;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
...@@ -61,7 +62,7 @@ public class AppUserAuthService implements AuthService { ...@@ -61,7 +62,7 @@ public class AppUserAuthService implements AuthService {
@Override @Override
@Transactional @Transactional
public void register(String login, String password) throws AuthenticationException { public void register(String login, String password, Locale locale) throws AuthenticationException {
AppUser user = appUserRepository.findByLogin(login); AppUser user = appUserRepository.findByLogin(login);
if (user != null) { if (user != null) {
throw new BadCredentialsException("AUTH.USER_ALREADY_EXISTS"); throw new BadCredentialsException("AUTH.USER_ALREADY_EXISTS");
...@@ -117,6 +118,21 @@ public class AppUserAuthService implements AuthService { ...@@ -117,6 +118,21 @@ public class AppUserAuthService implements AuthService {
} }
@Override
@Transactional
public void delete(AppUser user) throws AuthenticationException {
if (user == null) {
throw new BadCredentialsException("AUTH.USER_NOT_FOUND");
}
user = appUserRepository.findByLogin(user.getEmail());
if (user == null) {
throw new BadCredentialsException("AUTH.USER_NOT_FOUND");
}
user.setDeleted(true);
}
@Autowired(required = false) @Autowired(required = false)
public void setAppUserRepository(AppUserRepository appUserRepository) { public void setAppUserRepository(AppUserRepository appUserRepository) {
this.appUserRepository = appUserRepository; this.appUserRepository = appUserRepository;
......
...@@ -64,8 +64,14 @@ public class TemplateEmailAuthServiceListener implements AuthServiceListener { ...@@ -64,8 +64,14 @@ public class TemplateEmailAuthServiceListener implements AuthServiceListener {
} }
@Override
public void deleteUser(AppUser user) {
//TODO
}
protected void sendEmail(String subject, String content, String html, AppUser user, Map<String, Object> data) { protected void sendEmail(String subject, String content, String html, AppUser user, Map<String, Object> data) {
Locale locale = new Locale(user.getLocale()); Locale locale = user.getLocale() == null ? Locale.getDefault() : new Locale(user.getLocale());
subject = templateService.evaluate(subject, data, locale); subject = templateService.evaluate(subject, data, locale);
content = templateService.evaluate(content, data, locale); content = templateService.evaluate(content, data, locale);
......
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