Skip to content

Commit

Permalink
Merge pull request #20 from Innovix-Matrix-Systems/develop
Browse files Browse the repository at this point in the history
JetStream Implementation
  • Loading branch information
AHS12 authored Oct 18, 2023
2 parents a03b85a + a0e1dda commit a8353bd
Show file tree
Hide file tree
Showing 102 changed files with 3,810 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=database
SESSION_DRIVER=file
SESSION_DRIVER=database
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ Unlike traditional API generators or code generators, this project simplifies th

## Features

- Role and permission-based operations
- Full Authentication System(using [Laravel jetstream](https://jetstream.laravel.com/introduction.html) package)
- Role and permission-based Authorization System(using [Laravel-Permission](https://spatie.be/docs/laravel-permission/v5/introduction) package)
- User management
- Profile settings
- Multiple Language Support
- Theme Customization
- Multiple Language Support (with the help of [Filament Translations](https://filamentphp.com/docs/3.x/panels/installation#publishing-translations) and [Larave-Lang](https://laravel-lang.com/) package)
- Theme Customization (Powered by [Tailwind CSS](https://tailwindcss.com/))

## Version Requirments
- Node 16+
Expand Down
35 changes: 35 additions & 0 deletions app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Actions\Fortify;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Jetstream\Jetstream;

class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;

/**
* Validate and create a newly registered user.
*
* @param array<string, string> $input
*/
public function create(array $input): User
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => $this->passwordRules(),
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '',
])->validate();

return User::create([
'name' => $input['name'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
]);
}
}
18 changes: 18 additions & 0 deletions app/Actions/Fortify/PasswordValidationRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Actions\Fortify;

use Laravel\Fortify\Rules\Password;

trait PasswordValidationRules
{
/**
* Get the validation rules used to validate passwords.
*
* @return array<int, \Illuminate\Contracts\Validation\Rule|array|string>
*/
protected function passwordRules(): array
{
return ['required', 'string', new Password, 'confirmed'];
}
}
29 changes: 29 additions & 0 deletions app/Actions/Fortify/ResetUserPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Actions\Fortify;

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

class ResetUserPassword implements ResetsUserPasswords
{
use PasswordValidationRules;

/**
* Validate and reset the user's forgotten password.
*
* @param array<string, string> $input
*/
public function reset(User $user, array $input): void
{
Validator::make($input, [
'password' => $this->passwordRules(),
])->validate();

$user->forceFill([
'password' => Hash::make($input['password']),
])->save();
}
}
32 changes: 32 additions & 0 deletions app/Actions/Fortify/UpdateUserPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Actions\Fortify;

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

class UpdateUserPassword implements UpdatesUserPasswords
{
use PasswordValidationRules;

/**
* Validate and update the user's password.
*
* @param array<string, string> $input
*/
public function update(User $user, array $input): void
{
Validator::make($input, [
'current_password' => ['required', 'string', 'current_password:web'],
'password' => $this->passwordRules(),
], [
'current_password.current_password' => __('The provided password does not match your current password.'),
])->validateWithBag('updatePassword');

$user->forceFill([
'password' => Hash::make($input['password']),
])->save();
}
}
56 changes: 56 additions & 0 deletions app/Actions/Fortify/UpdateUserProfileInformation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Actions\Fortify;

use App\Models\User;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;

class UpdateUserProfileInformation implements UpdatesUserProfileInformation
{
/**
* Validate and update the given user's profile information.
*
* @param array<string, string> $input
*/
public function update(User $user, array $input): void
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
'photo' => ['nullable', 'mimes:jpg,jpeg,png', 'max:1024'],
])->validateWithBag('updateProfileInformation');

if (isset($input['photo'])) {
$user->updateProfilePhoto($input['photo']);
}

if ($input['email'] !== $user->email &&
$user instanceof MustVerifyEmail) {
$this->updateVerifiedUser($user, $input);
} else {
$user->forceFill([
'name' => $input['name'],
'email' => $input['email'],
])->save();
}
}

/**
* Update the given verified user's profile information.
*
* @param array<string, string> $input
*/
protected function updateVerifiedUser(User $user, array $input): void
{
$user->forceFill([
'name' => $input['name'],
'email' => $input['email'],
'email_verified_at' => null,
])->save();

$user->sendEmailVerificationNotification();
}
}
19 changes: 19 additions & 0 deletions app/Actions/Jetstream/DeleteUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Actions\Jetstream;

use App\Models\User;
use Laravel\Jetstream\Contracts\DeletesUsers;

class DeleteUser implements DeletesUsers
{
/**
* Delete the given user.
*/
public function delete(User $user): void
{
$user->deleteProfilePhoto();
$user->tokens->each->delete();
$user->delete();
}
}
52 changes: 52 additions & 0 deletions app/Http/Middleware/AdminAuthenticate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Http\Middleware;

use Filament\Facades\Filament;
use Filament\Models\Contracts\FilamentUser;
use Illuminate\Auth\Middleware\Authenticate as Middleware;

class AdminAuthenticate extends Middleware
{
const DEFAULT_AUTH_GUARD = 'web';

protected function authenticate($request, array $guards): void
{
if (empty($guards)) {
$guards = [null];
}

// Check the default guard and return early if it's not 'web'
if ($this->shouldUnauthenticate($request)) {
$this->unauthenticated($request, $guards);
return;
}

$this->auth->shouldUse(self::DEFAULT_AUTH_GUARD);

$user = $request->user();
$panel = Filament::getCurrentPanel();

if ($this->isNotAdmin($user, $panel)) {
abort(403, 'You are not an Admin!');
}
}

protected function redirectTo($request): ?string
{
return route('login');
}

protected function shouldUnauthenticate($request): bool
{
return self::DEFAULT_AUTH_GUARD !== 'web';
}

protected function isNotAdmin($user, $panel): bool
{
if ($user instanceof FilamentUser) {
return !$user->canAccessPanel($panel);
}
return config('app.env') !== 'local';
}
}
23 changes: 20 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@
namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;

use App\Http\Traits\UserTrait;
use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements FilamentUser
{
use HasApiTokens, HasFactory, Notifiable, HasRoles, UserTrait;
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
use HasRoles;
use UserTrait;

/**
* The attributes that are mass assignable.
Expand All @@ -37,6 +44,8 @@ class User extends Authenticatable implements FilamentUser
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];

/**
Expand All @@ -46,7 +55,15 @@ class User extends Authenticatable implements FilamentUser
*/
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];

/**
* The accessors to append to the model's array form.
*
* @var array<int, string>
*/
protected $appends = [
'profile_photo_url',
];

public function isSuperAdmin(): bool
Expand Down
9 changes: 6 additions & 3 deletions app/Providers/Filament/AdminPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use App\Filament\Pages\Settings;
use App\Filament\Resources\RoleResource;
use App\Filament\Resources\UserResource;
use App\Http\Middleware\AdminAuthenticate;
use App\Http\Middleware\Authenticate;
use App\Http\Middleware\LanguageMiddleware;
use Filament\Http\Middleware\Authenticate;
//use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Navigation\MenuItem;
Expand All @@ -33,11 +35,11 @@ public function panel(Panel $panel): Panel
return $panel
->default()
->brandName('IMS Admin')
->brandLogo(asset('assets/logo.jpg'))
->brandLogo(asset('assets/logo.svg'))
->favicon(asset('favicon.ico'))
->id('admin')
->path('admin')
->login()
//->login()
//->registration()
//->profile()
//->passwordReset()
Expand Down Expand Up @@ -94,6 +96,7 @@ public function panel(Panel $panel): Panel
])
->authMiddleware([
Authenticate::class,
AdminAuthenticate::class,
])
->renderHook(
'panels::body.end',
Expand Down
Loading

0 comments on commit a8353bd

Please sign in to comment.