Skip to content
This repository has been archived by the owner on Jan 20, 2021. It is now read-only.

Commit

Permalink
Anonymous edit feature (#9)
Browse files Browse the repository at this point in the history
* Remove deprecated middleware
* Add password to Paste entity
* Add new custom rule to determine password match
* Improve paste menu
* Add new FormRequest for update paste
* Add fixed Paste test on PasteTableSeeder
* Add the ability to edit a paste
* Improve tests suite
* Remove dead code
* Add new alert style for callouts
* Add the ability to set a password on paste creation and fork
* Fix a bug that caused crashes when a password was null
* Fix a stupid bug on LanguageFactory causing tests to fail
  • Loading branch information
ludo237 authored Nov 8, 2017
1 parent 70bcd16 commit 1ebdbf5
Show file tree
Hide file tree
Showing 29 changed files with 613 additions and 67 deletions.
17 changes: 16 additions & 1 deletion app/Entities/Paste.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ final class Paste extends Model
protected $table = self::TABLE_NAME;

/** {@inheritdoc} */
protected $fillable = ["paste_id", "language_id", "slug", "name", "extension", "code", "description"];
protected $fillable = ["paste_id", "language_id", "slug", "name", "extension", "code", "description", "password"];

/** {@inheritdoc} */
protected $guarded = ["id"];

/** {@inheritdoc} */
protected $hidden = ["password"];

/** {@inheritdoc} */
protected $casts = [
"paste_id" => "integer",
Expand All @@ -45,6 +48,18 @@ public function getFileNameAttribute() : string
return "{$this->name}.{$this->extension}";
}

/**
* Use built-in function to store the password for
* a paste
*
* @param string|null $value
* @return void
*/
public function setPasswordAttribute($value)
{
$this->attributes["password"] = !is_null($value) ? bcrypt($value) : $value;
}

/**
* Determine if a paste is a fork
* of another one
Expand Down
10 changes: 0 additions & 10 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,4 @@ public function render($request, Exception $exception)
{
return parent::render($request, $exception);
}

/** {@inheritdoc} */
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(["error" => "Unauthenticated."], 401);
}

return redirect()->guest(route("login"));
}
}
23 changes: 23 additions & 0 deletions app/Http/Handlers/Paste/EditPasteHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Wdi\Http\Handlers\Paste;

use Illuminate\Http\Request;
use Illuminate\View\View;
use Wdi\Http\Handlers\Handler;

/**
* Class EditPasteHandler
* @package Wdi\Http\Handlers\Paste
*/
final class EditPasteHandler extends Handler
{
/**
* @param \Illuminate\Http\Request $request
* @return \Illuminate\View\View
*/
public function __invoke(Request $request) : View
{
return view("paste.edit")->with("paste", $request->paste);
}
}
26 changes: 26 additions & 0 deletions app/Http/Handlers/Paste/UpdatePasteHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Wdi\Http\Handlers\Paste;

use Illuminate\Http\RedirectResponse;
use Wdi\Http\Handlers\Handler;
use Wdi\Http\Requests\UpdatePasteRequest;

/**
* Class UpdatePasteHandler
* @package Wdi\Http\Handlers\Paste
*/
final class UpdatePasteHandler extends Handler
{
/**
* @param \Wdi\Http\Requests\UpdatePasteRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function __invoke(UpdatePasteRequest $request) : RedirectResponse
{
$paste = $request->paste;
$paste->update($request->only(["name", "description", "language_id", "extension", "code"]));

return redirect()->route("paste.show", $paste);
}
}
31 changes: 0 additions & 31 deletions app/Http/Middleware/RedirectIfAuthenticated.php

This file was deleted.

13 changes: 10 additions & 3 deletions app/Http/Requests/AddPasteRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@ public function rules() : array
"min:3",
],
"code" => [
"required"
"required",
],
"password" => [
"sometimes",
"confirmed",
],
"language_id" => [
"required",
Rule::exists(Language::TABLE_NAME, "id")
]
Rule::exists(Language::TABLE_NAME, "id"),
],
"extension" => [
"required",
],
];
}
}
54 changes: 54 additions & 0 deletions app/Http/Requests/UpdatePasteRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Wdi\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Wdi\Entities\Language;
use Wdi\Rules\PasswordMatch;

/**
* Class UpdatePasteRequest
* @package Wdi\Http\Requests
*/
final class UpdatePasteRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize() : bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules() : array
{
return [
"name" => [
"required",
"min:3",
],
"code" => [
"required",
],
"password" => [
"required",
new PasswordMatch($this->paste->password),
],
"language_id" => [
"required",
Rule::exists(Language::TABLE_NAME, "id"),
],
"extension" => [
"required",
],
];
}
}
2 changes: 1 addition & 1 deletion app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function boot()
Route::model("language", Language::class);

Route::bind("paste", function ($slug) {
return Paste::select(["id", "language_id", "paste_id", "user_id", "slug", "name", "extension", "code", "description", "created_at", "updated_at"])
return Paste::select(["id", "language_id", "paste_id", "user_id", "slug", "name", "extension", "code", "description", "password", "created_at", "updated_at"])
->where("slug", $slug)->firstOrFail();
});
}
Expand Down
47 changes: 47 additions & 0 deletions app/Rules/PasswordMatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Wdi\Rules;

use Hash;
use Illuminate\Contracts\Validation\Rule;

/**
* Class PasswordMatch
* @package Wdi\Rules
*/
final class PasswordMatch implements Rule
{
/** @var string */
private $password;

/**
* Create a new rule instance.
* @param string $password
*/
public function __construct(string $password)
{
$this->password = $password;
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return Hash::check($value, $this->password);
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return "La password non corrisponde.";
}
}
2 changes: 1 addition & 1 deletion database/factories/LanguageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(\Wdi\Entities\Language::class, function (Faker\Generator $faker) {
return [
"name" => $faker->unique()->word,
"name" => $faker->lexify("???????"),
"extensions" => $faker->randomElements(config("procedural.extensions"), 3),
];
});
6 changes: 6 additions & 0 deletions database/factories/PasteFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@
"code" => $faker->paragraph,
];
});

$factory->state(\Wdi\Entities\Paste::class, "with-password", function (Faker\Generator $faker) {
return [
"password" => $faker->password,
];
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

/**
* Class AddPasswordColumnToPastesTable
*/
final class AddPasswordColumnToPastesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table("pastes", function (Blueprint $table) {
$table->string("password")->nullable()->after("description");
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table("pastes", function (Blueprint $table) {
$table->dropColumn("password");
});
}
}
13 changes: 10 additions & 3 deletions database/seeds/PastesTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ final class PastesTableSeeder extends Seeder
*/
public function run()
{
// Create a password fixed test
factory(\Wdi\Entities\Paste::class)->states("with-password")->create([
"language_id" => \Wdi\Entities\Language::first()->id,
"name" => "DatabaseFactory",
"password" => "foobar",
]);

// Create a bunch of anon pastes
factory(\Wdi\Entities\Paste::class, 100)->create([
factory(\Wdi\Entities\Paste::class, 100)->states("with-password")->create([
"language_id" => \Wdi\Entities\Language::first()->id,
]);

// Create forks of a single paste
factory(\Wdi\Entities\Paste::class, 10)->create([
factory(\Wdi\Entities\Paste::class, 10)->states("with-password")->create([
"paste_id" => \Wdi\Entities\Paste::first()->id,
]);
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"moment": "^2.19.1",
"popper.js": "^1.12.5",
"tether": "^1.4.0",
"vue": "^2.5.2",
"vue": "^2.5.3",
"vuex": "^2.5.0"
},
"devDependencies": {
"cross-env": "^5.1.1",
"laravel-mix": "^1.5.0"
"laravel-mix": "^1.6.1"
}
}
Loading

0 comments on commit 1ebdbf5

Please sign in to comment.