Compare commits

...

3 commits
2.x ... 2.6.0

Author SHA1 Message Date
Kode
43f9b76ff3 Merge branch '2.x' into 2.5.0 2022-03-18 16:42:02 +00:00
Kode
ffcfef8d1c Merge branch '2.x' into 2.5.0 2022-03-18 16:36:30 +00:00
Kode
03edaea99a Initial start of adding an API 2022-03-18 16:33:46 +00:00
10 changed files with 170 additions and 4 deletions

View 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)
{
//
}
}

View file

@ -8,6 +8,7 @@ use App\SettingGroup;
use App\User; use App\User;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Support\Str;
class SettingsController extends Controller class SettingsController extends Controller
{ {
@ -77,7 +78,8 @@ class SettingsController extends Controller
$path = $request->file('value')->store('backgrounds'); $path = $request->file('value')->store('backgrounds');
$setting_value = $path; $setting_value = $path;
} }
} elseif ($setting->type == 'apikey') {
$setting_value = Str::random(40);
} else { } else {
$setting_value = $data->value; $setting_value = $data->value;
} }

View file

@ -58,5 +58,6 @@ class Kernel extends HttpKernel
'can' => \Illuminate\Auth\Middleware\Authorize::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'apikey' => \App\Http\Middleware\UserApiKey::class,
]; ];
} }

View 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);
}
}

View file

@ -134,7 +134,15 @@ class Setting extends Model
$value = Form::select('value', $options, null, ['class' => 'form-control']); $value = Form::select('value', $options, null, ['class' => 'form-control']);
break; break;
case 'textarea': 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; break;
default: default:
$value = Form::text('value', null, ['class' => 'form-control']); $value = Form::text('value', null, ['class' => 'form-control']);

View file

@ -14,7 +14,7 @@ return [
*/ */
'name' => env('APP_NAME', 'Heimdall'), 'name' => env('APP_NAME', 'Heimdall'),
'version' => '2.4.4', 'version' => '2.5.0-beta1',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

View file

@ -237,6 +237,22 @@ class SettingsSeeder extends Seeder
$setting->save(); $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)) { if(!$home_tag = \App\Item::find(0)) {
$home_tag = new \App\Item; $home_tag = new \App\Item;
$home_tag->id = 0; $home_tag->id = 0;

View file

@ -39,6 +39,9 @@ return [
'settings.custom_css' => 'Custom CSS', 'settings.custom_css' => 'Custom CSS',
'settings.custom_js' => 'Custom JavaScript', '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.none' => '- not set -',
'options.google' => 'Google', 'options.google' => 'Google',
'options.ddg' => 'DuckDuckGo', 'options.ddg' => 'DuckDuckGo',

View file

@ -13,7 +13,7 @@
{!! Form::select('supported', \App\Item::supportedOptions(), array('placeholder' => 'Title','class' => 'form-control')) !!} {!! Form::select('supported', \App\Item::supportedOptions(), array('placeholder' => 'Title','class' => 'form-control')) !!}
</div>*/ ?> </div>*/ ?>
<div class="input"> <div class="input" style="width: 100%">
{!! $setting->edit_value !!} {!! $setting->edit_value !!}
</div> </div>

View file

@ -13,3 +13,4 @@ use Illuminate\Http\Request;
| |
*/ */
Route::resource('items', 'ApiItemController');