Skip to content

Commit

Permalink
wip (#272)
Browse files Browse the repository at this point in the history
* wip

* fix: disable email sending in seeder (#273)

* fix conflicts

* fix conflicts

* Fix set password for admin ngo

* Set admin email fix

* Fix emailing

---------

Co-authored-by: Andrei Ioniță <[email protected]>
  • Loading branch information
gheorghelupu17 and andreiio authored Aug 29, 2023
1 parent 4667896 commit 1290e3e
Show file tree
Hide file tree
Showing 37 changed files with 1,348 additions and 63 deletions.
51 changes: 51 additions & 0 deletions app/Concerns/MustSetInitialPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

use App\Enums\UserRole;
use App\Notifications\Admin\WelcomeNotification as AdminWelcomeNotification;
use App\Notifications\Ngo\WelcomeNotification;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

trait MustSetInitialPassword
{
protected static function bootMustSetInitialPassword(): void
{
static::creating(function (self $user) {
if (! $user->password) {
$user->password = Hash::make(Str::random(128));
}
});

static::created(function (self $user) {
if (! app()->runningInConsole()) {
$user->sendWelcomeNotification();
}
});
}

public function hasSetPassword(): bool
{
return ! \is_null($this->password_set_at);
}

public function markPasswordAsSet(): bool
{
return $this->forceFill([
'password_set_at' => $this->freshTimestamp(),
])->save();
}

public function sendWelcomeNotification(): void
{
if ($this->role===UserRole::ngo_admin)
{
$this->notify(new WelcomeNotification());
return;
}
$this->notify(new AdminWelcomeNotification());
}
}
9 changes: 9 additions & 0 deletions app/Enums/UserRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@

namespace App\Enums;

use App\Concerns\ArrayableEnum;

enum UserRole: string
{
use ArrayableEnum;
case donor = 'donor';
case ngo_admin = 'ngo-admin';
case bb_manager = 'bb-manager';
case bb_admin = 'bb-admin';

public function translationKeyPrefix(): string
{
return 'user.roles';
}

}
12 changes: 7 additions & 5 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

namespace App\Filament\Resources;

use App\Enums\UserRole;
use App\Filament\Resources\UserResource\Pages;
use App\Models\User;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;

class UserResource extends Resource
{
Expand All @@ -23,17 +27,15 @@ class UserResource extends Resource

public static function form(Form $form): Form
{
return $form
->schema([
//
]);
return $form;
}

public static function table(Table $table): Table
{
return $table
->columns([
//
TextColumn::make('name')->searchable()->sortable(),
TextColumn::make('email')->searchable()->sortable(),
])
->filters([
//
Expand Down
41 changes: 41 additions & 0 deletions app/Filament/Resources/UserResource/Pages/CreateUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,51 @@

namespace App\Filament\Resources\UserResource\Pages;

use App\Enums\UserRole;
use App\Filament\Resources\UserResource;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Form;
use Filament\Resources\Pages\CreateRecord;

class CreateUser extends CreateRecord
{
protected static string $resource = UserResource::class;
protected static bool $canCreateAnother = false;

public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->label(__('user.name'))
->required(),
TextInput::make('email')
->label(__('user.email'))
->email()
->unique('users', 'email')
->required(),
Select::make('role')
->label(__('user.role'))
->options(collect(
UserRole::options())->only([
UserRole::bb_admin->value,
UserRole::bb_manager->value,
UserRole::ngo_admin->value
]
)->toArray()
)->reactive()
->required(),
Select::make('organization')
->label(__('user.organization'))
->relationship('organization', 'name')
->hidden(function (callable $get) {
return $get('role') !== UserRole::ngo_admin->value;
})
->searchable()
->preload()
->required(),

]);
}
}
40 changes: 40 additions & 0 deletions app/Filament/Resources/UserResource/Pages/EditUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace App\Filament\Resources\UserResource\Pages;

use App\Enums\UserRole;
use App\Filament\Resources\UserResource;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Pages\Actions;
use Filament\Resources\Form;
use Filament\Resources\Pages\EditRecord;

class EditUser extends EditRecord
Expand All @@ -18,4 +22,40 @@ protected function getActions(): array
Actions\DeleteAction::make(),
];
}
public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->label(__('user.name'))
->required(),
TextInput::make('email')
->label(__('user.email'))
->email()
->unique('users', 'email')
->required(),
Select::make('role')
->label(__('user.role'))
->options(collect(
UserRole::options())->only([
UserRole::bb_admin->value,
UserRole::bb_manager->value,
UserRole::ngo_admin->value
]
)->toArray()
)->reactive()
->required(),
Select::make('organization')
->label(__('user.organization'))
->relationship('organization', 'name')
->hidden(function (callable $get) {
return $get('role') !== UserRole::ngo_admin->value;
})
->searchable()
->preload()
->required(),

]);
}

}
13 changes: 0 additions & 13 deletions app/Filament/Resources/UsersResource/Pages/CreateUsers.php

This file was deleted.

21 changes: 0 additions & 21 deletions app/Filament/Resources/UsersResource/Pages/EditUsers.php

This file was deleted.

21 changes: 0 additions & 21 deletions app/Filament/Resources/UsersResource/Pages/ListUsers.php

This file was deleted.

38 changes: 38 additions & 0 deletions app/Http/Controllers/Auth/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;
use Inertia\Inertia;

class PasswordController extends Controller
{
Expand All @@ -28,4 +31,39 @@ public function update(Request $request): RedirectResponse

return back();
}

public function setInitialPassword(User $user, Request $request): \Inertia\Response
{
if (! $request->hasValidSignature()) {
abort(Response::HTTP_FORBIDDEN, __('auth.welcome.invalid_signature'));
}

if (\is_null($user)) {
abort(Response::HTTP_FORBIDDEN, __('auth.welcome.no_user'));
}

if ($user->hasSetPassword()) {
abort(Response::HTTP_FORBIDDEN, __('auth.welcome.already_used'));
}
return Inertia::render('Auth/SetInitialPassword', [
'user' => $user,
'token' => sha1($user->email),
]);
}
public function storeInitialPassword(Request $request, User $user): RedirectResponse
{
if ($request->token !== sha1($user->email)) {
abort(401);
}
$validated = $request->validate([
'password' => ['required', Password::defaults(), 'confirmed'],
]);

$user->update([
'password' => Hash::make($validated['password']),
]);
$user->markPasswordAsSet();

return redirect()->route('login')->with('success_message', __('user.messages.set_initial_password_success'));
}
}
Loading

0 comments on commit 1290e3e

Please sign in to comment.