Search in sources :

Example 1 with AdminUser

use of uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser in project lumberjack by fn-ctional.

the class AuthenticationController method confirmRegistration.

@GetMapping(value = "/registrationConfirm")
public String confirmRegistration(WebRequest request, Model model, @RequestParam("token") String token) {
    VerificationToken verificationToken = userService.getVerificationToken(token);
    if (verificationToken == null) {
        model.addAttribute("messageType", "Bad Verification Link");
        model.addAttribute("messageString", "Try and register again.");
        return "message";
    }
    AdminUser user = verificationToken.getAdminUser();
    Calendar cal = Calendar.getInstance();
    if ((verificationToken.getExpiryDate().getTime() - cal.getTime().getTime()) <= 0) {
        model.addAttribute("messageType", "Token Expired");
        model.addAttribute("messageString", "Your verification token expired, try to register again.");
        model.addAttribute("expired", true);
        model.addAttribute("token", token);
        return "message";
    }
    user.setEnabled(true);
    try {
        userService.saveRegisteredUser(user);
        model.addAttribute("messageType", "Registration Successful");
        model.addAttribute("messageString", "You can now login.");
        return "message";
    } catch (SQLException e) {
        model.addAttribute("messageType", "Database Error");
        model.addAttribute("messageString", "Sorry! Try and register again.");
        return "message";
    } catch (MailAuthenticationException e) {
        model.addAttribute("messageType", "Email Server Error");
        model.addAttribute("messageString", "Sorry! Try and register again.");
        return "message";
    } catch (Exception e) {
        model.addAttribute("messageType", "Unknown Server Error");
        model.addAttribute("messageString", "Sorry! Try and register again.");
        return "message";
    }
}
Also used : VerificationToken(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.VerificationToken) MailAuthenticationException(org.springframework.mail.MailAuthenticationException) SQLException(java.sql.SQLException) Calendar(java.util.Calendar) AdminUser(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser) EmailNotPermittedException(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailNotPermittedException) MailAuthenticationException(org.springframework.mail.MailAuthenticationException) SQLException(java.sql.SQLException) EmailExistsException(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailExistsException)

Example 2 with AdminUser

use of uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser in project lumberjack by fn-ctional.

the class AuthenticationController method resendRegistrationToken.

@GetMapping(value = "/user/resendRegistrationToken")
@ResponseBody
public ModelAndView resendRegistrationToken(HttpServletRequest request, @RequestParam("token") String existingToken, Model model) {
    try {
        VerificationToken newToken = userService.generateNewVerificationToken(existingToken);
        AdminUser user = userService.getUser(newToken.getToken());
        String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
        SimpleMailMessage email = constructResendVerificationTokenEmail(appUrl, request.getLocale(), newToken, user);
        mailSender.send(email);
        model.addAttribute("messageType", "Successfully resent!");
        model.addAttribute("messageString", "Please check your inbox!");
    } catch (SQLException e) {
        model.addAttribute("messageType", "Database Error!");
        model.addAttribute("messageString", "Please try again.");
    } catch (Exception e) {
        model.addAttribute("messageType", "Unknown Server Error!");
        model.addAttribute("messageString", "Please try again.");
    }
    return new ModelAndView("message", "user", hashCode());
}
Also used : VerificationToken(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.VerificationToken) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) SQLException(java.sql.SQLException) ModelAndView(org.springframework.web.servlet.ModelAndView) AdminUser(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser) EmailNotPermittedException(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailNotPermittedException) MailAuthenticationException(org.springframework.mail.MailAuthenticationException) SQLException(java.sql.SQLException) EmailExistsException(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailExistsException)

Example 3 with AdminUser

use of uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser in project lumberjack by fn-ctional.

the class MyUserDetailsService method loadUserByUsername.

public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
    final Logger log = LoggerFactory.getLogger(LumberjackApplication.class);
    AdminUser user = authenticationBackend.findByEmail(email);
    log.info(email);
    log.info(user.getEmail());
    if (user.isEnabled()) {
        log.info("enabled");
    } else {
        log.info("disabled");
    }
    boolean enabled = user.isEnabled();
    final boolean accountNonExpired = true;
    final boolean credentialsNonExpired = true;
    final boolean accountNonLocked = true;
    return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, getAuthorities(Collections.singletonList("ADMINISTRATOR")));
}
Also used : AdminUser(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser) AdminUser(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser) Logger(org.slf4j.Logger)

Example 4 with AdminUser

use of uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser in project lumberjack by fn-ctional.

the class DatabaseAdminUsers method loadAdminUserFromResultSet.

private AdminUser loadAdminUserFromResultSet(ResultSet rs) throws UsernameNotFoundException, SQLException {
    if (rs.next()) {
        AdminUser adminUser = new AdminUser();
        adminUser.setEmail(rs.getString("Email"));
        adminUser.setPassword(rs.getString("Password"));
        adminUser.setName(rs.getString("Username"));
        adminUser.setEnabled(rs.getBoolean("Enabled"));
        return adminUser;
    }
    return null;
}
Also used : AdminUser(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser)

Example 5 with AdminUser

use of uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser in project lumberjack by fn-ctional.

the class DatabaseAdminUsers method loadAdminUser.

public AdminUser loadAdminUser(String email) throws UsernameNotFoundException, SQLException {
    PreparedStatement stmt = databaseConnection.getConnection().prepareStatement("SELECT * FROM Admins WHERE Email = ?");
    stmt.setString(1, email);
    ResultSet rs = stmt.executeQuery();
    AdminUser adminUser = loadAdminUserFromResultSet(rs);
    if (adminUser == null)
        throw new UsernameNotFoundException("Username not found: " + email);
    return adminUser;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) AdminUser(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser)

Aggregations

AdminUser (uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser)10 EmailExistsException (uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailExistsException)4 EmailNotPermittedException (uk.ac.bris.cs.rfideasalreadytaken.lumberjack.exceptions.EmailNotPermittedException)4 SQLException (java.sql.SQLException)3 MailAuthenticationException (org.springframework.mail.MailAuthenticationException)3 VerificationToken (uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.VerificationToken)3 Logger (org.slf4j.Logger)2 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Calendar (java.util.Calendar)1 Authentication (org.springframework.security.core.Authentication)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1 Transactional (org.springframework.transaction.annotation.Transactional)1