2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2015-11-01 23:10:20 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Events\UserSignedUp;
|
|
|
|
use App\Http\Requests\RegisterRequest;
|
|
|
|
use App\Http\Requests\UpdateAccountRequest;
|
2015-11-01 23:10:20 +01:00
|
|
|
use App\Models\Account;
|
2017-10-22 22:11:50 +02:00
|
|
|
use App\Models\User;
|
2017-03-30 15:01:42 +02:00
|
|
|
use App\Ninja\OAuth\OAuth;
|
2015-11-01 23:10:20 +01:00
|
|
|
use App\Ninja\Repositories\AccountRepository;
|
|
|
|
use App\Ninja\Transformers\AccountTransformer;
|
2015-11-03 20:03:24 +01:00
|
|
|
use App\Ninja\Transformers\UserAccountTransformer;
|
2017-01-30 20:40:43 +01:00
|
|
|
use App\Services\AuthService;
|
|
|
|
use Auth;
|
|
|
|
use Cache;
|
|
|
|
use Exception;
|
|
|
|
use Illuminate\Http\Request;
|
2017-11-09 11:21:08 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Response;
|
|
|
|
use Socialite;
|
|
|
|
use Utils;
|
2016-02-03 13:41:40 +01:00
|
|
|
|
2015-11-03 20:03:24 +01:00
|
|
|
class AccountApiController extends BaseAPIController
|
2015-11-01 23:10:20 +01:00
|
|
|
{
|
|
|
|
protected $accountRepo;
|
|
|
|
|
|
|
|
public function __construct(AccountRepository $accountRepo)
|
|
|
|
{
|
2016-03-02 15:36:46 +01:00
|
|
|
parent::__construct();
|
2015-11-03 20:03:24 +01:00
|
|
|
|
2015-11-01 23:10:20 +01:00
|
|
|
$this->accountRepo = $accountRepo;
|
|
|
|
}
|
|
|
|
|
2017-01-11 08:34:29 +01:00
|
|
|
public function ping(Request $request)
|
2016-05-02 10:38:01 +02:00
|
|
|
{
|
|
|
|
$headers = Utils::getApiHeaders();
|
|
|
|
|
2018-05-24 09:31:00 +02:00
|
|
|
// Legacy support for Zapier
|
2018-05-06 21:45:47 +02:00
|
|
|
if (request()->v2) {
|
2018-05-24 09:31:00 +02:00
|
|
|
return $this->response(auth()->user()->email);
|
2018-05-06 21:45:47 +02:00
|
|
|
} else {
|
|
|
|
return Response::make(RESULT_SUCCESS, 200, $headers);
|
|
|
|
}
|
2016-05-02 10:38:01 +02:00
|
|
|
}
|
|
|
|
|
2016-03-13 11:54:31 +01:00
|
|
|
public function register(RegisterRequest $request)
|
2015-11-02 19:43:22 +01:00
|
|
|
{
|
2017-05-15 15:49:58 +02:00
|
|
|
if (! \App\Models\LookupUser::validateField('email', $request->email)) {
|
2017-05-01 20:46:31 +02:00
|
|
|
return $this->errorResponse(['message' => trans('texts.email_taken')], 500);
|
2017-05-02 12:35:42 +02:00
|
|
|
}
|
2017-05-01 20:46:31 +02:00
|
|
|
|
2016-07-15 15:47:18 +02:00
|
|
|
$account = $this->accountRepo->create($request->first_name, $request->last_name, $request->email, $request->password);
|
2016-03-08 22:22:59 +01:00
|
|
|
$user = $account->users()->first();
|
2016-07-15 15:47:18 +02:00
|
|
|
|
2017-11-01 20:22:29 +01:00
|
|
|
Auth::login($user);
|
2016-03-08 22:22:59 +01:00
|
|
|
event(new UserSignedUp());
|
2016-07-15 15:47:18 +02:00
|
|
|
|
2016-03-08 22:22:59 +01:00
|
|
|
return $this->processLogin($request);
|
|
|
|
}
|
2015-11-02 19:43:22 +01:00
|
|
|
|
2016-03-08 22:22:59 +01:00
|
|
|
public function login(Request $request)
|
|
|
|
{
|
2017-10-22 22:11:50 +02:00
|
|
|
$user = User::where('email', '=', $request->email)->first();
|
|
|
|
|
|
|
|
if ($user && $user->failed_logins >= MAX_FAILED_LOGINS) {
|
|
|
|
sleep(ERROR_DELAY);
|
|
|
|
return $this->errorResponse(['message' => 'Invalid credentials'], 401);
|
|
|
|
}
|
|
|
|
|
2015-11-02 19:43:22 +01:00
|
|
|
if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
|
2017-10-22 22:11:50 +02:00
|
|
|
if ($user && $user->failed_logins > 0) {
|
|
|
|
$user->failed_logins = 0;
|
|
|
|
$user->save();
|
|
|
|
}
|
2015-11-03 00:55:31 +01:00
|
|
|
return $this->processLogin($request);
|
2015-11-02 19:43:22 +01:00
|
|
|
} else {
|
2017-11-01 20:22:29 +01:00
|
|
|
error_log('login failed');
|
2017-10-22 22:11:50 +02:00
|
|
|
if ($user) {
|
|
|
|
$user->failed_logins = $user->failed_logins + 1;
|
|
|
|
$user->save();
|
|
|
|
}
|
2015-11-03 20:03:24 +01:00
|
|
|
sleep(ERROR_DELAY);
|
2017-01-30 20:40:43 +01:00
|
|
|
return $this->errorResponse(['message' => 'Invalid credentials'], 401);
|
2015-11-02 19:43:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 20:03:24 +01:00
|
|
|
private function processLogin(Request $request)
|
2015-11-01 23:10:20 +01:00
|
|
|
{
|
2015-11-03 20:03:24 +01:00
|
|
|
// Create a new token only if one does not already exist
|
2015-11-09 03:10:01 +01:00
|
|
|
$user = Auth::user();
|
|
|
|
$this->accountRepo->createTokens($user, $request->token_name);
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2015-11-09 03:10:01 +01:00
|
|
|
$users = $this->accountRepo->findUsers($user, 'account.account_tokens');
|
2015-11-27 13:55:28 +01:00
|
|
|
$transformer = new UserAccountTransformer($user->account, $request->serializer, $request->token_name);
|
|
|
|
$data = $this->createCollection($users, $transformer, 'user_account');
|
2015-11-03 03:02:15 +01:00
|
|
|
|
2018-06-17 10:27:36 +02:00
|
|
|
if (request()->include_static) {
|
|
|
|
$data = [
|
|
|
|
'accounts' => $data,
|
|
|
|
'static' => Utils::getStaticData(),
|
|
|
|
'version' => NINJA_VERSION,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
return $this->response($data);
|
2015-11-03 20:03:24 +01:00
|
|
|
}
|
2018-05-06 21:45:47 +02:00
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
public function show(Request $request)
|
2015-11-03 20:03:24 +01:00
|
|
|
{
|
2015-11-08 10:43:32 +01:00
|
|
|
$account = Auth::user()->account;
|
2015-12-27 12:08:58 +01:00
|
|
|
$updatedAt = $request->updated_at ? date('Y-m-d H:i:s', $request->updated_at) : false;
|
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
$transformer = new AccountTransformer(null, $request->serializer);
|
2017-05-17 11:18:48 +02:00
|
|
|
$account->load(array_merge($transformer->getDefaultIncludes(), ['projects.client']));
|
2015-11-27 13:55:28 +01:00
|
|
|
$account = $this->createItem($account, $transformer, 'account');
|
2015-11-03 03:02:15 +01:00
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
return $this->response($account);
|
2015-11-03 00:55:31 +01:00
|
|
|
}
|
|
|
|
|
2015-11-19 12:37:30 +01:00
|
|
|
public function getStaticData()
|
|
|
|
{
|
2018-06-17 10:27:36 +02:00
|
|
|
return $this->response(Utils::getStaticData());
|
2015-11-19 12:37:30 +01:00
|
|
|
}
|
2015-12-22 12:19:21 +01:00
|
|
|
|
|
|
|
public function getUserAccounts(Request $request)
|
|
|
|
{
|
2017-11-09 11:21:08 +01:00
|
|
|
$user = Auth::user();
|
|
|
|
|
|
|
|
$users = $this->accountRepo->findUsers($user, 'account.account_tokens');
|
|
|
|
$transformer = new UserAccountTransformer($user->account, $request->serializer, $request->token_name);
|
|
|
|
$data = $this->createCollection($users, $transformer, 'user_account');
|
|
|
|
|
|
|
|
return $this->response($data);
|
2015-12-22 12:19:21 +01:00
|
|
|
}
|
2016-02-01 04:42:05 +01:00
|
|
|
|
2016-02-03 13:41:40 +01:00
|
|
|
public function update(UpdateAccountRequest $request)
|
2016-02-01 04:42:05 +01:00
|
|
|
{
|
2016-02-03 13:41:40 +01:00
|
|
|
$account = Auth::user()->account;
|
|
|
|
$this->accountRepo->save($request->input(), $account);
|
2016-02-01 04:42:05 +01:00
|
|
|
|
2016-02-03 13:41:40 +01:00
|
|
|
$transformer = new AccountTransformer(null, $request->serializer);
|
|
|
|
$account = $this->createItem($account, $transformer, 'account');
|
2016-02-01 04:42:05 +01:00
|
|
|
|
2016-02-03 13:41:40 +01:00
|
|
|
return $this->response($account);
|
2016-02-01 04:42:05 +01:00
|
|
|
}
|
2016-03-01 03:57:59 +01:00
|
|
|
|
|
|
|
public function addDeviceToken(Request $request)
|
|
|
|
{
|
|
|
|
$account = Auth::user()->account;
|
|
|
|
|
|
|
|
//scan if this user has a token already registered (tokens can change, so we need to use the users email as key)
|
2017-01-30 17:05:31 +01:00
|
|
|
$devices = json_decode($account->devices, true);
|
2016-03-01 03:57:59 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
for ($x = 0; $x < count($devices); $x++) {
|
2017-11-09 11:21:08 +01:00
|
|
|
if ($devices[$x]['email'] == $request->email) {
|
2017-01-30 17:05:31 +01:00
|
|
|
$devices[$x]['token'] = $request->token; //update
|
2017-03-28 04:30:47 +02:00
|
|
|
$devices[$x]['device'] = $request->device;
|
2016-03-01 05:06:54 +01:00
|
|
|
$account->devices = json_encode($devices);
|
2017-01-30 17:05:31 +01:00
|
|
|
$account->save();
|
|
|
|
$devices[$x]['account_key'] = $account->account_key;
|
2016-03-03 10:52:47 +01:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
return $this->response($devices[$x]);
|
2016-03-01 03:57:59 +01:00
|
|
|
}
|
2017-01-30 17:05:31 +01:00
|
|
|
}
|
2016-03-01 05:06:54 +01:00
|
|
|
|
2016-03-01 03:57:59 +01:00
|
|
|
//User does not have a device, create new record
|
|
|
|
|
|
|
|
$newDevice = [
|
|
|
|
'token' => $request->token,
|
|
|
|
'email' => $request->email,
|
|
|
|
'device' => $request->device,
|
2016-03-03 09:48:33 +01:00
|
|
|
'account_key' => $account->account_key,
|
2017-01-30 17:05:31 +01:00
|
|
|
'notify_sent' => true,
|
|
|
|
'notify_viewed' => true,
|
|
|
|
'notify_approved' => true,
|
|
|
|
'notify_paid' => true,
|
2016-03-01 03:57:59 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$devices[] = $newDevice;
|
|
|
|
$account->devices = json_encode($devices);
|
|
|
|
$account->save();
|
|
|
|
|
2016-03-01 11:31:23 +01:00
|
|
|
return $this->response($newDevice);
|
2016-03-01 03:57:59 +01:00
|
|
|
}
|
|
|
|
|
2017-11-09 11:21:08 +01:00
|
|
|
public function removeDeviceToken(Request $request) {
|
|
|
|
|
|
|
|
$account = Auth::user()->account;
|
|
|
|
|
|
|
|
$devices = json_decode($account->devices, true);
|
|
|
|
|
2018-02-04 10:04:43 +01:00
|
|
|
for($x=0; $x<count($devices); $x++)
|
2017-11-09 11:21:08 +01:00
|
|
|
{
|
2018-02-04 10:04:43 +01:00
|
|
|
if($request->token == $devices[$x]['token'])
|
|
|
|
unset($devices[$x]);
|
2017-11-09 11:21:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$account->devices = json_encode(array_values($devices));
|
|
|
|
$account->save();
|
|
|
|
|
|
|
|
return $this->response(['success']);
|
|
|
|
}
|
|
|
|
|
2016-03-01 04:44:14 +01:00
|
|
|
public function updatePushNotifications(Request $request)
|
|
|
|
{
|
|
|
|
$account = Auth::user()->account;
|
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
$devices = json_decode($account->devices, true);
|
2016-03-01 04:44:14 +01:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
if (count($devices) < 1) {
|
2017-01-30 20:40:43 +01:00
|
|
|
return $this->errorResponse(['message' => 'No registered devices.'], 400);
|
2017-01-30 17:05:31 +01:00
|
|
|
}
|
2016-03-01 04:44:14 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
for ($x = 0; $x < count($devices); $x++) {
|
2017-01-30 17:05:31 +01:00
|
|
|
if ($devices[$x]['email'] == Auth::user()->username) {
|
2016-03-01 04:44:14 +01:00
|
|
|
$newDevice = [
|
2016-03-03 01:24:13 +01:00
|
|
|
'token' => $devices[$x]['token'],
|
|
|
|
'email' => $devices[$x]['email'],
|
|
|
|
'device' => $devices[$x]['device'],
|
2016-03-03 09:48:33 +01:00
|
|
|
'account_key' => $account->account_key,
|
2016-03-01 04:44:14 +01:00
|
|
|
'notify_sent' => $request->notify_sent,
|
|
|
|
'notify_viewed' => $request->notify_viewed,
|
|
|
|
'notify_approved' => $request->notify_approved,
|
|
|
|
'notify_paid' => $request->notify_paid,
|
|
|
|
];
|
|
|
|
|
2016-03-03 10:52:47 +01:00
|
|
|
$devices[$x] = $newDevice;
|
2016-03-01 04:44:14 +01:00
|
|
|
$account->devices = json_encode($devices);
|
|
|
|
$account->save();
|
|
|
|
|
2016-03-01 11:31:23 +01:00
|
|
|
return $this->response($newDevice);
|
2016-03-01 04:44:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-05 20:51:08 +02:00
|
|
|
|
2016-10-06 14:46:27 +02:00
|
|
|
public function oauthLogin(Request $request)
|
2016-10-05 20:51:08 +02:00
|
|
|
{
|
2016-10-06 14:46:27 +02:00
|
|
|
$user = false;
|
2016-10-05 20:51:08 +02:00
|
|
|
$token = $request->input('token');
|
|
|
|
$provider = $request->input('provider');
|
|
|
|
|
2017-03-30 15:01:42 +02:00
|
|
|
$oAuth = new OAuth();
|
|
|
|
$user = $oAuth->getProvider($provider)->getTokenResponse($token);
|
2016-10-06 14:46:27 +02:00
|
|
|
|
2017-05-01 18:58:51 +02:00
|
|
|
if ($user) {
|
2016-10-06 14:46:27 +02:00
|
|
|
Auth::login($user);
|
|
|
|
return $this->processLogin($request);
|
|
|
|
}
|
2017-03-30 15:01:42 +02:00
|
|
|
else
|
|
|
|
return $this->errorResponse(['message' => 'Invalid credentials'], 401);
|
|
|
|
|
2016-10-05 20:51:08 +02:00
|
|
|
}
|
2017-11-14 10:56:41 +01:00
|
|
|
|
|
|
|
public function iosSubscriptionStatus() {
|
|
|
|
|
|
|
|
//stubbed for iOS callbacks
|
2018-05-06 21:45:47 +02:00
|
|
|
|
2017-11-14 10:56:41 +01:00
|
|
|
}
|
|
|
|
|
2015-11-01 23:10:20 +01:00
|
|
|
}
|