base.jee.api.cassandra.GetPerson 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.cassandra;
import java.io.IOException;
import base.Query;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PreparedStatement;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import base.jee.Constants;
import base.security.PermissionException;
import base.security.Person;
import base.security.User;
import static base.jee.api.cassandra.util.Log.log;
/**
* Return the personal details associated with a particular person. A user
* may only lookup the details associated with their own account unless they
* have the Types.PERSON_MANAGE permission.
*/
public class GetPerson extends Query {
private CassandraAPI api;
private User user;
private UUID personUuid;
public GetPerson(CassandraAPI api, User user, UUID personUuid) throws PermissionException {
if(api == null) {
throw new IllegalArgumentException("Invalid parameter: api");
}
if(user == null || !user.isAuthenticated()) {
throw new PermissionException(getClass().getSimpleName(), user, "Authenticate before invoking this command.", null);
}
this.api = api;
this.user = user;
this.personUuid = personUuid;
}
public GetPerson() {
}
@Override
public Query newWithParameters(Map parameters) throws PermissionException {
return new GetPerson(
(CassandraAPI)parameters.get("api"),
(User)parameters.get("user"),
UUID.fromString((String)parameters.get("personUuid")));
}
public List execute() throws IOException {
List results = new LinkedList<>();
Session s = api.getCassandraSession();
if(!user.hasRole(Constants.PERSON_MANAGE_ROLE) && !personUuid.equals(user.getPersonUuid())) {
log(s, "WARN", user, "Permission denied invoking: " + GetPerson.class.getSimpleName() + " " + getJsonParameters());
throw new PermissionException(this.getClass().getSimpleName(), user, "You do not have permission to view this persons account details.", Constants.PERSON_MANAGE_ROLE);
}
PreparedStatement q = s.prepare(
"select first_name, last_name, email, username, last_auth, last_auth_ip, created, updated, expiry " +
"from person " +
"where uuid=?");
for(Row r : s.execute(q.bind(personUuid))) {
Person p = new Person();
p.setUuid(personUuid);
p.setFirstName(r.getString(0));
p.setLastName(r.getString(1));
p.setEmail(r.getString(2));
p.setUsername(r.getString(3));
if(r.getLong(4) != 0) {
p.setLastAuth(new Date(r.getLong(4)));
}
p.setLastAuthIp(r.getString(5));
p.setCreated(new Date(r.getLong(6)));
p.setUpdated(new Date(r.getLong(7)));
if(r.getLong(8) > 0) {
p.setExpiry(new Date(r.getLong(8)));
}
results.add(p);
return results;
}
throw new IllegalArgumentException("Invalid person id");
}
@Override
public String getJsonParameters() {
return "{" +
"\"requesting.person\":\"" + user.getPersonUuid() + "\"," +
"\"person\":\"" + personUuid + "\"" +
"}";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy