_table->belongsTo('Users', [ 'foreignKey' => 'user_id', 'joinType' => 'INNER', 'className' => Configure::read('Users.table') ]); } /** * After save callback * * @param Event $event event * @param Entity $entity entity * @param \ArrayObject $options options * @return mixed */ public function afterSave(Event $event, Entity $entity, $options) { if ($entity->active) { return true; } $user = $this->_table->Users->find()->where(['Users.id' => $entity->user_id, 'Users.active' => true])->first(); if (empty($user)) { return true; } return $this->sendSocialValidationEmail($entity, $user); } /** * Send social validation email to the user * * @param EntityInterface $socialAccount social account * @param EntityInterface $user user * @return void */ protected function sendSocialValidationEmail(EntityInterface $socialAccount, EntityInterface $user) { return $this ->getMailer(Configure::read('Users.Email.mailerClass') ?: 'CakeDC/Users.Users') ->send('socialAccountValidation', [$user, $socialAccount]); } /** * Validates the social account * * @param string $provider provider * @param string $reference reference * @param string $token token * @throws RecordNotFoundException * @throws AccountAlreadyActiveException * @return User */ public function validateAccount($provider, $reference, $token) { $socialAccount = $this->_table->find() ->select(['id', 'provider', 'reference', 'active', 'token']) ->where(['provider' => $provider, 'reference' => $reference]) ->first(); if (!empty($socialAccount) && $socialAccount->token === $token) { if ($socialAccount->active) { throw new AccountAlreadyActiveException(__d('CakeDC/Users', "Account already validated")); } } else { throw new RecordNotFoundException(__d('CakeDC/Users', "Account not found for the given token and email.")); } return $this->_activateAccount($socialAccount); } /** * Validates the social account * * @param string $provider provider * @param string $reference reference * @throws RecordNotFoundException * @throws AccountAlreadyActiveException * @return User */ public function resendValidation($provider, $reference) { $socialAccount = $this->_table->find() ->where(['provider' => $provider, 'reference' => $reference]) ->contain('Users') ->first(); if (!empty($socialAccount)) { if ($socialAccount->active) { throw new AccountAlreadyActiveException(__d('CakeDC/Users', "Account already validated")); } } else { throw new RecordNotFoundException(__d('CakeDC/Users', "Account not found for the given token and email.")); } return $this->sendSocialValidationEmail($socialAccount, $socialAccount->user); } /** * Activates an account * * @param SocialAccount $socialAccount social account * @return EntityInterface */ protected function _activateAccount($socialAccount) { $socialAccount->active = true; $result = $this->_table->save($socialAccount); return $result; } }