From 42e54d0bd5368de0cc2d0f46d3a0b9d3e736fd54 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 11 Sep 2019 10:37:53 +1000 Subject: [PATCH] include statics with login response --- app/Http/Controllers/Auth/LoginController.php | 8 +++- app/Http/Controllers/BaseController.php | 37 ++++++++++++++----- app/Http/Middleware/ApiSecretCheck.php | 5 +-- app/Http/Middleware/StartupCheck.php | 8 ++-- app/Models/CompanyUser.php | 2 +- app/Models/Industry.php | 2 +- app/Utils/Statics.php | 13 +++++-- .../2014_10_13_000000_create_users_table.php | 3 +- 8 files changed, 53 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 6e3ff52c10..c498825d29 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -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); diff --git a/app/Http/Controllers/BaseController.php b/app/Http/Controllers/BaseController.php index 26ea3b9546..775b1aa057 100644 --- a/app/Http/Controllers/BaseController.php +++ b/app/Http/Controllers/BaseController.php @@ -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; } + } \ No newline at end of file diff --git a/app/Http/Middleware/ApiSecretCheck.php b/app/Http/Middleware/ApiSecretCheck.php index e67c4c1a93..9f47e25175 100644 --- a/app/Http/Middleware/ApiSecretCheck.php +++ b/app/Http/Middleware/ApiSecretCheck.php @@ -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')); } diff --git a/app/Http/Middleware/StartupCheck.php b/app/Http/Middleware/StartupCheck.php index 39557dd1e9..3a356bad85 100644 --- a/app/Http/Middleware/StartupCheck.php +++ b/app/Http/Middleware/StartupCheck.php @@ -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); diff --git a/app/Models/CompanyUser.php b/app/Models/CompanyUser.php index f81062e50e..546890ab8f 100644 --- a/app/Models/CompanyUser.php +++ b/app/Models/CompanyUser.php @@ -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() diff --git a/app/Models/Industry.php b/app/Models/Industry.php index 05ab36f1b9..f1b42949b9 100644 --- a/app/Models/Industry.php +++ b/app/Models/Industry.php @@ -15,5 +15,5 @@ use Illuminate\Database\Eloquent\Model; class Industry extends Model { - // + public $timestamps = false; } diff --git a/app/Utils/Statics.php b/app/Utils/Statics.php index 581f9f7def..69ee00d4ea 100644 --- a/app/Utils/Statics.php +++ b/app/Utils/Statics.php @@ -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; diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index 4cfbe789c7..6aba055557 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -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) {