base.jee.api.sql.GetPersonRoles 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.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.sql.DataSource;
import base.Query;
import base.jee.Constants;
import base.security.PermissionException;
import base.security.PersonRole;
import base.security.User;
import static base.jee.api.sql.util.Log.log;
public class GetPersonRoles extends Query {
private DataSource ds;
private User user;
private UUID personUuid;
public GetPersonRoles() {
}
public GetPersonRoles(DataSource ds, User user, UUID personUuid) throws PermissionException {
if(ds == null) {
throw new IllegalArgumentException("Invalid parameter: ds");
}
if(personUuid == null) {
throw new IllegalArgumentException("Invalid parameter: personUuid");
}
if(user == null || !user.isAuthenticated()) {
throw new PermissionException(getClass().getSimpleName(), user, "Requires authenticated user.", null);
}
this.ds = ds;
this.user = user;
this.personUuid = personUuid;
}
@Override
public Query newWithParameters(Map parameters) throws PermissionException {
return new GetPersonRoles(
((SqlAPI)parameters.get("api")).getDataSource(),
(User)parameters.get("user"),
UUID.fromString(((String)parameters.get("uuid"))));
}
public List execute() throws IOException {
List results = new LinkedList<>();
Connection c = null;
PreparedStatement q = null;
ResultSet r = null;
try {
c = ds.getConnection();
c.setAutoCommit(false);
if(!user.hasRole(Constants.PERSON_MANAGE_ROLE) && !personUuid.equals(user.getPersonUuid())) {
c.rollback();
log(c, "WARN", user, "Permission denied invoking: " + GetPersonRoles.class.getSimpleName() + " " + getJsonParameters());
c.commit();
throw new IllegalStateException("You do not have permission to manage roles");
}
q = c.prepareStatement(
"select distinct resource, uid, role "+
"from role " +
"where assignee_uuid=? " +
"order by role, resource, uid");
q.setString(1, personUuid.toString());
r = q.executeQuery();
while(r.next()) {
results.add(new PersonRole(personUuid, r.getString(1), r.getString(2), r.getString(3)));
}
r.close();
r = null;
q.close();
q = null;
c.rollback();
c.close();
c = null;
} catch(SQLException e) {
throw new IOException(e);
} finally {
if(r != null) { try { r.close(); } catch(Exception f) {} }
if(q != null) { try { q.close(); } catch(Exception f) {} }
if(c != null) {
try { c.rollback(); } catch (SQLException e) { }
try { c.close(); } catch (SQLException e) { }
}
}
return results;
}
@Override
public String getJsonParameters() {
return "{" +
"\"requesting.person\":" + user.getPersonUuid() + "," +
"\"person.uuid\":\"" + personUuid + "\"" +
"}";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy