Search in sources :

Example 1 with PermissionNode

use of fr.xephi.authme.permission.PermissionNode in project AuthMeReloaded by AuthMe.

the class HelpProvider method addPermissionsInfo.

/**
     * Adds help info about the given command's permissions into the provided list.
     *
     * @param command the command to generate permissions info for
     * @param sender the command sender, used to evaluate permissions
     * @param lines the output collection to add the info to
     */
private void addPermissionsInfo(CommandDescription command, CommandSender sender, List<String> lines) {
    PermissionNode permission = command.getPermission();
    if (permission == null) {
        return;
    }
    lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.PERMISSIONS) + ":");
    boolean hasPermission = permissionsManager.hasPermission(sender, permission);
    lines.add(String.format(" " + ChatColor.YELLOW + ChatColor.ITALIC + "%s" + ChatColor.GRAY + " (%s)", permission.getNode(), getLocalPermissionText(hasPermission)));
    // Addendum to the line to specify whether the sender has permission or not when default is OP_ONLY
    final DefaultPermission defaultPermission = permission.getDefaultPermission();
    String addendum = "";
    if (DefaultPermission.OP_ONLY.equals(defaultPermission)) {
        addendum = " (" + getLocalPermissionText(defaultPermission.evaluate(sender)) + ")";
    }
    lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpMessage.DEFAULT) + ": " + ChatColor.GRAY + ChatColor.ITALIC + helpMessagesService.getMessage(defaultPermission) + addendum);
    // Evaluate if the sender has permission to the command
    ChatColor permissionColor;
    String permissionText;
    if (permissionsManager.hasPermission(sender, command.getPermission())) {
        permissionColor = ChatColor.GREEN;
        permissionText = getLocalPermissionText(true);
    } else {
        permissionColor = ChatColor.DARK_RED;
        permissionText = getLocalPermissionText(false);
    }
    lines.add(String.format(ChatColor.GOLD + " %s: %s" + ChatColor.ITALIC + "%s", helpMessagesService.getMessage(HelpMessage.RESULT), permissionColor, permissionText));
}
Also used : PermissionNode(fr.xephi.authme.permission.PermissionNode) DefaultPermission(fr.xephi.authme.permission.DefaultPermission) ChatColor(org.bukkit.ChatColor)

Example 2 with PermissionNode

use of fr.xephi.authme.permission.PermissionNode in project AuthMeReloaded by AuthMe.

the class GeneratePluginYml method gatherChildren.

private Map<String, Boolean> gatherChildren(String parentNode) {
    String parentPath = parentNode.replaceAll("\\.\\*$", "");
    Map<String, Boolean> children = new TreeMap<>();
    for (PermissionNode node : permissionNodes) {
        if (node.getNode().startsWith(parentPath)) {
            children.put(node.getNode(), Boolean.TRUE);
        }
    }
    return children;
}
Also used : TreeMap(java.util.TreeMap) PermissionNode(fr.xephi.authme.permission.PermissionNode)

Example 3 with PermissionNode

use of fr.xephi.authme.permission.PermissionNode in project AuthMeReloaded by AuthMe.

the class HasPermissionCheckerTest method shouldShowSuccessfulTestWithRegularPlayer.

@Test
public void shouldShowSuccessfulTestWithRegularPlayer() {
    // given
    String name = "Chuck";
    Player player = mock(Player.class);
    given(bukkitService.getPlayerExact(name)).willReturn(player);
    PermissionNode permission = AdminPermission.CHANGE_EMAIL;
    given(permissionsManager.hasPermission(player, permission)).willReturn(true);
    CommandSender sender = mock(CommandSender.class);
    // when
    hasPermissionChecker.execute(sender, asList(name, permission.getNode()));
    // then
    verify(bukkitService).getPlayerExact(name);
    verify(permissionsManager).hasPermission(player, permission);
    verify(sender).sendMessage(argThat(containsString("Success: player '" + player.getName() + "' has permission '" + permission.getNode() + "'")));
}
Also used : Player(org.bukkit.entity.Player) CommandSender(org.bukkit.command.CommandSender) Matchers.containsString(org.hamcrest.Matchers.containsString) PermissionNode(fr.xephi.authme.permission.PermissionNode) Test(org.junit.Test)

Example 4 with PermissionNode

use of fr.xephi.authme.permission.PermissionNode in project AuthMeReloaded by AuthMe.

the class GeneratePluginYml method generatePermissions.

private Map<String, Object> generatePermissions() {
    PermissionNodesGatherer gatherer = new PermissionNodesGatherer();
    Map<String, String> permissionDescriptions = gatherer.gatherNodesWithJavaDoc();
    permissionNodes = gatherer.getPermissionClasses().stream().map(clz -> clz.getEnumConstants()).flatMap((PermissionNode[] nodes) -> Arrays.stream(nodes)).collect(Collectors.toList());
    Map<String, Object> descriptions = new TreeMap<>();
    for (PermissionNode node : permissionNodes) {
        descriptions.put(node.getNode(), buildPermissionEntry(node, permissionDescriptions.get(node.getNode())));
    }
    addWildcardPermissions(descriptions);
    return descriptions;
}
Also used : PermissionNode(fr.xephi.authme.permission.PermissionNode) PermissionNodesGatherer(tools.docs.permissions.PermissionNodesGatherer) Arrays(java.util.Arrays) ImmutableMap(com.google.common.collect.ImmutableMap) DefaultPermission(fr.xephi.authme.permission.DefaultPermission) CommandUtils(fr.xephi.authme.command.CommandUtils) Collection(java.util.Collection) ToolsConstants(tools.utils.ToolsConstants) FileIoUtils(tools.utils.FileIoUtils) Collectors(java.util.stream.Collectors) CommandInitializer(fr.xephi.authme.command.CommandInitializer) CommandDescription(fr.xephi.authme.command.CommandDescription) LinkedHashMap(java.util.LinkedHashMap) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) List(java.util.List) AutoToolTask(tools.utils.AutoToolTask) StringReader(java.io.StringReader) TreeMap(java.util.TreeMap) Paths(java.nio.file.Paths) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) Map(java.util.Map) PermissionNodesGatherer(tools.docs.permissions.PermissionNodesGatherer) TreeMap(java.util.TreeMap) PermissionNode(fr.xephi.authme.permission.PermissionNode)

Example 5 with PermissionNode

use of fr.xephi.authme.permission.PermissionNode in project AuthMeReloaded by AuthMe.

the class CommonServiceTest method shouldCheckPermission.

@Test
public void shouldCheckPermission() {
    // given
    Player player = mock(Player.class);
    PermissionNode permission = PlayerPermission.CHANGE_PASSWORD;
    given(permissionsManager.hasPermission(player, permission)).willReturn(true);
    // when
    boolean result = commonService.hasPermission(player, permission);
    // then
    verify(permissionsManager).hasPermission(player, permission);
    assertThat(result, equalTo(true));
}
Also used : Player(org.bukkit.entity.Player) PermissionNode(fr.xephi.authme.permission.PermissionNode) Test(org.junit.Test)

Aggregations

PermissionNode (fr.xephi.authme.permission.PermissionNode)5 DefaultPermission (fr.xephi.authme.permission.DefaultPermission)2 TreeMap (java.util.TreeMap)2 Player (org.bukkit.entity.Player)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 CommandDescription (fr.xephi.authme.command.CommandDescription)1 CommandInitializer (fr.xephi.authme.command.CommandInitializer)1 CommandUtils (fr.xephi.authme.command.CommandUtils)1 StringReader (java.io.StringReader)1 Paths (java.nio.file.Paths)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 ChatColor (org.bukkit.ChatColor)1 CommandSender (org.bukkit.command.CommandSender)1 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)1