mirror of
https://github.com/NyaaStudios/nyaabooru.git
synced 2025-12-09 21:42:57 +00:00
This commit is contained in:
parent
21e59d775a
commit
f60ae41bf6
26 changed files with 741 additions and 70 deletions
33
app/Enums/RolesEnum.php
Normal file
33
app/Enums/RolesEnum.php
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum RolesEnum: string
|
||||
{
|
||||
case RESTRICTED = 'restricted';
|
||||
case MEMBER = 'member';
|
||||
case MODERATOR = 'moderator';
|
||||
case ADMIN = 'admin';
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this)
|
||||
{
|
||||
RolesEnum::RESTRICTED => 'Restricted',
|
||||
RolesEnum::MEMBER => 'Member',
|
||||
RolesEnum::MODERATOR => 'Moderator',
|
||||
RolesEnum::ADMIN => 'Admin',
|
||||
};
|
||||
}
|
||||
|
||||
public function variant(): string
|
||||
{
|
||||
return match ($this)
|
||||
{
|
||||
RolesEnum::RESTRICTED => 'danger',
|
||||
RolesEnum::MEMBER => 'neutral',
|
||||
RolesEnum::MODERATOR => 'success',
|
||||
RolesEnum::ADMIN => 'brand',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Enums\RolesEnum;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Laravel\Socialite\Facades\Socialite;
|
||||
|
|
@ -17,9 +18,15 @@ class AuthController extends Controller
|
|||
{
|
||||
$user = Socialite::driver('authentik')->user();
|
||||
|
||||
$authUser = User::updateOrCreate(
|
||||
[ 'email' => $user->getEmail() ],
|
||||
);
|
||||
$authUser = User::where('email', $user->getEmail())->first();
|
||||
if ($authUser == null)
|
||||
{
|
||||
$authUser = User::create([
|
||||
'email' => $user->getEmail(),
|
||||
'name' => $user->getName(),
|
||||
]);
|
||||
// $authUser->assignRole(RolesEnum::MEMBER);
|
||||
}
|
||||
|
||||
if ($authUser)
|
||||
{
|
||||
|
|
|
|||
46
app/Livewire/App/Role.php
Normal file
46
app/Livewire/App/Role.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace App\Livewire\App;
|
||||
|
||||
use App\Enums\RolesEnum;
|
||||
use App\Models\User;
|
||||
use Livewire\Component;
|
||||
|
||||
class Role extends Component
|
||||
{
|
||||
public User $user;
|
||||
public string $size = 'small';
|
||||
protected string $variant = 'neutral';
|
||||
protected string $name = '';
|
||||
|
||||
public function mount(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
|
||||
if ($user->hasRole(RolesEnum::RESTRICTED))
|
||||
{
|
||||
$this->variant = RolesEnum::RESTRICTED->variant();
|
||||
$this->name = RolesEnum::RESTRICTED->label();
|
||||
}
|
||||
if ($user->hasRole(RolesEnum::MEMBER))
|
||||
{
|
||||
$this->variant = RolesEnum::MEMBER->variant();
|
||||
$this->name = RolesEnum::MEMBER->label();
|
||||
}
|
||||
if ($user->hasRole(RolesEnum::MODERATOR))
|
||||
{
|
||||
$this->variant = RolesEnum::MODERATOR->variant();
|
||||
$this->name = RolesEnum::MODERATOR->label();
|
||||
}
|
||||
if ($user->hasRole(RolesEnum::ADMIN))
|
||||
{
|
||||
$this->variant = RolesEnum::ADMIN->variant();
|
||||
$this->name = RolesEnum::ADMIN->label();
|
||||
}
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.app.role');
|
||||
}
|
||||
}
|
||||
|
|
@ -15,8 +15,8 @@ class Upload extends Component
|
|||
{
|
||||
use WithFileUploads;
|
||||
|
||||
#[Validate('image|max:65536')]
|
||||
public $file;
|
||||
#[Validate(['files.*' => 'required|image|max:65536'])]
|
||||
public $files = [];
|
||||
|
||||
#[Validate('required|in:safe,suggestive,explicit')]
|
||||
public $rating = 'safe';
|
||||
|
|
@ -32,33 +32,35 @@ class Upload extends Component
|
|||
$this->validate();
|
||||
$author = Auth::user();
|
||||
|
||||
if ($this->file)
|
||||
foreach ($this->files as $file)
|
||||
{
|
||||
$post = Post::create([
|
||||
'extension' => $this->file->getClientOriginalExtension(),
|
||||
'rating' => $this->rating,
|
||||
]);
|
||||
|
||||
if ($post)
|
||||
if ($file)
|
||||
{
|
||||
$author->posts()->save($post);
|
||||
$post = Post::create([
|
||||
'extension' => $file->getClientOriginalExtension(),
|
||||
'rating' => $this->rating,
|
||||
'hash' => $file->hashName(),
|
||||
]);
|
||||
|
||||
// Save the full image
|
||||
$this->file->storeAs("posts/$post->id", 'full');
|
||||
$fullImg = Storage::get("posts/$post->id/full");
|
||||
if ($post)
|
||||
{
|
||||
$author->posts()->save($post);
|
||||
|
||||
// Create thumbnail preview
|
||||
$thumb = Image::read($fullImg)->cover(width: 512, height: 512);
|
||||
Storage::put("posts/$post->id/thumb", $thumb->encodeByMediaType());
|
||||
// Save the full image
|
||||
$file->storeAs("posts/$post->id", 'full');
|
||||
$fullImg = Storage::get("posts/$post->id/full");
|
||||
|
||||
// Create smaller preview image
|
||||
$preview = Image::read($fullImg)->scaleDown(width: 1280, height: 720);
|
||||
Storage::put("posts/$post->id/preview", $preview->encodeByMediaType());
|
||||
// Create thumbnail preview
|
||||
$thumb = Image::read($fullImg)->cover(width: 512, height: 512);
|
||||
Storage::put("posts/$post->id/thumb", $thumb->encodeByMediaType());
|
||||
|
||||
return $this->redirect("/posts/$post->id");
|
||||
// Create smaller preview image
|
||||
$preview = Image::read($fullImg)->scaleDown(width: 1280, height: 720);
|
||||
Storage::put("posts/$post->id/preview", $preview->encodeByMediaType());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->redirect('/upload');
|
||||
return $this->redirect('/posts');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use Livewire\Component;
|
|||
class PostFeature extends Component
|
||||
{
|
||||
public ?Post $post = null;
|
||||
public $tags = null;
|
||||
|
||||
public function mount()
|
||||
{
|
||||
|
|
@ -18,6 +19,7 @@ class PostFeature extends Component
|
|||
['$sample' => ['size' => 1]]
|
||||
]);
|
||||
})->first();
|
||||
$this->tags = $this->post->tags()->take(5)->get();
|
||||
}
|
||||
|
||||
public function placeholder()
|
||||
|
|
@ -36,7 +38,6 @@ HTML;
|
|||
{
|
||||
if ($this->post == null)
|
||||
{
|
||||
$href = route('posts.home');
|
||||
return view('livewire.post-feature-empty');
|
||||
}
|
||||
return view('livewire.post-feature');
|
||||
|
|
|
|||
|
|
@ -3,9 +3,7 @@
|
|||
namespace App\Livewire\Posts;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Models\Tag;
|
||||
use Livewire\Attributes\Title;
|
||||
use Livewire\Attributes\Url;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
|
||||
|
|
@ -13,11 +11,20 @@ class Index extends Component
|
|||
{
|
||||
use WithPagination;
|
||||
|
||||
public $posts = [];
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->posts = Post::orderBy('created_at', 'desc')->get();
|
||||
}
|
||||
|
||||
#[Title('Posts')]
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.posts.index', [
|
||||
'posts' => Post::orderBy('created_at', 'desc')->paginate(25),
|
||||
]);
|
||||
if ($this->posts->count() == 0)
|
||||
{
|
||||
return view('livewire.posts.index-empty');
|
||||
}
|
||||
return view('livewire.posts.index');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,28 @@
|
|||
|
||||
namespace App\Livewire\Tags;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Models\Tag;
|
||||
use Livewire\Component;
|
||||
|
||||
class View extends Component
|
||||
{
|
||||
public ?Tag $tag = null;
|
||||
public $posts = [];
|
||||
|
||||
public function mount(?Tag $tag)
|
||||
{
|
||||
$this->tag = $tag;
|
||||
if ($tag)
|
||||
{
|
||||
$this->posts = $tag->posts;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->posts = Post::doesntHave('tags')->get();
|
||||
}
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.tags.view');
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ class Post extends Model
|
|||
{
|
||||
use SoftDeletes, Favoriteable;
|
||||
|
||||
protected $fillable = [ 'rating', 'extension', 'featured' ];
|
||||
protected $fillable = [ 'rating', 'extension', 'hash', 'featured' ];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
|
|
|
|||
|
|
@ -9,13 +9,14 @@ use Laravel\Sanctum\HasApiTokens;
|
|||
use MongoDB\Laravel\Auth\User as Authenticatable;
|
||||
use MongoDB\Laravel\Relations\HasMany;
|
||||
use Overtrue\LaravelFavorite\Traits\Favoriter;
|
||||
use Spatie\Permission\Traits\HasRoles;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
protected $connection = 'mongodb';
|
||||
protected $table = 'users';
|
||||
|
||||
use HasApiTokens, HasFactory, Notifiable, Favoriter;
|
||||
use HasApiTokens, HasFactory, Notifiable, Favoriter, HasRoles;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ namespace App\Providers;
|
|||
|
||||
use App\Models\PersonalAccessToken;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use SocialiteProviders\Authentik\Provider as AuthentikProvider;
|
||||
|
|
@ -24,6 +25,12 @@ class AppServiceProvider extends ServiceProvider
|
|||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
// Setup admin role access
|
||||
Gate::before(function ($user, $ability)
|
||||
{
|
||||
return $user->hasRole('admin') ? true : null;
|
||||
});
|
||||
|
||||
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
|
||||
|
||||
// Authentik
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue