4

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

luwes
  • 145
  • 7

2 Answers2

11

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

nfourtythree
  • 850
  • 7
  • 9
6

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

carlcs
  • 36,220
  • 5
  • 62
  • 139