All Downloads are FREE. Search and download functionalities are using the official Maven repository.

base.jee.api.cassandra.UpsertResourceRole 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 com.datastax.driver.core.Session;
import java.util.UUID;

import base.Command;
import base.jee.Constants;
import base.json.Json;
import base.security.PermissionException;
import base.security.User;

import static base.jee.api.cassandra.util.Log.log;
import static base.jee.api.cassandra.util.UpsertResourceRole.upsertResourceRole;

/**
 * Grant personId a new role against a specific resource uid. ie Grant "John Smith" permission to
 * update "Document" with uid "401".
 */
public class UpsertResourceRole extends Command {

	private CassandraAPI c;
	private User user;
	private UUID personUuid;
	private String role;
	private String resource;
	private String uid;

	public UpsertResourceRole(CassandraAPI c, User user, UUID personUuid, String role, String resource, String uid) {
		if(c == null) {
			throw new IllegalArgumentException("Invalid parameter: c");
		}
		if(role == null || role.length() == 0) {
			throw new IllegalArgumentException("Invalid parameter: role");
		}
		if(user == null) {
			throw new IllegalArgumentException("Invalid parameter: user");
		}
		if(personUuid == null) {
			throw new IllegalArgumentException("Invalid parameter: personUuid");
		}
		if(resource == null || resource.length() == 0) {
			throw new IllegalArgumentException("Invalid parameter: resource");
		}
		if(uid == null || uid.length() == 0) {
			throw new IllegalArgumentException("Invalid parameter: uid");
		}

		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.");
		}
		if(resource.trim().length() > Constants.MAX_ROLE_RESOURCE_LENGTH) {
			throw new IllegalArgumentException("Role resources should not contain more than " + Constants.MAX_ROLE_RESOURCE_LENGTH + " characters.");
		}
		if(uid.trim().length() > Constants.MAX_ROLE_RESOURCE_UID_LENGTH) {
			throw new IllegalArgumentException("Role resources UID should not contain more than " + Constants.MAX_ROLE_RESOURCE_UID_LENGTH + " characters.");
		}


		this.c = c;
		this.personUuid = personUuid;
		this.role = role.trim();
		this.user = user;
		this.resource = resource.trim();
		this.uid = uid.trim();
	}

	public UpsertResourceRole() {
	}

	@Override
	protected void execute() throws IOException {
		Session s = c.getCassandraSession();

		if(!user.hasRole(Constants.PERSON_MANAGE_ROLE)) {
			log(s, "WARN", user, "Permission denied invoking: " + UpsertResourceRole.class.getSimpleName() + " " + getJsonParameters());
			throw new PermissionException(this.getClass().getSimpleName(), user, "You do not have permission to manage this persons roles.", Constants.PERSON_MANAGE_ROLE, "Person", personUuid.toString());
		}

		upsertResourceRole(s, personUuid, resource, uid, role, user);
	}

	@Override
	public String getJsonParameters() {
		return "{" +
				"\"person\":" + user.getPersonUuid() + "," +
				"\"role.person\":" + personUuid + "," +
				"\"role\":\"" + Json.escape(role) + "\"," +
				"\"resource\":\"" + Json.escape(resource) + "\"," +
				"\"uid\":\"" + Json.escape(uid) + "\"" +
				"}";
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy