src/Pages/MemberProfilePageController.php
public function confirm(HTTPRequest $request)
    {
        if ($this->EmailType !== 'Confirmation' &&
            $this->EmailType !== 'Validation') {
            return $this->httpError(400, 'No confirmation required.');
        }

        $currentMember = Member::currentUser();
        $id = (int)$request->param('ID');
        $key = $request->getVar('key');

        if ($currentMember) {
            if ($currentMember->ID == $id) {
                return Security::permissionFailure($this, _t(
                    'MemberProfiles.ALREADYCONFIRMED',
                    'Your account is already confirmed.'
                ));
            }
            return Security::permissionFailure($this, _t(
                'MemberProfiles.CANNOTCONFIRMLOGGEDIN',
                'You cannot confirm account while you are logged in.'
            ));
        }

        if (!$id ||
            !$key) {
            return $this->httpError(404);
        }

        $confirmationTitle = $this->data()->dbObject('ConfirmationTitle');

        /**
         * @var Member|null $member
         */
        $member = DataObject::get_by_id(Member::class, $id);
        if (!$member) {
            return $this->invalidRequest('Member #'.$id.' does not exist.');
        }
        if (!$member->NeedsValidation) {
            // NOTE(Jake): 2018-05-03
            //
            // You might be hitting this if you set your MemberProfilePage to use
            // Email Setting 'Confirmation' rather than 'Validation' and you didn't
            // edit the original Email template to not include the copy about confirmation.
            //
            return $this->invalidRequest('Member #'.$id.' does not need validation.');
        }
        if (!$member->ValidationKey) {
            return $this->invalidRequest('Member #'.$id.' does not have a validation key.');
        }
        if ($member->ValidationKey !== $key) {
            return $this->invalidRequest('Validation key does not match.');
        }

        // Allow member to login
        $member->NeedsValidation = false;
        $member->ValidationKey = null;

        $validationResult = $member->validateCanLogin();
        if (!$validationResult->isValid()) {
            $this->getResponse()->setStatusCode(500);
            $validationMessages = $validationResult->getMessages();
            return [
                'Title'   => $confirmationTitle,
                'Content' => $validationMessages ? $validationMessages[0]['message'] : _t('MemberProfiles.ERRORCONFIRMATION', 'An unexpected error occurred.'),
            ];
        }
        $member->write();

        $this->extend('onConfirm', $member);

        if ($member->canLogin()) {
            Injector::inst()->get(IdentityStore::class)->logIn($member);
        } else {
            throw new Exception('Permission issue occurred. Was the "$member->validateCanLogin" check above this code block removed?');
        }

        return [
            'Title'   => $this->data()->dbObject('ConfirmationTitle'),
            'Content' => $this->data()->dbObject('ConfirmationContent')
        ];
    }
public function confirm(HTTPRequest $request)
    {
        if ($this->EmailType !== 'Confirmation' &&
            $this->EmailType !== 'Validation') {
            return $this->httpError(400, 'No confirmation required.');
        }

        $currentMember = Member::currentUser();
        $id = (int)$request->param('ID');
        $key = $request->getVar('key');

        if ($currentMember) {
            if ($currentMember->ID == $id) {
                return Security::permissionFailure($this, _t(
                    'MemberProfiles.ALREADYCONFIRMED',
                    'Your account is already confirmed.'
                ));
            }
            return Security::permissionFailure($this, _t(
                'MemberProfiles.CANNOTCONFIRMLOGGEDIN',
                'You cannot confirm account while you are logged in.'
            ));
        }

        if (!$id ||
            !$key) {
            return $this->httpError(404);
        }

        $confirmationTitle = $this->data()->dbObject('ConfirmationTitle');

        /**
         * @var Member|null $member
         */
        $member = DataObject::get_by_id(Member::class, $id);
        if (!$member) {
            return $this->invalidRequest('Member #'.$id.' does not exist.');
        }
        if (!$member->NeedsValidation) {
            // NOTE(Jake): 2018-05-03
            //
            // You might be hitting this if you set your MemberProfilePage to use
            // Email Setting 'Confirmation' rather than 'Validation' and you didn't
            // edit the original Email template to not include the copy about confirmation.
            //
            return $this->invalidRequest('Member #'.$id.' does not need validation.');
        }
        if (!$member->ValidationKey) {
            return $this->invalidRequest('Member #'.$id.' does not have a validation key.');
        }
        if ($member->ValidationKey !== $key) {
            return $this->invalidRequest('Validation key does not match.');
        }

        // Allow member to login
        $member->NeedsValidation = false;
        $member->ValidationKey = null;

        $validationResult = $member->validateCanLogin();
        if (!$validationResult->isValid()) {
            $this->getResponse()->setStatusCode(500);
            $validationMessages = $validationResult->getMessages();
            return [
                'Title'   => $confirmationTitle,
                'Content' => $validationMessages ? $validationMessages[0]['message'] : _t('MemberProfiles.ERRORCONFIRMATION', 'An unexpected error occurred.'),
            ];
        }
        $member->write();

        $this->extend('onConfirm', $member);

        if ($member->canLogin()) {
            Injector::inst()->get(IdentityStore::class)->logIn($member);
        } else {
            throw new Exception('Permission issue occurred. Was the "$member->validateCanLogin" check above this code block removed?');
        }

        return [
            'Title'   => $this->data()->dbObject('ConfirmationTitle'),
            'Content' => $this->data()->dbObject('ConfirmationContent')
        ];
    }
public function confirm ( HTTPRequest $ request ) { if ( $ this -> EmailType !== 'Confirmation' && $ this -> EmailType !== 'Validation' ) { return $ this -> httpError ( 400 , 'No confirmation required.' ) ; } $ currentMember = Member :: currentUser ( ) ; $ id = ( int ) $ request -> param ( 'ID' ) ; $ key = $ request -> getVar ( 'key' ) ; if ( $ currentMember ) { if ( $ currentMember -> ID == $ id ) { return Security :: permissionFailure ( $ this , _t ( 'MemberProfiles.ALREADYCONFIRMED' , 'Your account is already confirmed.' ) ) ; } return Security :: permissionFailure ( $ this , _t ( 'MemberProfiles.CANNOTCONFIRMLOGGEDIN' , 'You cannot confirm account while you are logged in.' ) ) ; } if ( ! $ id || ! $ key ) { return $ this -> httpError ( 404 ) ; } $ confirmationTitle = $ this -> data ( ) -> dbObject ( 'ConfirmationTitle' ) ; /**
         * @var Member|null $member
         */ $ member = DataObject :: get_by_id ( Member :: class , $ id ) ; if ( ! $ member ) { return $ this -> invalidRequest ( 'Member #' . $ id . ' does not exist.' ) ; } if ( ! $ member -> NeedsValidation ) { // NOTE(Jake): 2018-05-03 // // You might be hitting this if you set your MemberProfilePage to use // Email Setting 'Confirmation' rather than 'Validation' and you didn't // edit the original Email template to not include the copy about confirmation. // return $ this -> invalidRequest ( 'Member #' . $ id . ' does not need validation.' ) ; } if ( ! $ member -> ValidationKey ) { return $ this -> invalidRequest ( 'Member #' . $ id . ' does not have a validation key.' ) ; } if ( $ member -> ValidationKey !== $ key ) { return $ this -> invalidRequest ( 'Validation key does not match.' ) ; } // Allow member to login $ member -> NeedsValidation = false ; $ member -> ValidationKey = null ; $ validationResult = $ member -> validateCanLogin ( ) ; if ( ! $ validationResult -> isValid ( ) ) { $ this -> getResponse ( ) -> setStatusCode ( 500 ) ; $ validationMessages = $ validationResult -> getMessages ( ) ; return [ 'Title' => $ confirmationTitle , 'Content' => $ validationMessages ? $ validationMessages [ 0 ] [ 'message' ] : _t ( 'MemberProfiles.ERRORCONFIRMATION' , 'An unexpected error occurred.' ) , ] ; } $ member -> write ( ) ; $ this -> extend ( 'onConfirm' , $ member ) ; if ( $ member -> canLogin ( ) ) { Injector :: inst ( ) -> get ( IdentityStore :: class ) -> logIn ( $ member ) ; } else { throw new Exception ( 'Permission issue occurred. Was the "$member->validateCanLogin" check above this code block removed?' ) ; } return [ 'Title' => $ this -> data ( ) -> dbObject ( 'ConfirmationTitle' ) , 'Content' => $ this -> data ( ) -> dbObject ( 'ConfirmationContent' ) ] ; }
Allows the user to confirm their account by clicking on the validation link in
the confirmation email.

@param HTTPRequest $request
@return array|HTTPResponse
Allows the user to confirm their account by clicking on the validation link in the confirmation email .