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

116 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;
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
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);
2015-11-02 19:43:22 +01:00
return 'Invalid secret';
}
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);
2015-11-02 19:43:22 +01:00
return 'Invalid credentials';
}
}
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;
if ($updatedAt) {
$account->load(['users' => function($query) use ($updatedAt) {
$query->where('updated_at', '>=', $updatedAt);
}])
->load(['clients' => function($query) use ($updatedAt) {
$query->where('updated_at', '>=', $updatedAt)->with('contacts');
}])
->load(['invoices' => function($query) use ($updatedAt) {
$query->where('updated_at', '>=', $updatedAt)->with('invoice_items', 'user', 'client');
}])
->load(['products' => function($query) use ($updatedAt) {
$query->where('updated_at', '>=', $updatedAt);
}])
->load(['tax_rates' => function($query) use ($updatedAt) {
$query->where('updated_at', '>=', $updatedAt);
}]);
} else {
$account->load(
'users',
'clients.contacts',
'invoices.invoice_items',
'invoices.user',
'invoices.client',
'products',
'tax_rates'
);
}
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);
}
2015-11-01 23:10:20 +01:00
}