Skip to content

Commit

Permalink
Merge pull request #107 from lara-zeus/password-posts
Browse files Browse the repository at this point in the history
add a form to check the password for protected posts and pages
  • Loading branch information
atmonshi authored Jun 18, 2023
2 parents b3b3735 + cfc8ff3 commit cf6db13
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 31 deletions.
4 changes: 3 additions & 1 deletion resources/lang/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,7 @@
"Library Categories": "تصنيف المكتبة",
"Library Type": "نوع المحتوى",
"Library File": "ملف المكتبة",
"file url": "رابط الملف"
"file url": "رابط الملف",
"Send": "ارسال",
"sorry, the password incorrect!": "عفوا، كلمة المرور غير صحيحة"
}
19 changes: 19 additions & 0 deletions resources/views/themes/zeus/partial/password-form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div class="max-w-3xl mx-auto">
<x-filament::card>
<x-filament::form method="post" action="{{ route('passwordConfirmation',[$post]) }}">
@csrf
<div class="flex flex-col gap-4">
<label for="password">{{ __('Password') }}:</label>
<input type="text" name="password" id="password" class="filament-forms-input block w-full mx-auto rounded-lg shadow-sm outline-none transition duration-75 focus:ring-1 focus:ring-inset disabled:opacity-70 dark:bg-gray-700 dark:text-white border-gray-300 focus:border-primary-500 focus:ring-primary-500 dark:border-gray-600 dark:focus:border-primary-500">
@if (session('status'))
<div class="py-1 text-red-600">
{{ session('status') }}
</div>
@endif
</div>
<x-filament::button type="submit">
{{ __('Send') }}
</x-filament::button>
</x-filament::form>
</x-filament::card>
</div>
24 changes: 18 additions & 6 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,29 @@
});
}

Route::prefix(config('zeus-sky.path'))
->post('passwordConfirmation/{slug}', function ($slug) {

$post = config('zeus-sky.models.post')::query()
->where('slug', $slug)
->where('password', request('password'))
->first();

if ($post !== null) {
request()->session()->put($slug . '-' . request('password'), request('password'));

return redirect()->route($post->post_type, ['slug' => $post->slug]);
}

return redirect()->back()->with('status', __('sorry, the password incorrect!'));
})
->name('passwordConfirmation');

Route::prefix(config('zeus-sky.path'))
->middleware(config('zeus-sky.middleware'))
->group(function () {
Route::get('/', Posts::class)->name('blogs');
Route::get(config('zeus-sky.post_uri_prefix') . '/{slug}', Post::class)->name('post');
Route::get(config('zeus-sky.page_uri_prefix') . '/{slug}', Page::class)->name('page');
Route::get('{type}/{slug}', Tags::class)->name('tags');

Route::get('passConf', function () {
session()->put(request('postID') . '-' . request('password'), request('password'));

return redirect()->back()->with('status', 'sorry, password incorrect!');
});
});
35 changes: 23 additions & 12 deletions src/Http/Livewire/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,33 @@ class Page extends Component
public function mount($slug)
{
$this->page = config('zeus-sky.models.post')::where('slug', $slug)->page()->firstOrFail();
}

public function render()
{
$this->setSeo();

if ($this->page->status !== 'publish') {
if ($this->page->status !== 'publish' && ! $this->page->require_password) {
abort_if(! auth()->check(), 404);
abort_if($this->page->user_id !== auth()->user()->id, 401);
}
}

public function render()
{
if (! $this->page->getMedia('pages')->isEmpty()) {
seo()->image($this->page->getFirstMediaUrl('pages'));
if ($this->page->require_password && ! session()->has($this->page->slug . '-' . $this->page->password)) {
return view(app('theme') . '.partial.password-form')
->with('post', $this->page)
->layout(config('zeus-sky.layout'));
}

return view(app('theme') . '.page')
->with([
'post' => $this->page,
'children' => config('zeus-sky.models.post')::with('parent')->where('parent_id', $this->page->id)->get(),
])
->layout(config('zeus-sky.layout'));
}

public function setSeo(): void
{
seo()
->title($this->page->title)
->description(($this->page->description ?? '') . ' ' . config('zeus-sky.site_description', 'Laravel'))
Expand All @@ -33,11 +47,8 @@ public function render()
->withUrl()
->twitter();

return view(app('theme') . '.page')
->with([
'post' => $this->page,
'children' => config('zeus-sky.models.post')::with('parent')->where('parent_id', $this->page->id)->get(),
])
->layout(config('zeus-sky.layout'));
if (! $this->page->getMedia('posts')->isEmpty()) {
seo()->image($this->page->getFirstMediaUrl('posts'));
}
}
}
30 changes: 20 additions & 10 deletions src/Http/Livewire/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,30 @@ class Post extends Component
public function mount($slug)
{
$this->post = config('zeus-sky.models.post')::where('slug', $slug)->firstOrFail();
}

public function render()
{
$this->setSeo();

if ($this->post->status !== 'publish') {
if ($this->post->status !== 'publish' && ! $this->post->require_password) {
abort_if(! auth()->check(), 404);
abort_if($this->post->user_id !== auth()->user()->id, 401);
}
}

public function render()
{
if (! $this->post->getMedia('posts')->isEmpty()) {
seo()->image($this->post->getFirstMediaUrl('posts'));
if ($this->post->require_password && ! session()->has($this->post->slug . '-' . $this->post->password)) {
return view(app('theme') . '.partial.password-form')
->layout(config('zeus-sky.layout'));
}

return view(app('theme') . '.post')
->with('post', $this->post)
->with('related', config('zeus-sky.models.post')::related($this->post)->take(4)->get())
->layout(config('zeus-sky.layout'));
}

public function setSeo(): void
{
seo()
->title($this->post->title)
->description(($this->post->description ?? '') . ' ' . config('zeus-sky.site_description', 'Laravel'))
Expand All @@ -33,9 +44,8 @@ public function render()
->withUrl()
->twitter();

return view(app('theme') . '.post')
->with('post', $this->post)
->with('related', config('zeus-sky.models.post')::related($this->post)->take(4)->get())
->layout(config('zeus-sky.layout'));
if (! $this->post->getMedia('posts')->isEmpty()) {
seo()->image($this->post->getFirstMediaUrl('posts'));
}
}
}
12 changes: 11 additions & 1 deletion src/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LaraZeus\Sky\Models;

use Database\Factories\PostFactory;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
Expand All @@ -20,7 +21,9 @@
* @property string $status
* @property string $slug
* @property string $post_type
* @property string $user_id
* @property int $user_id
* @property bool $require_password
* @property string $password
*/
class Post extends Model implements HasMedia
{
Expand Down Expand Up @@ -93,4 +96,11 @@ public function image()
return $this->featured_image ?? config('zeus-sky.default_featured_image', null);
}
}

protected function requirePassword(): Attribute
{
return Attribute::make(
get: fn () => $this->status === 'private' && $this->password !== null,
);
}
}
1 change: 0 additions & 1 deletion src/Models/PostScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public function scopeRelated(Builder $query, Post $post): void
public function scopePage(Builder $query): void
{
$query->where('post_type', 'page')
->where('status', 'publish')
->whereDate('published_at', '<=', now());
}

Expand Down

0 comments on commit cf6db13

Please sign in to comment.