base.jee.api.sql.DeleteRole Maven / Gradle / Ivy
/**
* 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.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import javax.sql.DataSource;
import base.Command;
import base.jee.Constants;
import base.json.Json;
import base.security.PermissionException;
import base.security.ResourceUid;
import base.security.User;
import static base.jee.api.sql.util.Log.log;
public class DeleteRole extends Command {
private DataSource ds;
private User updator;
private UUID personUuid;
private String role;
public DeleteRole(DataSource ds, User user, UUID personUuid, String role) throws PermissionException {
if(ds == null) {
throw new IllegalArgumentException("Invalid parameter: ds");
}
if(role == null || role.length() == 0) {
throw new IllegalArgumentException("Invalid parameter: role");
}
if(user == null || !user.isAuthenticated()) {
throw new PermissionException(this.getClass().getSimpleName(), user, "Requires an authenticated user.", "");
}
if(personUuid == null) {
throw new IllegalArgumentException("Invalid parameter: personUuid");
}
if(role.trim().length() > Constants.MAX_ROLE_NAME_LENGTH) {
throw new IllegalArgumentException("Role names should not have more than " + Constants.MAX_ROLE_NAME_LENGTH + " characters.");
}
this.ds = ds;
this.personUuid = personUuid;
this.role = role.trim();
this.updator = user;
}
public DeleteRole() {
}
@Override
protected void execute() throws IOException {
Connection c = null;
PreparedStatement s = null;
PreparedStatement t = null;
PreparedStatement u = null;
ResultSet r = null;
try {
c = ds.getConnection();
c.setAutoCommit(false);
if(!updator.hasRole(Constants.PERSON_MANAGE_ROLE)) {
c.rollback();
log(c, "WARN", updator, "Permission denied invoking: " + DeleteRole.class.getSimpleName() + " " + getJsonParameters());
c.commit();
throw new IllegalStateException("You do not have permission to manage roles.");
}
s = c.prepareStatement("select (select count(*) from role where uid is null and resource is null and assignee_uuid=? and role=?), first_name, last_name from person where uuid=?");
s.setString(1, personUuid.toString());
s.setString(2, role);
s.setString(3, personUuid.toString());
r = s.executeQuery();
if(r.next() && r.getInt(1) > 0) {
t = c.prepareStatement("delete from role where resource is null and uid is null and role=? and assignee_uuid=?");
t.setString(1, role);
t.setString(2, personUuid.toString());
int count = t.executeUpdate();
if(count > 0) {
u = c.prepareStatement("update token set roles='!' where person_uuid=?");
u.setString(1, personUuid.toString());
u.executeUpdate();
u.close();
u = null;
log(c, "INFO", updator, "Removed role \"" + Json.escape(role) + "\" from person \"" + Json.escape(r.getString(2) + " " + r.getString(3)) + "\".", new ResourceUid("Person", personUuid.toString()));
}
t.close();
t = null;
}
s.close();
s = null;
c.commit();
c.close();
c = null;
} catch(SQLException 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(t != null) { try { t.close(); } catch(SQLException e) { } }
if(u != null) { try { u.close(); } catch(SQLException e) { } }
if(c != null) {
try { c.rollback(); } catch (SQLException e) { }
try { c.close(); } catch (SQLException e) { }
}
}
}
@Override
public String getJsonParameters() {
return "{" +
"\"person\":\"" + personUuid + "\"," +
"\"role\":\"" + Json.escape(role) + "\"," +
"\"updator.person\":\"" + updator.getPersonUuid() + "\"" +
"}";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy