/** * View register form and process submit action * @throws ForbiddenException * @throws \Ffcms\Core\Exception\NativeException * @throws \Ffcms\Core\Exception\SyntaxException */ public function actionSignup() { if (App::$User->isAuth()) { // always auth? prevent any actions throw new ForbiddenException(); } // load configs $configs = $this->getConfigs(); // init register model $registerForm = new FormRegister($configs['captchaOnRegister'] === 1); // registration based on invite. Check conditions. if ($configs['registrationType'] === 0) { // get token and email $inviteToken = $this->request->query->get('token'); $inviteEmail = $this->request->query->get('email'); // data sounds like a invalid? if (Str::length($inviteToken) < 32 || !Str::isEmail($inviteEmail)) { throw new ForbiddenException(__('Registration allowed only if you have invite!')); } // remove oldest data Invite::clean(); // try to find token $find = Invite::where('token', '=', $inviteToken)->where('email', '=', $inviteEmail)->count(); // token not foud? invalid invite key if ($find !== 1) { throw new ForbiddenException(__('Your invite token is invalid! Contact with administrator')); } // notify the invite token is accepted if (!$registerForm->send()) { App::$Session->getFlashBag()->add('success', __('Invite was accepted! Continue registration')); } // set email from token data $registerForm->email = $inviteEmail; } // if register data is send and valid if ($registerForm->send() && $registerForm->validate()) { $activation = $configs['registrationType'] === 1; if ($registerForm->tryRegister($activation)) { // initialize succes signup event App::$Event->run(static::EVENT_USER_REGISTER_SUCCESS, ['model' => $registerForm]); // if no activation is required - just open session and redirect user to main page if (!$activation) { $loginModel = new FormLogin(); $loginModel->openSession($registerForm->_userObject); $this->response->redirect('/'); // session is opened, refresh page } // send notification of successful registering App::$Session->getFlashBag()->add('success', __('Your account is registered. You must confirm account via email')); } else { // init fail signup event App::$Event->run(static::EVENT_USER_REGISTER_FAIL, ['model' => $registerForm]); App::$Session->getFlashBag()->add('error', __('Login or email is always used on website')); } } // render view return $this->view->render('signup', ['model' => $registerForm, 'config' => $configs, 'useCaptcha' => $configs['captchaOnRegister'] === 1]); }
/** * Override default registration function with social auth data compatability * @param bool $activation * @return bool */ public function tryRegister($activation = false) { // try to complete register process $success = parent::tryRegister($activation); if ($success && $this->_userObject !== null) { // save remote auth data to relation table $provider = new UserProvider(); $provider->provider_name = $this->_provider_name; $provider->provider_id = $this->_provider_id; $provider->user_id = $this->_userObject->id; $provider->save(); // get profile object from attr $profile = $this->_profileObject; // set nickname from remote service if (!Str::likeEmpty($this->_identity->displayName)) { $profile->nick = $this->_identity->displayName; } // set profile as user website $profile->url = $this->_identity->profileURL; // try to get gender (sex) if ($this->_identity->gender !== null) { $profile->sex = $this->_identity->gender === 'female' ? 2 : 1; } // set birthday if available if ($this->_identity->birthDay !== null && $this->_identity->birthMonth !== null && $this->_identity->birthYear !== null) { $profile->birthday = $this->_identity->birthYear . '-' . $this->_identity->birthMonth . '-' . $this->_identity->birthDay; } // try to parse avatar from remote service if ($this->_identity->photoURL !== null) { $this->parseAvatar($this->_identity->photoURL, $this->_userObject->id); } // save profile data $profile->save(); } return $success; }