public function afterInsert() { $user = $this->getUser(); $identity = new Identity(); $identity->auth_provider = $this->authProviderName; $identity->external_user_id = $this->externalUserId; $identity->internal_user_id = $user->id; if (!$identity->save()) { throw new Exception('Failed to save identity. Errors: ' . print_r($identity->errors, true)); } }
/** * @return User|null */ public function getUser() { if ($this->_user === false) { $externalIdentity = Identity::findOne(['auth_provider' => $this->authProvider, 'external_user_id' => $this->externalUserId]); $this->_user = $externalIdentity instanceof Identity ? $externalIdentity->internalUser : null; } return $this->_user; }