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.

Limit "liking" access to certain roles

martzmartz New
edited March 2013 in Vanilla 2.0 - 2.8

Hi, would it be possible to add a configuration item somehow that will limit the "like" option to certain roles? Let's say I have also a role "senior members", I would only want senior members, moderators and administrators to have access. So not guests, applicants and regular members.

They still should see the likes, just no be able to influence the counter.

I would happily sponsor creating such an additional feature.

Comments

  • this will work - at least it did for me...

    change the function to this in LikeThis/default.php

     public function FormatLikes($Likes, $Url, $UID, $Self) {
        $CountLikes = count($Likes);
        $Src = '';
    
        if($CountLikes)
            $Src .= '<span class="likes">' .$CountLikes. sprintf($CountLikes>1?T(' Likes'):T(' Like')).'</span> ';
    
    
        $UID = Gdn::Session()->User->UserID;
    
        $UserModel = new UserModel();
        $UserRoleData = $UserModel->GetRoles($UID)->ResultArray();
        $RoleIDs = ConsolidateArrayValuesByKey($UserRoleData, 'RoleID');
    
        $LikerRole = Gdn::Config("Plugins.LikeThis.LikeRole");    
    
    
         if (((in_array($LikerRole, $RoleIDs))) || ($LikerRole == "")) {      
    
        if(!$Self){
          if (in_array($UID, $Likes))
          {
            $QuoteURL = Url("discussion/Like/{$Url}/remove",TRUE);
            $QuoteText = T('Unlike');
          } 
          else 
          {
            $QuoteURL = Url("discussion/Like/{$Url}",TRUE);
            $QuoteText = T('Like');
          }
        }
    
          $Src .= '<span class="LikeThis"><a href="'.$QuoteURL.'">'.$QuoteText.'</a></span>';
        }
    
        return $Src;
    
      }
    

    add this to your config.php - if you want to restrict liking to Admin (16)

    choose wahtever roleid you want and replace the "16" with the id you want.

    e.g.

    $Configuration['Plugins']['LikeThis']['LikeRole'] = '16';

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • martzmartz New
    edited March 2013

    Thanks!

    I was trying out another approach: registering an extra permission and then checking it. However,
    it doesn't yet work for me. So I give it another shot and then copy your code :-) thanks again!

    Here's a diff of my try with the original code (default.php):

    16c16
    <    'RegisterPermissions' => FALSE,
    ---
    

    'RegisterPermissions' => array('Plugins.LikeThis.AllowedToLike'),

    108c108
    <       if (!( $Session->IsValid() && $Session->CheckPermission('Vanilla.Comments.Add', TRUE, 'Category',           $Discussion->PermissionCategoryID))) {
    ---
    
      if (!( $Session->IsValid() && $Session->CheckPermission('Plugins.LikeThis.AllowedToLike')  )) {
    

    After changing this code you have to disable and enable the code to get the extra permission to register and then check it for every role you like to have access.

  • I think I got it working now, your code gave me a clue. The code did not check if the user was in the correct role when rendering the view. So I added

    if(!CheckPermission('Plugins.LikeThis.AllowedToLike')) { return;}
    

    in the DiscussionController_CommentOptions_Handler function. That seems to do the trick. However, I am quite tired now and need to recheck when I got some sleep :-)

    Thanks for your input!

  • Your idea is better with register permissions than manually reading roles. You may need to put the conditional in the same place as where I did - if you don't see the likes reflecting the way you want. I haven't tested further, but whatever works for you.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • hbfhbf wiki guy? MVP

    @martz, if you send me your updated default.php i will merge your changes into the baseline.

  • You can download the file here: https://mega.co.nz/#!3EFjiApJ!G_Yx6H9TulH3cdgoXxjgHUsOho4jwJq3iNrQ8BsP8F8

    It is important to make clear to forum owners who download the plugin that, once installed, they have to alter their permissions setup. They have to check the permission for the applicable roles, otherwise people get confused!

  • hbfhbf wiki guy? MVP

    this has been incorporated into the latest version of the plugin.

Sign In or Register to comment.