XF 2.1 Shared Ip's

Ozzy47

Well-known member
I'm extending XF/Pub/Controller/Member.php specifically the actionSharedIps which looks like this.
PHP:
    public function actionSharedIps(ParameterBag $params)
    {
        $user = $this->assertViewableUser($params->user_id);

        if (!\XF::visitor()->canViewIps())
        {
            return $this->noPermission();
        }

        $shared = $user->getSharedIpUsers($this->options()->sharedIpsCheckLimit);

        $viewParams = [
            'user' => $user,
            'shared' => $shared
        ];
        return $this->view('XF:Member\SharedIps', 'member_shared_ips_list', $viewParams);
    }

Now In my file I do this.
PHP:
    public function actionUserIps(ParameterBag $params)
    {
        $parent = parent::actionUserIps($params);

        $options = \XF::options();

        $user = $parent->getParam('user');

          $usernames = $options->ozzmodzHideIp_users;

              if (in_array($user['user_id'], $usernames))
            {
                return $this->noPermission();
            }

        return $parent;
    }

Which works as intended if I am trying to view a members shared IP's that I have in my option. Now the issue I am having trouble with is if another user is sharing IP's with the previously mentioned user, their name shows up in the list. What I want is to remove any users that I have in my setting from any others shared IP list, if they are sharing an IP with one in the option. I just can't quite figure out what I need to do. :(
 
You'll need to unset any matching users from the shared array. It's keyed by user ID so it should be fairly straight forward.

PS: You generally want to guard controller action extensions:
PHP:
if ($reply instanceof \XF\Mvc\Reply\View) {
    // ...
}
 
Something like this (assuming $usernames is an array of user IDs, which it seems to be judging your previous code):

Code:
$shared = $parent->getParam('shared');
foreach ($usernames as $userId) {
    unset($shared[$userId]);
}
 
Okay, I have this now:
PHP:
    public function actionSharedIps(ParameterBag $params)
    {
        $parent = parent::actionSharedIps($params);

        if ($parent instanceof \XF\Mvc\Reply\View)
        {
            $options = \XF::options();

            $user = $parent->getParam('user');

            $usernames = $options->ozzmodzHideIp_users;

            if (in_array($user['user_id'], $usernames))
            {
                return $this->noPermission();
            }
          
            $shared = $parent->getParam('shared');
          
            foreach ($usernames as $userId)
            {
                unset($shared[$userId]);
            }
        }
        return $parent;
    }

But viewing the shared IP's of the user that is not in the list, still returns the user that is on the list.
 
Top Bottom