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

70 lines
1.9 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;
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-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
$this->accountRepo->createTokens(Auth::user(), $request->token_name);
2015-11-04 16:23:22 +01:00
2015-11-03 20:03:24 +01:00
$users = $this->accountRepo->findUsers(Auth::user(), 'account.account_tokens');
2015-11-08 10:43:32 +01:00
$data = $this->createCollection($users, new UserAccountTransformer($request->token_name));
2015-11-03 03:02:15 +01:00
2015-11-08 10:43:32 +01:00
$response = [
'user_accounts' => $data,
'default_url' => SITE_URL
];
return $this->response($response);
2015-11-03 20:03:24 +01:00
}
2015-11-03 03:02:15 +01:00
2015-11-08 10:43:32 +01:00
public function show()
2015-11-03 20:03:24 +01:00
{
2015-11-08 10:43:32 +01:00
$account = Auth::user()->account;
$response = $this->createItem($account, new AccountTransformer);
2015-11-03 03:02:15 +01:00
2015-11-08 10:43:32 +01:00
return $this->response($response);
2015-11-03 00:55:31 +01:00
}
2015-11-01 23:10:20 +01:00
}