toby/app/Eloquent/Observers/UserObserver.php
Adrian Hopek d8ac2bd61f
#84 - fix remembering user (#89)
* #84 - fix remembering user

* #84 - ecs fix

* #84 - fix

* #84 - fix test

* #84 - wip

* #84 - add comment to observer

* #84 - cr fix

* Apply suggestions from code review

Co-authored-by: Krzysztof Rewak <krzysztof.rewak@blumilk.pl>

* #84 - cr fix

Co-authored-by: Krzysztof Rewak <krzysztof.rewak@blumilk.pl>
2022-03-21 14:36:18 +01:00

38 lines
953 B
PHP

<?php
declare(strict_types=1);
namespace Toby\Eloquent\Observers;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Support\Str;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\YearPeriod;
class UserObserver
{
public function __construct(
protected Hasher $hash,
) {}
public function creating(User $user): void
{
/**
* A random password for user is generated because AuthenticateSession middleware needs a user's password
* for some checks. Users use Google to login, so they don't need to know the password (GitHub issue #84)
*/
$user->password = $this->hash->make(Str::random(40));
}
public function created(User $user): void
{
$yearPeriods = YearPeriod::all();
foreach ($yearPeriods as $yearPeriod) {
$user->vacationLimits()->create([
"year_period_id" => $yearPeriod->id,
]);
}
}
}