Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Creative commons Attribution-NonCommercial license. http://creativecommons.org/licenses/by-nc/2.5/au/deed.en_GB NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
  
  package base.jee.api.cassandra;
  
 
 
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
 
 
 import base.Query;
 import base.json.Json;
 
 import static base.jee.api.cassandra.util.IsThrottled.isThrottled;
 import static base.jee.api.cassandra.util.Log.log;
 import static base.jee.api.cassandra.util.MarkForThrottling.markForThrottling;

Request that an email be sent to an email address associated with a user of the system, that enables resetting of this users password. Password reset requests are also throttled, they considered to be an authentication request.
 
 public class RequestPasswordResetEmail extends Query<StringQueryResult> {
 
 	private CassandraAPI c;
 	private String email;
 	private String ip;

Parameters:
c
templates A template manager with template files 'password_reset_email_html.txt' and 'password_reset_email_text.txt'
email Email address corresponding with the users account.
ip
 
 	public RequestPasswordResetEmail(CassandraAPI cTemplateManager templatesString emailString ip) {
 		if(c == null) {
 			throw new IllegalArgumentException("Invalid parameter: c");
 		}
 		if(ip == null) {
 			throw new IllegalArgumentException("Invalid parameter: ip");
 		}
 		if(templates == null) {
 			throw new IllegalArgumentException("Invalid parameter: templates");
 		}
 		if(email == null) {
 			throw new IllegalArgumentException("Invalid parameter: email");
 		}
 
 		if(email.trim().length() > .) {
 			throw new IllegalArgumentException("Please choose a shorter email address. Email should not have more than " + . + " characters.");
 		}
 
 		this. = c;
 		this. = templates;
 		this. = email.trim().toLowerCase();
 	}
 
 	public List<StringQueryResultexecute() throws IOException {
 		List<StringQueryResultresults = new LinkedList<>();
 		User u = null;
 
 		Settings settings = .getSettingsCache();
 
 		if(isThrottled(s"auth")) {
 			log(s"SEVERE", User.userWithIp(), "Blocked password reset request for throttled address: " + );
 			throw new IllegalStateException("This account is temporarily disabled due to repated sign in failures. Please try again shortly.");
 		}
 
 		String supportTeam = settings.get("support_team.name");
 		String supportEmail = settings.get("support_team.email");
 
 		PreparedStatement p = s.prepare("select uuid, first_name, last_name from person where email=?");
		for(Row r : s.execute(p.bind())) {
			u = new User(r.getUUID(0),
					null,
					null,
					null,
		}
		if(u == null) {
			markForThrottling(s"password_reset_" + "auth");
			log(s"FINE", User.userWithIp(), "Invalid email: " + );
			results.add(new StringQueryResult("error"));
			return results;
		}
		String token = UUID.randomUUID().toString();
		log(s"DEBUG"u"Storing token " + token + " for person " + );
		p = s.prepare("insert into request_token (uid, person_uuid, type, ip, expiry) values(?,?,'password_reset',?,?)");
		s.execute(p.bind(tokenu.getPersonUuid(), , (new Date()).getTime()/1000));
		ST html = .getCurrentTemplate().getInstanceOf("password_reset_email_html");
		html.add("name"u.getDisplayName());
		html.add("email"this.);
		html.add("token"token);
		html.add("formurl"settings.get("base.url"));
		ST text = .getCurrentTemplate().getInstanceOf("password_reset_email_text");
		text.add("name"u.getDisplayName());
		text.add("email"this.);
		text.add("token"token);
		text.add("formurl"settings.get("base.url"));
		Email email = new Email();
		email.setTo(u.getDisplayName() + " <" + this. + ">");
		email.setFrom(supportTeam + " <" + supportEmail + ">");
		email.setText(text.render());
		email.setHtml(html.render());
		email.setSubject("Password reset request verification");
			try(java.sql.PreparedStatement ps = ec.prepareStatement("insert into email (uuid, to_address,email,retries,attempt_at,in_progress) values(?,?,?,0,?,0)")) {
				ps.setString(1, new base.uuid.UUID().toString());
				ps.setString(2, email.getTo());
				ps.setString(3, email.toJson());
				ps.setLong(4, new Date().getTime());
				ps.execute();
			}
catch (SQLException e) {
			log(s"SEVERE""Failed inserting email into queue. Email never sent. " + StringHelper.exceptionToString(e"|"));
		}
		log(s"INFO"u"Sending password reset request for " + this. + " with token " + token);
		results.add(new StringQueryResult(token));
		return results;
	}
		return "{" +
				"\"email\":\"" + Json.escape() + "\"," +
				"\"ip\":\"" + Json.escape() + "\"" +
				"}";
	}
		throw new IllegalArgumentException("RequestPasswordResetEmail may not be instantiated using a parameter map");
	}
New to GrepCode? Check out our FAQ X