diff --git a/app/Http/Controllers/AccountApiController.php b/app/Http/Controllers/AccountApiController.php index 65f4eff634..619d9652e9 100644 --- a/app/Http/Controllers/AccountApiController.php +++ b/app/Http/Controllers/AccountApiController.php @@ -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); + } + + } diff --git a/app/Http/Controllers/Auth/AuthController.php b/app/Http/Controllers/Auth/AuthController.php index 586f55e673..2966a9fd83 100644 --- a/app/Http/Controllers/Auth/AuthController.php +++ b/app/Http/Controllers/Auth/AuthController.php @@ -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) { diff --git a/app/Models/Account.php b/app/Models/Account.php index fe16924f68..0ffacd5051 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -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'); diff --git a/app/Ninja/Repositories/AccountRepository.php b/app/Ninja/Repositories/AccountRepository.php index c431584b88..a824315e14 100644 --- a/app/Ninja/Repositories/AccountRepository.php +++ b/app/Ninja/Repositories/AccountRepository.php @@ -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; } } diff --git a/app/Ninja/Transformers/AccountTokenTransformer.php b/app/Ninja/Transformers/AccountTokenTransformer.php new file mode 100644 index 0000000000..b2022a7782 --- /dev/null +++ b/app/Ninja/Transformers/AccountTokenTransformer.php @@ -0,0 +1,21 @@ + (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 + ]; + } +} \ No newline at end of file diff --git a/app/Ninja/Transformers/AccountTransformer.php b/app/Ninja/Transformers/AccountTransformer.php index 469365d61d..f172356eb4 100644 --- a/app/Ninja/Transformers/AccountTransformer.php +++ b/app/Ninja/Transformers/AccountTransformer.php @@ -1,15 +1,24 @@ id)->whereName('ios_api_token')->first(); + + return $this->Item($account_token, new AccountTokenTransformer); + + } public function includeUsers($account) { $users = $account->users; diff --git a/composer.lock b/composer.lock index 876a30f371..567a3b2e99 100644 --- a/composer.lock +++ b/composer.lock @@ -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": "" },