Skip to content

Commit

Permalink
Change the default layout. Add categories for posts. Add roles and pe…
Browse files Browse the repository at this point in the history
…rmissions functionality. Add factories for seeders. Add GSAP for frontend.
  • Loading branch information
hackedhorizon committed Jun 21, 2024
1 parent 077c7b1 commit 1b11dae
Show file tree
Hide file tree
Showing 59 changed files with 1,112 additions and 240 deletions.
9 changes: 9 additions & 0 deletions app/Enums/RoleName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Enums;

enum RoleName: string
{
case ADMIN = 'admin';
case USER = 'user';
}
13 changes: 13 additions & 0 deletions app/Livewire/Posts/CreatePost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Livewire\Posts;

use Livewire\Component;

class CreatePost extends Component
{
public function render()
{
return view('livewire.posts.create-post');
}
}
13 changes: 13 additions & 0 deletions app/Livewire/Posts/Featured.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Livewire\Posts;

use Livewire\Component;

class Featured extends Component
{
public function render()
{
return view('livewire.posts.featured');
}
}
19 changes: 19 additions & 0 deletions app/Livewire/Posts/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Livewire\Posts;

use Livewire\Component;

class Post extends Component
{
public bool $myModal1 = false;

public function updatedMyModal1(){
dd($this->myModal1);
}

public function render()
{
return view('livewire.posts.post');
}
}
20 changes: 20 additions & 0 deletions app/Models/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Category extends Model
{
use HasFactory;

/**
* Get the posts that belong to the category.
*/
public function posts(): BelongsToMany
{
return $this->belongsToMany(Post::class);
}
}
11 changes: 11 additions & 0 deletions app/Models/Permission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{
use HasFactory;
}
20 changes: 20 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Post extends Model
{
use HasFactory;

/**
* Get the categories that belong to the post.
*/
public function categories(): BelongsToMany
{
return $this->belongsToMany(Category::class);
}
}
11 changes: 11 additions & 0 deletions app/Models/PostCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PostCategory extends Model
{
use HasFactory;
}
28 changes: 28 additions & 0 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Role extends Model
{
/**
* Get the permissions for the role.
*/
public function permissions(): BelongsToMany
{
return $this->belongsToMany(Permission::class);
}

/**
* Get the users for the role.
*/
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class);
}

use HasFactory;
}
67 changes: 67 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace App\Models;

use App\Enums\RoleName;
use Illuminate\Auth\Passwords\CanResetPassword;
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;
use Laravel\Sanctum\HasApiTokens;
Expand Down Expand Up @@ -49,4 +51,69 @@ class User extends Authenticatable implements MustVerifyEmail
'email_verified_at' => 'datetime',
'password' => 'hashed',
];

/**
* Retrieves the roles associated with the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany The roles associated with the user.
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class);
}

/**
* Checks if the user is an admin.
*
* @return bool True if the user is an admin, false otherwise.
*/
public function isAdmin(): bool
{
return $this->hasRole(RoleName::ADMIN);
}

/**
* Checks if the user is a regular user.
*
* @return bool True if the user is a regular user, false otherwise.
*/
public function isUser(): bool
{
return $this->hasRole(RoleName::USER);
}

/**
* Checks if the user has the specified role.
*
* @param RoleName $role The name of the role to check.
* @return bool True if the user has the specified role, false otherwise.
*/
public function hasRole(RoleName $role): bool
{
return $this->roles()->where('name', $role->value)->exists();
}

/**
* Retrieves the permissions associated with the user's roles.
*
* @return array An array of unique permission names associated with the user's roles.
*/
public function permissions(): array
{
return $this->roles()->with('permissions')->get()
->map(function ($role) {
return $role->permissions->pluck('name');
})->flatten()->values()->unique()->toArray();
}

/**
* Checks if the user has the specified permission.
*
* @param string $permission The name of the permission to check.
* @return bool True if the user has the specified permission, false otherwise.
*/
public function hasPermission(string $permission): bool
{
return in_array($permission, $this->permissions(), true);
}
}
43 changes: 43 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Providers;

use App\Models\Permission;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -23,5 +25,46 @@ public function boot(): void
Model::shouldBeStrict(
! app()->isProduction()
);
$this->registerGates();
}

/**
* Registers the defined gates.
*/
protected function registerGates(): void
{
/**
* Attempts to register the defined gates.
*
* @throws \Exception If the database is not found or not yet migrated.
*/
try {
foreach (Permission::pluck('name') as $permission) {
/**
* Defines a new gate with the specified name and callback.
*
* @param string $permission The name of the gate.
* @param callable $callback The callback function to determine whether the user has access.
*/
Gate::define($permission, function ($user) use ($permission) {
/**
* Checks if the user has the specified permission.
*
* @param string $permission The name of the permission.
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Contracts\Auth\Authenticatable|null $user The user to check.
* @return bool Whether the user has the specified permission.
*/
return $user->hasPermission($permission);
});
}
} catch (\Exception $e) {
/**
* Logs a message indicating that the database is not found or not yet migrated.
*
* @param string $message The message to log.
* @param int $level The log level.
*/
info('registerPermissions(): Database not found or not yet migrated. Ignoring user permissions while booting app.');
}
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"andreiio/blade-iconoir": "^4.3",
"blade-ui-kit/blade-icons": "^1.6",
"codeat3/blade-carbon-icons": "^2.23",
"codeat3/blade-eos-icons": "^1.14",
"codeat3/blade-google-material-design-icons": "^1.19",
"danharrin/livewire-rate-limiting": "^1.3",
"guzzlehttp/guzzle": "^7.8.1",
Expand Down
73 changes: 72 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1b11dae

Please sign in to comment.