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

base.jee.api.sql.LeaveGroup 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.sql;

import base.Command;
import base.jee.Constants;
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.LeaveGroup.leaveGroup;
import static base.jee.api.sql.util.Log.log;

public class LeaveGroup extends Command {

	private DataSource ds;
	private User user;
	private UUID group;
	private UUID person;

	public LeaveGroup(DataSource ds, User user, UUID group, UUID person) throws PermissionException {

		if(ds == null) {
			throw new IllegalArgumentException("Invalid parameter: ds");
		}
		if(group == null) {
			throw new IllegalArgumentException("Invalid parameter: group");
		}
		if(person == null) {
			throw new IllegalArgumentException("Invalid parameter: person");
		}
		if(user == null) {
			throw new PermissionException(this.getClass().getSimpleName(), user, "You do not have permission to manage groups.", Constants.GROUP_MANAGE_ROLE);
		}

		this.ds = ds;
		this.group = group;
		this.person = person;
		this.user = user;
	}

	public LeaveGroup() {
	}

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

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

			if(!user.getPersonUuid().equals(person) && !user.hasRole(Constants.GROUP_MANAGE_ROLE)) {
				c.rollback();
				log(c, "WARN", user, "Permission denied invoking: " + LeaveGroup.class.getSimpleName() + " " + getJsonParameters());
				c.commit();
				throw new PermissionException(this.getClass().getSimpleName(), user, "You do not have permission to manage groups.", Constants.GROUP_MANAGE_ROLE);
			}

			leaveGroup(c, group, person, 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 "{" +
				"\"user.person_uuid\":" + user.getPersonUuid() + "," +
				"\"group.uuid\":\"" + group + "\"," +
				"\"person.uuid\":\"" + person + "\"" +
				"}";
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy