1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/Http/Controllers/AccountApiController.php

191 lines
5.7 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;
2016-02-01 04:42:05 +01:00
use Validator;
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
2016-03-08 22:22:59 +01:00
use App\Events\UserSignedUp;
use App\Http\Requests\RegisterRequest;
2016-02-03 13:41:40 +01:00
use App\Http\Requests\UpdateAccountRequest;
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)
{
2016-03-02 15:36:46 +01:00
parent::__construct();
2015-11-03 20:03:24 +01:00
2015-11-01 23:10:20 +01:00
$this->accountRepo = $accountRepo;
}
2016-03-13 11:54:31 +01:00
public function register(RegisterRequest $request)
2015-11-02 19:43:22 +01:00
{
2016-03-13 10:10:25 +01:00
2016-03-08 22:22:59 +01:00
$account = $this->accountRepo->create($request->first_name, $request->last_name, $request->email, $request->password);
$user = $account->users()->first();
Auth::login($user, true);
event(new UserSignedUp());
return $this->processLogin($request);
}
2015-11-02 19:43:22 +01:00
2016-03-08 22:22:59 +01:00
public function login(Request $request)
{
2015-11-02 19:43:22 +01:00
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);
2016-02-15 11:24:06 +01:00
return $this->errorResponse(['message'=>'Invalid credentials'],401);
2015-11-02 19:43:22 +01:00
}
}
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);
2016-03-02 14:36:42 +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-12-27 12:08:58 +01:00
$updatedAt = $request->updated_at ? date('Y-m-d H:i:s', $request->updated_at) : false;
2016-03-13 12:56:56 +01:00
$account->loadAllData($updatedAt);
2015-12-27 12:08:58 +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-12-22 12:19:21 +01:00
public function getUserAccounts(Request $request)
{
return $this->processLogin($request);
}
2016-02-01 04:42:05 +01:00
2016-02-03 13:41:40 +01:00
public function update(UpdateAccountRequest $request)
2016-02-01 04:42:05 +01:00
{
2016-02-03 13:41:40 +01:00
$account = Auth::user()->account;
$this->accountRepo->save($request->input(), $account);
2016-02-01 04:42:05 +01:00
2016-02-03 13:41:40 +01:00
$transformer = new AccountTransformer(null, $request->serializer);
$account = $this->createItem($account, $transformer, 'account');
2016-02-01 04:42:05 +01:00
2016-02-03 13:41:40 +01:00
return $this->response($account);
2016-02-01 04:42:05 +01:00
}
2016-03-01 03:57:59 +01:00
public function addDeviceToken(Request $request)
{
$account = Auth::user()->account;
//scan if this user has a token already registered (tokens can change, so we need to use the users email as key)
$devices = json_decode($account->devices,TRUE);
2016-03-01 05:06:54 +01:00
for($x=0; $x<count($devices); $x++)
{
if ($devices[$x]['email'] == Auth::user()->username) {
$devices[$x]['token'] = $request->token; //update
$account->devices = json_encode($devices);
$account->save();
2016-03-03 09:48:33 +01:00
$devices[$x]['account_key'] = $account->account_key;
2016-03-03 10:52:47 +01:00
2016-03-02 11:52:59 +01:00
return $this->response($devices[$x]);
2016-03-01 03:57:59 +01:00
}
}
2016-03-01 05:06:54 +01:00
2016-03-01 03:57:59 +01:00
//User does not have a device, create new record
$newDevice = [
'token' => $request->token,
'email' => $request->email,
'device' => $request->device,
2016-03-03 09:48:33 +01:00
'account_key' => $account->account_key,
2016-03-01 03:57:59 +01:00
'notify_sent' => TRUE,
'notify_viewed' => TRUE,
'notify_approved' => TRUE,
'notify_paid' => TRUE,
];
$devices[] = $newDevice;
$account->devices = json_encode($devices);
$account->save();
2016-03-01 11:31:23 +01:00
return $this->response($newDevice);
2016-03-01 03:57:59 +01:00
}
2016-03-01 04:44:14 +01:00
public function updatePushNotifications(Request $request)
{
$account = Auth::user()->account;
$devices = json_decode($account->devices, TRUE);
2016-03-03 03:28:16 +01:00
if(count($devices) < 1)
2016-03-03 01:24:13 +01:00
return $this->errorResponse(['message'=>'No registered devices.'], 400);
2016-03-01 04:44:14 +01:00
for($x=0; $x<count($devices); $x++)
{
if($devices[$x]['email'] == Auth::user()->username)
{
$newDevice = [
2016-03-03 01:24:13 +01:00
'token' => $devices[$x]['token'],
'email' => $devices[$x]['email'],
'device' => $devices[$x]['device'],
2016-03-03 09:48:33 +01:00
'account_key' => $account->account_key,
2016-03-01 04:44:14 +01:00
'notify_sent' => $request->notify_sent,
'notify_viewed' => $request->notify_viewed,
'notify_approved' => $request->notify_approved,
'notify_paid' => $request->notify_paid,
];
2016-03-03 10:52:47 +01:00
//unset($devices[$x]);
2016-03-03 01:24:13 +01:00
2016-03-03 10:52:47 +01:00
$devices[$x] = $newDevice;
2016-03-01 04:44:14 +01:00
$account->devices = json_encode($devices);
$account->save();
2016-03-01 11:31:23 +01:00
return $this->response($newDevice);
2016-03-01 04:44:14 +01:00
}
}
}
2015-11-01 23:10:20 +01:00
}