Skip to content

Commit

Permalink
Media Controller code refactor and set config variable for authorizat…
Browse files Browse the repository at this point in the history
…ion (#16)

* Add Media Gallery Package

- Add Media Gallery Package
- Remove image column from the User model
- Remove Media Helper Package (It's Already included in media gallery package)
- Remove Cache Controler
- Add Avatar attribute in user model and return user image
- Add Gravatar API for default image in user model

* Feature/media gallery (#15)
 
 ---------

Co-authored-by: anisAronno <[email protected]>
  • Loading branch information
anisAronno and anisAronno authored Dec 31, 2023
1 parent 100d9d6 commit bfd6c23
Show file tree
Hide file tree
Showing 27 changed files with 342 additions and 80 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_DATABASE=laravel-starter
DB_USERNAME=root
DB_PASSWORD=

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ jobs:
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling
commit_message: Fix styling by bot
branch: ${{ github.head_ref }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ yarn-error.log
/.idea
/.vscode
.DS_Store
/media
media
2 changes: 1 addition & 1 deletion .php-cs-fixer.cache

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ Welcome to the Laravel Starter Project! This repository provides a kickstart set

## Table of Contents

- [Laravel Starter Project (Version 10)](#laravel-starter-project-version-10)
- [Table of Contents](#table-of-contents)
- [Uses Packages](#uses-packages)
- [Other Uses](#other-uses)
- [Features](#features)
- [Getting Started](#getting-started)
- [Installation](#installation)
- [Starting with Docker](#starting-with-docker)
- [Deploy via FTP with CI/CD](#deploy-via-ftp-with-cicd)
- [GitFlow for Merge Requests](#gitflow-for-merge-requests)
- [FTP Deployment Process](#ftp-deployment-process)
- [Contribution Guide](#contribution-guide)
- [License](#license)
- [Laravel Starter Project (Version 10)](#laravel-starter-project-version-10)
- [Table of Contents](#table-of-contents)
- [Uses Packages](#uses-packages)
- [Other Uses](#other-uses)
- [Features](#features)
- [Getting Started](#getting-started)
- [Installation](#installation)
- [Starting with Docker](#starting-with-docker)
- [Deploy via FTP with CI/CD](#deploy-via-ftp-with-cicd)
- [GitFlow for Merge Requests](#gitflow-for-merge-requests)
- [FTP Deployment Process](#ftp-deployment-process)
- [Contribution Guide](#contribution-guide)
- [License](#license)

## Uses Packages

Expand All @@ -27,7 +27,7 @@ Welcome to the Laravel Starter Project! This repository provides a kickstart set
- Laravel [Horizon](https://laravel.com/docs/10.x/horizon) (Job And Queue manage)
- Laravel [Debugbar](https://github.com/barryvdh/laravel-debugbar) (Monitoring every request)
- Laravel [Settings](https://github.com/anisAronno/laravel-settings) (Personal Package for application settings)
- Laravel [Media Uploader](https://github.com/anisAronno/Laravel-Media-Helper) (Personal Package for file management)
- Laravel [Media Gallery](https://github.com/anisAronno/laravel-media-gallery) (Personal Package for file management)
- Laravel [Activity Log](https://github.com/spatie/laravel-activitylog) (Track User Activity)
- Beautiful [Log Viewer](https://github.com/opcodesio/log-viewer) for local and production
- [Flowbite](https://flowbite.com/) For Tailwind Component
Expand Down Expand Up @@ -179,4 +179,4 @@ For guidelines on contributing to this project, refer to the [Contribution Guide

## License

This application is open-source software licensed under the [MIT license](https://opensource.org/licenses/MIT).
This application is open-source software licensed under the [MIT license](https://opensource.org/licenses/MIT).
22 changes: 22 additions & 0 deletions app/Http/Controllers/Admin/NotificationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
public function markNotification(Request $request)
{
auth()->user()
->unreadNotifications
->when($request->input('id'), function ($query) use ($request)
{
return $query->where('id', $request->input('id'));
})
->markAsRead();

return response()->noContent();
}
}
33 changes: 32 additions & 1 deletion app/Http/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace App\Http\Resources;

use App\Http\Resources\AddressResource;
use App\Http\Resources\PermissionResource;
use App\Http\Resources\RoleResource;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;

class UserResource extends JsonResource
{
Expand All @@ -14,6 +18,33 @@ class UserResource extends JsonResource
*/
public function toArray(Request $request): array
{
return parent::toArray($request);
return [
'id' => $this->id,
'name' => $this->name,
'username' => $this->username,
'email' => $this->email,
'avatar' => $this->avatar,
'gender' => $this->gender,
'email_verified_at' => $this->email_verified_at,
'ip' => $this->ip,
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'time_zone' => $this->time_zone,
'language' => $this->language,
'status' => $this->status,
'is_deletable' => $this->is_deletable,
'is_editable' => $this->is_editable,
'unreadNotifications' => $this->whenLoaded('unreadNotifications'),
'roles' => $this->whenLoaded('roles', function ()
{
return RoleResource::collection($this->roles)->pluck('name');
}),
'permissions' => $this->whenLoaded('permissions', function ()
{
return PermissionResource::collection($this->getAllPermissions())->pluck('name');
}),
'addresses' => AddressResource::collection($this->whenLoaded('addresses')),
'created_at' => Carbon::parse($this->created_at)->diffForHumans(),
];
}
}
48 changes: 0 additions & 48 deletions app/Http/Resources/UserResources.php

This file was deleted.

12 changes: 10 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use AnisAronno\MediaGallery\Traits\HasMedia;
use App\Enums\UserStatus;
use App\Helpers\UniqueSlug;
use App\Notifications\ResetPasswordNotification;
Expand All @@ -26,6 +27,7 @@ class User extends Authenticatable implements MustVerifyEmail
use LogsActivity;
use Notifiable;
use SoftDeletes;
use HasMedia;

/**
* The attributes that are mass assignable.
Expand All @@ -37,7 +39,6 @@ class User extends Authenticatable implements MustVerifyEmail
'email',
'password',
'phone',
'image',
'api_token',
'status',
'gender',
Expand Down Expand Up @@ -73,7 +74,7 @@ class User extends Authenticatable implements MustVerifyEmail
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly(['name', 'email', 'password', 'image', 'status', 'api_token'])
->logOnly(['name', 'email', 'password', 'status', 'api_token'])
->logOnlyDirty()
->dontSubmitEmptyLogs();
}
Expand Down Expand Up @@ -138,4 +139,11 @@ public function hasAdministrativeRole(): bool
{
return $this->hasRole(['superadmin', 'admin']);
}

protected $appends = ['avatar'];

public function getAvatarAttribute() : string
{
return $this->image[0]?->url ?? 'https://www.gravatar.com/avatar/'.md5(strtolower(trim($this->email)));
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "anisaronno/laravel-starter",
"type": "project",
"version": "0.0.2",
"version": "0.0.3",
"description": "A perfect laravel starter project for any kind of project.",
"keywords": [
"laravel",
Expand All @@ -19,7 +19,7 @@
],
"require": {
"php": "^8.1",
"anisaronno/laravel-media-helper": "^0.1.1",
"anisaronno/laravel-media-gallery": "0.3.0",
"anisaronno/laravel-settings": "1.0.0",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.10",
Expand Down
65 changes: 64 additions & 1 deletion composer.lock

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

8 changes: 8 additions & 0 deletions config/gallery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
'cache_expiry_time' => 1440,
'guard' => ['auth:api'],
'batch_delete_secret' => '1a463c2c-81b0-441e-9e4f-04691a7e5e0f',
'view_all_media_anyone' => false,
];
34 changes: 34 additions & 0 deletions database/factories/ImageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Database\Factories;

use AnisAronno\MediaGallery\Models\Image;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\AnisAronno\MediaGallery\Models\Image>
*/
class ImageFactory extends Factory
{
protected $model = Image::class;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'title' => $this->faker->name(),
'url' => $this->faker->imageUrl(),
'mimes' => 'images/png',
'type' => 'images/png',
'size' => '3 MB',
'directory' => 'images',
'owner_id' => User::factory(),
'owner_type' => User::class,
];
}
}
1 change: 0 additions & 1 deletion database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function definition(): array
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
'image' => Media::getDefaultAvatar(),
'phone' => fake()->phoneNumber,
'gender' => fake()->randomElement(UserGender::values()),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public function up(): void
$table->string('name');
$table->string('username', 100)->unique();
$table->string('phone', 50)->unique()->nullable();
$table->string('image')->nullable();
$table->string('email', 100)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('api_token', 100)->unique();
Expand Down
Loading

0 comments on commit bfd6c23

Please sign in to comment.