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