Skip to content
This repository was archived by the owner on Jul 8, 2024. It is now read-only.

June Release #42

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
29 changes: 27 additions & 2 deletions app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Actions\Compositions\HasAuthAttempt;
use App\Models\BetaCode;
use Laravel\Fortify\Contracts\CreatesNewUsers;

class CreateNewUser implements CreatesNewUsers
Expand Down Expand Up @@ -42,10 +43,21 @@ public function create(array $input): User
'ip_current' => $userIp,
'account_day_of_birth' => strtotime($input['birthday']),
'look' => $input['look'] ?? (getSetting($input['gender'] == 'M' ? 'start_male_look' : 'start_female_look')),
'beta_code' => !! getSetting('beta_period') ? $input['beta_code'] : null,
]), function (User $user) use ($input) {
if(!isset($input['referrer_code'])) return;
if(isset($input['referrer_code'])) {
$this->setReferrer($user, $input['referrer_code']);
}

$this->setReferrer($user, $input['referrer_code']);
if(isset($input['beta_code'])) {
$code = BetaCode::whereCode($input['beta_code'])->whereNull('rescued_at')->first();

if(!$code) return;

$code->update([
'rescued_at' => now()
]);
}
});
});
}
Expand Down Expand Up @@ -92,6 +104,19 @@ private function validateForm(array $input)
$validations['cf-turnstile-response'] = ['required', 'string', new TurnstileCheck];
}

if(!! getSetting('beta_period')) {
$validations['beta_code'] = ['required', 'string', function($attribute, $value, $fail) {
if(! $key = BetaCode::whereCode($value)->whereNull('rescued_at')->first()) {
$fail(__('Beta code not found or already used.'));
return;
}

if($key->valid_at != null && $key->valid_at->lte(now())) {
$fail(__('This beta code has expired.'));
}
}];
}

try {
$gender = config('hotel.cms.register.register_looks')[$input['gender']];

Expand Down
23 changes: 22 additions & 1 deletion app/Actions/Fortify/RedirectIfTwoFactorAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ protected function validateCredentials($request)
$this->throwFailedAuthenticationExceptionDuringMaintenance($request);
}

if(!! getSetting('beta_period') && $user->rank < getSetting('min_rank_to_bypass_beta_period') && (!$user->betaCode || $user->betaCode->valid_at->lte(now()))) {
$this->throwFailedAuthenticationExceptionDuringBetaPeriod($request);
}

$this->validateCaptcha($request->all());

if (!$user->homeItems()->count()) {
Expand All @@ -63,7 +67,24 @@ protected function throwFailedAuthenticationExceptionDuringMaintenance($request)
$this->limiter->increment($request);

throw ValidationException::withMessages([
Fortify::username() => ['Only staffs can login during maintenance.'],
Fortify::username() => [__('Only staffs can login during maintenance.')],
]);
}

/**
* Throw a failed authentication validation exception.
*
* @param \Illuminate\Http\Request $request
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
protected function throwFailedAuthenticationExceptionDuringBetaPeriod($request)
{
$this->limiter->increment($request);

throw ValidationException::withMessages([
Fortify::username() => [__('You need a valid beta code to login.')],
]);
}

Expand Down
1 change: 1 addition & 0 deletions app/Filament/Resources/Orion/ArticleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public static function getRelations(): array
{
return [
RelationManagers\TagsRelationManager::class,
RelationManagers\CommentsRelationManager::class,
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Filament\Resources\Orion\ArticleResource\RelationManagers;

use App\Models\Article\ArticleComment;
use Filament\Forms;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\HtmlString;

class CommentsRelationManager extends RelationManager
{
protected static string $relationship = 'comments';

public function form(Form $form): Form
{
return $form
->schema([
Placeholder::make('content')
->label(__('filament::resources.inputs.content'))
->columnSpanFull()
->extraAttributes(['class' => 'border rounded-lg p-2'])
->content(fn (ArticleComment $record): HtmlString => new HtmlString(renderBBCodeText($record->content, true))),
]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('id')
->columns([
TextColumn::make('id')
->toggleable(),

TextColumn::make('user.username')
->searchable()
->label(__('filament::resources.columns.by')),

ToggleColumn::make('visible')
->label(__('filament::resources.columns.visible')),

ToggleColumn::make('fixed')
->label(__('filament::resources.columns.fixed')),

ToggleColumn::make('innapropriate')
->label(__('filament::resources.columns.innapropriate')),
])
->filters([
//
])
->headerActions([])
->actions([
Tables\Actions\ViewAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
101 changes: 101 additions & 0 deletions app/Filament/Resources/Orion/BetaCodeResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace App\Filament\Resources\Orion;

use Filament\Tables;
use App\Models\BetaCode;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Resources\Resource;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\TextInput;
use App\Filament\Traits\TranslatableResource;
use Filament\Forms\Components\DateTimePicker;
use App\Filament\Resources\Orion\BetaCodeResource\Pages;

class BetaCodeResource extends Resource
{
use TranslatableResource;

protected static ?string $model = BetaCode::class;

protected static ?string $navigationIcon = 'heroicon-o-key';

protected static ?string $navigationGroup = 'Website';

protected static ?string $slug = 'website/beta-codes';

public static string $translateIdentifier = 'beta-codes';

public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('code')
->default(\Str::random(60))
->label(__('filament::resources.inputs.code'))
->unique(ignoreRecord: true)
->required()
->columnSpan('full')
->maxLength(64),

DateTimePicker::make('valid_at')
->label(__('filament::resources.inputs.valid_at'))
->columnSpan('full')
->helperText(__('filament::resources.helpers.beta_code_data_helper'))
]);
}

public static function table(Table $table): Table
{
return $table
->defaultSort('id', 'desc')
->columns([
TextColumn::make('id'),

TextColumn::make('code')
->label(__('filament::resources.columns.code'))
->limit(30)
->searchable(),

TextColumn::make('valid_at')
->date('d/m/Y H:i')
->label(__('filament::resources.columns.valid_at')),

TextColumn::make('rescued_at')
->date('d/m/Y H:i')
->label(__('filament::resources.columns.rescued_at')),

TextColumn::make('user.username')
->searchable()
->formatStateUsing(fn (?string $state): string => $state ?? '-')
->label(__('filament::resources.columns.username')),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\ViewAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ManageBetaCodes::route('/'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\Orion\BetaCodeResource\Pages;

use App\Filament\Resources\Orion\BetaCodeResource;
use Filament\Actions;
use Filament\Resources\Pages\ManageRecords;

class ManageBetaCodes extends ManageRecords
{
protected static string $resource = BetaCodeResource::class;

protected function getActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Filament\Resources\Orion\WriteableBoxResource\Pages;

use App\Filament\Resources\Orion\WriteableBoxResource;
use Filament\Actions;
use Filament\Resources\Pages\ManageRecords;
use App\Filament\Resources\Orion\WriteableBoxResource;

class ManageWriteableBoxes extends ManageRecords
{
Expand Down
23 changes: 18 additions & 5 deletions app/Helpers/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ function getSetting(string $key, ?string $defaultValue = null): mixed
}
}

if(!function_exists('convertTagsToHtml')) {
/**
* Converts tags to HTML.
*/
function convertTagsToHtml(string $tagStart, string $tagEnd, string $htmlTagStart, string $htmlTagEnd, string $content): string
{
$tagStart = preg_quote($tagStart, '/');
$tagEnd = preg_quote($tagEnd, '/');

return preg_replace("/{$tagStart}(.*){$tagEnd}/s", "{$htmlTagStart}$1{$htmlTagEnd}", $content);
};
}

if(!function_exists('renderBBCodeText')) {
/**
* Render BBCode text to HTML.
Expand All @@ -79,11 +92,11 @@ function renderBBCodeText(string $content, bool $reflectLineBreaks = false): str
{
return Pipeline::send($content)
->through([
fn (string $content, \Closure $next) => $next(str_replace(['[b]', '[/b]'], ['<b>', '</b>'], $content)),
fn (string $content, \Closure $next) => $next(str_replace(['[i]', '[/i]'], ['<i>', '</i>'], $content)),
fn (string $content, \Closure $next) => $next(str_replace(['[u]', '[/u]'], ['<u>', '</u>'], $content)),
fn (string $content, \Closure $next) => $next(str_replace(['[s]', '[/s]'], ['<s>', '</s>'], $content)),
fn (string $content, \Closure $next) => $next(str_replace(['[h]', '[/h]'], ['<span class="bbcode-highlighter">', '</span>'], $content)),
fn (string $content, \Closure $next) => $next(convertTagsToHtml('[b]', '[/b]', '<b>', '</b>', $content)),
fn (string $content, \Closure $next) => $next(convertTagsToHtml('[i]', '[/i]', '<i>', '</i>', $content)),
fn (string $content, \Closure $next) => $next(convertTagsToHtml('[u]', '[/u]', '<u>', '</u>', $content)),
fn (string $content, \Closure $next) => $next(convertTagsToHtml('[s]', '[/s]', '<s>', '</s>', $content)),
fn (string $content, \Closure $next) => $next(convertTagsToHtml('[h]', '[/h]', '<span class="bbcode-highlighter">', '</span>', $content)),
])->then(fn (string $content) => $reflectLineBreaks ? nl2br($content) : $content);
}
}
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Controllers/Article/ArticleCommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ArticleCommentController extends Controller
public function store(string $id, string $slug, Request $request): JsonResponse
{
$data = $request->validate([
'content' => 'required|string|min:5'
'content' => 'required|string'
]);

if (!$article = Article::fromIdAndSlug($id, $slug)->first()) {
Expand All @@ -27,6 +27,10 @@ public function store(string $id, string $slug, Request $request): JsonResponse
return $this->jsonResponse(['message' => __('You are commenting too fast')], 422);
}

if(strlen(preg_replace("/\[(\/?).*?\]/", '', $data['content'])) < 5) {
return $this->jsonResponse(['message' => __('Please, type a valid comment.')], 422);
}

$comment = $article->comments()->create([
'content' => PreventXssService::sanitize($data['content']),
'user_id' => $user->id
Expand Down
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ class Kernel extends HttpKernel
'cms.maintenance' => \App\Http\Middleware\RedirectIfMaintenance::class,
'findretros.vote' => \App\Http\Middleware\RedirectIfVoteMissing::class,
'vpn.prevent' => \App\Http\Middleware\VerifyVpnAddresses::class,
'beta.code' => \App\Http\Middleware\RedirectIfBetaCodeMissing::class,
];
}
Loading