<commit_before>/*
 * PermissionsEx - Permissions plugin for Bukkit
 * Copyright (C) 2011 t3hk0d3 http://www.tehkode.ru
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package ru.tehkode.permissions.bukkit.superperms;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permissible;
import org.bukkit.permissions.PermissibleBase;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo;
import ru.tehkode.permissions.PermissionGroup;
import ru.tehkode.permissions.PermissionUser;
import ru.tehkode.permissions.bukkit.BukkitPermissions;
import ru.tehkode.permissions.bukkit.PermissionsEx;

public class PermissiblePEX extends PermissibleBase {

	protected Player player = null;
	protected boolean strictMode = false;
	protected boolean injectMetadata = true;
	protected BukkitPermissions bridge;

	protected PermissiblePEX(Player player, BukkitPermissions bridge) {
		super(player);

		this.bridge = bridge;

		this.strictMode = bridge.isStrictMode();
		this.player = player;
	}

	public static void inject(Player player, BukkitPermissions bridge) {
		if (player.hasPermission("permissionsex.handler.injected")) { // already injected
			return;
		}

		try {
			Permissible permissible = new PermissiblePEX(player, bridge);

			if (player.getClass().getName().contains("Spout")) { // we have spout installed
				injectSpout(player, permissible);
			} else {
				injectCraftBukkit(player, permissible);
			}

			permissible.recalculatePermissions();

			Logger.getLogger("Minecraft").info("[PermissionsEx] Permissions handler for " + player.getName() + " successfuly injected");
		} catch (Throwable e) {
			Logger.getLogger("Minecraft").warning("[PermissionsEx] Failed to inject own Permissible");
			e.printStackTrace();
		}
	}

	protected static void injectCraftBukkit(Player player, Permissible permissible) throws Throwable {
		Class humanEntity = Class.forName("org.bukkit.craftbukkit.entity.CraftHumanEntity");

		Field permField = humanEntity.getDeclaredField("perm");
		// Make it public for reflection
		permField.setAccessible(true);

		PermissibleBase oldBase = (PermissibleBase) permField.get(player);

		// Copy permissions and attachments from old Permissible

		// Attachments
		Field attachmentField = PermissibleBase.class.getDeclaredField("attachments");
		attachmentField.setAccessible(true);
		attachmentField.set(permissible, attachmentField.get(oldBase));

		// Permissions
		Field permissionsField = PermissibleBase.class.getDeclaredField("permissions");
		permissionsField.setAccessible(true);
		permissionsField.set(permissible, permissionsField.get(oldBase));

		// Inject permissible
		permField.set(player, permissible);
	}

	protected static void injectSpout(Player player, Permissible permissible) throws Throwable {
		Class humanEntity = Class.forName("org.getspout.spout.player.SpoutCraftPlayer");
		Field permField = humanEntity.getDeclaredField("perm");
		permField.setAccessible(true);

		permField.set(player, permissible);
	}

	@Override
	public boolean hasPermission(String inName) {
		if (inName.equals("permissionsex.handler.injected")) {
			return PermissionsEx.isAvailable();
		}

		String worldName = player.getWorld().getName();

		try {
			PermissionUser user = PermissionsEx.getUser(this.player);
			if (user == null) {
				return super.hasPermission(inName);
			}

			// Check using PEX
			String expression = user.getMatchingExpression(inName.toLowerCase(), worldName);

			if (expression != null || this.strictMode) {
				if (user.isDebug()) {
					Logger.getLogger("Minecraft").info("User " + user.getName() + " checked for \"" + inName + "\", " + (expression == null ? "no permission found" : "\"" + expression + "\" found"));
				}

				return user.explainExpression(expression);
			}

			if (this.bridge.isEnableParentNodes()) {
				// check using parent nodes
				Map<String, Boolean> parentNodes = this.bridge.getChildPermissions().get(inName.toLowerCase());

				if (parentNodes != null) {
					for (String parentPermission : parentNodes.keySet()) {
						if (user.has(parentPermission, worldName)) {
							if (user.isDebug()) {
								Logger.getLogger("Minecraft").info("User " + user.getName() + " checked for \"" + inName + "\", " + (expression == null ? "no permission found" : " found from \"" + parentPermission + "\""));
							}

							return parentNodes.get(parentPermission).booleanValue();
						}
					}
				}
			}

			// Pass check to superperms
			if (super.hasPermission(inName)) {
				if (user.isDebug()) {
					Logger.getLogger("Minecraft").info("User " + user.getName() + " checked for \"" + inName + "\", found in superperms");
				}

				return true;
			}

			// No permission found
			if (user.isDebug()) {
				Logger.getLogger("Minecraft").info("User " + user.getName() + " checked for \"" + inName + "\", no permission found");
			}
		} catch (Throwable e) {
			// This should stay so if something will gone wrong user have chance to understand whats wrong actually
			e.printStackTrace();
		}
<commit_msg>Fixed parent permissions check not being recursive. issue #107<commit_after>/*
 * PermissionsEx - Permissions plugin for Bukkit
 * Copyright (C) 2011 t3hk0d3 http://www.tehkode.ru
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package ru.tehkode.permissions.bukkit.superperms;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permissible;
import org.bukkit.permissions.PermissibleBase;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo;
import ru.tehkode.permissions.PermissionGroup;
import ru.tehkode.permissions.PermissionUser;
import ru.tehkode.permissions.bukkit.BukkitPermissions;
import ru.tehkode.permissions.bukkit.PermissionsEx;

public class PermissiblePEX extends PermissibleBase {

	protected Player player = null;
	protected boolean strictMode = false;
	protected boolean injectMetadata = true;
	protected BukkitPermissions bridge;

	protected PermissiblePEX(Player player, BukkitPermissions bridge) {
		super(player);

		this.bridge = bridge;

		this.strictMode = bridge.isStrictMode();
		this.player = player;
	}

	public static void inject(Player player, BukkitPermissions bridge) {
		if (player.hasPermission("permissionsex.handler.injected")) { // already injected
			return;
		}

		try {
			Permissible permissible = new PermissiblePEX(player, bridge);

			if (player.getClass().getName().contains("Spout")) { // we have spout installed
				injectSpout(player, permissible);
			} else {
				injectCraftBukkit(player, permissible);
			}

			permissible.recalculatePermissions();

			Logger.getLogger("Minecraft").info("[PermissionsEx] Permissions handler for " + player.getName() + " successfuly injected");
		} catch (Throwable e) {
			Logger.getLogger("Minecraft").warning("[PermissionsEx] Failed to inject own Permissible");
			e.printStackTrace();
		}
	}

	protected static void injectCraftBukkit(Player player, Permissible permissible) throws Throwable {
		Class humanEntity = Class.forName("org.bukkit.craftbukkit.entity.CraftHumanEntity");

		Field permField = humanEntity.getDeclaredField("perm");
		// Make it public for reflection
		permField.setAccessible(true);

		PermissibleBase oldBase = (PermissibleBase) permField.get(player);

		// Copy permissions and attachments from old Permissible

		// Attachments
		Field attachmentField = PermissibleBase.class.getDeclaredField("attachments");
		attachmentField.setAccessible(true);
		attachmentField.set(permissible, attachmentField.get(oldBase));

		// Permissions
		Field permissionsField = PermissibleBase.class.getDeclaredField("permissions");
		permissionsField.setAccessible(true);
		permissionsField.set(permissible, permissionsField.get(oldBase));

		// Inject permissible
		permField.set(player, permissible);
	}

	protected static void injectSpout(Player player, Permissible permissible) throws Throwable {
		Class humanEntity = Class.forName("org.getspout.spout.player.SpoutCraftPlayer");
		Field permField = humanEntity.getDeclaredField("perm");
		permField.setAccessible(true);

		permField.set(player, permissible);
	}

	@Override
	public boolean hasPermission(String inName) {
		if (inName.equals("permissionsex.handler.injected")) {
			return PermissionsEx.isAvailable();
		}

		String worldName = player.getWorld().getName();

		try {
			PermissionUser user = PermissionsEx.getUser(this.player);
			if (user == null) {
				return super.hasPermission(inName);
			}

			// Check using PEX
			String expression = user.getMatchingExpression(inName.toLowerCase(), worldName);

			if (expression != null || this.strictMode) {
				if (user.isDebug()) {
					Logger.getLogger("Minecraft").info("User " + user.getName() + " checked for \"" + inName + "\", " + (expression == null ? "no permission found" : "\"" + expression + "\" found"));
				}

				return user.explainExpression(expression);
			}

			if (this.bridge.isEnableParentNodes()) {
				// check using parent nodes
				Map<String, Boolean> parentNodes = this.bridge.getChildPermissions().get(inName.toLowerCase());

				if (parentNodes != null) {
					for (String parentPermission : parentNodes.keySet()) {
						if (this.hasPermission(parentPermission)) {
							if (user.isDebug()) {
								Logger.getLogger("Minecraft").info("User " + user.getName() + " checked for \"" + inName + "\", " + (expression == null ? "no permission found" : " found from \"" + parentPermission + "\""));
							}

							return parentNodes.get(parentPermission).booleanValue();
						}
					}
				}
			}

			// Pass check to superperms
			if (super.hasPermission(inName)) {
				if (user.isDebug()) {
					Logger.getLogger("Minecraft").info("User " + user.getName() + " checked for \"" + inName + "\", found in superperms");
				}

				return true;
			}

			// No permission found
			if (user.isDebug()) {
				Logger.getLogger("Minecraft").info("User " + user.getName() + " checked for \"" + inName + "\", no permission found");
			}
		} catch (Throwable e) {
			// This should stay so if something will gone wrong user have chance to understand whats wrong actually
			e.printStackTrace();
		}

{'fraction_non_alphanumeric': 0.11531972330015185, 'fraction_numerical': 0.0034587481019065294, 'mean_word_length': 4.309001343484102, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3230489', 'n_tokens_mistral': 3884, 'n_tokens_neox': 3380, 'n_words': 887}