user profiles
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class ProfileController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the specified user's profile.
|
||||
*/
|
||||
public function show(User $user)
|
||||
{
|
||||
return view('profile.show', compact('user'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the authenticated user's profile.
|
||||
*/
|
||||
public function edit()
|
||||
{
|
||||
$user = Auth::user();
|
||||
return view('profile.edit', compact('user'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the authenticated user's profile.
|
||||
*/
|
||||
public function update(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
|
||||
'avatar' => ['nullable', 'image', 'mimes:jpeg,png,jpg,gif', 'max:2048'],
|
||||
'bio' => ['nullable', 'string', 'max:1000'],
|
||||
'website' => ['nullable', 'url', 'max:255'],
|
||||
'twitter' => ['nullable', 'string', 'max:255'],
|
||||
'github' => ['nullable', 'string', 'max:255'],
|
||||
'linkedin' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
// Handle avatar upload
|
||||
if ($request->hasFile('avatar')) {
|
||||
// Delete old avatar if exists
|
||||
if ($user->avatar) {
|
||||
Storage::disk('public')->delete('avatars/' . $user->avatar);
|
||||
}
|
||||
|
||||
// Store new avatar
|
||||
$avatarName = time() . '_' . $user->id . '.' . $request->avatar->extension();
|
||||
$request->avatar->storeAs('avatars', $avatarName, 'public');
|
||||
$validated['avatar'] = $avatarName;
|
||||
}
|
||||
|
||||
// Update user profile
|
||||
$user->update($validated);
|
||||
|
||||
return redirect()->route('profile.show', $user)
|
||||
->with('success', 'Profile updated successfully!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the user's avatar.
|
||||
*/
|
||||
public function deleteAvatar()
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($user->avatar) {
|
||||
Storage::disk('public')->delete('avatars/' . $user->avatar);
|
||||
$user->update(['avatar' => null]);
|
||||
}
|
||||
|
||||
return redirect()->route('profile.edit')
|
||||
->with('success', 'Avatar deleted successfully!');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class IsAdmin
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if (!auth()->check() || !auth()->user()->isAdmin()) {
|
||||
abort(403, 'Unauthorized action. Admin access required.');
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class IsModerator
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if (!auth()->check() || !auth()->user()->hasAnyRole(['moderator', 'admin'])) {
|
||||
abort(403, 'Unauthorized action. Moderator access required.');
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class Role extends Model
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'description',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the users that belong to the role.
|
||||
*/
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
@@ -21,6 +22,12 @@ class User extends Authenticatable
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
'avatar',
|
||||
'bio',
|
||||
'website',
|
||||
'twitter',
|
||||
'github',
|
||||
'linkedin',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -45,4 +52,63 @@ class User extends Authenticatable
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the roles that belong to the user.
|
||||
*/
|
||||
public function roles(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Role::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user has a specific role.
|
||||
*/
|
||||
public function hasRole(string $role): bool
|
||||
{
|
||||
return $this->roles()->where('slug', $role)->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user has any of the given roles.
|
||||
*/
|
||||
public function hasAnyRole(array $roles): bool
|
||||
{
|
||||
return $this->roles()->whereIn('slug', $roles)->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user is an admin.
|
||||
*/
|
||||
public function isAdmin(): bool
|
||||
{
|
||||
return $this->hasRole('admin');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user is a moderator.
|
||||
*/
|
||||
public function isModerator(): bool
|
||||
{
|
||||
return $this->hasRole('moderator');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user is a regular user.
|
||||
*/
|
||||
public function isUser(): bool
|
||||
{
|
||||
return $this->hasRole('user');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user's avatar URL.
|
||||
*/
|
||||
public function getAvatarUrlAttribute(): ?string
|
||||
{
|
||||
if ($this->avatar) {
|
||||
return asset('storage/avatars/' . $this->avatar);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
@@ -19,6 +20,50 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
// Product-related Gates
|
||||
Gate::define('create-product', function ($user) {
|
||||
return $user->hasAnyRole(['user', 'moderator', 'admin']);
|
||||
});
|
||||
|
||||
Gate::define('edit-product', function ($user, $product) {
|
||||
return $user->id === $product->user_id || $user->hasAnyRole(['moderator', 'admin']);
|
||||
});
|
||||
|
||||
Gate::define('delete-product', function ($user, $product) {
|
||||
return $user->id === $product->user_id || $user->hasAnyRole(['moderator', 'admin']);
|
||||
});
|
||||
|
||||
Gate::define('approve-product', function ($user) {
|
||||
return $user->hasAnyRole(['moderator', 'admin']);
|
||||
});
|
||||
|
||||
Gate::define('reject-product', function ($user) {
|
||||
return $user->hasAnyRole(['moderator', 'admin']);
|
||||
});
|
||||
|
||||
// Comment-related Gates
|
||||
Gate::define('delete-comment', function ($user, $comment) {
|
||||
return $user->id === $comment->user_id || $user->hasAnyRole(['moderator', 'admin']);
|
||||
});
|
||||
|
||||
// User management Gates
|
||||
Gate::define('manage-users', function ($user) {
|
||||
return $user->isAdmin();
|
||||
});
|
||||
|
||||
Gate::define('manage-roles', function ($user) {
|
||||
return $user->isAdmin();
|
||||
});
|
||||
|
||||
Gate::define('manage-settings', function ($user) {
|
||||
return $user->isAdmin();
|
||||
});
|
||||
|
||||
// Admin check
|
||||
Gate::before(function ($user, $ability) {
|
||||
if ($user->isAdmin()) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user