2018-07-24 14:33:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Customer\Http\Controllers;
|
|
|
|
|
|
2018-12-21 12:48:34 +00:00
|
|
|
use Illuminate\Support\Facades\Event;
|
2018-11-22 12:43:03 +00:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
use Webkul\Customer\Mail\VerificationEmail;
|
2018-08-22 09:46:27 +00:00
|
|
|
use Webkul\Customer\Repositories\CustomerRepository;
|
2019-05-21 07:16:43 +00:00
|
|
|
use Webkul\Customer\Repositories\CustomerGroupRepository;
|
2018-11-27 10:55:23 +00:00
|
|
|
use Cookie;
|
2018-07-24 14:33:49 +00:00
|
|
|
|
|
|
|
|
/**
|
2018-08-23 13:11:56 +00:00
|
|
|
* Registration controller
|
2018-07-24 14:33:49 +00:00
|
|
|
*
|
|
|
|
|
* @author Prashant Singh <prashant.singh852@webkul.com>
|
|
|
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
|
|
|
*/
|
|
|
|
|
class RegistrationController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
2019-07-01 11:33:36 +00:00
|
|
|
* Contains route related configuration
|
2018-07-24 14:33:49 +00:00
|
|
|
*
|
2019-07-01 11:33:36 +00:00
|
|
|
* @var array
|
2018-07-24 14:33:49 +00:00
|
|
|
*/
|
|
|
|
|
protected $_config;
|
|
|
|
|
|
2018-11-27 10:55:23 +00:00
|
|
|
/**
|
2019-07-01 11:33:36 +00:00
|
|
|
* CustomerRepository object
|
|
|
|
|
*
|
|
|
|
|
* @var Object
|
|
|
|
|
*/
|
|
|
|
|
protected $customerRepository;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* CustomerGroupRepository object
|
|
|
|
|
*
|
|
|
|
|
* @var Object
|
|
|
|
|
*/
|
|
|
|
|
protected $customerGroupRepository;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new Repository instance.
|
|
|
|
|
*
|
|
|
|
|
* @param \Webkul\Customer\Repositories\CustomerRepository $customer
|
|
|
|
|
* @param \Webkul\Customer\Repositories\CustomerGroupRepository $customerGroupRepository
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
CustomerRepository $customerRepository,
|
|
|
|
|
CustomerGroupRepository $customerGroupRepository
|
|
|
|
|
)
|
2018-12-21 12:48:34 +00:00
|
|
|
{
|
2018-07-24 14:33:49 +00:00
|
|
|
$this->_config = request('_config');
|
2019-07-01 11:33:36 +00:00
|
|
|
|
|
|
|
|
$this->customerRepository = $customerRepository;
|
|
|
|
|
|
|
|
|
|
$this->customerGroupRepository = $customerGroupRepository;
|
2018-07-24 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-27 12:07:22 +00:00
|
|
|
* Opens up the user's sign up form.
|
2018-09-22 08:31:18 +00:00
|
|
|
*
|
2018-07-24 14:33:49 +00:00
|
|
|
* @return view
|
|
|
|
|
*/
|
2018-12-21 12:48:34 +00:00
|
|
|
public function show()
|
|
|
|
|
{
|
2018-07-24 14:33:49 +00:00
|
|
|
return view($this->_config['view']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-27 12:07:22 +00:00
|
|
|
* Method to store user's sign up form data to DB.
|
2018-09-22 08:31:18 +00:00
|
|
|
*
|
2019-07-01 11:33:36 +00:00
|
|
|
* @return Response
|
2018-07-24 14:33:49 +00:00
|
|
|
*/
|
2019-07-01 11:33:36 +00:00
|
|
|
public function create()
|
2018-12-21 12:48:34 +00:00
|
|
|
{
|
2019-07-01 11:33:36 +00:00
|
|
|
$this->validate(request(), [
|
2018-07-24 14:33:49 +00:00
|
|
|
'first_name' => 'string|required',
|
|
|
|
|
'last_name' => 'string|required',
|
2018-10-25 07:46:45 +00:00
|
|
|
'email' => 'email|required|unique:customers,email',
|
2018-09-22 08:31:18 +00:00
|
|
|
'password' => 'confirmed|min:6|required',
|
2018-07-24 14:33:49 +00:00
|
|
|
]);
|
2018-08-22 09:46:27 +00:00
|
|
|
|
2018-09-22 08:31:18 +00:00
|
|
|
$data = request()->input();
|
|
|
|
|
|
|
|
|
|
$data['password'] = bcrypt($data['password']);
|
|
|
|
|
|
2018-10-23 11:46:00 +00:00
|
|
|
$data['channel_id'] = core()->getCurrentChannel()->id;
|
|
|
|
|
|
2019-05-01 11:31:03 +00:00
|
|
|
if (core()->getConfigData('customer.settings.email.verification')) {
|
2019-04-09 02:32:42 +00:00
|
|
|
$data['is_verified'] = 0;
|
|
|
|
|
} else {
|
|
|
|
|
$data['is_verified'] = 1;
|
|
|
|
|
}
|
2018-11-22 12:43:03 +00:00
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$data['customer_group_id'] = $this->customerGroupRepository->findOneWhere(['code' => 'general'])->id;
|
2018-12-07 05:25:33 +00:00
|
|
|
|
2018-11-27 10:55:23 +00:00
|
|
|
$verificationData['email'] = $data['email'];
|
|
|
|
|
$verificationData['token'] = md5(uniqid(rand(), true));
|
|
|
|
|
$data['token'] = $verificationData['token'];
|
2018-11-22 12:43:03 +00:00
|
|
|
|
2018-12-21 12:48:34 +00:00
|
|
|
Event::fire('customer.registration.before');
|
|
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$customer = $this->customerRepository->create($data);
|
2018-12-21 12:48:34 +00:00
|
|
|
|
|
|
|
|
Event::fire('customer.registration.after', $customer);
|
|
|
|
|
|
|
|
|
|
if ($customer) {
|
2019-05-01 11:31:03 +00:00
|
|
|
if (core()->getConfigData('customer.settings.email.verification')) {
|
2019-04-09 02:32:42 +00:00
|
|
|
try {
|
2019-05-01 11:31:03 +00:00
|
|
|
Mail::queue(new VerificationEmail($verificationData));
|
2019-04-09 02:32:42 +00:00
|
|
|
|
|
|
|
|
session()->flash('success', trans('shop::app.customer.signup-form.success-verify'));
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
session()->flash('info', trans('shop::app.customer.signup-form.success-verify-email-unsent'));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-11-27 10:55:23 +00:00
|
|
|
session()->flash('success', trans('shop::app.customer.signup-form.success'));
|
2018-11-22 12:43:03 +00:00
|
|
|
}
|
2018-11-27 10:55:23 +00:00
|
|
|
|
|
|
|
|
return redirect()->route($this->_config['redirect']);
|
2018-07-24 14:33:49 +00:00
|
|
|
} else {
|
2018-11-27 10:55:23 +00:00
|
|
|
session()->flash('error', trans('shop::app.customer.signup-form.failed'));
|
2018-08-24 05:18:53 +00:00
|
|
|
|
2018-07-24 14:33:49 +00:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-22 12:43:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method to verify account
|
|
|
|
|
*
|
2018-12-21 12:50:58 +00:00
|
|
|
* @param string $token
|
2018-11-22 12:43:03 +00:00
|
|
|
*/
|
2018-12-21 12:48:34 +00:00
|
|
|
public function verifyAccount($token)
|
|
|
|
|
{
|
2019-07-01 11:33:36 +00:00
|
|
|
$customer = $this->customerRepository->findOneByField('token', $token);
|
2018-11-22 12:43:03 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($customer) {
|
2018-11-27 10:55:23 +00:00
|
|
|
$customer->update(['is_verified' => 1, 'token' => 'NULL']);
|
|
|
|
|
|
|
|
|
|
session()->flash('success', trans('shop::app.customer.signup-form.verified'));
|
2018-11-22 12:43:03 +00:00
|
|
|
} else {
|
2018-11-27 10:55:23 +00:00
|
|
|
session()->flash('warning', trans('shop::app.customer.signup-form.verify-failed'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()->route('customer.session.index');
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 12:48:34 +00:00
|
|
|
public function resendVerificationEmail($email)
|
|
|
|
|
{
|
2018-11-27 10:55:23 +00:00
|
|
|
$verificationData['email'] = $email;
|
|
|
|
|
$verificationData['token'] = md5(uniqid(rand(), true));
|
|
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$customer = $this->customerRepository->findOneByField('email', $email);
|
2018-11-27 10:55:23 +00:00
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$this->customerRepository->update(['token' => $verificationData['token']], $customer->id);
|
2018-11-27 10:55:23 +00:00
|
|
|
|
|
|
|
|
try {
|
2019-05-01 11:31:03 +00:00
|
|
|
Mail::queue(new VerificationEmail($verificationData));
|
2018-11-27 10:55:23 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (Cookie::has('enable-resend')) {
|
2018-11-27 10:55:23 +00:00
|
|
|
\Cookie::queue(\Cookie::forget('enable-resend'));
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (Cookie::has('email-for-resend')) {
|
2018-11-27 10:55:23 +00:00
|
|
|
\Cookie::queue(\Cookie::forget('email-for-resend'));
|
|
|
|
|
}
|
2019-05-01 11:31:03 +00:00
|
|
|
} catch (\Exception $e) {
|
2019-04-09 02:32:42 +00:00
|
|
|
session()->flash('error', trans('shop::app.customer.signup-form.verification-not-sent'));
|
2018-11-27 10:55:23 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
2018-11-22 12:43:03 +00:00
|
|
|
}
|
2018-11-27 10:55:23 +00:00
|
|
|
session()->flash('success', trans('shop::app.customer.signup-form.verification-sent'));
|
2018-11-22 12:43:03 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2019-05-01 11:31:03 +00:00
|
|
|
}
|