3

We're building a content platform for a bank for a client of us. One required feature lets relationship managers add clients (members) to the platform which get assigned a specific group.

We listen to the users.onSaveUser event and when the specific group is chosen and the user gets added via the CP there should be sent out a specific email to that client. This is the code I have so far:

public function initRmAddsClient()
{
    craft()->on('users.onSaveUser', function(Event $event) {

        $user = $event->params['user'];

        if ($event->params['isNewUser'])
        {
            //get new user group from form post
            $groups = craft()->request->getPost('groups');
            if ($groups) 
            {
                if (count($groups) > 0) 
                {
                    $userGroup = craft()->userGroups->getGroupById($groups[0]);

                    if ($userGroup->handle == 'premium' || $userGroup->handle == 'private') 
                    {
                        craft()->email->sendEmailByKey($user, 'rm_adds_client', array(
                            'user' => $user,
                            'link' => TemplateHelper::getRaw(craft()->users->getPasswordResetUrl($user)),
                            'rm' => craft()->userSession->getUser(),
                        ));
                    }
                }

            }
        }
    });
}

I just want to add one extra check if the submission is done via the control panel since public registration is enabled as well.

Is there an easy check for that? Thanks

6

Yes there is, craft()->request->isCpRequest() returns whether the request is from the CP or not.

| improve this answer | |
6

If anyone comes here looking for this answer for Craft 3 the function you are looking for is Craft::$app->getRequest()->getIsCpRequest()

| 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.