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')); 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()); return $this->itemResponse($this->guard()->user());
}
else { else {
$this->incrementLoginAttempts($request); $this->incrementLoginAttempts($request);

View File

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

View File

@ -13,7 +13,6 @@ namespace App\Http\Middleware;
use App\Models\User; use App\Models\User;
use Closure; use Closure;
use Illuminate\Support\Facades\Log;
class ApiSecretCheck class ApiSecretCheck
{ {
@ -28,14 +27,12 @@ class ApiSecretCheck
{ {
if( $request->header('X-API-SECRET') && ($request->header('X-API-SECRET') == config('ninja.api_secret')) ) if( $request->header('X-API-SECRET') && ($request->header('X-API-SECRET') == config('ninja.api_secret')) )
{
return $next($request); return $next($request);
}
else { else {
$error['error'] = ['message' => 'Invalid secret']; $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) public function handle(Request $request, Closure $next)
{ {
$start = microtime(true); // $start = microtime(true);
Log::error('start up check'); // Log::error('start up check');
$cached_tables = config('ninja.cached_tables'); $cached_tables = config('ninja.cached_tables');
@ -66,8 +66,8 @@ class StartupCheck
} }
} }
$end = microtime(true) - $start; // $end = microtime(true) - $start;
Log::error("middleware cost = {$end} ms"); // Log::error("middleware cost = {$end} ms");
$response = $next($request); $response = $next($request);

View File

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

View File

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

View File

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

View File

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