Ejemplo n.º 1
0
 /**
  * Create an account on this platform.
  * 
  * @param string $email The email address.
  * @param string $nickname The nickname.
  * @param string $password The password in clear text (not encrypted).
  * @param bool $activation_required Specifies if the account activation is needed or not. If yes: generates an activation token. If no: the account will be stored as ACTIVE. 
  * @param bool $notification Specifies if the user will be notified or not (via mail) about this action.
  * @param bool $update_account Specifies if the account already exists and then update its data.
  * @return User Returns the $user object.
  */
 public static function createAccount($email, $nickname = null, $password = null, $activation_required = true, $notification = true, $update_account = false, $update_old_email = null)
 {
     $user = null;
     if (isset($email)) {
         $user = new User();
         if ($update_account) {
             $user = User::model()->findByAttributes(array('email' => isset($update_old_email) ? $update_old_email : $email));
         }
         $user->email = $email;
         if (!isset($nickname)) {
             $parts = explode('@', $email);
             $user->nickname = $parts[0];
         } else {
             $user->nickname = $nickname;
         }
         $user->changeAccountPassword(isset($password) ? $password : ($password = self::generateRandomPassword()));
         $user->status = $activation_required ? User::STATUS_INACTIVE : User::STATUS_ACTIVE;
         if ($user->save()) {
             // TODO: write a log here
             /* created time */
             if (!$update_account) {
                 $user->addMeta(User::METADATA_KEY_ACCOUNT_CREATED_TIME, date('Y-m-d H:i:s', time()));
             }
             /* activation */
             $activation_token = null;
             if ($activation_required) {
                 $activation_token = self::generateActivationToken();
                 $meta = null;
                 if ($update_account) {
                     $meta = UserMetadata::model()->findByAttributes(array('user_id' => $user->id, 'key' => User::METADATA_KEY_ACCOUNT_ACTIVATION_TOKEN));
                 }
                 if (isset($meta)) {
                     $meta->value = $activation_token;
                     $meta->save();
                 } else {
                     $user->addMeta(User::METADATA_KEY_ACCOUNT_ACTIVATION_TOKEN, $activation_token);
                 }
             }
             /* notification */
             if ($notification) {
                 BasicNotifier::sendTemplatedEmail($email, Yii::t('UsersModule.create', 'email.subject'), 'users/account_created', array('{USER_EMAIL_ADDRESS}' => $email, '{USER_PASSWORD}' => $password), Yii::app()->session['lang']);
                 if ($activation_required) {
                     $activation_link = Yii::app()->createAbsoluteUrl('users/account/activate?token=' . $activation_token);
                     BasicNotifier::sendTemplatedEmail($email, Yii::t('UsersModule.activate', 'email.subject.required'), 'users/account_activation_required', array('{USER_ACTIVATION_LINK}' => $activation_link), Yii::app()->session['lang']);
                 }
             }
         }
     }
     return $user;
 }
Ejemplo n.º 2
0
 /**
  * Generates a new random password and then send it via email to the user.
  */
 public function actionResetPassword()
 {
     $model = new ResetPasswordForm();
     $success = false;
     $email = '';
     if (isset($_POST['ResetPasswordForm'])) {
         $model->attributes = $_POST['ResetPasswordForm'];
         $this->performAjaxValidation($model);
         $user = User::model()->findByAttributes(array('email' => $model->email));
         if (isset($user)) {
             $email = $user->email;
             $password_clear = UserHelper::generateRandomPassword();
             $user->password = $user->changeAccountPassword($password_clear);
             if ($user->save()) {
                 /* write an INFO syslog */
                 BasicNotifier::sendTemplatedEmail($user->email, Yii::t('UsersModule.resetPassword', 'email.subject'), 'users/account_reset_password', array('{USER_PASSWORD}' => $password_clear), Yii::app()->session['lang']);
                 $success = true;
             }
         }
     }
     $this->render('resetPassword', array('model' => $model, 'success' => $success, 'email' => $email));
 }
Ejemplo n.º 3
0
 /**
  * 
  * @param unknown $slug
  */
 public function actionPassword($slug)
 {
     $user = User::model()->findbyAttributes(array('slug' => $slug, 'status' => User::STATUS_ACTIVE));
     $model = new ChangePasswordForm();
     if (isset($_POST['ChangePasswordForm'])) {
         $model->attributes = $_POST['ChangePasswordForm'];
         $this->performAjaxValidationChangePasswordForm($model);
         $user->changeAccountPassword($model->newPassword);
         if ($user->save()) {
             BasicNotifier::sendTemplatedEmail($user->email, Yii::t('ProfileModule.password', 'email.subject.notification'), 'profile/change_password_notifcation', array('{USER_PASSWORD}' => $model->newPassword), Yii::app()->session['lang']);
             /* set flash messages */
             Yii::app()->user->setFlash('success', true);
             Yii::app()->user->setFlash('pending', 'password');
             $this->redirect(Yii::app()->createUrl('profile'));
         }
     }
     $this->render('password', array('model' => $model, 'slug' => $slug));
 }