/** * Convert user phone field in to E.164 format * @param unknown $user * @return Ambigous <NULL, string, unknown, string> */ protected function normalizePhone($user) { $cleanPhone = null; $key = 'valid_phone'; try { if (!($cleanPhone = UserExtend::parsePhone($user->phone))) { // Emails that contain numbers can be mistaken as a vanity number ( 800 GODMA ) // so just for reporting purpose I check if the phone containts '@' is more likely it is an email $isEmail = preg_match('/@/', $user->phone); $key = $isEmail ? 'not_a_phone' : 'invalid_phone'; $this->addToReport($user, $key, $user->phone); } } catch (\libphonenumber\NumberParseException $e) { $key = 'phone_empty'; if (!empty($user->phone)) { $key = 'not_a_phone'; $this->addToReport($user, $key, $user->phone); } } $this->increaseCounter($key); return $cleanPhone; }
/** * Register a user * * @param array $data * An array of attributes to register a user. * Any fields that are not properties on the user object * Will be applied to the Usermeta object * * @param array $rules * A set of validation rules to validate against * see http://laravel.com/docs/5.1/validation * * @return User $user * return the user object after registration */ public static function register($data, $rules = []) { $rules += ['first_name' => 'required|min:2', 'last_name' => 'required|min:2', 'email' => 'required|email|between:2,64', 'password' => 'required|min:6', 'password_confirmation' => 'required|min:6']; Event::fire('auth.preRegister', [$data, $rules]); $validation = Validator::make($data, $rules); if ($validation->fails()) { throw new ValidationException($validation); } /* * Register user */ $requireActivation = UserSettings::get('require_activation', true); $automaticActivation = UserSettings::get('activate_mode') == UserSettings::ACTIVATE_AUTO; $userActivation = UserSettings::get('activate_mode') == UserSettings::ACTIVATE_USER; /* * Data altercations */ $data['first_name'] = ucwords($data['first_name']); $data['last_name'] = ucwords($data['last_name']); $data['birth_date'] = UserExtend::parseBirthdate($data['birthday']); $data['phone'] = UserExtend::parsePhone($data['phone']); $data['email_optin'] = isset($data['email_optin']) ? $data['email_optin'] : false; // Split the data into whats required for the user and usermeta models $userData = ['name' => $data['first_name'] . ' ' . $data['last_name'], 'password' => $data['password'], 'password_confirmation' => $data['password_confirmation'], 'email' => $data['email'], 'street_addr' => $data['street_addr'], 'city' => $data['city'], 'state' => $data['state'], 'zip' => $data['zip'], 'phone' => $data['phone']]; $user = Auth::register($userData, $automaticActivation); // Save user metadata $usermeta = Usermeta::create($data); $user->metadata()->save($usermeta); if (isset($data['avatar'])) { UserExtend::uploadAvatar($user, $data['avatar']); } /* * Activation is by the user, send the email */ if ($userActivation) { $this->sendActivationEmail($user); } /* * Automatically activated or not required, log the user in */ if ($automaticActivation || !$requireActivation) { Auth::login($user); } if ($user) { /* * Fire event that user has registered */ Event::fire('auth.register', [$user]); return $user; } return false; }
/** * Save the users profile */ public function onSave() { $user = $this->getUser(); $vars = post(); foreach ($vars as $key => $val) { if ($key == 'metadata') { foreach ($val as $metakey => $metaval) { $user->metadata->{$metakey} = $metaval; } } else { if ($key == "phone") { $val = UserExtend::parsePhone($val); } $user->{$key} = $val; } } if ($user->push()) { Flash::info(Lang::get('dma.friends::lang.user.save')); } else { Flash::error(Lang::get('dma.friends::lang.user.saveFailed')); } return ['#flashMessages' => $this->renderPartial('@flashMessages')]; }