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

121 lines
3.6 KiB
PHP
Raw Normal View History

2015-11-01 23:10:20 +01:00
<?php namespace App\Http\Controllers;
use Auth;
use Utils;
use Response;
use Input;
2016-02-01 04:42:05 +01:00
use Validator;
2015-11-19 12:37:30 +01:00
use Cache;
2015-11-01 23:10:20 +01:00
use App\Models\Client;
use App\Models\Account;
2015-11-03 03:02:15 +01:00
use App\Models\AccountToken;
2015-11-01 23:10:20 +01:00
use App\Ninja\Repositories\AccountRepository;
2015-11-02 19:43:22 +01:00
use Illuminate\Http\Request;
2015-11-01 23:10:20 +01:00
use League\Fractal;
use League\Fractal\Manager;
use App\Ninja\Serializers\ArraySerializer;
use App\Ninja\Transformers\AccountTransformer;
2015-11-03 20:03:24 +01:00
use App\Ninja\Transformers\UserAccountTransformer;
use App\Http\Controllers\BaseAPIController;
2015-11-08 21:34:26 +01:00
use Swagger\Annotations as SWG;
2015-11-01 23:10:20 +01:00
2016-02-03 13:41:40 +01:00
use App\Http\Requests\UpdateAccountRequest;
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)
{
2015-11-03 20:03:24 +01:00
parent::__construct();
2015-11-01 23:10:20 +01:00
$this->accountRepo = $accountRepo;
}
2015-11-02 19:43:22 +01:00
public function login(Request $request)
{
if ( ! env(API_SECRET) || $request->api_secret !== env(API_SECRET)) {
2015-11-03 20:03:24 +01:00
sleep(ERROR_DELAY);
2016-02-15 11:24:06 +01:00
return $this->errorResponse(['message'=>'Invalid secret'],401);
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);
2016-02-15 11:24:06 +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);
2015-11-04 16:23:22 +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;
$map = [
'users' => [],
'clients' => ['contacts'],
'invoices' => ['invoice_items', 'user', 'client', 'payments'],
'products' => [],
'tax_rates' => [],
2016-02-17 11:50:52 +01:00
'expenses' => ['client', 'invoice', 'vendor'],
'payments' => ['invoice'],
];
foreach ($map as $key => $values) {
$account->load([$key => function($query) use ($values, $updatedAt) {
$query->withTrashed()->with($values);
if ($updatedAt) {
$query->where('updated_at', '>=', $updatedAt);
}
2015-12-27 12:08:58 +01:00
}]);
}
2015-11-27 13:55:28 +01:00
$transformer = new AccountTransformer(null, $request->serializer);
$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
}
2015-11-01 23:10:20 +01:00
}