use of com.naturalprogrammer.spring.tutorial.domain.User in project SpringStepByStep by JavaProgrammerLB.
the class UserServiceImpl method findById.
@Override
public User findById(long userId) {
User user = userRepository.findOne(userId);
MyUtil.validate(user != null, "userNotFound");
if (!user.isAdminOrSelfLoggedIn())
user.setEmail("Confidential");
return user;
}
use of com.naturalprogrammer.spring.tutorial.domain.User in project SpringStepByStep by JavaProgrammerLB.
the class UserServiceImpl method afterApplicationReady.
@Override
@EventListener
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void afterApplicationReady(ApplicationReadyEvent event) {
if (!userRepository.findByEmail(adminEmail).isPresent()) {
User user = new User();
user.setEmail(adminEmail);
user.setName(adminName);
user.setPassword(adminPassword);
user.setPassword(passwordEncoder.encode(adminPassword));
user.getRoles().add(Role.ADMIN);
userRepository.save(user);
}
}
use of com.naturalprogrammer.spring.tutorial.domain.User in project SpringStepByStep by JavaProgrammerLB.
the class UserServiceImpl method update.
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void update(User user, User updatedData) {
// Ensure that a user with the given id exists
MyUtil.validate(user != null, "userNotFound");
// Only self or an admin can edit the profile data
MyUtil.validate(user.isAdminOrSelfLoggedIn(), "notPermitted");
// Update the name
user.setName(updatedData.getName());
User loggedIn = MyUtil.getUser();
// Only an admin can edit roles
if (loggedIn.isAdmin())
user.setRoles(updatedData.getRoles());
// save the updates
userRepository.save(user);
MyUtil.afterCommit(() -> {
// gets updated
if (loggedIn.getId() == user.getId())
MyUtil.login(user);
});
}
use of com.naturalprogrammer.spring.tutorial.domain.User in project SpringStepByStep by JavaProgrammerLB.
the class UserServiceImpl method forgotPassword.
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void forgotPassword(ForgotPasswordForm forgotPasswordForm) {
// fetch the user record from database
User user = userRepository.findByEmail(forgotPasswordForm.getEmail()).get();
// set a reset password code
user.setResetPasswordCode(UUID.randomUUID().toString());
userRepository.save(user);
// after successful commit, mail him a link to reset his password
MyUtil.afterCommit(() -> mailResetPasswordLink(user));
}
use of com.naturalprogrammer.spring.tutorial.domain.User in project SpringStepByStep by JavaProgrammerLB.
the class UserServiceImpl method verify.
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void verify(String verificationCode) {
// get the current-user from the session
User currentUser = MyUtil.getUser();
// fetch a fresh copy from the database
User user = userRepository.findOne(currentUser.getId());
// ensure that the user is unverified
MyUtil.validate(user.getRoles().contains(Role.UNVERIFIED), "alreadyVerified");
// ensure that the verification code of the user matches
// with the given one
MyUtil.validate(verificationCode.equals(user.getVerificationCode()), "wrongVerificationCode");
// make him verified
makeVerified(user);
userRepository.save(user);
// after successful commit,
MyUtil.afterCommit(() -> {
// Re-login the user, so that the UNVERIFIED role is removed
MyUtil.login(user);
});
}
Aggregations