Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
Alpha-version of SecretNotes (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
gomzyakov authored May 4, 2022
1 parent 926269f commit 3b390ad
Show file tree
Hide file tree
Showing 38 changed files with 257,110 additions and 138 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ npm-debug.log
yarn-error.log
/.idea
/.vscode
.DS_Store
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
- Вручную готовим VDS
- Задаем секретки в репозитории на GitHub

### Local development

Чтобы собрать фронт (CSS, JS):
sail npm run dev

npm run watch

Чтобы удалить всё ранее установленное:
sail down --rmi all -v

### Prepare VDS

Создаём VDS-виртуалку, например в [NetAngels](https://panel.netangels.ru).
Expand Down
182 changes: 182 additions & 0 deletions app/Http/Controllers/NoteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Note;
use Illuminate\Http\Response;
use Illuminate\Support\Str;
use Hashids\Hashids;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Crypt;

class NoteController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return view('home');
}

/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
return view('new-note');
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return Response
*/
public function store(Request $request)
{

$hashids = new Hashids('', 5);

// TODO To request
$request->validate(
[
'text' => 'string|required|max:20000',
'encrypt_password' => 'string|min:6|max:100|nullable',
],
[
'text.required' => 'Это поле обязательно к заполнению',
'text.string' => 'Текст пуст или произошла ошибка',
'text.max' => 'Ограничение составляет 20 000 символов',
'encrypt_password.string' => 'К сожалению, произошла ошибка',
'encrypt_password.min' => 'Пароль должен быть не менее 6 символов',
'encrypt_password.max' => 'Пароль не может превышать 100 символов'
]
);

if ($request->expiration_date !== 'never') {
switch ($request->expiration_date) {
case '1_hour':
$expiration_date = date_format(now()->addHours(1), 'Y-m-d H:i:s');
break;
case '1_day':
$expiration_date = date_format(now()->addDays(1), 'Y-m-d H:i:s');
break;
case '1_month':
$expiration_date = date_format(now()->addMonths(1), 'Y-m-d H:i:s');
break;
case '1_week':
$expiration_date = date_format(now()->addWeeks(1), 'Y-m-d H:i:s');
break;
}
} else {
$expiration_date = null;
}


$request->encrypt_password ? $password = \Hash::make($request->encrypt_password) : $password = 'none';

$note = Note::create([
'text' => Crypt::encryptString($request->text),
'expiration_date' => $expiration_date,
'password' => $password,
'slug' => '',
]);

$note->slug = $hashids->encode($note->id);
$note->save();


return back()->with(['success' => route('note.display', $note->slug)]);
}

/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($slug)
{
$note = Note::where('slug', $slug)->first();

if ($note->expiration_date ?? '' && $note->expiration_date < now()) {
$note->delete();
return view('password-note');
}

$note->password ?? '' === "none" ? $password = false : $password = true;

return view('password-note', compact('note', 'password'));
}


public function decrypt($slug)
{
request()->validate([
'decrypt_password' => 'string|max:100'
], [
'decrypt_password.string' => 'Le champ est vide ou une erreur est survenue',
'decrypt_password.max' => 'Le mot de passe ne peut pas faire plus de 100 caractères'
]);

$note = Note::where('slug', $slug)->firstOr(
function () {
return back()->withErrors(['404' => 'Désolé cette note n\'existe pas ou elle a déjà été lue']);
}
);

if ($note->password !== "none") {
if (Hash::check(request()->decrypt_password, $note->password)) {
$note->text = Crypt::decryptString($note->text);
} else {
return back()->withErrors(['bad_password' => 'Mot de passe incorrect']);
}
} else {
$note->text = Crypt::decryptString($note->text);
}

$note->delete();

return view('show-note', compact('note'));
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}
13 changes: 13 additions & 0 deletions app/Models/Note.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Note extends Model
{
use HasFactory;

protected $fillable = ['text', 'slug', 'expiration_date', 'password'];
}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"ext-bcmath": "*",
"php": "^8.0.2",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.2",
"laravel/sanctum": "^2.14.1",
"laravel/tinker": "^2.7"
"laravel/tinker": "^2.7",
"hashids/hashids": "^4.1"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
Expand Down
73 changes: 72 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions database/migrations/2022_04_30_000000_create_notes_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

class CreateNotesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('notes', function (Blueprint $table) {
$table->id();
$table->text('text');
$table->timestamp('expiration_date')->nullable();
$table->string('password');
$table->string('slug')->unique();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notes');
}
}
Loading

0 comments on commit 3b390ad

Please sign in to comment.