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

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

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

import static base.jee.api.cassandra.util.Log.log;
import static base.jee.api.cassandra.util.UpdatePersonTokenRoleString.updatePersonTokenRoleString;

public class DeleteRole extends Command {

	private CassandraAPI c;
	private User updator;
	private UUID personUuid;
	private String role;

	public DeleteRole(CassandraAPI c, User user, UUID personUuid, String role) {

		if(c == null) {
			throw new IllegalArgumentException("Invalid parameter: c");
		}
		if(role == null || role.length() == 0) {
			throw new IllegalArgumentException("Invalid parameter: role");
		}
		if(user == null || !user.isAuthenticated()) {
			throw new IllegalArgumentException("Invalid parameter: user");
		}
		if(personUuid == null) {
			throw new IllegalArgumentException("Invalid parameter: personId");
		}

		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.");
		}

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

	public DeleteRole() {
	}

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

		if(!updator.hasRole(Constants.PERSON_MANAGE_ROLE)) {
			log(s, "WARN", updator, "Permission denied invoking: " + DeleteRole.class.getSimpleName() + " " + getJsonParameters());
			throw new IllegalStateException("You do not have permission to manage roles.");
		}

		PreparedStatement p = s.prepare("delete from role where resource='#' and uid='#' and role=? and person_uuid=?");
		s.execute(p.bind(role, personUuid));

		updatePersonTokenRoleString(s, personUuid);

		log(s, "INFO", updator, "Removed role " + role + " for person " + personUuid, new ResourceUid("Person", personUuid.toString()));
	}

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

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy