Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How to create a role...?

xDaizuxDaizu New
edited May 2015 in Vanilla 2.0 - 2.8

In the documentation, it says that the request parameters to create a new role are

POST /api/roles HTTP/1.1
Content-Type: application/json

{
"Name": "Foo",
"Description": "Lorem ipsum dolor sit amet",
"PersonalInfo": false,
"Permission": Array
}

but for the most important/tricky part, permissions, it only says "array". Array of what? What is the format model of the permissions?

PS: In general, I find that the documentation has too many "Array" and "Object" placeholders that really don't help at all. I would expand it myself, but I'm still new to Vanilla. :( I think a more verbose specification would be really appreciated and helpful for new users

Comments

  • R_JR_J Ex-Fanboy Munich Admin

    Look at /applications/dashboard/settings/structure.php and you find things like that:

       // Set initial member permissions.
       $PermissionModel->Save(array(
          'Role' => 'Member',
          'Garden.SignIn.Allow' => 1,
          'Garden.Activity.View' => 1,
          'Garden.Profiles.View' => 1,
          'Garden.Profiles.Edit' => 1,
          'Garden.Email.View' => 1
          ));
    

    So you need an associative array of permissions:

    $Permissions = array (
          'Garden.SignIn.Allow' => 1,
          'Garden.Activity.View' => 1,
          'Garden.Profiles.View' => 1,
          'Garden.Profiles.Edit' => 1,
          'Garden.Email.View' => 1
    );
    

    ... at least that's what I would guess...

  • xDaizuxDaizu New
    edited May 2015

    @R_J said:
    Look at /applications/dashboard/settings/structure.php and you find....

    I love you.

    (I haven't tried it yet but at least now I know where the "templates" are. Just what I needed!)

  • R_JR_J Ex-Fanboy Munich Admin

    @xDaizu said:
    I love you.

    I feel slightly aroused now... :blush:

    Generally whenever you see a simple Array or Object it might help to look at the Vanilla source. So what is needed when a role is added? A role name and a permission. And how are permissions saved? Look in the source for PermissionModel->Save and you'll find examples.

  • hgtonighthgtonight ∞ · New Moderator

    @xDaizu said:

    I haven't tried it yet but at least now I know where the "templates" are. Just what I needed!

    The important thing you need to know about the API application is that it doesn't do any of the heavy lifting. It authenticates the request and passes the pertinent data on to the standard controller.

    If you want to know what is needed, look at the api file for that API request and it tells you exactly what class and method are being called.

    For example, your request uses the API registered here: https://github.com/kasperisager/vanilla-api/blob/1d28504ef55fed8c0894705a4f39385b02449d8b/library/apis/class.rolesapi.php#L35-L38

    So now you know that you need to look at the RoleController::Add() method. Check out /applications/dashboard/controllers/class.rolecontroller.php for how the data gets handled.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • xDaizuxDaizu New
    edited May 2015

    Ok, I almost understand it.... but I still don't understand how the permissions creation work. I'll explain my problem, an some help/indications would be extremely appreciated. In return, after I figure this out, I will create a detailed wiki about this topic, to help the next one. Deal? :)

    I wanna create role-categories on the fly, programatically.

    Let's say I have one activitity, let's call it... Activity1 (so original, I know). So I wanna create a category called Activity1 and a role called Activity1Participant. Said role will ONLY have read/write permissions for the category Activity1. So, if I understand the schema and workflow correctly, I should:
    1. Create a Category, calling {{domain}}/api/categories with POST:

        {
          "Name": "Activity1",
          "UrlCode": "Activity1"
        }
    

    2. Save the ActivityID returned, i.e. $activityID
    2. Create a Role, POSTing to {{domain}}/api/roles:

    {
      "Name": "Activity1Participant",
      "Permission": [ {permissionObject} ]
    }
    

    But is this permission field that gives me trouble. I tried something like

     "Permission": [ {
                           "Category": $activityID,
                           'Vanilla.Comments.Add': 1
                           } ]
    

    but it doesn't work.

    I also tried:

     "Permission": [ {
                           "JunctionTable": "Category",
                           "JunctionColumn": "PermissionCategoryID",
                           "JunctionID": $activityID,
                           'Vanilla.Comments.Add': 1
                           } ]
    

    and it didn't work either (both times, it creates a row in gdn_permissions with its auto-increment ID (cool), RoleID equal to the new created role (fine), and the rest of fields equal to NULL or 0 (dammit!)

    Any help, plz? Ç_Ç

  • hgtonighthgtonight ∞ · New Moderator

    This is the form of the permissions data that gets submitted to the model:

    'Checkboxes' => 
        array (size=42)
          0 => string 'Permission' (length=10)
          1 => string 'Permission' (length=10)
          2 => string 'Permission' (length=10)
          3 => string 'Permission' (length=10)
          4 => string 'Permission' (length=10)
          5 => string 'Permission' (length=10)
          6 => string 'Permission' (length=10)
          7 => string 'Permission' (length=10)
          8 => string 'Permission' (length=10)
          9 => string 'Permission' (length=10)
          10 => string 'Permission' (length=10)
          11 => string 'Permission' (length=10)
          12 => string 'Permission' (length=10)
          13 => string 'Permission' (length=10)
          14 => string 'Permission' (length=10)
          15 => string 'Permission' (length=10)
          16 => string 'Permission' (length=10)
          17 => string 'Permission' (length=10)
          18 => string 'Permission' (length=10)
          19 => string 'Permission' (length=10)
          20 => string 'Permission' (length=10)
          21 => string 'Permission' (length=10)
          22 => string 'Permission' (length=10)
          23 => string 'Permission' (length=10)
          24 => string 'Permission' (length=10)
          25 => string 'Permission' (length=10)
          26 => string 'Permission' (length=10)
          27 => string 'Permission' (length=10)
          28 => string 'Permission' (length=10)
          29 => string 'Permission' (length=10)
          30 => string 'Permission' (length=10)
          31 => string 'Permission' (length=10)
          32 => string 'Permission' (length=10)
          33 => string 'Permission' (length=10)
          34 => string 'Permission' (length=10)
          35 => string 'Permission' (length=10)
          36 => string 'Permission' (length=10)
          37 => string 'Permission' (length=10)
          38 => string 'Permission' (length=10)
          39 => string 'Permission' (length=10)
          40 => string 'Permission' (length=10)
          41 => string 'Permission' (length=10)
      'Permission' => 
        array (size=15)
          0 => string 'Garden.Activity.View' (length=20)
          1 => string 'Garden.Email.View' (length=17)
          2 => string 'Garden.Profiles.Edit' (length=20)
          3 => string 'Garden.Profiles.View' (length=20)
          4 => string 'Garden.SignIn.Allow' (length=19)
          5 => string 'Conversations.Conversations.Add' (length=31)
          6 => string 'Vanilla.Comments.Me' (length=19)
          7 => string 'Vanilla.PrefixDiscussion.Add' (length=28)
          8 => string 'Vanilla.PrefixDiscussion.View' (length=29)
          9 => string 'Category/PermissionCategoryID/-1//Vanilla.Comments.Add' (length=54)
          10 => string 'Category/PermissionCategoryID/-1//Vanilla.Discussions.Add' (length=57)
          11 => string 'Category/PermissionCategoryID/-1//Vanilla.Discussions.View' (length=58)
          12 => string 'Category/PermissionCategoryID/1//Vanilla.Comments.Add' (length=53)
          13 => string 'Category/PermissionCategoryID/1//Vanilla.Discussions.Add' (length=56)
          14 => string 'Category/PermissionCategoryID/1//Vanilla.Discussions.View' (length=57)
    

    It looks like you should be adding an array of permission names you want set. Something like:

    "Permission": {
                          "Category/PermissionCategoryID/1//Vanilla.Comments.Add",
                          "Category/PermissionCategoryID/1//Vanilla.Discussions.Add",
                          "Category/PermissionCategoryID/1//Vanilla.Discussions.View" 
                          }
    

    Where 1 is the permission category id.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • @hgtonight said:

    It looks like you should be adding an array of permission names you want set. Something like:

    "Permission": {
                          "Category/PermissionCategoryID/1//Vanilla.Comments.Add",
                          "Category/PermissionCategoryID/1//Vanilla.Discussions.Add",
                          "Category/PermissionCategoryID/1//Vanilla.Discussions.View" 
                          }
    

    Where 1 is the permission category id.

    It worked like a charm! I'm literally crying :_D

    Thank you so so much!

    image

    014.jpg 184.7K
Sign In or Register to comment.