Advertisement
Guest User

Untitled

a guest
May 30th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.38 KB | None | 0 0
  1. <?php
  2. //Registers user
  3. $user = $modx->user;
  4.  
  5.  
  6. if ($user->get('id') == 0) {
  7.     //New user, lets first do some checks to ensure that they have set a valid password
  8.     $password = $hook->getValue('password');
  9.     $password_confirm = $hook->getValue('password_confirm');
  10.  
  11.     //Nothing in password fields?
  12.     if ($password == "" || $password_confirm == "") {
  13.         $hook->addError('password','Please complete both password fields');
  14.         return false;
  15.     }
  16.  
  17.     //Do passwords match?
  18.     if ($password != $password_confirm) {
  19.         //Paswords don't match, error
  20.         $hook->addError('password','Passwords do not match. Please try again');
  21.         return false;
  22.     }
  23.  
  24.     //Yes, they match, now, is it long enough?
  25.     if (strlen($password) < 7) {
  26.         //Pasword isn't log enough
  27.         $hook->addError('password','Please create a password that is greater than 8.');
  28.         return false;
  29.     }
  30.  
  31.     //We have a valid password, now, lets build a user account.
  32.     //Does this user already exist?
  33.     $user = $modx->getObject('modUser', array ('username' => $hook->getValue('email')));
  34.     if ($user) {
  35.         //User exisits... error
  36.         $hook->addError('personal', "We're sorry, " . $hook->getValue('email') . " has already been registered");
  37.         return false;
  38.     }
  39.     $user = $modx->newObject('modUser', array ('username' => $hook->getValue('email')));
  40.     $user->set('password', $password);
  41.  
  42.     $userProfile = $modx->newObject('modUserProfile');
  43.     $userProfile->set('fullname', $hook->getValue('fname') . ' ' . $hook->getValue('lname'));
  44.     $userProfile->set('email', $hook->getValue('email'));
  45.     $userProfile->set('mobilephone', $hook->getValue('mobile'));
  46.  
  47.     $user->addOne($userProfile);
  48.  
  49.     if ($user->save() == false){
  50.         $hook->addError('email','Sorry, this email address is already registered. If you own this account, please log in.');
  51.         return false;
  52.     }
  53.    
  54.     $user->joinGroup(1, 2); //GROUP Name,Group role
  55.  
  56.     $modx->runProcessor('security/login',
  57.         array(
  58.             'login_context' =>  $modx->context->get('key'),
  59.             'add_contexts' => '',
  60.             'username' => $hook->getValue('email'),
  61.             'password' => $password,
  62.             'returnUrl' => null,
  63.             'rememberme' => true
  64.         )
  65.     );
  66.    
  67. }
  68.  
  69. //Return, send forward, redirect? Anything can go here.
  70. return true;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement