Skip to content
Merged

Dev #21

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

ADMIN_EMAIL_FOR_NOTIFICATIONS = 'example@mail.ru'
ADMIN_EMAIL_FOR_NOTIFICATIONS=example@mail.ru

PUSH_ALL_API_KEY=
PUSH_ALL_API_ID=
47 changes: 47 additions & 0 deletions app/Console/Commands/SendNewPosts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Console\Commands;

use App\Notifications\PostsForPeriod;
use App\Post;
use App\User;
use Illuminate\Console\Command;

class SendNewPosts extends Command
{
protected $signature = 'admin:send-news-posts {since : The dateTime with format 2020-09-26}';

protected $description = 'Send information to all users about new posts for the {since} ';

public function __construct()
{
parent::__construct();
}

public function handle()
{
$users = User::all();
$posts = Post::whereDate('created_at', '>=', $this->argument('since'))->latest()->take(5)->get();

if ($posts->isNotEmpty() && $users->isNotEmpty()) {
$this->alert('Письма отправляются...');

$bar = $this->output->createProgressBar(count($users));

$bar->start();

foreach ($users as $user) {
$user->notify(new PostsForPeriod($posts));

$bar->advance();
}

$bar->finish();
$this->info(PHP_EOL . 'Письма успешно отправленны');
} else {
$this->info(PHP_EOL . 'Письма не были отправлены. Проверьте введенные параметры.');
}

return 0;
}
}
6 changes: 5 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Console;

use App\Console\Commands\SendNewPosts;
use Carbon\Carbon;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand All @@ -24,7 +26,9 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
$now = Carbon::now()->format('Y-m-d');

$schedule->command(SendNewPosts::class, [$now])->weekly()->mondays()->at('09:00');
}

/**
Expand Down
23 changes: 23 additions & 0 deletions app/Http/Controllers/AdministrationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Controllers;

use App\Post;
use Illuminate\Http\Request;

class AdministrationController extends Controller
{
public function __construct()
{
$this->middleware('role:admin');
}

public function index() {
return view('admin.index');
}

public function posts() {
$posts = Post::with('tags')->latest()->get();
return view('/admin.posts', compact('posts'));
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function validator(array $data)
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:1', 'confirmed'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/FeedbacksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class FeedbacksController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('role:admin');
}

public function index()
Expand Down
25 changes: 16 additions & 9 deletions app/Http/Controllers/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class PostsController extends Controller
public function __construct()
{
$this->middleware('auth');
$this->middleware('can:update,post')->except(['index', 'userPosts', 'adminIndex', 'create', 'store']);
}

public function validateRequest($request, $post)
Expand All @@ -31,7 +30,7 @@ public function validateRequest($request, $post)

public function index()
{
$posts = Post::with('tags')->latest()->get();
$posts = Post::with('tags')->where('published', 1)->latest()->get();
return view('/index', compact('posts'));
}

Expand All @@ -41,11 +40,6 @@ public function userPosts()
return view('/posts.index', compact('posts'));
}

public function adminIndex() {
$posts = Post::with('tags')->latest()->get();
return view('/posts.admin-index', compact('posts'));
}

public function create()
{
return view('/posts.create');
Expand Down Expand Up @@ -73,24 +67,33 @@ public function store(Request $request)

sendMailNotifyToAdmin(new PostCreated($post));
flash( 'Post created successfully');
pushNotification('Post created successfully', 'New Notification');

return redirect('/');
}

public function show(Post $post)
{
$this->authorize('view', $post);

return view('/posts.show', compact('post'));
}

public function edit(Post $post)
{
$this->authorize('update', $post);

return view('/posts.edit', compact('post'));
}

public function update(Request $request, Post $post)
{
$this->authorize('update', $post);

$values = $this->validateRequest($request, $post);

$values['published'] = $request->has('published');

$post->update($values);

$postTags = $post->tags->keyBy('name');
Expand Down Expand Up @@ -121,17 +124,21 @@ public function update(Request $request, Post $post)

sendMailNotifyToAdmin(new PostEdited($post));
flash( 'Post edited successfully');
pushNotification('Post edited successfully', 'New Notification');

return back();
}

public function destroy(Post $post)
{
$this->authorize('delete', $post);

$post->delete();

sendMailNotifyToAdmin(new PostDeleted($post));
flash( 'Post delete successfully');
flash( 'Post deleted successfully');
pushNotification('Post deleted successfully', 'New Notification');

return redirect('/posts');
return back();
}
}
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ class Kernel extends HttpKernel
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'role' => \App\Http\Middleware\RoleMiddleware::class,
];
}
26 changes: 26 additions & 0 deletions app/Http/Middleware/RoleMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class RoleMiddleware
{
public function handle(Request $request, Closure $next, $role, $permission = null)
{
if (auth()->user()) {
if (!auth()->user()->hasRole($role)) {
abort(403, 'THIS ACTION IS UNAUTHORIZED.');
}

if ($permission !== null && !auth()->user()->hasPermissionTo($permission)) {
abort(403, 'THIS ACTION IS UNAUTHORIZED.');
}
} else {
return redirect('/');
}

return $next($request);
}
}
37 changes: 37 additions & 0 deletions app/Notifications/PostsForPeriod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class PostsForPeriod extends Notification
{
use Queueable;

public $posts;

public function __construct($posts)
{
$this->posts = $posts;
}

public function via($notifiable)
{
return ['mail'];
}

public function toMail($notifiable)
{
return (new MailMessage)->markdown('mail.posts-for-period', ['posts' => $this->posts]);
}

public function toArray($notifiable)
{
return [
//
];
}
}
25 changes: 25 additions & 0 deletions app/Permission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App;

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

class Permission extends Model
{
use HasFactory;

protected $guarded = [];

public $timestamps = false;

public function roles()
{
return $this->belongsToMany(Role::class);
}

public function users()
{
return $this->belongsToMany(User::class);
}
}
10 changes: 10 additions & 0 deletions app/Policies/PostPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ class PostPolicy
{
use HandlesAuthorization;

public function view(User $user, Post $post)
{
return $post->owner_id == $user->id;
}

public function update(User $user, Post $post)
{
return $post->owner_id == $user->id;
}

public function delete(User $user, Post $post)
{
return $post->owner_id == $user->id;
}
}
3 changes: 3 additions & 0 deletions app/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace App;

use App\Events\PostCreated;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use HasFactory;

protected $table = 'posts';

protected $guarded = [];
Expand Down
6 changes: 4 additions & 2 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Policies\PostPolicy;
use App\Post;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Contracts\Auth\Access\Gate;

Expand All @@ -14,7 +16,7 @@ class AuthServiceProvider extends ServiceProvider
*/
protected $policies = [
// 'App\Model' => 'App\Policies\ModelPolicy',
'App\Post' => 'App\Policies\PostPolicy',
Post::class => PostPolicy::class
];

/**
Expand All @@ -28,7 +30,7 @@ public function boot(Gate $gate)
$this->registerPolicies();

$gate->before(function ($user) {
if ($user->email == 'admin@mail.ru') {
if ($user->email == env('ADMIN_EMAIL_FOR_NOTIFICATIONS')) {
return true;
}
});
Expand Down
21 changes: 21 additions & 0 deletions app/Providers/PushNotificationServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Providers;

use App\Services\PushNotificationsService;
use Illuminate\Support\ServiceProvider;

class PushNotificationServiceProvider extends ServiceProvider
{
public function register()
{

}

public function boot()
{
$this->app->singleton(PushNotificationsService::class, function() {
return new PushNotificationsService(env('PUSH_ALL_API_ID'), env('PUSH_ALL_API_KEY'));
});
}
}
Loading