mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-09 20:52:56 +01:00
working on api
This commit is contained in:
parent
c3066c824f
commit
38c3f14081
@ -6,6 +6,7 @@ use Response;
|
||||
use Input;
|
||||
use App\Models\Client;
|
||||
use App\Models\Account;
|
||||
use App\Models\AccountToken;
|
||||
use App\Ninja\Repositories\AccountRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use League\Fractal;
|
||||
@ -54,7 +55,25 @@ class AccountApiController extends Controller
|
||||
|
||||
private function processLogin(Request $request)
|
||||
{
|
||||
\Log::info('authed user = '.Auth::user()->email);
|
||||
|
||||
//Create a new token only if one does not already exist
|
||||
if(!AccountToken::where('user_id', '=', Auth::user()->id)->firstOrFail())
|
||||
{
|
||||
$account_token = $this->accountRepo->createToken($request->token_name);
|
||||
}
|
||||
|
||||
$manager = new Manager();
|
||||
$manager->setSerializer(new ArraySerializer());
|
||||
|
||||
$account = Auth::user()->account->load('users','tokens');
|
||||
$resource = new Item($account, new AccountTransformer, 'account');
|
||||
|
||||
$response = $manager->createData($resource)->toArray();
|
||||
$response = json_encode($response, JSON_PRETTY_PRINT);
|
||||
$headers = Utils::getApiHeaders();
|
||||
|
||||
return Response::make($response, 200, $headers);
|
||||
}
|
||||
|
||||
|
||||
|
@ -41,6 +41,10 @@ class Account extends Eloquent
|
||||
'invoice_settings' => 'object',
|
||||
];
|
||||
*/
|
||||
public function tokens()
|
||||
{
|
||||
return $this->hasMany('App\Models\AccountToken');
|
||||
}
|
||||
|
||||
public function users()
|
||||
{
|
||||
|
21
app/Ninja/Transformers/AccountTokenTransformer.php
Normal file
21
app/Ninja/Transformers/AccountTokenTransformer.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php namespace App\Ninja\Transformers;
|
||||
|
||||
use App\Models\AccountToken;
|
||||
use League\Fractal;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
class AccountTokenTransformer extends TransformerAbstract
|
||||
{
|
||||
|
||||
public function transform(AccountToken $account_token)
|
||||
{
|
||||
return [
|
||||
'id' => (int) $account_token->id,
|
||||
'account_id' =>(int) $account_token->account_id,
|
||||
'user_id' => (int) $account_token->user_id,
|
||||
'public_id' => (int) $account_token->public_id,
|
||||
'name' => $account_token->name,
|
||||
'token' => $account_token->token
|
||||
];
|
||||
}
|
||||
}
|
@ -7,9 +7,17 @@ use League\Fractal\TransformerAbstract;
|
||||
class AccountTransformer extends TransformerAbstract
|
||||
{
|
||||
protected $defaultIncludes = [
|
||||
'users'
|
||||
'users',
|
||||
'account_tokens'
|
||||
];
|
||||
|
||||
public function includeAccountTokens($account)
|
||||
{
|
||||
$account_tokens = $account->tokens;
|
||||
|
||||
return $this->collection($account_tokens, new AccountTokenTransformer);
|
||||
|
||||
}
|
||||
public function includeUsers($account)
|
||||
{
|
||||
$users = $account->users;
|
||||
|
Loading…
Reference in New Issue
Block a user