1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

include statics with login response

This commit is contained in:
David Bomba 2019-09-11 10:37:53 +10:00
parent 5dc171128c
commit 42e54d0bd5
8 changed files with 53 additions and 25 deletions

View File

@ -96,8 +96,14 @@ class LoginController extends BaseController
return response()->json(['message' => 'Too many login attempts, you are being throttled'], 401)->header('X-API-VERSION', config('ninja.api_version'));
}
if ($this->attemptLogin($request))
if ($this->attemptLogin($request)) {
$user = $this->guard()->user();
$user->setCompany($user->user_companies->first()->account->default_company);
return $this->itemResponse($this->guard()->user());
}
else {
$this->incrementLoginAttempts($request);

View File

@ -13,6 +13,7 @@ namespace App\Http\Controllers;
use App\Transformers\ArraySerializer;
use App\Transformers\EntityTransformer;
use App\Utils\Statics;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
@ -23,20 +24,20 @@ use League\Fractal\Resource\Item;
use League\Fractal\Serializer\JsonApiSerializer;
/**
*
* Class BaseController
*/
class BaseController extends Controller
{
/**
* Passed from the parent when we need to force
* includes internally rather than externally via
* the REQUEST 'include' variable.
* the $_REQUEST 'include' variable.
*
* @var array
*/
public $forced_includes;
/**
* Passed from the parent when we need to force
* the key of the response object
@ -107,22 +108,28 @@ class BaseController extends Controller
*/
public function notFound()
{
return response()->json([
'message' => '404 | Nothing to see here!'], 404)->header('X-API-VERSION', config('ninja.api_version'));
return response()->json(['message' => '404 | Nothing to see here!'], 404)
->header('X-API-VERSION', config('ninja.api_version'));
}
public function notFoundClient()
{
return abort(404);
}
protected function errorResponse($response, $httpErrorCode = 400)
{
$error['error'] = $response;
$error = json_encode($error, JSON_PRETTY_PRINT);
$headers = self::getApiHeaders();
return response()->make($error, $httpErrorCode, $headers);
}
protected function listResponse($query)
@ -140,6 +147,7 @@ class BaseController extends Controller
$data = $this->createCollection($query, $transformer, $this->entity_type);
return $this->response($data);
}
protected function createCollection($query, $transformer, $entity_type)
@ -163,10 +171,12 @@ class BaseController extends Controller
}
return $this->manager->createData($resource)->toArray();
}
protected function response($response)
{
$index = request()->input('index') ?: $this->forced_index;
if ($index == 'none') {
@ -187,10 +197,12 @@ class BaseController extends Controller
$headers = self::getApiHeaders();
return response()->make($response, 200, $headers);
}
protected function itemResponse($item)
{
$this->buildManager();
$transformer = new $this->entity_transformer(Input::get('serializer'));
@ -198,40 +210,46 @@ class BaseController extends Controller
$data = $this->createItem($item, $transformer, $this->entity_type);
if(request()->include_static)
$data['static'] = Statics::company();
$data['static'] = Statics::company(auth()->user()->getCompany()->getLocale());
return $this->response($data);
}
protected function createItem($data, $transformer, $entity_type)
{
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON) {
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON)
$entity_type = null;
}
$resource = new Item($data, $transformer, $entity_type);
return $this->manager->createData($resource)->toArray();
}
public static function getApiHeaders($count = 0)
{
return [
'Content-Type' => 'application/json',
//'Access-Control-Allow-Origin' => '*',
//'Access-Control-Allow-Methods' => 'GET',
//'Access-Control-Allow-Headers' => 'Origin, Content-Type, Accept, Authorization, X-Requested-With',
//'Access-Control-Allow-Credentials' => 'true',
'X-Total-Count' => $count,
//'X-Total-Count' => $count,
'X-API-VERSION' => config('ninja.api_version'),
//'X-Rate-Limit-Limit' - The number of allowed requests in the current period
//'X-Rate-Limit-Remaining' - The number of remaining requests in the current period
//'X-Rate-Limit-Reset' - The number of seconds left in the current period,
];
}
protected function getRequestIncludes($data)
{
$included = request()->input('include');
$included = explode(',', $included);
@ -245,4 +263,5 @@ class BaseController extends Controller
return $data;
}
}

View File

@ -13,7 +13,6 @@ namespace App\Http\Middleware;
use App\Models\User;
use Closure;
use Illuminate\Support\Facades\Log;
class ApiSecretCheck
{
@ -28,14 +27,12 @@ class ApiSecretCheck
{
if( $request->header('X-API-SECRET') && ($request->header('X-API-SECRET') == config('ninja.api_secret')) )
{
return $next($request);
}
else {
$error['error'] = ['message' => 'Invalid secret'];
return response()->json(json_encode($error, JSON_PRETTY_PRINT) ,403);
return response()->json(json_encode($error, JSON_PRETTY_PRINT) ,403)->header('X-API-VERSION', config('ninja.api_version'));
}

View File

@ -35,8 +35,8 @@ class StartupCheck
*/
public function handle(Request $request, Closure $next)
{
$start = microtime(true);
Log::error('start up check');
// $start = microtime(true);
// Log::error('start up check');
$cached_tables = config('ninja.cached_tables');
@ -66,8 +66,8 @@ class StartupCheck
}
}
$end = microtime(true) - $start;
Log::error("middleware cost = {$end} ms");
// $end = microtime(true) - $start;
// Log::error("middleware cost = {$end} ms");
$response = $next($request);

View File

@ -29,7 +29,7 @@ class CompanyUser extends Pivot
public function account()
{
return $this->hasOne(Account::class);
return $this->belongsTo(Account::class);
}
public function user_pivot()

View File

@ -15,5 +15,5 @@ use Illuminate\Database\Eloquent\Model;
class Industry extends Model
{
//
public $timestamps = false;
}

View File

@ -11,8 +11,9 @@
namespace App\Utils;
use = namespace\Cache;
use Psy\Util\Str;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
/**
* Statics
@ -71,17 +72,22 @@ class Statics
$data = [];
$cached_tables = config('ninja.cached_tables');
foreach ($cached_tables as $name => $class) {
$data[$name] = Cache::get($name);
}
Log::error($data);
if ($locale) {
$data['industries'] = Cache::get('industries')->each(function ($industry) {
$industry->name = ctrans('texts.industry_'.$industry->name);
})->sortBy(function ($industry) {
return $industry->name;
})->values();
$data['countries'] = Cache::get('countries')->each(function ($country) {
$country->name = ctrans('texts.country_'.$country->name);
})->sortBy(function ($country) {
@ -101,10 +107,11 @@ class Statics
})->values();
$data['currencies'] = Cache::get('currencies')->each(function ($currency) {
$currency->name = ctrans('texts.currency_' . \Str::slug($currency->name, '_'));
$currency->name = ctrans('texts.currency_' . Str::slug($currency->name, '_'));
})->sortBy(function ($currency) {
return $currency->name;
})->values();
}
return $data;

View File

@ -74,15 +74,14 @@ class CreateUsersTable extends Migration
Schema::create('industries', function ($table) {
$table->increments('id');
$table->string('name');
$table->timestamps(6);
});
Schema::create('gateways', function ($table) {
$table->increments('id');
$table->timestamps(6);
$table->string('name');
$table->string('provider');
$table->boolean('visible')->default(true);
$table->timestamps();
});
Schema::create('accounts', function ($table) {