
base.jee.api.sql.CreatePerson Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of base Show documentation
Show all versions of base Show documentation
A collection of basic java utility classes that provide basic features for a standalone/simple JEE application. Backed by a Cassandra, MySQL, or SQLite database, it provides, web page templates, user and group management, and a searchable online audit log of all user activity.
/**
* 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.sql;
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.text.ParseException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.sql.DataSource;
import base.UuidQueryResult;
import base.jee.api.Settings;
import base.template.TemplateManager;
import base.Query;
import base.email.EmailAddressParse;
import base.jee.Constants;
import base.jee.JeeBase;
import base.json.Json;
import base.security.PermissionException;
import base.security.User;
import static base.jee.api.sql.util.CreatePerson.createPerson;
import static base.jee.api.sql.util.Log.log;
import static base.jee.api.sql.util.UpsertPersonResourceRole.upsertPersonResourceRole;
import static base.jee.api.sql.util.UpsertPersonRole.upsertPersonRole;
import static base.text.StringHelper.chomp;
import static base.text.TagsToArray.tagsToArray;
public class CreatePerson extends Query {
private DataSource ds;
private User user;
private TemplateManager templateManager;
private Settings settings;
private String site;
private String firstName;
private String lastName;
private String email;
private String username;
private String password;
private String[] initialRoles;
private Date expiry;
public CreatePerson(DataSource ds, TemplateManager templateManager, Settings settings, User user, String firstName, String lastName, String email, String username, String password, String initialRoles, String expiry) throws PermissionException {
if(ds == null) {
throw new IllegalArgumentException("Invalid parameter: ds");
}
if(templateManager == null) {
throw new IllegalArgumentException("Invalid parameter: templateManager");
}
if(user == null || !user.isAuthenticated()) {
throw new PermissionException(getClass().getSimpleName(), user, "Requires authenticated user.", Constants.PERSON_MANAGE_ROLE);
}
if(firstName == null || chomp(firstName).length() == 0) {
throw new IllegalArgumentException("Invalid parameter: firstName");
}
if(lastName == null || chomp(lastName).length() == 0) {
throw new IllegalArgumentException("Invalid parameter: lastName");
}
if(email == null || chomp(email).length() == 0) {
throw new IllegalArgumentException("Please specify an email address.");
}
if(chomp(email).length() > Constants.MAX_EMAIL_LENGTH) {
throw new IllegalArgumentException("Please choose a shorter email address. Email should not have more than " + Constants.MAX_EMAIL_LENGTH + " characters.");
}
EmailAddressParse parse = new EmailAddressParse();
if(!parse.isValid(chomp(email).toLowerCase())) {
throw new IllegalArgumentException("Invalid email address. " + parse.getError());
}
if(username != null && chomp(username).length() > Constants.MAX_USERNAME_LENGTH) {
throw new IllegalArgumentException("Please choose a shorter username. Usernames should not have more than " + Constants.MAX_USERNAME_LENGTH + " characters.");
}
if(password != null && chomp(password).length() > Constants.MAX_PASSWORD_LENGTH) {
throw new IllegalArgumentException("Please choose a shorter password. Passwords should not have more than " + Constants.MAX_PASSWORD_LENGTH + " characters.");
}
if(chomp(firstName).length() > Constants.MAX_FIRST_NAME_LENGTH) {
throw new IllegalArgumentException("Please choose a shorter first name. First name should not have more than " + Constants.MAX_FIRST_NAME_LENGTH + " characters.");
}
if(chomp(lastName).length() > Constants.MAX_LAST_NAME_LENGTH) {
throw new IllegalArgumentException("Please choose a shorter last name. Last name should not have more than " + Constants.MAX_FIRST_NAME_LENGTH + " characters.");
}
if(initialRoles != null && chomp(initialRoles).length() > Constants.MAX_PERSON_INITIAL_ROLES_LENGTH) {
throw new IllegalArgumentException("List of initial roles is too long. Initial roles list should not have more than " + Constants.MAX_PERSON_INITIAL_ROLES_LENGTH + " characters.");
}
if(expiry != null && expiry.length() > 0) {
try {
this.expiry = Constants.DATE_FORMAT.parse(expiry);
} catch (ParseException e1) {
throw new IllegalArgumentException("Please leave expiry field empty, or enter date using date format: " + Constants.DATE_FORMAT_STRING);
}
}
this.ds = ds;
this.templateManager = templateManager;
this.settings = settings;
this.site = site;
this.firstName = chomp(firstName);
this.lastName = chomp(lastName);
this.email = chomp(email).toLowerCase();
this.password = password;
this.user = user;
if(initialRoles != null) {
this.initialRoles = tagsToArray(initialRoles);
for(String i : this.initialRoles) {
if(i.length() > Constants.MAX_ROLE_NAME_LENGTH) {
throw new IllegalArgumentException("Invalid initial role name. Role names do not exceed" + Constants.MAX_ROLE_NAME_LENGTH + " characters.");
}
}
} else {
this.initialRoles = new String[]{};
}
if(username != null) {
this.username = chomp(username).toLowerCase();
}
}
public CreatePerson() {
}
@Override
public Query newWithParameters(Map parameters) throws IOException, PermissionException {
return new CreatePerson(
((SqlAPI)parameters.get("api")).getDataSource(),
((JeeBase)parameters.get("jee")).getTemplateManager(),
((JeeBase)parameters.get("jee")).getSettings(),
(User)parameters.get("user"),
(String)parameters.get("first_name"),
(String)parameters.get("last_name"),
(String)parameters.get("email"),
(String)parameters.get("username"),
(String)parameters.get("password"),
(String)parameters.get("initial_roles"),
(String)parameters.get("expires"));
}
@Override
protected List execute() throws IOException {
Connection c = null;
PreparedStatement s = null;
ResultSet r = null;
List results = new LinkedList<>();
try {
c = ds.getConnection();
c.setAutoCommit(false);
if(!user.hasRole(Constants.PERSON_MANAGE_ROLE)) {
c.rollback();
log(c, "WARN", user, "Permission denied invoking: " + CreatePerson.class.getSimpleName() + " " + getJsonParameters());
c.commit();
throw new PermissionException(this.getClass().getSimpleName(), user, "You do not have permission to create new user accounts.", Constants.PERSON_MANAGE_ROLE);
}
UUID uuid = createPerson(c, templateManager, settings, user, firstName, lastName, email, username, password, expiry);
for(String role : initialRoles) {
if(role.contains(":")) {
String[] parts = role.split(":");
if(parts.length == 3) {
upsertPersonResourceRole(c, uuid, parts[1], parts[2], parts[0], user);
}
} else {
upsertPersonRole(c, uuid, role, user);
}
}
results.add(new UuidQueryResult(uuid));
c.commit();
} catch(SQLException | NoSuchAlgorithmException e) {
throw new IOException(e);
} finally {
if(r != null) { try { r.close(); } catch (SQLException e) { } }
if(s != null) { try { s.close(); } catch (SQLException e) { } }
if(c != null) {
try { c.rollback(); } catch (SQLException e) { }
try { c.close(); } catch (SQLException e) { }
}
}
return results;
}
@Override
public String getJsonParameters() {
return "{" +
"\"first_name\":\"" + Json.escape(firstName)+ "\"," +
"\"last_name\":\"" + Json.escape(lastName)+ "\"," +
(username != null && username.length() > 0 ? "\"username\":\"" + Json.escape(email)+ "\",":"") +
"\"email\":\"" + Json.escape(email)+ "\"" +
"}";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy