1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\HeyMan; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Gate; |
6
|
|
|
|
7
|
|
|
class YouShouldHave |
8
|
|
|
{ |
9
|
|
|
public $predicate; |
10
|
|
|
|
11
|
41 |
|
public function youShouldHaveRole(string $role) |
12
|
|
|
{ |
13
|
41 |
|
return $this->thisGateShouldAllow('heyman.youShouldHaveRole', $role); |
14
|
|
|
} |
15
|
|
|
|
16
|
45 |
|
public function thisGateShouldAllow($gate, ...$parameters) |
17
|
|
|
{ |
18
|
45 |
|
$gate = $this->defineNewGate($gate); |
19
|
|
|
|
20
|
|
|
$this->predicate = function (...$payload) use ($gate, $parameters) { |
21
|
42 |
|
return Gate::allows($gate, (array_merge($parameters, ...$payload))); |
22
|
|
|
}; |
23
|
|
|
|
24
|
45 |
|
return new Otherwise(); |
25
|
|
|
} |
26
|
|
|
|
27
|
4 |
|
public function thisClosureShouldAllow($callback, array $parameters = []) |
28
|
|
|
{ |
29
|
4 |
|
return $this->thisMethodShouldAllow($callback, $parameters); |
30
|
|
|
} |
31
|
|
|
|
32
|
6 |
|
public function thisMethodShouldAllow($callback, array $parameters = []) |
33
|
|
|
{ |
34
|
|
|
$this->predicate = function (...$payload) use ($callback, $parameters) { |
35
|
6 |
|
return (bool) app()->call($callback, array_merge($parameters, ...$payload)); |
36
|
|
|
}; |
37
|
|
|
|
38
|
6 |
|
return new Otherwise(); |
39
|
|
|
} |
40
|
|
|
|
41
|
3 |
|
public function thisValueShouldAllow($value) |
42
|
|
|
{ |
43
|
|
|
$this->predicate = function () use ($value) { |
44
|
3 |
|
return (bool) $value; |
45
|
|
|
}; |
46
|
|
|
|
47
|
3 |
|
return new Otherwise(); |
48
|
|
|
} |
49
|
|
|
|
50
|
6 |
|
public function youShouldBeGuest() |
51
|
|
|
{ |
52
|
|
|
$this->predicate = function () { |
53
|
6 |
|
return auth()->guest(); |
54
|
|
|
}; |
55
|
|
|
|
56
|
6 |
|
return new Otherwise(); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function sessionShouldHave($key) |
60
|
|
|
{ |
61
|
|
|
$this->predicate = function () use ($key) { |
62
|
1 |
|
return session()->has($key); |
63
|
|
|
}; |
64
|
|
|
|
65
|
1 |
|
return new Otherwise(); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function youShouldBeLoggedIn() |
69
|
|
|
{ |
70
|
|
|
$this->predicate = function () { |
71
|
1 |
|
return auth()->check(); |
72
|
|
|
}; |
73
|
|
|
|
74
|
1 |
|
return new Otherwise(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function immediately() |
78
|
|
|
{ |
79
|
|
|
$this->predicate = function () { |
80
|
|
|
return false; |
81
|
|
|
}; |
82
|
|
|
|
83
|
|
|
return new Actions(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param $gate |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
45 |
|
private function defineNewGate($gate): string |
92
|
|
|
{ |
93
|
|
|
// Define a Gate for inline closures passed as gate |
94
|
45 |
|
if (is_callable($gate)) { |
95
|
1 |
|
$closure = $gate; |
96
|
1 |
|
$gate = str_random(10); |
97
|
1 |
|
Gate::define($gate, $closure); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Define a Gate for "class@method" gates |
101
|
45 |
|
if (is_string($gate) && str_contains($gate, '@')) { |
102
|
1 |
|
Gate::define($gate, $gate); |
103
|
|
|
} |
104
|
|
|
|
105
|
45 |
|
return $gate; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|