1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 22:22:32 +01:00

Merge branch 'master' of github.com:hillelcoren/invoice-ninja

This commit is contained in:
Hillel Coren 2015-11-03 13:20:55 +02:00
commit 26ec972819
7 changed files with 78 additions and 18 deletions

View File

@ -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;
@ -30,7 +31,7 @@ class AccountApiController extends Controller
}
if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
return $this->accountRepo->createToken($request->token_name);
return $this->processLogin($request);
} else {
return 'Invalid credentials';
}
@ -50,4 +51,25 @@ class AccountApiController extends Controller
return Response::make($response, 200, $headers);
}
private function processLogin(Request $request)
{
//Create a new token only if one does not already exist
$this->accountRepo->createToken('ios_api_token');
$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);
}
}

View File

@ -75,6 +75,7 @@ class AuthController extends Controller {
public function postLoginWrapper(Request $request)
{
$userId = Auth::check() ? Auth::user()->id : null;
$user = User::where('email', '=', $request->input('email'))->first();
@ -98,6 +99,7 @@ class AuthController extends Controller {
$users = $this->accountRepo->loadAccounts(Auth::user()->id);
}
Session::put(SESSION_USER_ACCOUNTS, $users);
} elseif ($user) {
$user->failed_logins = $user->failed_logins + 1;
$user->save();
@ -106,6 +108,7 @@ class AuthController extends Controller {
return $response;
}
public function getLogoutWrapper()
{
if (Auth::check() && !Auth::user()->registered) {

View File

@ -41,7 +41,11 @@ class Account extends Eloquent
'invoice_settings' => 'object',
];
*/
public function tokens()
{
return $this->hasMany('App\Models\AccountToken');
}
public function users()
{
return $this->hasMany('App\Models\User');

View File

@ -462,7 +462,7 @@ class AccountRepository
$name = trim($name) ?: 'TOKEN';
if ($token = AccountToken::scope()->whereName($name)->first()) {
return $token->token;
return $token;
}
$token = AccountToken::createNew();
@ -470,6 +470,6 @@ class AccountRepository
$token->token = str_random(RANDOM_KEY_LENGTH);
$token->save();
return $token->token;
return $token;
}
}

View 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
];
}
}

View File

@ -1,15 +1,24 @@
<?php namespace App\Ninja\Transformers;
use App\Models\Account;
use App\Models\AccountToken;
use League\Fractal;
use League\Fractal\TransformerAbstract;
class AccountTransformer extends TransformerAbstract
{
protected $defaultIncludes = [
'users'
'users',
'account_tokens'
];
public function includeAccountTokens($account)
{
$account_token = AccountToken::whereAccountId($account->id)->whereName('ios_api_token')->first();
return $this->Item($account_token, new AccountTokenTransformer);
}
public function includeUsers($account)
{
$users = $account->users;

27
composer.lock generated
View File

@ -1,10 +1,11 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "d0f3825f6d361f655c7393dd024b676e",
"hash": "70ef9e09bca60a19c396c138d8a01d50",
"content-hash": "399b36f7735987d2daf3d182603354b3",
"packages": [
{
"name": "alfaproject/omnipay-neteller",
@ -339,7 +340,7 @@
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Chumper/Datatable/zipball/7fa47cb5469f07c620fb69dee94b8e1a96943ee2",
"url": "https://api.github.com/repos/Chumper/Datatable/zipball/b44834db3d4e560d4368c1a04248b9e6a422ccff",
"reference": "7fa47cb",
"shasum": ""
},
@ -351,7 +352,7 @@
},
"require-dev": {
"mockery/mockery": "dev-master",
"orchestra/testbench": "2.1.*",
"orchestra/testbench": "3.1.*",
"phpunit/phpunit": "3.7.*"
},
"type": "library",
@ -380,7 +381,7 @@
"jquery",
"laravel"
],
"time": "2015-04-20 09:21:21"
"time": "2015-10-26 01:21:31"
},
{
"name": "classpreloader/classpreloader",
@ -4943,12 +4944,12 @@
"target-dir": "Symfony/Component/Console",
"source": {
"type": "git",
"url": "https://github.com/symfony/Console.git",
"url": "https://github.com/symfony/console.git",
"reference": "0e5e18ae09d3f5c06367759be940e9ed3f568359"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/Console/zipball/0e5e18ae09d3f5c06367759be940e9ed3f568359",
"url": "https://api.github.com/repos/symfony/console/zipball/0e5e18ae09d3f5c06367759be940e9ed3f568359",
"reference": "0e5e18ae09d3f5c06367759be940e9ed3f568359",
"shasum": ""
},
@ -5001,12 +5002,12 @@
"target-dir": "Symfony/Component/Debug",
"source": {
"type": "git",
"url": "https://github.com/symfony/Debug.git",
"url": "https://github.com/symfony/debug.git",
"reference": "fca5696e0c9787722baa8f2ad6940dfd7a6a6941"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/Debug/zipball/fca5696e0c9787722baa8f2ad6940dfd7a6a6941",
"url": "https://api.github.com/repos/symfony/debug/zipball/fca5696e0c9787722baa8f2ad6940dfd7a6a6941",
"reference": "fca5696e0c9787722baa8f2ad6940dfd7a6a6941",
"shasum": ""
},
@ -5215,12 +5216,12 @@
"target-dir": "Symfony/Component/HttpFoundation",
"source": {
"type": "git",
"url": "https://github.com/symfony/HttpFoundation.git",
"url": "https://github.com/symfony/http-foundation.git",
"reference": "e8fd1b73ac1c3de1f76c73801ddf1a8ecb1c1c9c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/HttpFoundation/zipball/e8fd1b73ac1c3de1f76c73801ddf1a8ecb1c1c9c",
"url": "https://api.github.com/repos/symfony/http-foundation/zipball/e8fd1b73ac1c3de1f76c73801ddf1a8ecb1c1c9c",
"reference": "e8fd1b73ac1c3de1f76c73801ddf1a8ecb1c1c9c",
"shasum": ""
},
@ -5269,12 +5270,12 @@
"target-dir": "Symfony/Component/HttpKernel",
"source": {
"type": "git",
"url": "https://github.com/symfony/HttpKernel.git",
"url": "https://github.com/symfony/http-kernel.git",
"reference": "a3f0ed713255c0400a2db38b3ed01989ef4b7322"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/HttpKernel/zipball/a3f0ed713255c0400a2db38b3ed01989ef4b7322",
"url": "https://api.github.com/repos/symfony/http-kernel/zipball/a3f0ed713255c0400a2db38b3ed01989ef4b7322",
"reference": "a3f0ed713255c0400a2db38b3ed01989ef4b7322",
"shasum": ""
},