Advertisement
mithereal

yumregistration controller - mithereal

Aug 18th, 2011
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.60 KB | None | 0 0
  1. <?php
  2.  
  3. /* This file handles a example registration process logic and some of the
  4.  * most used functions for Registration and Activation. It is recommended to
  5.  * extend from this class and implement your own, project-specific
  6.  * Registration process. If this example does exactly what you want in your
  7.  * Project, then you can feel lucky already! */
  8.  
  9. Yii::import('application.modules.user.controllers.YumController');
  10. Yii::import('application.modules.user.models.*');
  11. Yii::import('application.modules.profile.models.*');
  12. Yii::import('application.modules.registration.models.*');
  13.  
  14. class YumRegistrationController extends YumController {
  15.     public $defaultAction = 'registration';
  16.  
  17.     // Only allow the registration if the user is not already logged in and
  18.     // the function is activated in the Module Configuration
  19.     public function beforeAction($action) {
  20.         if (!Yii::app()->user->isGuest)
  21.             $this->redirect(Yii::app()->user->returnUrl);
  22.         return parent::beforeAction($action);
  23.     }
  24.  
  25.     public function accessRules() {
  26.         return array(
  27.                 array('allow',
  28.                     'actions' => array('index', 'registration', 'recovery', 'activation', 'resendactivation'),
  29.                     'users' => array('*'),
  30.                     ),
  31.                 array('allow',
  32.                     'actions' => array('captcha'),
  33.                     'users' => array('*'),
  34.                     ),
  35.                 array('deny', // deny all other users
  36.                     'users' => array('*'),
  37.                     ),
  38.                 );
  39.     }
  40.  
  41.     public function actions() {
  42.         return array(
  43.                 'captcha' => array(
  44.                     'class' => 'CCaptchaAction',
  45.                     'backColor' => 0xFFFFFF,
  46.                     ),
  47.                 );
  48.     }
  49.  
  50.     /*
  51.      * an Example implementation of an registration of an new User in the system.
  52.      *
  53.      * please see the documentation of yii-user-management for examples on how to
  54.      * extend from this class and implement your own registration logic in
  55.      * user/docs/registration.txt
  56.      */
  57.     public function actionRegistration() {
  58.         if(!Yii::app()->user->isGuest)
  59.         {
  60.             Yii::log('user to reg form is not a guest','ERROR');
  61.         }
  62.        
  63.        
  64.         Yii::import('application.modules.profile.models.*');
  65.             Yii::import('application.modules.registration.models/.*');
  66.             Yii::import('application.modules.user.vendors.facebook.*');
  67.             require_once('Facebook.php');
  68.             $facebook = new Facebook(Yum::module()->facebookConfig);
  69.             $session = $facebook->getSession();
  70.             if ($session !== null){
  71.             Yii::log('session is not null','info');
  72.             }
  73.         $form = new YumRegistrationForm;
  74.         $profile = new YumProfile;
  75.         $stateslist=CHtml::listData(States::model()->findAll($condition='display =:display',array(':display'=>1)), 'state_code', 'state' );
  76.  
  77.  
  78.         $this->performAjaxValidation('YumRegistrationForm', $form);
  79.        
  80.         $fb_uid = null;
  81.        
  82.         if(isset($session))
  83.         $fb_uid =  $facebook->api('/me');
  84.  
  85.         if (isset($_POST['YumRegistrationForm'])) {
  86.             $form->attributes = $_POST['YumRegistrationForm'];
  87.             $profile->attributes = $_POST['YumProfile'];
  88.            
  89.             if($_POST['YumRegistrationForm']['email'])
  90.             {
  91.             $form->email = $_POST['YumRegistrationForm']['email'];
  92.             $profile->email = $_POST['YumRegistrationForm']['email'];
  93.             }
  94.            
  95.             if (Yum::module()->loginType & UserModule::LOGIN_BY_EMAIL){
  96.                 //$form->email = $form->username;
  97.                 $form->username = 'em_'. YumRegistrationForm::genRandomString(Yum::module()->usernameRequirements['maxLen']-3);
  98.                 //Yii::app()->user->setFlash('successf', Yum::t('form username is '.$form->username));
  99.  
  100.             }
  101.            
  102.            
  103.             if (Yum::module()->loginType & UserModule::LOGIN_BY_FACEBOOK && isset($session)){
  104.                 $form->password = YumUser::encrypt(YumUserChangePassword::createRandomPassword());
  105.                 $form->verifyPassword=$form->password;
  106.                 $form->username = 'fb_'. YumRegistrationForm::genRandomString(Yum::module()->usernameRequirements['maxLen']-3);
  107.  
  108.             }
  109.            
  110.             if (isset($_POST['terms'])){
  111.                 $form->terms=true;
  112.             }
  113.  
  114.            
  115.             $form->validate();
  116.             $profile->validate();
  117.            
  118.            
  119.             if(!$form->hasErrors() && !$profile->hasErrors()) {
  120.                 $user = new YumUser;
  121.                 if ($session !== null){
  122.                    
  123.                         $user->register($form->username, $form->password, $profile->email,YumUser::STATUS_NOCONTRACT);
  124.                         $user->status= -4;
  125.                         $user->save();
  126.                         //$user->register($form->username, $form->password, $profile->email);
  127.                         Yii::log('status should be -4','info');
  128.                     }else{
  129.                 //$user->register($form->username, $form->password, $profile->email);
  130.                 Yii::log('status should be 0','info');
  131.                         }
  132.                 $profile->user_id = $user->id;
  133.                 if (Yum::module()->loginType & UserModule::LOGIN_BY_FACEBOOK && isset($session)){
  134.                     //$profile->$fb_uid->uid;
  135.                     //Yii::log('We need to log the users facebook id here yumregistrationcontroller line:126','info');
  136.                     Yum::setFlash('We need to log the users facebook id here yumregistrationcontroller line:126','info');
  137.                 }
  138.                 $profile->save();
  139.            
  140.                 if (Yum::module()->loginType & UserModule::LOGIN_BY_FACEBOOK && isset($session)){
  141.                     $user->status= -4;
  142.                         $user->save();
  143.                         //echo 'user status is' . $user->status;
  144.                 //$this->sendRegistrationEmail($user);
  145.             }
  146.                 Yum::setFlash('Thank you for your registration. Please check your email.');
  147.                 if(isset($user)){
  148.         Yii::log('user status is' .$user->status,'info');
  149.     }
  150.                 $this->redirect(Yum::module()->loginUrl);
  151.             }
  152.         }
  153.        
  154.        
  155.         $this->render(Yum::module()->registrationView, array(
  156.                     'form' => $form,
  157.                     'profile' => $profile,
  158.                     'stateslist'=> $stateslist,
  159.                     'fb_uid'=>$fb_uid,
  160.                     'session'=>$session,
  161.                     )
  162.                 );  
  163.     }
  164.  
  165.     // Send the Email to the given user object. $user->email needs to be set.
  166.     public function sendRegistrationEmail($user,$facebook=null,$password=null) {
  167.         if (!isset($user->profile->email)) {
  168.             throw new CException(Yum::t('Email is not set when trying to send Registration Email'));
  169.         }
  170.        
  171.         $content = YumTextSettings::model()->find('language = :lang', array(
  172.                     'lang' => Yii::app()->language));
  173.         $sent = null;
  174.        
  175.         if(isset($facebook))
  176.         {
  177.            
  178.         }else{
  179.         $activation_url = $user->getActivationUrl();
  180.     }
  181.         // get the text to sent from the yumtextsettings table
  182.        
  183.  
  184.         if (is_object($content)) {
  185.             if(isset($facebook))
  186.         {
  187.             $body = strtr($content->text_email_registration_fb, array(
  188.                         '{password}' => $password));
  189.         }else{
  190.             $body = strtr($content->text_email_registration, array(
  191.                         '{username}' => $user->username,
  192.                         '{activation_url}' => $activation_url));
  193.                     }
  194.  
  195.             $mail = array(
  196.                     'from' => Yum::module('registration')->registrationEmail,
  197.                     'to' => $user->profile->email,
  198.                     'subject' => strtr($content->subject_email_registration, array(
  199.                             '{username}' => $user->username)),
  200.                     'body' => $body,
  201.                     );
  202.             $sent = YumMailer::send($mail);
  203.         }
  204.         else {
  205.             throw new CException(Yum::t('The messages for your application language are not defined.'));
  206.         }
  207.  
  208.         return $sent;
  209.     }
  210.  
  211.     /**
  212.      * Activation of an user account. The Email and the Activation key send
  213.      * by email needs to correct in order to continue. The Status will
  214.      * be initially set to 1 (active - first Visit) so the administrator
  215.      * can see, which accounts have been activated, but not yet logged in
  216.      * (more than once)
  217.      */
  218.     public function actionActivation($email, $key) {
  219.         // If already logged in, we dont activate anymore
  220.         if (!Yii::app()->user->isGuest) {
  221.             Yum::setFlash('You are already logged in, please log out to activate your account');
  222.             $this->redirect(Yii::app()->user->returnUrl);
  223.         }
  224.  
  225.         // If everything is set properly, let the model handle the Validation
  226.         // and do the Activation
  227.         $status = YumUser::activate($email, $key);
  228.        
  229.         if ($status != false)
  230.             {
  231.             if($status->status == YumUser::STATUS_NOCONTRACT)
  232.             {
  233.                 $session = Yii::app()->session;
  234.                 $session['email']=$email;
  235.                 $url=$this->createUrl('/contract');
  236.                 $this->redirect($url); //FIXME: render the contract
  237.             }else if($status->status == YumUser::STATUS_ACTIVE || $status->status == YumUser::STATUS_ACTIVATED || $status->status == YumUser::STATUS_ACTIVE_FIRST_VISIT){
  238.             //add group membership for lobby
  239.             $lobby=Groups::findByPk('1');
  240.             Membership::join($lobby);
  241.             }else if($status instanceof YumUser)
  242.             $this->render(Yum::module('registration')->activationSuccessView);
  243.         else
  244.             $this->render(Yum::module('registration')->activationFailureView, array(
  245.                         'error' => $status));
  246.     }
  247.     }
  248.  
  249.     /**
  250.      * Password recovery routine. The User will receive an email with an
  251.      * activation link. If clicked, he will be prompted to enter his new
  252.      * password.
  253.      */
  254.     public function actionRecovery($email = null, $key = null) {
  255.         $form = new YumPasswordRecoveryForm;
  256.  
  257.         if ($email != null && $key != null) {
  258.             if($profile = YumProfile::model()->find('email = :email', array(
  259.                             'email' =>  $email))) {
  260.                 $user = $profile->user;
  261.                 if($user->activationKey == $key) {
  262.                     $passwordform = new YumUserChangePassword;
  263.                     if (isset($_POST['YumUserChangePassword'])) {
  264.                         $passwordform->attributes = $_POST['YumUserChangePassword'];
  265.                         if ($passwordform->validate()) {
  266.                             $user->password = YumUser::encrypt($passwordform->password);
  267.                             $user->activationKey = YumUser::encrypt(microtime() . $passwordform->password);
  268.                             $user->save();
  269.                             Yum::setFlash('Your new password has been saved.');
  270.                             $this->redirect(Yum::module()->loginUrl);
  271.                         }
  272.                     }
  273.                     $this->render(
  274.                             Yum::module('registration')->changePasswordView, array(
  275.                                 'form' => $passwordform));
  276.                     Yii::app()->end();
  277.                 } else {
  278.                     $form->addError('login_or_email', Yum::t('Invalid recovery key'));
  279.                     Yum::log(Yum::t(
  280.                                 'Someone tried to recover a password, but entered a wrong recovery key. Email is {email}, associated user is {username} (id: {uid})', array(
  281.                                     '{email}' => $email,
  282.                                     '{uid}' => $user->id,
  283.                                     '{username}' => $user->username)));
  284.                 }
  285.             }
  286.         } else {
  287.             if (isset($_POST['YumPasswordRecoveryForm'])) {
  288.                 $form->attributes = $_POST['YumPasswordRecoveryForm'];
  289.  
  290.                 if ($form->validate()) {
  291.                     Yum::setFlash(
  292.                             'Instructions have been sent to you. Please check your email.');
  293.  
  294.                     if($form->user instanceof YumUser) {
  295.                         $form->user->generateActivationKey();
  296.                         $recovery_url = $this->createAbsoluteUrl(
  297.                                 Yum::module('registration')->recoveryUrl[0], array(
  298.                                     'key' => $form->user->activationKey,
  299.                                     'email' => $form->user->profile->email));
  300.  
  301.                         Yum::log(Yum::t(
  302.                                     '{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(
  303.                                         '{email}' => $form->user->profile->email,
  304.                                         '{recovery_url}' => $recovery_url,
  305.                                         '{username}' => $form->user->username)));
  306.  
  307.                         $content = YumTextSettings::model()->find(
  308.                                 'language = :lang', array('lang' => Yii::app()->language));
  309.                         $sent = null;
  310.  
  311.                         if (is_object($content)) {
  312.                             $mail = array(
  313.                                     'from' => Yii::app()->params['adminEmail'],
  314.                                     'to' => $form->user->profile->email,
  315.                                     'subject' => $content->subject_email_registration,
  316.                                     'body' => strtr($content->text_email_recovery, array(
  317.                                             '{recovery_url}' => $recovery_url)),
  318.                                     );
  319.                             $sent = YumMailer::send($mail);
  320.                         } else {
  321.                             throw new CException(Yum::t('The messages for your application language are not defined.'));
  322.                         }
  323.                     } else
  324.                         Yum::log(Yum::t(
  325.                                     'A password has been requested, but no associated user was found in the database. Requested user/email is: {username}', array(
  326.                                         '{username}' => $form->login_or_email)));
  327.                     $this->redirect(Yum::module()->loginUrl);
  328.                 }
  329.             }
  330.         }
  331.         $this->render(Yum::module('registration')->recoverPasswordView, array(
  332.                     'form' => $form));
  333.  
  334.     }
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement