Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Doha2012 committed Nov 14, 2014
2 parents ac6db7c + 222fb31 commit 111a7ab
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 162 deletions.
2 changes: 2 additions & 0 deletions spring-security-login-and-registration/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.baeldung.event;

import org.springframework.context.ApplicationEvent;

@SuppressWarnings("serial")
public class OnRegistrationComplete extends ApplicationEvent {

public final Registration registration;

public OnRegistrationComplete(Registration source) {
super(source);
this.registration=source;
}

public Registration getRegistration() {
return registration;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.baeldung.event;

import java.util.Locale;
import org.baeldung.persistence.model.User;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;

@Component
public class Registration implements ApplicationEventPublisherAware {

private ApplicationEventPublisher eventPublisher;

public String getAppUrl() {
return appUrl;
}

public Locale getLocale() {
return locale;
}

public void setAppUrl(String appUrl) {
this.appUrl = appUrl;
}

public void setLocale(Locale locale) {
this.locale = locale;
}

public void setUser(User user) {
this.user = user;
}

public User getUser() {
return user;
}

private String appUrl;
private Locale locale;
private User user;

public Registration() {
super();
}

public void deliver() {
this.eventPublisher.publishEvent(new OnRegistrationComplete(this));
}

@Override
public void setApplicationEventPublisher(
ApplicationEventPublisher applicationEventPublisher) {
this.eventPublisher = applicationEventPublisher;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.baeldung.event.service;


import java.util.UUID;
import org.baeldung.event.OnRegistrationComplete;
import org.baeldung.persistence.model.User;
import org.baeldung.persistence.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.MessageSource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class RegistrationService implements
ApplicationListener<OnRegistrationComplete> {
@Autowired
private IUserService service;
@Autowired
private MessageSource messages;

@Autowired
private JavaMailSender mailSender;

@Override
public void onApplicationEvent(OnRegistrationComplete event) {
this.confirmRegistration(event);
}

private void confirmRegistration(OnRegistrationComplete event) {
User user = event.getRegistration().getUser();
String token = UUID.randomUUID().toString();
service.addVerificationToken(user, token);
String recipientAddress = user.getEmail();
String subject = "Registration Confirmation";
String confirmationUrl = event.getRegistration().getAppUrl()
+ "/regitrationConfirm.html?token=" + token;
String message = messages.getMessage("message.regSucc", null, event
.getRegistration().getLocale());
SimpleMailMessage email = new SimpleMailMessage();
email.setTo(recipientAddress);
email.setSubject(subject);
email.setText(message + " \r\n" + "http://localhost:8080"
+ confirmationUrl);
mailSender.send(email);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.baeldung.persistence.model.User;
import org.baeldung.persistence.model.VerificationToken;

public interface UserRepository extends JpaRepository<User, Long> {
public User findByEmail(String email);
//NOV 5th
// public User findByVerificationToken(VerificationToken token);
//OCT 21
public void delete(User user);
public User findByEmail(String email);

public void delete(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@
@Entity
@Table
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="firstName")
private String firstName;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "firstName")
private String firstName;
@Column(name="lastName")
private String lastName;
@Column(name="email")
private String email;
@Column(name="password")
private String password;

//EMAIL CONF ARTICLE
@OneToOne(mappedBy="user",
fetch = FetchType.EAGER,
cascade= CascadeType.ALL)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.baeldung.persistence.model;

import java.util.Calendar;
import java.io.Serializable;
import java.sql.Date;
import java.sql.Timestamp;
import javax.persistence.Column;
Expand All @@ -14,13 +13,11 @@
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity(/*name = "verrification_token"*/)
@Table(/*name = "verrification_token"*/)
public class VerificationToken /*implements Serializable*/ {
@Entity()
@Table()
public class VerificationToken {

//private static final long serialVersionUID = 1L;

private static final int EXPIRATION = 60 * 24/* 1 */;
private static final int EXPIRATION = 60 * 24;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package org.baeldung.persistence.service;

import org.baeldung.persistence.model.User;
import org.baeldung.persistence.model.VerificationToken;
import org.baeldung.validation.service.EmailExistsException;

public interface IUserService {

public User registerNewUserAccount(UserDto accountDto) throws EmailExistsException;

//OCT 21 EMAIL VERIFICATION
public User getRegisteredUser(String email);

public User getUser(String verificationToken);

public void verifyRegisteredUser(User user);

public void addVerificationToken(User user, String token);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,7 @@ public class UserDto {
@NotNull
@NotEmpty
private String email;
private String token;

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}


public String getEmail() {
return email;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.transaction.Transactional;

import org.baeldung.persistence.dao.UserRepository;
import org.baeldung.persistence.dao.VerificationTokenRepository;
import org.baeldung.persistence.model.Role;
import org.baeldung.persistence.model.User;
import org.baeldung.persistence.model.VerificationToken;
Expand All @@ -12,49 +13,63 @@

@Service
public class UserService implements IUserService {
@Autowired
private UserRepository repository;

@Transactional
@Override
public User registerNewUserAccount(UserDto accountDto) throws EmailExistsException {
if (emailExist(accountDto.getEmail())) {
throw new EmailExistsException("There is an account with that email adress: " + accountDto.getEmail());
}
User user = new User();
user.setFirstName(accountDto.getFirstName());
user.setLastName(accountDto.getLastName());
user.setPassword(accountDto.getPassword());
user.setEmail(accountDto.getEmail());
// ROLE WILL ALWAYS BE USER. HARDCODING IT
user.setRole(new Role(Integer.valueOf(1), user));
//OCT 21 EMAIL VERIFICATION VERSION
//MIGHT CHANGE HERE
VerificationToken myToken = new VerificationToken(accountDto.getToken(),user);
user.setVerificationToken(myToken);
return repository.save(user);
}

private boolean emailExist(String email) {
User user = repository.findByEmail(email);
if (user != null) {
return true;
}
return false;
}

//OCT 21 EMAIL VERIFICATION
@Override
public User getRegisteredUser(String email){

User user = repository.findByEmail(email);
return user;

}

@Transactional
@Override
public void verifyRegisteredUser(User user){
repository.save(user);
}
@Autowired
private UserRepository repository;
// NOV 6
@Autowired
private VerificationTokenRepository tokenRepository;

@Transactional
@Override
public User registerNewUserAccount(UserDto accountDto)
throws EmailExistsException {
if (emailExist(accountDto.getEmail())) {
throw new EmailExistsException(
"There is an account with that email adress: "
+ accountDto.getEmail());
}
User user = new User();
user.setFirstName(accountDto.getFirstName());
user.setLastName(accountDto.getLastName());
user.setPassword(accountDto.getPassword());
user.setEmail(accountDto.getEmail());
user.setRole(new Role(Integer.valueOf(1), user));
return repository.save(user);
}

private boolean emailExist(String email) {
User user = repository.findByEmail(email);
if (user != null) {
return true;
}
return false;
}

@Override
public User getRegisteredUser(String email) {

User user = repository.findByEmail(email);
return user;

}

@Override
public User getUser(String verificationToken) {
User user = tokenRepository.findByToken(verificationToken).getUser();
return user;
}

@Transactional
@Override
public void verifyRegisteredUser(User user) {
repository.save(user);
}

@Transactional
@Override
public void addVerificationToken(User user, String token) {
VerificationToken myToken = new VerificationToken(token, user);
user.setVerificationToken(myToken);
repository.save(user);
}
}
Loading

0 comments on commit 111a7ab

Please sign in to comment.