|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\HeyMan; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Gate; |
|
6
|
|
|
|
|
7
|
|
|
class YouShouldHave |
|
8
|
|
|
{ |
|
9
|
|
|
public $predicate; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var Chain |
|
13
|
|
|
*/ |
|
14
|
|
|
private $chain; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* YouShouldHave constructor. |
|
18
|
|
|
* |
|
19
|
|
|
* @param Chain $chain |
|
20
|
|
|
*/ |
|
21
|
83 |
|
public function __construct(Chain $chain) |
|
22
|
|
|
{ |
|
23
|
83 |
|
$this->chain = $chain; |
|
24
|
83 |
|
} |
|
25
|
|
|
|
|
26
|
11 |
|
public function youShouldHaveRole(string $role): Otherwise |
|
27
|
|
|
{ |
|
28
|
11 |
|
return $this->thisGateShouldAllow('heyman.youShouldHaveRole', $role); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
14 |
|
public function thisGateShouldAllow($gate, ...$parameters): Otherwise |
|
32
|
|
|
{ |
|
33
|
14 |
|
$gate = $this->defineNewGate($gate); |
|
34
|
|
|
|
|
35
|
|
|
$this->chain->predicate = function (...$payload) use ($gate, $parameters) { |
|
36
|
14 |
|
return Gate::allows($gate, (array_merge($parameters, ...$payload))); |
|
37
|
|
|
}; |
|
38
|
|
|
|
|
39
|
14 |
|
return app(Otherwise::class); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
4 |
|
public function thisClosureShouldAllow(callable $callback, array $parameters = []): Otherwise |
|
43
|
|
|
{ |
|
44
|
4 |
|
return $this->thisMethodShouldAllow($callback, $parameters); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
6 |
|
public function thisMethodShouldAllow($callback, array $parameters = []): Otherwise |
|
48
|
|
|
{ |
|
49
|
|
|
$this->chain->predicate = function (...$payload) use ($callback, $parameters) { |
|
50
|
6 |
|
return (bool) app()->call($callback, array_merge($parameters, ...$payload)); |
|
51
|
|
|
}; |
|
52
|
|
|
|
|
53
|
6 |
|
return app(Otherwise::class); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
53 |
|
public function thisValueShouldAllow($value): Otherwise |
|
57
|
|
|
{ |
|
58
|
|
|
$this->chain->predicate = function () use ($value) { |
|
59
|
37 |
|
return (bool) $value; |
|
60
|
|
|
}; |
|
61
|
|
|
|
|
62
|
53 |
|
return app(Otherwise::class); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
public function youShouldBeGuest($guard = null): Otherwise |
|
66
|
|
|
{ |
|
67
|
|
|
$this->chain->predicate = function () use ($guard) { |
|
68
|
2 |
|
return auth($guard)->guest(); |
|
69
|
|
|
}; |
|
70
|
|
|
|
|
71
|
2 |
|
return app(Otherwise::class); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
public function sessionShouldHave($key): Otherwise |
|
75
|
|
|
{ |
|
76
|
|
|
$this->chain->predicate = function () use ($key) { |
|
77
|
1 |
|
return session()->has($key); |
|
78
|
|
|
}; |
|
79
|
|
|
|
|
80
|
1 |
|
return app(Otherwise::class); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
3 |
|
public function youShouldBeLoggedIn($guard = null): Otherwise |
|
84
|
|
|
{ |
|
85
|
|
|
$this->chain->predicate = function () use ($guard) { |
|
86
|
2 |
|
return auth($guard)->check(); |
|
87
|
|
|
}; |
|
88
|
|
|
|
|
89
|
3 |
|
return app(Otherwise::class); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
3 |
|
public function youShouldAlways(): Reactions |
|
93
|
|
|
{ |
|
94
|
|
|
$this->chain->predicate = function () { |
|
95
|
3 |
|
return false; |
|
96
|
|
|
}; |
|
97
|
|
|
|
|
98
|
3 |
|
return app(Reactions::class); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param $rules |
|
103
|
|
|
* |
|
104
|
|
|
* @return null |
|
105
|
|
|
*/ |
|
106
|
3 |
|
public function yourRequestShouldBeValid($rules) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->chain->predicate = function () use ($rules) { |
|
109
|
2 |
|
if (is_callable($rules)) { |
|
110
|
1 |
|
$rules = $rules(); |
|
111
|
|
|
} |
|
112
|
2 |
|
$validator = \Illuminate\Support\Facades\Validator::make(request()->all(), $rules); |
|
113
|
2 |
|
$validator->validate(); |
|
114
|
|
|
}; |
|
115
|
|
|
|
|
116
|
3 |
|
app(Chain::class)->submitChainConfig(); |
|
117
|
3 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param $gate |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
14 |
|
private function defineNewGate($gate): string |
|
125
|
|
|
{ |
|
126
|
|
|
// Define a Gate for inline closures passed as gate |
|
127
|
14 |
|
if (is_callable($gate)) { |
|
128
|
1 |
|
$closure = $gate; |
|
129
|
1 |
|
$gate = str_random(10); |
|
130
|
1 |
|
Gate::define($gate, $closure); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// Define a Gate for "class@method" gates |
|
134
|
14 |
|
if (is_string($gate) && str_contains($gate, '@')) { |
|
135
|
1 |
|
Gate::define($gate, $gate); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
14 |
|
return $gate; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|