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

83 lines
2.3 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-11-12 21:36:28 +01:00
$account->loadAllData();
2015-11-10 08:13:40 +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-11-01 23:10:20 +01:00
}