All Downloads are FREE. Search and download functionalities are using the official Maven repository.

base.jee.api.sql.util.CreatePerson Maven / Gradle / Ivy

/*
 This is free and unencumbered software released into the public domain.

 Anyone is free to copy, modify, publish, use, compile, sell, or
 distribute this software, either in source code form or as a compiled
 binary, for any purpose, commercial or non-commercial, and by any
 means.

 In jurisdictions that recognize copyright laws, the author or authors
 of this software dedicate any and all copyright interest in the
 software to the public domain. We make this dedication for the benefit
 of the public at large and to the detriment of our heirs and
 successors. We intend this dedication to be an overt act of
 relinquishment in perpetuity of all present and future rights to this
 software under copyright law.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
package base.jee.api.sql.util;

import base.jee.api.Settings;
import base.jee.api.model.Email;
import base.security.ResourceUid;
import base.security.User;
import base.template.TemplateManager;
import org.stringtemplate.v4.ST;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.UUID;

import static base.jee.api.sql.util.AddPerson.addPerson;
import static base.jee.api.sql.util.Log.log;

public class CreatePerson {

	public static UUID createPerson(Connection c, TemplateManager templateManager, Settings settings, User user, String firstName, String lastName, String email, String username, String password, Date expiry) throws IOException, SQLException, NoSuchAlgorithmException {
		PreparedStatement s = null;
		PreparedStatement t = null;
		ResultSet r = null;
		UUID uuid = null;

		if(username != null) {
			username = username.trim();
			if(username.length() == 0) {
				username = null;
			}
		}

		String site = user.getSite();

		if(email == null || email.length() < 4) {
			throw new IllegalArgumentException("Invalid email parameter");
		}

		try {
			uuid = addPerson(c, site, firstName, lastName, email, username, password, expiry);

			log(c, "INFO", user, "Created new account " + firstName + " " + lastName + ", " + email, new ResourceUid("Person", uuid.toString()));

			if(username == null || username.length() == 0 || settings.get("ldap.enabled", "true").equals("false")) {
				String supportTeam = settings.get("support_team.name");
				String supportEmail = settings.get("support_team.email");

				String token = UUID.randomUUID().toString();
				log(c, "FINEST", user, "Storing account activation token " + token + " for person " + email);
				s = c.prepareStatement("insert into request_token (site, token, person_uuid, type, ip, expiry) values(?,?,?,'account_activation',?,?)");
				s.setString(1, site);
				s.setString(2, token);
				s.setString(3, uuid.toString());
				s.setString(4, user.getIp());
				s.setLong(5, (new Date()).getTime()/1000);
				s.executeUpdate();
				s.close();
				s = null;

				ST html = templateManager.getCurrentTemplate(site).getInstanceOf("account_activation_email_html");
				html.add("first_name", firstName);
				html.add("last_name", lastName);
				html.add("email", email);
				html.add("user", user);
				html.add("token", token);
				html.add("formurl", settings.get("base.url"));

				ST text = templateManager.getCurrentTemplate(site).getInstanceOf("account_activation_email_text");
				text.add("first_name", firstName);
				text.add("last_name", lastName);
				text.add("email", email);
				text.add("user", user);
				text.add("token", token);
				text.add("formurl", settings.get("base.url"));

				Email e = new Email();
				e.setTo(firstName + " " + lastName + " <" + email + ">");
				e.setFrom(supportTeam + " <" + supportEmail + ">");
				e.setText(text.render());
				e.setHtml(html.render());
				e.setSubject("Account activation");

				s = c.prepareStatement("insert into email (uuid, to_address,email,retries,attempt_at,in_progress) values(?,?,?,0,?,0)");
				s.setString(1, new base.uuid.UUID().toString());
				s.setString(2, e.getTo());
				s.setString(3, e.toJson());
				s.setLong(4, new Date().getTime());
				s.execute();
				s.close();
				s = null;
			}

		} finally {
			if(r != null) {
				try { r.close(); } catch(Exception e) {}
			}
			if(s != null) {
				try { s.close(); } catch(Exception e) {}
			}
			if(t != null) {
				try { t.close(); } catch(Exception e) {}
			}
		}

		return uuid;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy