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

base.jee.api.sql.UpsertGroupResourceRole Maven / Gradle / Ivy

/*
 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.Command;
import base.jee.Constants;
import base.json.Json;
import base.security.PermissionException;
import base.security.User;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.UUID;

import static base.jee.api.sql.util.Log.log;
import static base.jee.api.sql.util.UpsertGroupResourceRole.upsertGroupResourceRole;

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

	private DataSource ds;
	private User user;
	private UUID groupUuid;
	private String role;
	private String resource;
	private String uid;

	public UpsertGroupResourceRole(DataSource ds, User user, UUID groupUuid, String role, String resource, String uid) throws PermissionException {
		if(ds == null) {
			throw new IllegalArgumentException("Invalid parameter: ds");
		}
		if(role == null || role.length() == 0) {
			throw new IllegalArgumentException("Invalid parameter: role");
		}
		if(user == null) {
			throw new PermissionException(this.getClass().getSimpleName(), user, "Requires an authenticated user.", "");
		}
		if(groupUuid == null) {
			throw new IllegalArgumentException("Invalid parameter: groupUuid");
		}
		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.ds = ds;
		this.groupUuid = groupUuid;
		this.role = role.trim();
		this.user = user;
		this.resource = resource.trim();
		this.uid = uid.trim();
	}

	public UpsertGroupResourceRole() {
	}

	@Override
	protected void execute() throws IOException {
		Connection c = null;

		try {
			c = ds.getConnection();
			c.setAutoCommit(false);

			if(!user.hasRole(Constants.GROUP_MANAGE_ROLE)) {
				c.rollback();
				log(c, "WARN", user, "Permission denied invoking: " + UpsertGroupResourceRole.class.getSimpleName() + " " + getJsonParameters());
				c.commit();
				throw new PermissionException(this.getClass().getSimpleName(), user, "You do not have permission to manage this persons roles.", Constants.GROUP_MANAGE_ROLE, "Group", groupUuid.toString());
			}

			upsertGroupResourceRole(c, groupUuid, resource, uid, role, user);

			c.commit();
			c.close();
			c = null;
		} catch(SQLException e) {
			throw new IOException(e);
		} finally {
			if(c != null) {
				try { c.rollback(); } catch (SQLException e) { }
				try { c.close(); } catch (SQLException e) { }
			}
		}
	}

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

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy