C0nw0nk
C0nw0nk

Reputation: 960

Joomla PHP plugin how to check the current component is not com_users

So I have the following code that causes a infinite redirect loop because I can't check if the current URL the user is on is the "com_users" component.

If they are on the com_users component i don't want any more code to execute.

public function onAfterInitialise() {
    $app  = JFactory::getApplication();
    $user = JFactory::getUser();
    if (!$user->guest) {
        //THIS CAN'T GET CURRENT COMPONENT AND CAUSES INFINITE redirect LOOP
        if ( !($app->input->get('option') == 'com_users' && JRequest::getVar('view') == 'profile') ) { //IF NOT on the EDIT PROFILE URL then force the user to go and change their email
            if ($user->email === "[email protected]") {
                $app->enqueueMessage('Please change your email address!');
                $app->redirect(
                    JRoute::_(
                        'index.php?option=com_users&view=profile&layout=edit'
                    )
                );
            }
        }
    }
}

Upvotes: 0

Views: 143

Answers (1)

nibra
nibra

Reputation: 4028

Use watchers to keep the complexity low.

JRequest is deprecated, use $app->input instead. Input::getCmd() does some automatic sanitation.

public function onAfterInitialise()
{
    $user = JFactory::getUser();
    $app  = JFactory::getApplication();

    if ($user->guest)
    {
        return;
    }

    if ($app->input->getCmd('option') === 'com_users' && $app->input->getCmd('view') === 'profile')
    {
        return;
    }

    if ($user->email === "[email protected]")
    {
        $app->enqueueMessage('Please change your email address!');
        $app->redirect(
            JRoute::_(
                'index.php?option=com_users&view=profile&layout=edit'
            )
        );
    }
}

Upvotes: 1

Related Questions