forked from JeffreyWay/council
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuthServiceProvider.php
142 lines (119 loc) · 4.93 KB
/
AuthServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace App\Providers;
use App\Exceptions\ThrottleException;
use App\Reply;
use App\Thread;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
use Imanghafoori\HeyMan\Facades\HeyMan;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
//\App\Thread::class => \App\Policies\ThreadPolicy::class,
//\App\Reply::class => \App\Policies\ReplyPolicy::class,
\App\User::class => \App\Policies\UserPolicy::class,
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
$this->authenticateRoutes();
$this->validateRequests();
$this->authorizeAdminRoutes();
$this->authorizeEloquentModels();
}
private function authenticateRoutes()
{
HeyMan::whenYouHitRouteName([
'avatar',
'user-notifications',
'user-notifications.destroy',
'replies.favorite',
'replies.unfavorite',
'threads.update',
'threads.destroy',
'threads.store',
'replies.store',
'replies.update',
'replies.destroy'
])->youShouldBeLoggedIn()->otherwise()->weRespondFrom('\App\Http\Responses\Authentication@handle');
}
private function validateRequests()
{
HeyMan::whenYouHitRouteName('replies.store')->yourRequestShouldBeValid(['body' => 'required|spamfree']);
HeyMan::whenYouHitRouteName('threads.update')->yourRequestShouldBeValid([
'title' => 'required',
'body' => 'required',
]);
HeyMan::whenYouHitRouteName('admin.channels.store')->yourRequestShouldBeValid([
'name' => 'required|unique:channels',
'color' => 'required',
'description' => 'required',
]);
HeyMan::whenYouHitRouteName('admin.channels.update')->yourRequestShouldBeValid(function () {
return [
'name' => ['required', Rule::unique('channels')->ignore(request()->route('channel'), 'slug')],
'description' => 'required',
'color' => 'required',
'archived' => 'required|boolean',
];
});
HeyMan::whenYouCallAction('RepliesController@update')->yourRequestShouldBeValid(['name' => 'required|spamfree',]);
HeyMan::whenYouHitRouteName('avatar')->yourRequestShouldBeValid(['avatar' => ['required', 'image']]);
HeyMan::whenYouSendPost('threads')->yourRequestShouldBeValid(function () {
return [
'title' => 'required|spamfree',
'body' => 'required|spamfree',
'channel_id' => [
'required',
Rule::exists('channels', 'id')->where(function ($query) {
$query->where('archived', false);
}),
],
//'g-recaptcha-response' => ['required', $recaptcha]
];
});
}
private function authorizeAdminRoutes()
{
Gate::define('isAdmin', function ($user) {
return $user->isAdmin();
});
HeyMan::whenYouHitRouteName([
'locked-threads.store',
'locked-threads.destroy',
'pinned-threads.store',
'pinned-threads.destroy',
])->thisGateShouldAllow('isAdmin')->otherwise()->abort(403, 'You do not have permission to perform this action.');
HeyMan::whenYouHitRouteName('admin.*')->thisGateShouldAllow('isAdmin')->otherwise()->weDenyAccess();
}
private function authorizeEloquentModels()
{
Gate::define('createReply', function ($user) {
if (! $lastReply = $user->fresh()->lastReply) {
return true;
}
return ! $lastReply->wasJustPublished();
});
$hasConfirmedEmail = function () {
return auth()->user()->confirmed or auth()->user()->isAdmin();
};
HeyMan::whenYouCreate(Thread::class)->thisClosureShouldAllow($hasConfirmedEmail)->otherwise()->redirect()->to('/threads')->with('flash', 'You must first confirm your email address.');
HeyMan::whenYouHitRouteName('replies.store')->thisGateShouldAllow('createReply')->otherwise()->weThrowNew(ThrottleException::class, 'You are replying too frequently. Please take a break.');
Gate::define('ownModel', function ($user, $model) {
return $user->id == $model->user_id or $user->isAdmin();
});
HeyMan::whenYouUpdate([Thread::class, Reply::class])->thisGateShouldAllow('ownModel')->otherwise()->weDenyAccess();
HeyMan::whenYouDelete([Thread::class, Reply::class])->thisGateShouldAllow('ownModel')->otherwise()->weDenyAccess();
}
}