summaryrefslogtreecommitdiffstats
path: root/EssentialsGroupManager/src/org/anjocaido/groupmanager/permissions/BukkitPermissions.java
blob: 202e27e3af8f9ea7ceb414ef556acf9155a2cbfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* 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 org.anjocaido.groupmanager.permissions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.anjocaido.groupmanager.GroupManager;
import org.anjocaido.groupmanager.data.User;
import org.anjocaido.groupmanager.dataholder.OverloadedWorldHolder;
import org.anjocaido.groupmanager.utils.PermissionCheckResult;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerPortalEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.event.server.ServerListener;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;


/**
 * 
 * BukkitPermissions overrides to force GM reponses to Superperms
 * 
 * @author ElgarL, based upon PermissionsEX implementation
 */
public class BukkitPermissions {

	protected Map<Player, PermissionAttachment> attachments = new HashMap<Player, PermissionAttachment>();
	protected LinkedList<Permission> registeredPermissions = new LinkedList<Permission>();
	protected GroupManager plugin;
	protected boolean dumpAllPermissions = true;
	protected boolean dumpMatchedPermissions = true;
	public boolean player_join = false;

	public BukkitPermissions(GroupManager plugin) {
		this.plugin = plugin;

		this.collectPermissions();
		this.registerEvents();

		this.updateAllPlayers();

		GroupManager.logger.info("Superperms support enabled.");
	}

	private void registerEvents() {
		PluginManager manager = plugin.getServer().getPluginManager();

		PlayerEvents playerEventListener = new PlayerEvents();

		manager.registerEvent(Event.Type.PLAYER_JOIN, playerEventListener, Event.Priority.Lowest, plugin);
		manager.registerEvent(Event.Type.PLAYER_KICK, playerEventListener, Event.Priority.Lowest, plugin);
		manager.registerEvent(Event.Type.PLAYER_QUIT, playerEventListener, Event.Priority.Lowest, plugin);

		manager.registerEvent(Event.Type.PLAYER_RESPAWN, playerEventListener, Event.Priority.Lowest, plugin);
		manager.registerEvent(Event.Type.PLAYER_TELEPORT, playerEventListener, Event.Priority.Lowest, plugin);
		manager.registerEvent(Event.Type.PLAYER_PORTAL, playerEventListener, Event.Priority.Lowest, plugin);

		ServerListener serverListener = new BukkitEvents();

		manager.registerEvent(Event.Type.PLUGIN_ENABLE, serverListener, Event.Priority.Normal, plugin);
		manager.registerEvent(Event.Type.PLUGIN_DISABLE, serverListener, Event.Priority.Normal, plugin);
	}

	public void collectPermissions() {
		registeredPermissions.clear();
		for (Plugin bukkitPlugin : Bukkit.getServer().getPluginManager().getPlugins()) {
			for (Permission permission : bukkitPlugin.getDescription().getPermissions())
				registeredPermissions.push(permission);
		}
	}

	public void updatePermissions(Player player) {
		this.updatePermissions(player, null);
	}

	public void updatePermissions(Player player, String world) {
		if (player == null || !GroupManager.isLoaded()) {
			return;
		}

		if (!this.attachments.containsKey(player)) {
			this.attachments.put(player, player.addAttachment(plugin));
		}

		if (world == null) {
			world = player.getWorld().getName();
		}

		// All permissions registered with Bukkit for this player
		PermissionAttachment attachment = this.attachments.get(player);

		OverloadedWorldHolder worldData = plugin.getWorldsHolder().getWorldData(world);

		User user = worldData.getUser(player.getName());

		// clear permissions
		for (String permission : attachment.getPermissions().keySet())
			attachment.unsetPermission(permission);

		/*
		 * find matching permissions
		 * 
		 * and base bukkit perms if we are set to allow bukkit permissions to
		 * override.
		 */
		Boolean value = false;
				
		for (Permission permission : registeredPermissions) {
			
			PermissionCheckResult result = worldData.getPermissionsHandler().checkFullGMPermission(user, permission.getName(), false);

			// Only check bukkit override IF we don't have the permission
			// directly.
			if (result.resultType == PermissionCheckResult.Type.NOTFOUND) {
				PermissionDefault permDefault = permission.getDefault();

				if ((plugin.getGMConfig().isBukkitPermsOverride()) && ((permDefault == PermissionDefault.TRUE)
						|| ((permDefault == PermissionDefault.NOT_OP) && !player.isOp())
						|| ((permDefault == PermissionDefault.OP) && player.isOp()))) {
					value = true;
				} else {
					value = false;
				}
			} else if (result.resultType == PermissionCheckResult.Type.NEGATION) {
				value = false;
			} else {
				value = true;
			}

			// Set the root permission
			if ((value == true) || (result.resultType == PermissionCheckResult.Type.NEGATION)) {
				attachment.setPermission(permission, value);
			}
			/*
			if ((value == true) || (result.resultType == PermissionCheckResult.Type.NOTFOUND)) {
				// fetch and set all children of this permission node
				Map<String, Boolean> children = permission.getChildren();
				if (children != null) {
					for (String child : children.keySet()) {
						if (children.get(child))
							attachment.setPermission(child, value);
					}
				}
			}*/

		}

		// Add any missing permissions for this player (non bukkit plugins and child nodes)
		List<String> playerPermArray = worldData.getPermissionsHandler().getAllPlayersPermissions(player.getName());

		for (String permission : playerPermArray) {
			value = true;
			if (permission.startsWith("-")) {
				permission = permission.substring(1); // cut off -
				value = false;
			}
			
			if (!attachment.getPermissions().containsKey(permission)) {
				attachment.setPermission(permission, value);
			}
		}
		player.recalculatePermissions();
	}

	/**
	 * Returns a map of the ALL child permissions as defined by the supplying plugin
	 * null is empty
	 * 
	 * @param node
	 * @return Map of child permissions
	 */
	public Map<String, Boolean> getAllChildren(String node, List<String> playerPermArray) {
		
		LinkedList<String> stack = new LinkedList<String>();
		Map<String, Boolean> alreadyVisited = new HashMap<String, Boolean>();
		stack.push(node);
		alreadyVisited.put(node, true);
		
		while (!stack.isEmpty()) {
			String now = stack.pop();
			
			Map<String, Boolean> children = getChildren(now);
			
			if ((children != null) && (!playerPermArray.contains("-"+now))) {
				for (String childName : children.keySet()) {
					if (!alreadyVisited.containsKey(childName)) {
						stack.push(childName);
						alreadyVisited.put(childName, children.get(childName));
					}
				}
			}
		}
		alreadyVisited.remove(node);
		if (!alreadyVisited.isEmpty()) return alreadyVisited;
		
		return null;
	}
		
	/**
	 * Returns a map of the child permissions (1 node deep) as defined by the supplying plugin
	 * null is empty
	 * 
	 * @param node
	 * @return Map of child permissions
	 */
	public Map<String, Boolean> getChildren(String node) {
		for (Permission permission : registeredPermissions) {
			if (permission.getName().equalsIgnoreCase(node)) {
				return permission.getChildren();
			}
		}
		
		return null;
	}

	public List<String> listPerms(Player player) {
		List<String> perms = new ArrayList<String>();

		/*
		 * // All permissions registered with Bukkit for this player
		 * PermissionAttachment attachment = this.attachments.get(player);
		 * 
		 * // List perms for this player perms.add("Attachment Permissions:");
		 * for(Map.Entry<String, Boolean> entry :
		 * attachment.getPermissions().entrySet()){ perms.add(" " +
		 * entry.getKey() + " = " + entry.getValue()); }
		 */

		perms.add("Effective Permissions:");
		for (PermissionAttachmentInfo info : player.getEffectivePermissions()) {
			if (info.getValue() == true)
				perms.add(" " + info.getPermission() + " = " + info.getValue());
		}
		return perms;
	}

	public void updateAllPlayers() {
		for (Player player : Bukkit.getServer().getOnlinePlayers()) {
			updatePermissions(player);
		}
	}

	protected class PlayerEvents extends PlayerListener {

		@Override
		public void onPlayerJoin(PlayerJoinEvent event) {
			player_join = true;
			Player player = event.getPlayer();
			// force GM to create the player if they are not already listed.
			if (plugin.getWorldsHolder().getWorldData(player.getWorld().getName()).getUser(player.getName()) != null) {
				player_join = false;
				updatePermissions(event.getPlayer());
			} else
				player_join = false;
		}

		@Override
		public void onPlayerPortal(PlayerPortalEvent event) { // will portal into another world
			if (event.getTo() != null && !event.getFrom().getWorld().equals(event.getTo().getWorld())) { // only if world actually changed
				updatePermissions(event.getPlayer(), event.getTo().getWorld().getName());
			}
		}

		@Override
		public void onPlayerRespawn(PlayerRespawnEvent event) { // can be respawned in another world
			updatePermissions(event.getPlayer(), event.getRespawnLocation().getWorld().getName());
		}

		@Override
		public void onPlayerTeleport(PlayerTeleportEvent event) { // can be teleported into another world
			if (event.getTo() != null && !event.getFrom().getWorld().equals(event.getTo().getWorld())) { // only if world actually changed
				updatePermissions(event.getPlayer(), event.getTo().getWorld().getName());
			}
		}

		@Override
		public void onPlayerQuit(PlayerQuitEvent event) {
			if (!GroupManager.isLoaded())
				return;

			attachments.remove(event.getPlayer());
		}

		@Override
		public void onPlayerKick(PlayerKickEvent event) {
			attachments.remove(event.getPlayer());
		}
	}

	protected class BukkitEvents extends ServerListener {

		@Override
		public void onPluginEnable(PluginEnableEvent event) {
			if (!GroupManager.isLoaded())
				return;

			collectPermissions();
			updateAllPlayers();
		}

		@Override
		public void onPluginDisable(PluginDisableEvent event) {
			// collectPermissions();
			// updateAllPlayers();
		}
	}

}