XF 2.1 Help with extending UserPrivacy class

KSA

Well-known member
I am trying to extend UserPrivacy to have ignored users unable to view profile page of users who ignored them. My code works fine except that I want to make :allow profile access upon ignore optional. I assume I need to create column in the xf_user_option table so added that by

PHP:
 $this->schemaManager()->alterTable('xf_user_option', function(Alter $table)
        {
            $table->addColumn('myaddon_enabled', 'tinyInt')->setDefault(1);
        });

My UserPrivacy class extension

PHP:
<?php

namespace addon\XF\Entity;

class UserPrivacy extends XFCP_UserPrivacy
{
    public function isPrivacyCheckMet($privacyKey, \XF\Entity\User $user)
    {
        if ($this->user_id == $user->user_id || $user->canBypassUserPrivacy())
        {
            return true;
        }

        if ($this->User->isIgnoring($user))
        {
            return false;
        }

        return parent::isPrivacyCheckMet($privacyKey, $user);
    }
}


My Account class extension

PHP:
<?php

namespace myaddon\XF\Pub\Controller;

use XF\Entity\User;

class Account extends XFCP_Account
{
    protected function preferencesSaveProcess(User $visitor)
    {
        $form = parent::preferencesSaveProcess($visitor);

        $input = $this->filter([
            'option' => ['my_addon_enabled' => 'bool']
        ]);

        $userOptions = $visitor->getRelationOrDefault('Option');
        $form->setupEntityInput($userOptions, $input['option']);

        return $form;
    }
}

My UserOption class extension

PHP:
<?php

namespace myaddon\ddon\Name\XF\Entity;
use XF\Mvc\Entity\Structure;

class UserOption extends XFCP_UserOption
{
    public static function getStructure(Structure $structure)
    {
        $parent = parent::getStructure($structure);
        $parent->columns['myaddon_enabled'] = ['type' => self::BOOL, 'default' => true];
        return $parent;
    }
}

then went ahead and created TM

HTML:
<xf:radiorow name="option[myaddon_enabled]" value="{$xf.visitor.Option.myaddon_enabled}"
             label="{{ phrase('xxxxxxx') }}"
             explain="{{ phrase('xxxxxxxxxxx') }}">
             <xf:option value="1">{{ phrase('yes') }}</xf:option>
             <xf:option value="0">{{ phrase('no') }}</xf:option>
</xf:radiorow>

Now the 1/0 value should return false/true or is there something I am not understanding here or missing

This is my first attempt trying to get familiar with the programming language so please save the laughs.
 
Last edited:
Looks good to me. You'd just need to check $this->User->myaddon_enabled in your isPrivacyCheckMet() method.
 
  • Like
Reactions: KSA
Looks good to me. You'd just need to check $this->User->myaddon_enabled in your isPrivacyCheckMet() method.

Thanks that seems logical but if you could show how to check I would appreciate that. Not sure when to add $this->User->myaddon_enabled exactly.
 
You can change your conditional to something like:

PHP:
if ($this->User->myaddon_enabled && $this->User->isIgnoring($user))
) {
    return false;
}

... assuming you only want to check it when the column is true.
 
You can change your conditional to something like:

PHP:
if ($this->User->myaddon_enabled && $this->User->isIgnoring($user))
) {
    return false;
}

... assuming you only want to check it when the column is true.

There is something small and stupid wrong that is driving me nuts I am pretty sure :(

I tried but got this error

Code:
#0 [internal function]: XF::handlePhpError(512, '[E_USER_WARNING...', 'C:\\xampp\\htdocs...', 190, Array)
#1 src\XF\Mvc\Entity\Entity.php(190): trigger_error('Accessed unknow...', 512)
#2 src\XF\Mvc\Entity\Entity.php(101): XF\Mvc\Entity\Entity->get('myaddon_enabled')
#3 src\addons\pblock\XF\Entity\UserPrivacy.php(9): XF\Mvc\Entity\Entity->__get('myaddon_enabled')
#4 src\XF\Entity\User.php(554): pblock\XF\Entity\UserPrivacy->isPrivacyCheckMet('allow_view_prof...', Object(XF\Entity\User))
#5 src\XF\Entity\User.php(615): XF\Entity\User->isPrivacyCheckMet('allow_view_prof...', Object(XF\Entity\User))
#6 src\XF\Pub\Controller\Member.php(1094): XF\Entity\User->canViewFullProfile(NULL)
#7 src\XF\Pub\Controller\Member.php(213): XF\Pub\Controller\Member->assertViewableUser('2')
#8 src\XF\Mvc\Dispatcher.php(350): XF\Pub\Controller\Member->actionView(Object(XF\Mvc\ParameterBag))
#9 src\XF\Mvc\Dispatcher.php(257): XF\Mvc\Dispatcher->dispatchClass('XF\\Pub\\Controll...', 'View', Object(XF\Mvc\RouteMatch), Object(XF\Pub\Controller\Member), Object(XF\Mvc\Reply\Reroute))
#10 src\XF\Mvc\Dispatcher.php(113): XF\Mvc\Dispatcher->dispatchFromMatch(Object(XF\Mvc\RouteMatch), Object(XF\Pub\Controller\Member), Object(XF\Mvc\Reply\Reroute))
#11 src\XF\Mvc\Dispatcher.php(55): XF\Mvc\Dispatcher->dispatchLoop(Object(XF\Mvc\RouteMatch))
#12 src\XF\App.php(2184): XF\Mvc\Dispatcher->run()
#13 src\XF.php(391): XF\App->run()
#14 index.php(20): XF::runApp('XF\\Pub\\App')
#15 {main}
 
Looks like your column is actually on the option entity, so that should be: $this->User->Option->myaddon_enabled
 
Top Bottom