Files
Toiminnanohjaus/app/Models/User.php
T
petriandClaude Opus 5.5 8aead06972 Add English to the UI and a profile menu for language and theme
Every UI string now exists in lang/fi and lang/en; a unit test checks that
both languages have the same keys and placeholders. A user's language is
stored on the account (users.locale) and applied by the SetLocale web
middleware, falling back to the session's choice on signed-out pages and
then APP_LOCALE. Emails use the user's language through
HasLocalePreference, and the login page keeps the language after logout.

The header's name, theme toggle and logout button become one profile menu
(a disclosure) with the language buttons, light/dark/system theme radios
kept on the device, and logging out. Signed-out pages get a Suomi/English
switch next to the theme toggle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-25 06:28:39 +02:00

106 lines
3.1 KiB
PHP

<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Modules\Users\Domain\Enums\Locale;
use App\Modules\Users\Domain\Enums\MoneyPermission;
use App\Modules\Users\Domain\Enums\Role;
use App\Modules\Users\Infrastructure\Persistence\Eloquent\UserMoneyPermissionsModel;
use Database\Factories\UserFactory;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
/**
* Laravel's authentication model. It belongs to the Users module's
* Infrastructure layer; other modules use it only through auth() and Gate.
*
* @property int $id
* @property string $name
* @property string $email
* @property Role $role
* @property bool $is_active
* @property Carbon|null $employment_ends_on
* @property int|null $supervisor_id
* @property Locale $locale
* @property-read UserMoneyPermissionsModel|null $moneyPermissions
*/
#[Fillable(['name', 'email', 'password', 'role', 'is_active', 'employment_ends_on', 'supervisor_id', 'locale'])]
#[Hidden(['password', 'remember_token'])]
class User extends Authenticatable implements HasLocalePreference
{
/** @use HasFactory<UserFactory> */
use HasFactory, Notifiable;
/**
* The column defaults, so a user made in this request has them too.
*
* @var array<string, mixed>
*/
protected $attributes = [
'locale' => 'fi',
];
/**
* @return BelongsTo<User, $this>
*/
public function supervisor(): BelongsTo
{
return $this->belongsTo(User::class, 'supervisor_id');
}
/**
* @return HasOne<UserMoneyPermissionsModel, $this>
*/
public function moneyPermissions(): HasOne
{
return $this->hasOne(UserMoneyPermissionsModel::class);
}
/**
* Whether the user may see the given kind of money amounts: admins and
* management always, employees and interns with a personal permission.
*/
public function canSeeMoney(MoneyPermission $permission): bool
{
if ($this->role->seesAllMoney()) {
return true;
}
return $this->role->hasPersonalMoneyPermissions()
&& ($this->moneyPermissions?->allows($permission) ?? false);
}
/**
* The language of the user's emails, such as the set-password link.
*/
public function preferredLocale(): string
{
return $this->locale->value;
}
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'role' => Role::class,
'is_active' => 'boolean',
'employment_ends_on' => 'date',
'locale' => Locale::class,
];
}
}