Skip to content

Commit

Permalink
[update] enable flag submission
Browse files Browse the repository at this point in the history
  • Loading branch information
ryotosaito committed May 15, 2018
1 parent a583f6a commit 67f582f
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 4 deletions.
56 changes: 56 additions & 0 deletions app/Http/Controllers/ChallengeController.php
@@ -0,0 +1,56 @@
<?php

namespace App\Http\Controllers;

use App\Challenge;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

class ChallengeController extends Controller
{
/**
* @param $id integer
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show($id)
{
$challenge = Challenge::find($id);
if (!$challenge)
{
abort(404);
}
return view('challenge', ['challenge' => $challenge]);
}

/**
* @param Request $request
* @param $id integer
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function submit(Request $request, $id)
{
$challenge = Challenge::find($id);
if (!$challenge)
{
abort(404);
return;
}
if (!$request)
{
return redirect(route('home'))->with('session', 'a');
}
if ($request->input('flag') !== $challenge->flag)
{
return redirect(route('challenge', ['id' => $id]))->with('error', 'Invalid flag...');
}
$user = User::find(Auth::id());
if (count($user->solves()->where('id', '=', $id)->get()) === 0)
{
$user->solves()->attach($challenge);
$user->save();
}
return redirect(route('home'))->with('status', 'Correct!');
}
}
2 changes: 1 addition & 1 deletion app/User.php
Expand Up @@ -28,6 +28,6 @@ class User extends Authenticatable
];

public function solves() {
return $this->hasMany('challenges');
return $this->belongsToMany('App\Challenge');
}
}
Expand Up @@ -19,6 +19,8 @@ public function up()
$table->unsignedInteger('number');
$table->string('title');
$table->text('description');
$table->string('url');
$table->string('flag');
$table->timestamps();

$table->unique(['category', 'number'], 'unique_category_number');
Expand Down
Expand Up @@ -20,6 +20,8 @@ public function up()

$table->foreign('challenge_id')->references('id')->on('challenges')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

$table->unique(['challenge_id', 'user_id']);
});
}

Expand Down
2 changes: 1 addition & 1 deletion resources/views/auth/login.blade.php
Expand Up @@ -15,7 +15,7 @@
<label for="name" class="col-sm-4 col-form-label text-md-right">{{ __('User Name') }}</label>

<div class="col-md-6">
<input id="name" type="name" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name') }}" required autofocus>
<input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name') }}" required autofocus>

@if ($errors->has('name'))
<span class="invalid-feedback">
Expand Down
34 changes: 34 additions & 0 deletions resources/views/challenge.blade.php
@@ -0,0 +1,34 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
@if (session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
@endif
<div class="card">
<div class="card-header">Tutorials</div>
<div class="card-body">
<h3>{{ $challenge->title }}</h3>
<p>{{ $challenge->description }}</p>
@if ($challenge->url)
<p><a href="{{ $challenge->url }}">{{ $challenge->url }}</a></p>
@endif
<form action="{{ route('submit', ['id' => $challenge->id]) }}" method="post">
{{ csrf_field() }}
<div class="form-group">
<input type="text" id="flag" class="form-control" name="flag" placeholder="Flag: m1z0r3{this_is_flag_format}">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
4 changes: 2 additions & 2 deletions resources/views/home.blade.php
Expand Up @@ -15,7 +15,7 @@
<div class="row">
@foreach ($tutorials as $tutorial)
<div class="col-sm-3 mb-2">
<a href="#" class="card text-center @if($tutorial->is_solved_by(Auth::id())) text-white bg-success @endif">
<a href="{{ route('challenge', ['id' => $tutorial->id]) }}" class="card text-center @if($tutorial->is_solved_by(Auth::id())) text-white bg-success @endif">
<div class="card-body">Stage {{ $tutorial->number }}</div>
</a>
</div>
Expand All @@ -29,7 +29,7 @@
<div class="row">
@foreach ($challenges as $challenge)
<div class="col-sm-3 mb-2">
<a href="#" class="card text-center @if($challenge->is_solved_by(Auth::id())) text-white bg-success @endif">
<a href="{{ route('challenge', ['id' => $challenge->id]) }}" class="card text-center @if($challenge->is_solved_by(Auth::id())) text-white bg-success @endif">
<div class="card-body">Stage {{ $challenge->number }}</div>
</a>
</div>
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Expand Up @@ -15,4 +15,6 @@

Route::middleware('auth')->group(function (){
Route::get('/', 'HomeController@index')->name('home');
Route::get('/challenge/{id}', 'ChallengeController@show')->name('challenge');
Route::post('/challenge/{id}', 'ChallengeController@submit')->name('submit');
});

0 comments on commit 67f582f

Please sign in to comment.