4

I have a public registration form and a plugin which needs to create an entry for a user when that user is activated. That part works. Because I want to force an Admin to activate the user, I am using this code block to stop Craft automatically activating a user on registration:

public function init() {
    craft()->on('users.beforeActivateUser', function(Event $event){
        // stop the user from being able to activate their account. Forces admins to do so.
        $event->performAction = false;
    });
}

But this also stops an Admin from activating the user account via the control panel.

I can't work out how to test that the user performing the event is the admin and therefore skip that blocking code. I'd tried this:

public function init() {
    craft()->on('users.beforeActivateUser', function(Event $event){
        // stop the user from being able to activate their account. Forces admins to do so.
        if($event->params['user']->isInGroup('artists')){
            $event->performAction = false;
        }
    });
}

But that doesn't work, presumably because a new registering user doesn't belong to any group at the point that code is run.

12

I'm a bit confused by your question.

It sounds as though you want to check if the currently logged-in user is an Administrator, but it looks like you're actually checking whether the newly-registered user is an Administrator.

Here's how to retrieve the currently logged-in User, and check whether they belong to a specific User Group.

function (Event $event)
{
    $user = craft()->userSession->getUser();

    if ($user && $user->isInGroup('groupHandle') {
        // Do stuff.
    }
}

Alternatively, you could check whether the current user has permission to perform a specific action:

if ($user && craft()->userPermissions->doesUserHavePermission($user->id, 'registerUsers')) {
    // Do stuff.
}
| improve this answer | |
2

Searching the class reference documentation, I see that the user is available from the userSession service — but never tried this personally.

craft()->on('users.beforeActivateUser', function(Event $event){
    // stop the user from being able to activate their account. Forces admins to do so.
    if(!craft()->userSession->isAdmin()) {
        $event->performAction = false;
    }
});

or to just get the current user for other testing, craft()->userSession->user.

| improve this answer | |
1

Just wanted to add to this thread...

Depending on which event you are listening to/watching for in your plugin, you might have to use a different approach to getting the user. For example, the onActivateUser event won't have a user's session to check just yet. But, you do have access to the event params...

MyPlugin.php

public function init() {
    ...

    craft()->on('users.onActivateUser', function (Event $event) {
        if (craft()->config->get('devMode')) {
        error_log('----- users.onActivateUser called -----');
    }

    // Get the user.
    $user = craft()->users->getUserById($event->params['user']->id);

    // Check for user group.
    if ($user && $user->isInGroup('groupHandle')) {
        // Do stuff..
    } else {
        // User does not belong to this group.
    }

    ...
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.