
base.jee.api.sql.GetGroupRoles 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.
/*
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;
import base.Query;
import base.jee.Constants;
import base.security.PermissionException;
import base.security.PersonRole;
import base.security.User;
import javax.sql.DataSource;
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 static base.jee.api.sql.util.Log.log;
public class GetGroupRoles extends Query {
private DataSource ds;
private User user;
private UUID groupUuid;
public GetGroupRoles(DataSource ds, User user, UUID groupUuid) throws PermissionException {
if(ds == null) {
throw new IllegalArgumentException("Invalid parameter: ds");
}
if(groupUuid == null) {
throw new IllegalArgumentException("Invalid parameter: groupUuid");
}
if(user == null || !user.isAuthenticated()) {
throw new PermissionException(getClass().getSimpleName(), user, "Requires authenticated user.", null);
}
this.ds = ds;
this.user = user;
this.groupUuid = groupUuid;
}
public GetGroupRoles() {
}
@Override
public Query newWithParameters(Map parameters) throws PermissionException {
return new GetGroupRoles(
((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.GROUP_MANAGE_ROLE)) {
c.rollback();
log(c, "WARN", user, "Permission denied invoking: " + GetGroupRoles.class.getSimpleName() + " " + getJsonParameters());
c.commit();
throw new PermissionException(getClass().getSimpleName(), user, "You do not have permission to manage group roles", Constants.GROUP_MANAGE_ROLE);
}
q = c.prepareStatement(
"select distinct resource_type, resource_uid, role "+
"from role " +
"where assignee_uuid=? " +
"order by role, resource_type, resource_uid");
q.setString(1, groupUuid.toString());
r = q.executeQuery();
while(r.next()) {
results.add(new PersonRole(groupUuid, 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() + "\"," +
"\"group.uuid\":\"" + groupUuid + "\"" +
"}";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy