Initial start of adding an API
This commit is contained in:
parent
a868a6cac1
commit
03edaea99a
10 changed files with 170 additions and 4 deletions
105
app/Http/Controllers/ApiItemController.php
Normal file
105
app/Http/Controllers/ApiItemController.php
Normal file
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Item;
|
||||
use App\SettingUser;
|
||||
use App\User;
|
||||
|
||||
class ApiItemController extends Controller
|
||||
{
|
||||
protected $api_key;
|
||||
protected $user;
|
||||
|
||||
function __construct(Request $request) {
|
||||
$this->middleware('apikey');
|
||||
|
||||
$key = $request->input('api_key');
|
||||
if ($key) {
|
||||
$details = SettingUser::where('setting_id', 12)->where('uservalue', $key)->first();
|
||||
$this->api_key = $key;
|
||||
$this->user = User::find($details->user_id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return $this->user->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->merge([
|
||||
'user_id' => $this->user->id
|
||||
]);
|
||||
// die(print_r($request->all()));
|
||||
Item::create($request->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ use App\SettingGroup;
|
|||
use App\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class SettingsController extends Controller
|
||||
{
|
||||
|
@ -77,7 +78,8 @@ class SettingsController extends Controller
|
|||
$path = $request->file('value')->store('backgrounds');
|
||||
$setting_value = $path;
|
||||
}
|
||||
|
||||
} elseif ($setting->type == 'apikey') {
|
||||
$setting_value = Str::random(40);
|
||||
} else {
|
||||
$setting_value = $data->value;
|
||||
}
|
||||
|
|
|
@ -58,5 +58,6 @@ class Kernel extends HttpKernel
|
|||
'can' => \Illuminate\Auth\Middleware\Authorize::class,
|
||||
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
|
||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||
'apikey' => \App\Http\Middleware\UserApiKey::class,
|
||||
];
|
||||
}
|
||||
|
|
30
app/Http/Middleware/UserApiKey.php
Normal file
30
app/Http/Middleware/UserApiKey.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use \App\SettingUser;
|
||||
|
||||
class UserApiKey
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
$key = $request->input('api_key');
|
||||
$details = SettingUser::where('setting_id', 12)->where('uservalue', $key)->first();
|
||||
// die(var_dump($details));
|
||||
if($details === null) {
|
||||
return response()->json([
|
||||
'status' => 401,
|
||||
'message' => 'invalid api key'
|
||||
], 401);
|
||||
}
|
||||
return $next($request);
|
||||
}
|
||||
}
|
|
@ -134,7 +134,15 @@ class Setting extends Model
|
|||
$value = Form::select('value', $options, null, ['class' => 'form-control']);
|
||||
break;
|
||||
case 'textarea':
|
||||
$value = Form::textarea('value', null, ['class' => 'form-control', 'cols' => '44', 'rows' => '15']);
|
||||
$value = Form::textarea('value', null, ['class' => 'form-control', 'cols' => '44', 'rows' => '15', 'style' => 'width: 100%;']);
|
||||
break;
|
||||
case 'apikey':
|
||||
if (isset($this->value) && !empty($this->value)) {
|
||||
$value = Form::text('value', null, ['class' => 'form-control']);
|
||||
} else {
|
||||
$value = '<div>'.$current.'</div>';
|
||||
}
|
||||
$value .= '<small style="margin-top: 10px; display: block">'.__('app.settings.click_generate').'</small>';
|
||||
break;
|
||||
default:
|
||||
$value = Form::text('value', null, ['class' => 'form-control']);
|
||||
|
|
|
@ -14,7 +14,7 @@ return [
|
|||
*/
|
||||
|
||||
'name' => env('APP_NAME', 'Heimdall'),
|
||||
'version' => '2.4.3',
|
||||
'version' => '2.5.0-beta1',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
|
@ -237,6 +237,22 @@ class SettingsSeeder extends Seeder
|
|||
$setting->save();
|
||||
}
|
||||
|
||||
if(!$setting = Setting::find(12)) {
|
||||
$setting = new Setting;
|
||||
$setting->id = 12;
|
||||
$setting->group_id = 1;
|
||||
$setting->key = 'api_key';
|
||||
$setting->type = 'apikey';
|
||||
$setting->label = 'app.settings.apikey';
|
||||
$setting->value = '';
|
||||
$setting->save();
|
||||
} else {
|
||||
$setting->type = 'apikey';
|
||||
$setting->group_id = 1;
|
||||
$setting->label = 'app.settings.apikey';
|
||||
$setting->save();
|
||||
}
|
||||
|
||||
if(!$home_tag = \App\Item::find(0)) {
|
||||
$home_tag = new \App\Item;
|
||||
$home_tag->id = 0;
|
||||
|
|
|
@ -39,6 +39,9 @@ return [
|
|||
'settings.custom_css' => 'Custom CSS',
|
||||
'settings.custom_js' => 'Custom JavaScript',
|
||||
|
||||
'settings.apikey' => 'API Key',
|
||||
'settings.click_generate' => 'Clicking the save button will generate a new API key.',
|
||||
|
||||
'options.none' => '- not set -',
|
||||
'options.google' => 'Google',
|
||||
'options.ddg' => 'DuckDuckGo',
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
{!! Form::select('supported', \App\Item::supportedOptions(), array('placeholder' => 'Title','class' => 'form-control')) !!}
|
||||
</div>*/ ?>
|
||||
|
||||
<div class="input">
|
||||
<div class="input" style="width: 100%">
|
||||
{!! $setting->edit_value !!}
|
||||
</div>
|
||||
|
||||
|
|
|
@ -13,3 +13,4 @@ use Illuminate\Http\Request;
|
|||
|
|
||||
*/
|
||||
|
||||
Route::resource('items', 'ApiItemController');
|
Loading…
Reference in a new issue