Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* This file handles a example registration process logic and some of the
- * most used functions for Registration and Activation. It is recommended to
- * extend from this class and implement your own, project-specific
- * Registration process. If this example does exactly what you want in your
- * Project, then you can feel lucky already! */
- Yii::import('application.modules.user.controllers.YumController');
- Yii::import('application.modules.user.models.*');
- Yii::import('application.modules.profile.models.*');
- Yii::import('application.modules.registration.models.*');
- class YumRegistrationController extends YumController {
- public $defaultAction = 'registration';
- // Only allow the registration if the user is not already logged in and
- // the function is activated in the Module Configuration
- public function beforeAction($action) {
- if (!Yii::app()->user->isGuest)
- $this->redirect(Yii::app()->user->returnUrl);
- return parent::beforeAction($action);
- }
- public function accessRules() {
- return array(
- array('allow',
- 'actions' => array('index', 'registration', 'recovery', 'activation', 'resendactivation'),
- 'users' => array('*'),
- ),
- array('allow',
- 'actions' => array('captcha'),
- 'users' => array('*'),
- ),
- array('deny', // deny all other users
- 'users' => array('*'),
- ),
- );
- }
- public function actions() {
- return array(
- 'captcha' => array(
- 'class' => 'CCaptchaAction',
- 'backColor' => 0xFFFFFF,
- ),
- );
- }
- /*
- * an Example implementation of an registration of an new User in the system.
- *
- * please see the documentation of yii-user-management for examples on how to
- * extend from this class and implement your own registration logic in
- * user/docs/registration.txt
- */
- public function actionRegistration() {
- if(!Yii::app()->user->isGuest)
- {
- Yii::log('user to reg form is not a guest','ERROR');
- }
- Yii::import('application.modules.profile.models.*');
- Yii::import('application.modules.registration.models/.*');
- Yii::import('application.modules.user.vendors.facebook.*');
- require_once('Facebook.php');
- $facebook = new Facebook(Yum::module()->facebookConfig);
- $session = $facebook->getSession();
- if ($session !== null){
- Yii::log('session is not null','info');
- }
- $form = new YumRegistrationForm;
- $profile = new YumProfile;
- $stateslist=CHtml::listData(States::model()->findAll($condition='display =:display',array(':display'=>1)), 'state_code', 'state' );
- $this->performAjaxValidation('YumRegistrationForm', $form);
- $fb_uid = null;
- if(isset($session))
- $fb_uid = $facebook->api('/me');
- if (isset($_POST['YumRegistrationForm'])) {
- $form->attributes = $_POST['YumRegistrationForm'];
- $profile->attributes = $_POST['YumProfile'];
- if($_POST['YumRegistrationForm']['email'])
- {
- $form->email = $_POST['YumRegistrationForm']['email'];
- $profile->email = $_POST['YumRegistrationForm']['email'];
- }
- if (Yum::module()->loginType & UserModule::LOGIN_BY_EMAIL){
- //$form->email = $form->username;
- $form->username = 'em_'. YumRegistrationForm::genRandomString(Yum::module()->usernameRequirements['maxLen']-3);
- //Yii::app()->user->setFlash('successf', Yum::t('form username is '.$form->username));
- }
- if (Yum::module()->loginType & UserModule::LOGIN_BY_FACEBOOK && isset($session)){
- $form->password = YumUser::encrypt(YumUserChangePassword::createRandomPassword());
- $form->verifyPassword=$form->password;
- $form->username = 'fb_'. YumRegistrationForm::genRandomString(Yum::module()->usernameRequirements['maxLen']-3);
- }
- if (isset($_POST['terms'])){
- $form->terms=true;
- }
- $form->validate();
- $profile->validate();
- if(!$form->hasErrors() && !$profile->hasErrors()) {
- $user = new YumUser;
- if ($session !== null){
- $user->register($form->username, $form->password, $profile->email,YumUser::STATUS_NOCONTRACT);
- $user->status= -4;
- $user->save();
- //$user->register($form->username, $form->password, $profile->email);
- Yii::log('status should be -4','info');
- }else{
- //$user->register($form->username, $form->password, $profile->email);
- Yii::log('status should be 0','info');
- }
- $profile->user_id = $user->id;
- if (Yum::module()->loginType & UserModule::LOGIN_BY_FACEBOOK && isset($session)){
- //$profile->$fb_uid->uid;
- //Yii::log('We need to log the users facebook id here yumregistrationcontroller line:126','info');
- Yum::setFlash('We need to log the users facebook id here yumregistrationcontroller line:126','info');
- }
- $profile->save();
- if (Yum::module()->loginType & UserModule::LOGIN_BY_FACEBOOK && isset($session)){
- $user->status= -4;
- $user->save();
- //echo 'user status is' . $user->status;
- //$this->sendRegistrationEmail($user);
- }
- Yum::setFlash('Thank you for your registration. Please check your email.');
- if(isset($user)){
- Yii::log('user status is' .$user->status,'info');
- }
- $this->redirect(Yum::module()->loginUrl);
- }
- }
- $this->render(Yum::module()->registrationView, array(
- 'form' => $form,
- 'profile' => $profile,
- 'stateslist'=> $stateslist,
- 'fb_uid'=>$fb_uid,
- 'session'=>$session,
- )
- );
- }
- // Send the Email to the given user object. $user->email needs to be set.
- public function sendRegistrationEmail($user,$facebook=null,$password=null) {
- if (!isset($user->profile->email)) {
- throw new CException(Yum::t('Email is not set when trying to send Registration Email'));
- }
- $content = YumTextSettings::model()->find('language = :lang', array(
- 'lang' => Yii::app()->language));
- $sent = null;
- if(isset($facebook))
- {
- }else{
- $activation_url = $user->getActivationUrl();
- }
- // get the text to sent from the yumtextsettings table
- if (is_object($content)) {
- if(isset($facebook))
- {
- $body = strtr($content->text_email_registration_fb, array(
- '{password}' => $password));
- }else{
- $body = strtr($content->text_email_registration, array(
- '{username}' => $user->username,
- '{activation_url}' => $activation_url));
- }
- $mail = array(
- 'from' => Yum::module('registration')->registrationEmail,
- 'to' => $user->profile->email,
- 'subject' => strtr($content->subject_email_registration, array(
- '{username}' => $user->username)),
- 'body' => $body,
- );
- $sent = YumMailer::send($mail);
- }
- else {
- throw new CException(Yum::t('The messages for your application language are not defined.'));
- }
- return $sent;
- }
- /**
- * Activation of an user account. The Email and the Activation key send
- * by email needs to correct in order to continue. The Status will
- * be initially set to 1 (active - first Visit) so the administrator
- * can see, which accounts have been activated, but not yet logged in
- * (more than once)
- */
- public function actionActivation($email, $key) {
- // If already logged in, we dont activate anymore
- if (!Yii::app()->user->isGuest) {
- Yum::setFlash('You are already logged in, please log out to activate your account');
- $this->redirect(Yii::app()->user->returnUrl);
- }
- // If everything is set properly, let the model handle the Validation
- // and do the Activation
- $status = YumUser::activate($email, $key);
- if ($status != false)
- {
- if($status->status == YumUser::STATUS_NOCONTRACT)
- {
- $session = Yii::app()->session;
- $session['email']=$email;
- $url=$this->createUrl('/contract');
- $this->redirect($url); //FIXME: render the contract
- }else if($status->status == YumUser::STATUS_ACTIVE || $status->status == YumUser::STATUS_ACTIVATED || $status->status == YumUser::STATUS_ACTIVE_FIRST_VISIT){
- //add group membership for lobby
- $lobby=Groups::findByPk('1');
- Membership::join($lobby);
- }else if($status instanceof YumUser)
- $this->render(Yum::module('registration')->activationSuccessView);
- else
- $this->render(Yum::module('registration')->activationFailureView, array(
- 'error' => $status));
- }
- }
- /**
- * Password recovery routine. The User will receive an email with an
- * activation link. If clicked, he will be prompted to enter his new
- * password.
- */
- public function actionRecovery($email = null, $key = null) {
- $form = new YumPasswordRecoveryForm;
- if ($email != null && $key != null) {
- if($profile = YumProfile::model()->find('email = :email', array(
- 'email' => $email))) {
- $user = $profile->user;
- if($user->activationKey == $key) {
- $passwordform = new YumUserChangePassword;
- if (isset($_POST['YumUserChangePassword'])) {
- $passwordform->attributes = $_POST['YumUserChangePassword'];
- if ($passwordform->validate()) {
- $user->password = YumUser::encrypt($passwordform->password);
- $user->activationKey = YumUser::encrypt(microtime() . $passwordform->password);
- $user->save();
- Yum::setFlash('Your new password has been saved.');
- $this->redirect(Yum::module()->loginUrl);
- }
- }
- $this->render(
- Yum::module('registration')->changePasswordView, array(
- 'form' => $passwordform));
- Yii::app()->end();
- } else {
- $form->addError('login_or_email', Yum::t('Invalid recovery key'));
- Yum::log(Yum::t(
- 'Someone tried to recover a password, but entered a wrong recovery key. Email is {email}, associated user is {username} (id: {uid})', array(
- '{email}' => $email,
- '{uid}' => $user->id,
- '{username}' => $user->username)));
- }
- }
- } else {
- if (isset($_POST['YumPasswordRecoveryForm'])) {
- $form->attributes = $_POST['YumPasswordRecoveryForm'];
- if ($form->validate()) {
- Yum::setFlash(
- 'Instructions have been sent to you. Please check your email.');
- if($form->user instanceof YumUser) {
- $form->user->generateActivationKey();
- $recovery_url = $this->createAbsoluteUrl(
- Yum::module('registration')->recoveryUrl[0], array(
- 'key' => $form->user->activationKey,
- 'email' => $form->user->profile->email));
- Yum::log(Yum::t(
- '{username} successfully requested a new password in the password recovery form. A email with the password recovery url {recovery_url} has been sent to {email}', array(
- '{email}' => $form->user->profile->email,
- '{recovery_url}' => $recovery_url,
- '{username}' => $form->user->username)));
- $content = YumTextSettings::model()->find(
- 'language = :lang', array('lang' => Yii::app()->language));
- $sent = null;
- if (is_object($content)) {
- $mail = array(
- 'from' => Yii::app()->params['adminEmail'],
- 'to' => $form->user->profile->email,
- 'subject' => $content->subject_email_registration,
- 'body' => strtr($content->text_email_recovery, array(
- '{recovery_url}' => $recovery_url)),
- );
- $sent = YumMailer::send($mail);
- } else {
- throw new CException(Yum::t('The messages for your application language are not defined.'));
- }
- } else
- Yum::log(Yum::t(
- 'A password has been requested, but no associated user was found in the database. Requested user/email is: {username}', array(
- '{username}' => $form->login_or_email)));
- $this->redirect(Yum::module()->loginUrl);
- }
- }
- }
- $this->render(Yum::module('registration')->recoverPasswordView, array(
- 'form' => $form));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement