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.

check for user role/permission

edited September 2010 in Vanilla 2.0 - 2.8
I was unable to find anything when searching.

---

Question: Is it possible for me to check the user role or permission? What i would like to do is something like this:

if($user->role("Moderator"))
{
// do something with moderator
}

I have tried different things and even looked through alot of the files that came with the installation but i cant seem to find any solution.

Do i need to write my own function to check this?

Comments

  • TimTim Vanilla Staff
    You cannot check for specific roles, but you can look for permissions for the logged-in user very easily:

    Gdn::Session()->CheckPermission('Application.Permission.Name');

    Vanilla Forums COO [GitHub, Twitter, About.me]

  • Okay that is also fine. Thanks.
  • @styrk: I wrote an add-on (not in the addon repository here yet) that looks up the Roles of users. The same concept could conceivably be used to look up this information and use it in the way you want to.

    My addon exists to print the users's role in their post meta-data, though. I use roles as a sort of "reward" for loyalty to the forums, which doesn't actually confer any special abilities. So displaying that role is sort of important to that process.

    I think @Tim has a better way for you, though. Since roles are 100% definable by the user of the software, it's much safer to just check for permissions.
  • a followup question regarding this, how can i get my app have its own kind if persmission? (So that i can check/uncheck it under a role).
  • TimTim Vanilla Staff
    In applications/your_app/settings/structure.php:

    // Define some global vanilla permissions. $PermissionModel->Define(array( 'YourApp.Some.Permission', 'YourApp.Some.OtherPermission' ));

    Vanilla Forums COO [GitHub, Twitter, About.me]

  • Okay - i guess that only works when i'am installing the app, but what if my app is already setup ?
  • TimTim Vanilla Staff
    Disable and enable it

    Vanilla Forums COO [GitHub, Twitter, About.me]

  • Okay, i put this in my app stucture.php (applications/community/settings/structure.php)

    $PermissionModel = Gdn::PermissionModel();
    $PermissionModel->Database = $Database;
    $PermissionModel->SQL = $SQL;

    $PermissionModel->Define(array(
    'Community.News.Manage'
    ));

    Then ive disabled/reenabled my app but still i cannot give the role "Moderator" the permission community.news.manage. It does not show up when i'am editing the role "Moderator".

    What am i doing wrong?
  • Okay I got it working. Here is how:

    In your about.php file you have to add an array to RegisterPermissions value and then re-enable the app. In my app my about.php looks like.


    $ApplicationInfo['Community'] = array( 'Description' => "A tournament community system", 'Version' => '1.0', 'RegisterPermissions' => array("Community.News.Manage", "Community.Tournaments.Manage", "Community.Tournaments.Master"), 'SetupController' => 'setup', 'AllowEnable' => TRUE, // You can remove this when you create your own application (leaving it will make it so the application can't be enabled by Garden) 'Author' => "Rasmus Styrk", 'AuthorEmail' => 'rasmnus@styrk-it.dk', 'AuthorUrl' => 'http://styrk-it.dk', 'License' => 'Copyright 2010 (C) Styrk-IT' );
Sign In or Register to comment.