base.jee.api.sql.GetGroupMembers 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.Person;
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.Date;
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 GetGroupMembers extends Query {
private DataSource ds;
private User user;
private UUID group;
public GetGroupMembers() {
}
public GetGroupMembers(DataSource ds, User user, UUID group) throws PermissionException {
if(ds == null) {
throw new IllegalArgumentException("Invalid parameter: ds");
}
if(group == null) {
throw new IllegalArgumentException("Invalid parameter: group");
}
if(user == null || !user.isAuthenticated()) {
throw new PermissionException(getClass().getSimpleName(), user, "Requires authenticated user.", Constants.GROUP_MANAGE_ROLE);
}
this.ds = ds;
this.user = user;
this.group = group;
}
@Override
public Query newWithParameters(Map parameters) throws PermissionException {
return new GetGroupMembers(
((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;
boolean isMember = false;
try {
c = ds.getConnection();
c.setAutoCommit(false);
q = c.prepareStatement(
"select distinct p.uuid, p.first_name, p.last_name, p.expiry "+
"from group_member m join person p on (m.group_uuid=? and m.person_uuid = p.uuid) " +
"order by first_name, last_name");
q.setString(1, group.toString());
r = q.executeQuery();
while(r.next()) {
Person p = new Person();
p.setUuid(UUID.fromString(r.getString(1)));
p.setFirstName(r.getString(2));
p.setLastName(r.getString(3));
if(r.getLong(4) != 0) {
p.setExpiry(new Date(r.getLong(4)));
}
results.add(p);
if(p.getUuid().equals(user.getPersonUuid())) {
isMember = true;
}
}
r.close();
r = null;
q.close();
q = null;
if(!isMember && !user.hasRole(Constants.GROUP_MANAGE_ROLE)) {
c.rollback();
log(c, "WARN", user, "Permission denied invoking: " + GetGroupMembers.class.getSimpleName() + " " + getJsonParameters());
c.commit();
c.close();
c = null;
throw new PermissionException(this.getClass().getSimpleName(), user, "You do not have permission view members of this group.", Constants.GROUP_MANAGE_ROLE);
}
c.rollback();
c.close();
c = null;
} catch(SQLException e) {
throw new IOException(e);
} finally {
if(q != null) { try { q.close(); } catch(SQLException e) {} }
if(r != null) { try { r.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 "{" +
"\"person\":\"" + user.getPersonUuid() + "\"," +
"\"group.uuid\":\"" + group + "\"" +
"}";
}
}