mirror of
https://github.com/Bubka/2FAuth.git
synced 2024-11-08 01:14:29 +01:00
Account refactored and better json responses
This commit is contained in:
parent
dcf3e6e1a0
commit
6cdaa12e21
@ -4,6 +4,7 @@
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
|
||||
|
||||
class Handler extends ExceptionHandler
|
||||
{
|
||||
@ -46,6 +47,13 @@ public function report(Exception $exception)
|
||||
*/
|
||||
public function render($request, Exception $exception)
|
||||
{
|
||||
if ($exception instanceof ModelNotFoundException)
|
||||
{
|
||||
return response()->json([
|
||||
'message' => 'Resource not found',
|
||||
], 404);
|
||||
}
|
||||
|
||||
return parent::render($request, $exception);
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,10 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Account;
|
||||
use App\TwoFAccount;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AccountController extends Controller
|
||||
class TwoFAccountController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
@ -14,18 +14,9 @@ class AccountController extends Controller
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return response()->json(Account::all()->toArray());
|
||||
return response()->json(TwoFAccount::all()->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
@ -35,52 +26,43 @@ public function create()
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$account = Account::create([
|
||||
$twofaccount = TwoFAccount::create([
|
||||
'name' => $request->name,
|
||||
'secret' => $request->secret
|
||||
]);
|
||||
|
||||
$data = [
|
||||
'data' => $account,
|
||||
'status' => (bool) $account,
|
||||
'message' => $account ? 'Account Created' : 'Error Creating Account',
|
||||
'data' => $twofaccount,
|
||||
'status' => (bool) $twofaccount,
|
||||
'message' => $twofaccount ? 'Account Created' : 'Error Creating Account',
|
||||
];
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param \App\account $account
|
||||
* @param \App\TwoFAccount $twofaccount
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show(account $account)
|
||||
public function show(TwoFAccount $twofaccount)
|
||||
{
|
||||
return response()->json($account);
|
||||
return response()->json($twofaccount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param \App\account $account
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit(account $account)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \App\account $account
|
||||
* @param \App\TwoFAccount $twofaccount
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, account $account)
|
||||
public function update(Request $request, TwoFAccount $twofaccount)
|
||||
{
|
||||
$status = $account->update($request->all());
|
||||
$status = $twofaccount->update($request->all());
|
||||
|
||||
return response()->json([
|
||||
'status' => $status,
|
||||
@ -90,20 +72,16 @@ public function update(Request $request, account $account)
|
||||
//return response()->json($request, 200);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param \App\account $account
|
||||
* @param \App\TwoFAccount $twofaccount
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy(account $account)
|
||||
public function destroy(TwoFAccount $twofaccount)
|
||||
{
|
||||
if($account->deleted_at == null){
|
||||
$status = $account->delete();
|
||||
}
|
||||
else {
|
||||
$status = $account->forceDelete();
|
||||
}
|
||||
$status = $twofaccount->delete();
|
||||
|
||||
return response()->json([
|
||||
'status' => $status,
|
||||
@ -111,4 +89,23 @@ public function destroy(account $account)
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove the specified soft deleted resource from storage.
|
||||
*
|
||||
* @param \App\TwoFAccount $twofaccount
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function forceDestroy($id)
|
||||
{
|
||||
$twofaccount = TwoFAccount::onlyTrashed()->findOrFail($id);
|
||||
|
||||
$status = $twofaccount->forceDelete();
|
||||
|
||||
return response()->json([
|
||||
'status' => $status,
|
||||
'message' => $status ? 'Account Deleted permanently' : 'Error Deleting Account'
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
@ -5,9 +5,17 @@
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Account extends Model
|
||||
class TwoFAccount extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $fillable = ['name', 'secret', 'icon'];
|
||||
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'twofaccounts';
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateAccountsTable extends Migration
|
||||
class CreateTwoFAccountsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
@ -13,7 +13,7 @@ class CreateAccountsTable extends Migration
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('accounts', function (Blueprint $table) {
|
||||
Schema::create('twofaccounts', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name')->unique();
|
||||
$table->string('secret');
|
||||
@ -30,6 +30,6 @@ public function up()
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('accounts');
|
||||
Schema::dropIfExists('twofaccounts');
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
use App\Account;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class AccountsTableSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
Account::create([
|
||||
'name' => 'Firefly',
|
||||
'secret' => '3GB2I2P365J575LS',
|
||||
]);
|
||||
Account::create([
|
||||
'name' => 'Facebook',
|
||||
'secret' => '3GB2I2P365J575LS',
|
||||
]);
|
||||
Account::create([
|
||||
'name' => 'Twitter',
|
||||
'secret' => '3GB2I2P365J575LS',
|
||||
]);
|
||||
Account::create([
|
||||
'name' => 'Google',
|
||||
'secret' => '3GB2I2P365J575LS',
|
||||
]);
|
||||
}
|
||||
}
|
@ -14,7 +14,7 @@ public function run()
|
||||
// $this->call(UsersTableSeeder::class);
|
||||
$this->call([
|
||||
UsersTableSeeder::class,
|
||||
AccountsTableSeeder::class,
|
||||
TwoFAccountsTableSeeder::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
32
database/seeds/TwoFAccountsTableSeeder.php
Normal file
32
database/seeds/TwoFAccountsTableSeeder.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use App\TwoFAccount;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class TwoFAccountsTableSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
TwoFAccount::create([
|
||||
'name' => 'Firefly',
|
||||
'secret' => '3G2I2P36J57LS',
|
||||
]);
|
||||
TwoFAccount::create([
|
||||
'name' => 'Facebook',
|
||||
'secret' => '3GB2I2P365J5S',
|
||||
]);
|
||||
TwoFAccount::create([
|
||||
'name' => 'Twitter',
|
||||
'secret' => '3GB2I25J575LS',
|
||||
]);
|
||||
TwoFAccount::create([
|
||||
'name' => 'Google',
|
||||
'secret' => '3GB2I25J575LS',
|
||||
]);
|
||||
}
|
||||
}
|
@ -21,5 +21,6 @@
|
||||
});
|
||||
|
||||
Route::group(['middleware' => 'auth:api'], function(){
|
||||
Route::resource('/account', 'AccountController');
|
||||
Route::apiResource('twofaccounts', 'TwoFAccountController');
|
||||
Route::delete('twofaccounts/force/{id}', 'TwoFAccountController@forceDestroy')->name('twofaccounts.forceDestroy');
|
||||
});
|
@ -16,4 +16,4 @@
|
||||
});
|
||||
|
||||
|
||||
Route::get('account/{account}', 'AccountController@show');
|
||||
Route::get('twofaccount/{TwoFAccount}', 'TwoFAccountController@show');
|
@ -66,18 +66,18 @@ public function testUserLogin()
|
||||
}
|
||||
|
||||
/**
|
||||
* test Account creation via API
|
||||
* test TwoFAccount creation via API
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAccountCreation()
|
||||
public function testTwoFAccountCreation()
|
||||
{
|
||||
//$this->withoutMiddleware();
|
||||
|
||||
$user = \App\User::find(1);
|
||||
|
||||
$response = $this->actingAs($user, 'api')
|
||||
->json('POST', '/api/account', [
|
||||
->json('POST', '/api/twofaccounts', [
|
||||
'name' => 'testCreation',
|
||||
'secret' => 'test',
|
||||
]);
|
||||
@ -91,16 +91,16 @@ public function testAccountCreation()
|
||||
|
||||
|
||||
/**
|
||||
* test Account index fetching via API
|
||||
* test TwoFAccount index fetching via API
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAccountFetch()
|
||||
public function testTwoFAccountFetch()
|
||||
{
|
||||
$user = \App\User::find(1);
|
||||
|
||||
$response = $this->actingAs($user, 'api')
|
||||
->json('GET', '/api/account')
|
||||
->json('GET', '/api/twofaccounts')
|
||||
->assertStatus(200)->assertJsonStructure([
|
||||
'*' => [
|
||||
'id',
|
||||
@ -117,20 +117,20 @@ public function testAccountFetch()
|
||||
|
||||
|
||||
/**
|
||||
* test Account deletion via API
|
||||
* test TwoFAccount deletion via API
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function testAccountDeletion()
|
||||
public function testTwoFAccountDeletion()
|
||||
{
|
||||
$user = \App\User::find(1);
|
||||
|
||||
$account = \App\Account::create([
|
||||
$twoFAccount = \App\TwoFAccount::create([
|
||||
'name' => 'testDelete',
|
||||
'secret' => 'test'
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user, 'api')
|
||||
->json('DELETE', "/api/account/{$account->id}")
|
||||
->json('DELETE', "/api/twofaccounts/{$twoFAccount->id}")
|
||||
->assertStatus(200)->assertJson([
|
||||
'status' => true,
|
||||
'message' => 'Account Deleted'
|
||||
|
Loading…
Reference in New Issue
Block a user