public function run() { $form = new RegistrationForm(); if (Yii::app()->request->isPostRequest && !empty($_POST['RegistrationForm'])) { $module = Yii::app()->getModule('user'); $form->setAttributes($_POST['RegistrationForm']); // проверка по "черным спискам" // проверить на email if (!$module->isAllowedEmail($form->email)) { // перенаправить на экшн для фиксации невалидных email-адресов $this->controller->redirect(array(Yii::app()->getModule('user')->invalidEmailAction)); } if (!$module->isAllowedIp(Yii::app()->request->userHostAddress)) { // перенаправить на экшн для фиксации невалидных ip-адресов $this->controller->redirect(array(Yii::app()->getModule('user')->invalidIpAction)); } if ($form->validate()) { // если требуется активация по email if ($module->emailAccountVerification) { $registration = new Registration(); // скопируем данные формы $registration->setAttributes($form->getAttributes()); if ($registration->save()) { // отправка email с просьбой активировать аккаунт $mailBody = $this->controller->renderPartial('application.modules.user.views.email.needAccountActivationEmail', array('model' => $registration), true); Yii::app()->mail->send($module->notifyEmailFrom, $registration->email, Yii::t('user', 'Регистрация на сайте {site} !', array('{site}' => Yii::app()->name)), $mailBody); // запись в лог о создании учетной записи Yii::log(Yii::t('user', "Создана учетная запись {nick_name}!", array('{nick_name}' => $registration->nick_name)), CLogger::LEVEL_INFO, UserModule::$logCategory); Yii::app()->user->setFlash(YFlashMessages::NOTICE_MESSAGE, Yii::t('user', 'Учетная запись создана! Инструкции по активации аккаунта отправлены Вам на email!')); $this->controller->refresh(); } else { $form->addErrors($registration->getErrors()); Yii::log(Yii::t('user', "Ошибка при создании учетной записи!"), CLogger::LEVEL_ERROR, UserModule::$logCategory); } } else { // если активации не требуется - сразу создаем аккаунт $user = new User(); $user->createAccount($form->nick_name, $form->email, $form->password); if ($user && !$user->hasErrors()) { Yii::log(Yii::t('user', "Создана учетная запись {nick_name} без активации!", array('{nick_name}' => $user->nick_name)), CLogger::LEVEL_INFO, UserModule::$logCategory); // отправить email с сообщением о успешной регистрации $emailBody = $this->controller->renderPartial('application.modules.user.views.email.accountCreatedEmail', array('model' => $user), true); Yii::app()->mail->send($module->notifyEmailFrom, $user->email, Yii::t('user', 'Регистрация на сайте {site} !', array('{site}' => Yii::app()->name)), $emailBody); Yii::app()->user->setFlash(YFlashMessages::NOTICE_MESSAGE, Yii::t('user', 'Учетная запись создана! Пожалуйста, авторизуйтесь!')); $this->controller->redirect(array('/user/account/login/')); } else { $form->addErrors($user->getErrors()); Yii::log(Yii::t('user', "Ошибка при создании учетной записи без активации!"), CLogger::LEVEL_ERROR, UserModule::$logCategory); } } } } $this->controller->render('registration', array('model' => $form)); }
/** * Registers new member */ public function actionRegister() { // is current user has the required privilege ? if (!Yii::app()->user->checkAccess('site_register')) { throw new CHttpException(403, 'You are not authorized to perform this action.'); } $registration = new RegistrationForm(); // collect user input data if (isset($_POST['RegistrationForm'])) { $registration->attributes = $_POST['RegistrationForm']; print_r($_POST['RegistrationForm']); // validate user input and redirect to the previous page if valid if ($registration->validate()) { $member = new Member(); $member->attributes = $registration->attributes; if ($member->save()) { // create an account model $account = new Account(); $account->username = $registration->username; $account->password = $registration->password; $account->id_member = $member->id; if ($account->save()) { $auth = Yii::app()->authManager; $auth->assign('member', (int) $account->id); // redirects to index page //$this->redirect(array('index')); } else { // what's wrong? get the error message $registration->addErrors($member->getErrors()); } } else { // what's wrong? get the error message $registration->addErrors($account->getErrors()); } } } // display the registration form $this->render('register', array('model' => $registration)); }
public function actionRegister() { $registration = new RegistrationForm(); // collect user input data $model = new User(); if (isset($_POST['RegistrationForm'])) { $registration->attributes = $_POST['RegistrationForm']; //$registration->attributes = $_POST['RegistrationForm']; $rnd = rand(0, 9999); // generate random number between 0-9999 $model->attributes = $_POST['User']; // validate user input and redirect to the previous page if valid if ($registration->validate()) { // create an account model $user = new User(); $user->username = $registration->username; $user->password = MD5($registration->password); $user->no_induk = $registration->NoInduk; $user->nama = $registration->first_name; $user->email = $registration->email; $user->t_lahir = $registration->date_of_birth; $uploadedFile = CUploadedFile::getInstance($user, 'foto'); $fileName = "{$rnd}-{$uploadedFile}"; $user->foto = $fileName; $user->foto = $registration->foto; $user->gender = $registration->sex; $user->alamat = $registration->address; if ($user->save()) { // redirects to index page $uploadedFile->saveAs(Yii::app()->basePath . '/foto/' . $fileName); $this->redirect(array('view', 'username' => $model->username)); $this->redirect(array('index')); } else { // what's wrong? get the error message $registration->addErrors($user->getErrors()); } } } // display the registration form $this->render('register', array('model' => $registration)); }