2019-05-07 11:36:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\API\Http\Controllers\Shop;
|
|
|
|
|
|
2020-10-05 14:03:57 +00:00
|
|
|
use Illuminate\Http\Request;
|
2020-08-07 18:38:14 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2019-05-07 11:36:21 +00:00
|
|
|
use Illuminate\Support\Facades\Event;
|
|
|
|
|
use Webkul\Customer\Repositories\CustomerRepository;
|
2020-01-30 10:27:56 +00:00
|
|
|
use Webkul\Customer\Repositories\CustomerGroupRepository;
|
2019-05-07 11:36:21 +00:00
|
|
|
|
|
|
|
|
class CustomerController extends Controller
|
|
|
|
|
{
|
2020-08-07 18:38:14 +00:00
|
|
|
/**
|
|
|
|
|
* Contains current guard
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $guard;
|
|
|
|
|
|
2019-05-07 11:36:21 +00:00
|
|
|
/**
|
|
|
|
|
* Contains route related configuration
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $_config;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Repository object
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @var \Webkul\Customer\Repositories\CustomerRepository
|
2019-05-07 11:36:21 +00:00
|
|
|
*/
|
|
|
|
|
protected $customerRepository;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-01-30 10:27:56 +00:00
|
|
|
* Repository object
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @var \Webkul\Customer\Repositories\CustomerGroupRepository
|
2019-05-07 11:36:21 +00:00
|
|
|
*/
|
2020-01-30 10:27:56 +00:00
|
|
|
protected $customerGroupRepository;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new controller instance.
|
|
|
|
|
*
|
2020-03-05 14:19:14 +00:00
|
|
|
* @param \Webkul\Customer\Repositories\CustomerRepository $customerRepository
|
2020-03-05 05:34:57 +00:00
|
|
|
* @param \Webkul\Customer\Repositories\CustomerGroupRepository $customerGroupRepository
|
2020-01-30 10:27:56 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
CustomerRepository $customerRepository,
|
|
|
|
|
CustomerGroupRepository $customerGroupRepository
|
|
|
|
|
) {
|
2020-08-07 18:38:14 +00:00
|
|
|
$this->guard = request()->has('token') ? 'api' : 'customer';
|
|
|
|
|
|
2019-05-07 11:36:21 +00:00
|
|
|
$this->_config = request('_config');
|
|
|
|
|
|
2020-08-07 18:38:14 +00:00
|
|
|
if (isset($this->_config['authorization_required']) && $this->_config['authorization_required']) {
|
|
|
|
|
|
|
|
|
|
auth()->setDefaultDriver($this->guard);
|
|
|
|
|
|
|
|
|
|
$this->middleware('auth:' . $this->guard);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 11:36:21 +00:00
|
|
|
$this->customerRepository = $customerRepository;
|
2020-01-30 10:27:56 +00:00
|
|
|
|
|
|
|
|
$this->customerGroupRepository = $customerGroupRepository;
|
2019-05-07 11:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method to store user's sign up form data to DB.
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
2019-05-07 11:36:21 +00:00
|
|
|
*/
|
2020-10-05 14:03:57 +00:00
|
|
|
public function create(Request $request)
|
2019-05-07 11:36:21 +00:00
|
|
|
{
|
2020-10-06 06:28:51 +00:00
|
|
|
$this->validate($request, [
|
2019-05-07 11:36:21 +00:00
|
|
|
'first_name' => 'required',
|
2020-02-19 11:24:04 +00:00
|
|
|
'last_name' => 'required',
|
|
|
|
|
'email' => 'email|required|unique:customers,email',
|
2020-03-04 06:32:53 +00:00
|
|
|
'password' => 'confirmed|min:6|required',
|
2019-05-07 11:36:21 +00:00
|
|
|
]);
|
|
|
|
|
|
2020-10-05 14:03:57 +00:00
|
|
|
$data = [
|
2020-10-06 05:01:54 +00:00
|
|
|
'first_name' => $request->get('first_name'),
|
|
|
|
|
'last_name' => $request->get('last_name'),
|
|
|
|
|
'email' => $request->get('email'),
|
|
|
|
|
'password' => $request->get('password'),
|
|
|
|
|
'password' => bcrypt($request->get('password')),
|
2020-10-05 14:03:57 +00:00
|
|
|
'channel_id' => core()->getCurrentChannel()->id,
|
|
|
|
|
'is_verified' => 1,
|
|
|
|
|
'customer_group_id' => $this->customerGroupRepository->findOneWhere(['code' => 'general'])->id
|
|
|
|
|
];
|
2020-01-30 10:27:56 +00:00
|
|
|
|
2019-12-24 14:01:13 +00:00
|
|
|
Event::dispatch('customer.registration.before');
|
2019-05-07 11:36:21 +00:00
|
|
|
|
|
|
|
|
$customer = $this->customerRepository->create($data);
|
|
|
|
|
|
2019-12-24 14:01:13 +00:00
|
|
|
Event::dispatch('customer.registration.after', $customer);
|
2019-05-07 11:36:21 +00:00
|
|
|
|
|
|
|
|
return response()->json([
|
2020-03-04 06:32:53 +00:00
|
|
|
'message' => 'Your account has been created successfully.',
|
2020-02-27 08:03:03 +00:00
|
|
|
]);
|
2019-05-07 11:36:21 +00:00
|
|
|
}
|
2020-08-07 18:38:14 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a current user data.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function get($id)
|
|
|
|
|
{
|
|
|
|
|
if (Auth::user($this->guard)->id === (int) $id) {
|
|
|
|
|
return new $this->_config['resource'](
|
|
|
|
|
$this->customerRepository->findOrFail($id)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'message' => 'Invalid Request.',
|
|
|
|
|
], 403);
|
|
|
|
|
}
|
2019-05-07 11:36:21 +00:00
|
|
|
}
|