1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Http/Controllers/AccountApiController.php

212 lines
6.5 KiB
PHP
Raw Normal View History

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;
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;
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;
}
public function ping(Request $request)
2016-05-02 10:38:01 +02:00
{
$headers = Utils::getApiHeaders();
2017-02-15 21:09:46 +01:00
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
{
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
2016-03-08 22:22:59 +01:00
Auth::login($user, true);
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)
{
2015-11-02 19:43:22 +01:00
if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
2015-11-03 00:55:31 +01:00
return $this->processLogin($request);
2015-11-02 19:43:22 +01:00
} else {
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
2015-11-27 13:55:28 +01:00
return $this->response($data);
2015-11-03 20:03:24 +01:00
}
2015-11-03 03:02:15 +01: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);
2016-11-29 18:47:26 +01: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()
{
$data = [];
$cachedTables = unserialize(CACHED_TABLES);
foreach ($cachedTables as $name => $class) {
$data[$name] = Cache::get($name);
}
return $this->response($data);
}
2015-12-22 12:19:21 +01:00
public function getUserAccounts(Request $request)
{
return $this->processLogin($request);
}
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-01-30 17:05:31 +01:00
if ($devices[$x]['email'] == Auth::user()->username) {
$devices[$x]['token'] = $request->token; //update
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
}
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');
try {
2016-11-25 10:56:48 +01:00
$user = Socialite::driver($provider)->stateless()->userFromToken($token);
2016-10-05 20:51:08 +02:00
} catch (Exception $exception) {
2016-10-06 14:46:27 +02:00
return $this->errorResponse(['message' => $exception->getMessage()], 401);
2016-10-05 20:51:08 +02:00
}
2016-10-06 14:46:27 +02:00
if ($user) {
$providerId = AuthService::getProviderId($provider);
$user = $this->accountRepo->findUserByOauth($providerId, $user->id);
}
if ($user) {
Auth::login($user);
2017-01-30 20:40:43 +01:00
2016-10-06 14:46:27 +02:00
return $this->processLogin($request);
} else {
sleep(ERROR_DELAY);
2017-01-30 20:40:43 +01:00
2016-10-06 14:46:27 +02:00
return $this->errorResponse(['message' => 'Invalid credentials'], 401);
}
2016-10-05 20:51:08 +02:00
}
2015-11-01 23:10:20 +01:00
}