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.

[Pockets Addon] Problem: Roles and Page settings broken?

2»

Comments

  • @badlearner - Can You limit pocket display to a specific user group ?
    I was trying this latest version from GitHub, and I still didn't see any change.

  • @vaniller : Absolutely. Let me show you.

  • edited March 2012

    @vaniller : I'm on Vanilla 2.1a, and Pockets doesn't seem to be working, like you said. But when I tested it on Vanilla 2.0.18.3, it actually worked. So, I don't understand what could be wrong.

    This site (belongs to @aery ) http://forum.gtricks.com/ uses pockets, and it's working.

  • @badlearner @vaniller Thanks for the responses.

    I've upgraded to Vanilla 2.0.18.4 and Pockets 1.0.4 from Github... And the roles conditions still don't seem to do anything. Also the new mobile-only/no-mobile conditions don't seem to work either, in fact, the boxes don't stay checked when I save the pocket.

    What am I missing here?

  • edited March 2012

    I'm not much of a coder so I could be totally wrong, but attempting to read the code, it certainly looks to me like the Conditions feature isn't implemented. Specificially, the CanRender() function of the Pocket class which returns whether the pocket should show up or not, has no clause to to test for role or permission conditions. Nor can I find this tested for anywhere else in the Pockets codebase...

    I'd guess it's just one or two lines of code to add this... Anyone?

  • Same problem. Pockets show to all roles :(

  • So there is still no solution for this?

  • I hacked a fix for Pockets on the current stable release version of Vanilla 2.0.18.8 based on the downloadable version of the Pockets plugin (v1.0.1b) from Add-ons.

    The Github version requires the development version of Vanilla which I am not using (plus the latest code appears to simply comment out the Conditions section) - and I am way too new to the Vanilla code-base to start pushing updates to Github! ;-)

    This hack only considers Roles because I did not need to worry about Permissions - my (client's) use case is quite simple - I just want to show a payment form to users who are "Suspended" (which is a custom Role in my forum).

    I am attaching the updated zip file for the Pockets plugin with my mods.

    How it works:
    Essentially, what it does is add a "Conditions" var to the Pocket class which is populated on Load:


    public function Load($Data) {
    ...
    if ($Data['Condition']) $this->Condition = Gdn_Condition::FromString($Data['Condition']);
    }

    Then, in the main class' ProcessPockets function, the current user's role is extracted from GdnSession and added to Data which is passed to "CanRender".


    ...
    // Add current user roles to Data
    if (Gdn::Session()->IsValid()) {
    $userModel = new UserModel();
    $roleData = $userModel->GetRoles( Gdn::Session()->User->UserID );
    $roles='';
    if( $roleData !== FALSE && $roleData->NumRows(DATASET_TYPE_ARRAY) > 0 ) {
    $roles = ConsolidateArrayValuesByKey( $roleData->Result(), 'Name' );

      $roleModel = new RoleModel();
      $allRoles = $roleModel->getArray();
    
      $idx = 0;
      $newRoles = array();
      $userRoles = array();
      foreach ($allRoles as $role) {
        if (in_array($role, $roles)) $userRoles[] = $idx;
        $newRoles[$idx++] = $role;
      }
    
      $Data['Roles'] = implode($userRoles, ',');
        }
      }
      ...
    

    CanRender compares the Pocket's condition with the user's role to determine if any of the user's roles match any of the condition roles. If they don't match, CanRender returns FALSE.

    public function CanRender($Data) {
    ...
    // Check to see if user role matches
    if ($this->Condition) {
    $foundMatch = FALSE;
    $userRole = explode(',',GetValue('Roles', $Data));
    foreach($userRole as $myRole) {
    foreach($this->Condition as $condition) {
    if (('role' == $condition[0]) && ($condition[1] == $myRole)) {
    $foundMatch = TRUE;
    break;
    }
    }
    if ($foundMatch) break;
    }
    if (!$foundMatch) return FALSE;
    }
    ...

    Hope it saves you some time!

Sign In or Register to comment.