Skip to content

Commit

Permalink
Merge pull request #315 from semanadeinformatica/feature/logging
Browse files Browse the repository at this point in the history
Started adding logs to application
  • Loading branch information
Naapperas authored Oct 31, 2023
2 parents f4331b3 + 98dca8b commit 3472f3a
Show file tree
Hide file tree
Showing 23 changed files with 220 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://meilisearch:7700

MEILISEARCH_NO_ANALYTICS=false

LOG_CHANNEL="sinfWebsite"
9 changes: 7 additions & 2 deletions app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Participant;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Jetstream\Jetstream;
Expand All @@ -27,17 +28,21 @@ public function create(array $input): User
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '',
])->validate();

$user = User::create([
$data = [
'name' => $input['name'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
'usertype_id' => '0',
'usertype_type' => Participant::class,
]);
];

$user = User::create($data);
$participant = Participant::create(['user_id' => $user->id]);
$user->usertype()->associate($participant);
$user->save();

Log::info('Created user with input: {input}', ['input' => $user->toArray()]);

return $user;
}
}
3 changes: 3 additions & 0 deletions app/Actions/Fortify/ResetUserPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\ResetsUserPasswords;

Expand All @@ -22,6 +23,8 @@ public function reset(User $user, array $input): void
'password' => $this->passwordRules(),
])->validate();

Log::info('Resetting password for {user}', ['user' => $user->name]);

$user->forceFill([
'password' => Hash::make($input['password']),
])->save();
Expand Down
3 changes: 3 additions & 0 deletions app/Actions/Fortify/UpdateUserPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\UpdatesUserPasswords;

Expand All @@ -25,6 +26,8 @@ public function update(User $user, array $input): void
'current_password.current_password' => __('The provided password does not match your current password.'),
])->validateWithBag('updatePassword');

Log::info('Updating password for {user}', ['user' => $user->name]);

$user->forceFill([
'password' => Hash::make($input['password']),
])->save();
Expand Down
3 changes: 3 additions & 0 deletions app/Actions/Fortify/UpdateUserProfileInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Speaker;
use App\Models\User;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\File;
Expand Down Expand Up @@ -42,6 +43,8 @@ public function update(User $user, array $input): void
'website' => 'sometimes|nullable|string|url:https',
])->validateWithBag('updateProfileInformation');

Log::info('Updating user profile information for {user}', ['user' => $user->name]);

if (isset($input['photo'])) {
$user->updateProfilePhoto($input['photo']);
}
Expand Down
4 changes: 4 additions & 0 deletions app/Actions/Jetstream/DeleteUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Actions\Jetstream;

use App\Models\User;
use Illuminate\Support\Facades\Log;
use Laravel\Jetstream\Contracts\DeletesUsers;

class DeleteUser implements DeletesUsers
Expand All @@ -12,6 +13,9 @@ class DeleteUser implements DeletesUsers
*/
public function delete(User $user): void
{

Log::alert('Deleting account for {user_name}', ['user_name' => $user->name]);

if ($user->isParticipant()) {
$user->usertype->deleteCv();
}
Expand Down
6 changes: 6 additions & 0 deletions app/Console/Commands/CreateScoutIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;

class CreateScoutIndexes extends Command
{
Expand All @@ -26,12 +27,16 @@ class CreateScoutIndexes extends Command
*/
public function handle()
{
Log::info('Creating Meilisearch Scout indices');
$this->call('scout:delete-all-indexes');

collect(File::allFiles(app_path('Models')))
->map(fn ($item) => 'App\\Models\\'.$item->getBasename('.php'))
->filter(fn ($item) => in_array(\Laravel\Scout\Searchable::class, class_uses($item)))
->each(function ($item) {

Log::info('Creating index for "{model}"', ['model' => $item]);

$this->call('scout:index', [
'name' => (new $item())->searchableAs(),
]);
Expand All @@ -42,5 +47,6 @@ public function handle()
});

$this->call('scout:sync-index-settings');
Log::info('Indices created');
}
}
5 changes: 5 additions & 0 deletions app/Console/Commands/SendMailToUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -99,6 +100,9 @@ public function handle()
$test = $this->option('test') ?? self::DEFAULT_TEST_FIRST;

if ($test) {

Log::info('Sending e-mail message to admins first to check if message is correct');

$admins = User::where('usertype_type', Admin::class)->get();

$this->info('Sending mail to all admins before sending to participants!');
Expand All @@ -115,6 +119,7 @@ public function handle()

$this->info("Sending mail to {$users->count()} users!");

Log::info('Sending e-mail message to {count} users of types: {types}', ['count' => $users->count(), 'types' => collect($types)->map(fn ($type) => end(explode('\\', $type)))->join(', ')]);
$users->each(fn ($user, $i) => Mail::to($user)->later($this->delayFactor($i), new UserNotification($subject, $text)));

$this->info('Sent mail to users!');
Expand Down
3 changes: 3 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function render($request, Throwable $e)
->toResponse($request)
->setStatusCode($response->status());
} elseif ($response->status() === 503) {

Log::info('Rendering "Coming soon..." page');

return Inertia::render('Maintenance')
->toResponse($request)
->setStatusCode($response->status());
Expand Down
15 changes: 15 additions & 0 deletions app/Http/Controllers/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;
use Nette\Utils\Json;

/**
* @template T
Expand Down Expand Up @@ -84,6 +86,8 @@ public function index(Request $request)

$with = $this->with();

Log::info('Displaying all {model} records with query: {search}', ['model' => $this->model, 'search' => $search ?? 'none']);

return Inertia::render("CRUD/{$this->view}/Index", [
'items' => $items,
'with' => $with,
Expand All @@ -97,6 +101,8 @@ public function show($id)

$with = $this->with();

Log::info('Displaying {model} record with id: {id}', ['model' => $this->model, 'id' => $id]);

return Inertia::render("CRUD/$this->view/Show", [
'item' => $item,
'with' => $with,
Expand Down Expand Up @@ -145,6 +151,9 @@ public function store(Request $request)
$newValues = $this->created($validated);

if ($newValues !== null) {

Log::info('Creating new {model} record with values: {values}', ['model' => $this->model, 'values' => Json::encode($newValues, true)]);

$this->model::create($newValues);
}

Expand Down Expand Up @@ -174,6 +183,9 @@ public function update(Request $request, $id)
$newValues = $this->updated($model, $validated);

if ($newValues !== null) {

Log::info('Updating {model} record with id {id} with values: {values}', ['model' => $this->model, 'id' => $model->id, 'values' => Json::encode($newValues, true)]);

$model->update($newValues);
}

Expand All @@ -197,6 +209,9 @@ public function destroy($id)
$model = $this->model::find($id);

if ($this->destroyed($model->toArray())) {

Log::alert('Deleting {model} record with id {id}', ['model' => $this->model, 'id' => $model->id]);

$model->delete();
}

Expand Down
5 changes: 5 additions & 0 deletions app/Http/Controllers/EnrollmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;

class EnrollmentController extends Controller
{
Expand All @@ -25,6 +26,10 @@ public function store(Request $request)
$user->usertype->enrollments()->create([
'edition_id' => $edition->id,
]);
Log::info('User {user} enrolled in edition {edition}', [
'user' => $user->name,
'edition' => $edition->name,
]);

return redirect()->route('profile.show')->banner('Inscrição realizada com sucesso!');
}
Expand Down
41 changes: 35 additions & 6 deletions app/Http/Controllers/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;

class EventController extends Controller
Expand Down Expand Up @@ -55,26 +56,45 @@ public function show(Request $request, Event $event)
*/
public function join(Request $request, Event $event)
{
$user = $request->user();

// FIXME: when attempting to join multiple times in a row, only the first attempt triggers the banner.
if ($user->cannot('join', $event)) {
return redirect()->back()->dangerBanner('Não podes inscrever-te neste evento');
}

$edition = $request->input('edition');

if ($edition === null) {
return response('No edition found', 500);
}

/** @var User|null */
$user = $request->user();

if ($user === null) {
return redirect()->back()->dangerBanner('Inicia sessão para fazer esta ação');
}

if ($user->cannot('join', $event)) {
Log::alert('User {user} attempted to join event "{event}" but was denied', [
'user' => $user->name,
'event' => $event->name,
]);

return redirect()->back()->dangerBanner('Não podes inscrever-te neste evento');
}

$currentEnrollment = $user->usertype->enrollments()->where('edition_id', $edition->id)->first(); // we can safely get only the first one because there should only be one.

if ($currentEnrollment === null) {
Log::alert('User {user} attempted to join event "{event}" while not enrolled in the current edition', [
'user' => $user->name,
'event' => $event->name,
]);

return redirect()->route('home')->dangerBanner('Não estás inscrito nesta edição!');
}

$currentEnrollment->events()->attach($event);
Log::info('User {user} joined event {event}', [
'user' => $user->name,
'event' => $event->name,
]);

return redirect()->route('profile.show')->banner('Inscrição realizada com sucesso!');
}
Expand All @@ -95,10 +115,19 @@ public function leave(Request $request, Event $event)
$currentEnrollment = $user->usertype->enrollments()->where('edition_id', $edition->id)->first(); // we can safely get only the first one because there should only be one.

if ($currentEnrollment === null) {
Log::alert('User {user} attempted to leave event "{event}" while not enrolled in the current edition', [
'user' => $user->name,
'event' => $event->name,
]);

return redirect()->route('home')->dangerBanner('Não estás inscrito nesta edição!');
}

$currentEnrollment->events()->detach($event);
Log::info('User {user} left event "{event}"', [
'user' => $user->name,
'event' => $event->name,
]);

return redirect()->route('profile.show')->banner('Inscrição cancelada com sucesso!');
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/ProgramController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function show(Request $request)
$eventDay = $edition->event_days()->orderBy('date', 'ASC')->skip($queryDay - 1)->first();

// FIXME: we need to do this since 'Model::load' and 'Model::with' work on the table, not on a specific model.
$eventDay->competitions;
$eventDay?->competitions;

return Inertia::render('Program', [
'eventDay' => fn () => $eventDay?->load([
Expand Down
23 changes: 21 additions & 2 deletions app/Http/Controllers/QuestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use App\Models\Participant;
use App\Models\Quest;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;

class QuestController extends Controller
{
Expand All @@ -23,11 +24,29 @@ public function give(Request $request, Quest $quest)
return redirect()->back()->dangerBanner('Participante não inscrito nesta edição!');
}

if (Gate::denies('give', [$quest, $enrollment])) {
/** @var User|null */
$user = $request->user();

if ($user === null) {
Log::warning('Unauthenticated user attempted to give quest to participant');

return redirect()->back()->dangerBanner('Inicia sessão para efetuar esta ação');
}

if ($user->cannot('give', [$quest, $enrollment])) {
Log::warning('Current user is not allowed to give quest "{quest}" to user {user}', [
'quest' => $quest->name,
'user' => $enrollment->participant->usertype->name,
]);

return redirect()->back()->dangerBanner('Não foi possível atribuir a tarefa ao participante!');
}

$enrollment->quests()->attach($quest);
Log::info('Quest {quest} given to user {user}', [
'quest' => $quest->name,
'user' => $enrollment->participant->usertype->name,
]);

return redirect()->back()->banner('Tarefa atribuída com sucesso!');
}
Expand Down
Loading

0 comments on commit 3472f3a

Please sign in to comment.