120 lines
3.1 KiB
PHP
120 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Webkul\API\Http\Controllers\Shop;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Webkul\Customer\Repositories\CustomerRepository;
|
|
use Webkul\Customer\Repositories\CustomerGroupRepository;
|
|
|
|
class CustomerController extends Controller
|
|
{
|
|
/**
|
|
* Contains current guard
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $guard;
|
|
|
|
/**
|
|
* Contains route related configuration
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $_config;
|
|
|
|
/**
|
|
* Repository object
|
|
*
|
|
* @var \Webkul\Customer\Repositories\CustomerRepository
|
|
*/
|
|
protected $customerRepository;
|
|
|
|
/**
|
|
* Repository object
|
|
*
|
|
* @var \Webkul\Customer\Repositories\CustomerGroupRepository
|
|
*/
|
|
protected $customerGroupRepository;
|
|
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @param \Webkul\Customer\Repositories\CustomerRepository $customerRepository
|
|
* @param \Webkul\Customer\Repositories\CustomerGroupRepository $customerGroupRepository
|
|
* @return void
|
|
*/
|
|
public function __construct(
|
|
CustomerRepository $customerRepository,
|
|
CustomerGroupRepository $customerGroupRepository
|
|
) {
|
|
$this->guard = request()->has('token') ? 'api' : 'customer';
|
|
|
|
$this->_config = request('_config');
|
|
|
|
if (isset($this->_config['authorization_required']) && $this->_config['authorization_required']) {
|
|
|
|
auth()->setDefaultDriver($this->guard);
|
|
|
|
$this->middleware('auth:' . $this->guard);
|
|
}
|
|
|
|
$this->customerRepository = $customerRepository;
|
|
|
|
$this->customerGroupRepository = $customerGroupRepository;
|
|
}
|
|
|
|
/**
|
|
* Method to store user's sign up form data to DB.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
request()->validate([
|
|
'first_name' => 'required',
|
|
'last_name' => 'required',
|
|
'email' => 'email|required|unique:customers,email',
|
|
'password' => 'confirmed|min:6|required',
|
|
]);
|
|
|
|
$data = request()->input();
|
|
|
|
$data = array_merge($data, [
|
|
'password' => bcrypt($data['password']),
|
|
'channel_id' => core()->getCurrentChannel()->id,
|
|
'is_verified' => 1,
|
|
]);
|
|
|
|
$data['customer_group_id'] = $this->customerGroupRepository->findOneWhere(['code' => 'general'])->id;
|
|
|
|
Event::dispatch('customer.registration.before');
|
|
|
|
$customer = $this->customerRepository->create($data);
|
|
|
|
Event::dispatch('customer.registration.after', $customer);
|
|
|
|
return response()->json([
|
|
'message' => 'Your account has been created successfully.',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
} |