Solved Checking for permission *

Discussion in 'Plugin Development' started by spy85, Jan 29, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    spy85

    I know how to check if a CommandSender or Player has a permission with sender.hasPermission(permission) but the problem with the hasPermission(String) method is that for example: if I check to see if a player.hasPermission("itutorial.user.start") this method does not check for "itutorial.user.*" or "itutorial.*" unless I myself manually do this which of course normally would not be a problem. In my case it is...

    Every command within my plugin implements ITSubCommand interface which has a method:
    public String permission();
    When a player executes a command ITCommand finds that command and checks if the player has the permission from the String permission() method of this command but how can I make it so it does not only check if they have specifically String permssion() but if they have a node with '*' making it return true?

    I hope this makes sense I know this is more of a complex and detailed specification but if anyone could help that would be fantastic! Thanks!
     
  2. Offline

    AoH_Ruthless

    spy85
    You don't need to do anything, the '*' is just basically all permissions so it should in fact check for itutorial.user.start.
     
  3. Offline

    spy85

    Surprisingly it does not but I just found out 2 solutions one of which I came up with on my own I will post about them and change the status of this thread to solved momentarily.

    I found that in order for hasPermission(String) to check for nodes with '*' such as 'itutorial.admin.*' you have to specifiy these within the plugin.yml under a section called permissions:

    Here is an example:
    Code:
    permissions:
      itutorial.*:
        description: Gives access to all iTutorial commands
        children:
          itutorial.user.next: true
          itutorial.user.start: true
      itutorial.user.next:
        description: Access to /it next
      itutorial.user.start:
        description: Access to /it start
    Now if a player has itutorial.* they will also have itutorial.user.next and itutorial.user.start as well!

    This was solution one and it seems to be the most organized but does take a bit longer of a time so I also came up with my own method to check for permissions within my plugin:

    (permission variable is the string of the permission, the sender variable is the CommandSender variable, these variables are given when the method is executed)
    Code:
            int nodeAmt = 1;
            for(int x=0; x<permission.length(); x++){
                if(permission.charAt(x) == '.'){
                    String testPerm = permission.substring(0,x)+".*";
                    sender.sendMessage(testPerm);
                    if(sender.hasPermission(testPerm)){
                        return true;
                    }
                }
            }
    This method takes the given permission and sets a '.*' at the end of each permission node to check if the player has that.

    I have solved the problem so now I hope this comes to good use to others who may be dealing with the same issue. Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  4. Offline

    Garris0n

    That stuff is done by permissions managers, it's not actually in Bukkit.
     
    spy85 likes this.
  5. Offline

    spy85

    Oh ok I see but bukkit must be hooking into the managers with its hasPermission(String) method correct?
     
  6. Offline

    Garris0n

    No, the permission managers just do extra checks with the asterisks and other fancy stuff to make it easier to add permissions.
     
    spy85 likes this.
  7. Offline

    spy85

    Oh ok that makes so sense thanks!
     
Thread Status:
Not open for further replies.

Share This Page