1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tinyissue package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tinyissue\Extensions\Auth; |
13
|
|
|
|
14
|
|
|
use Tinyissue\Model\User; |
15
|
|
|
use Illuminate\Contracts\Auth\Guard; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
trait LoggedUser |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var mixed |
24
|
|
|
*/ |
25
|
|
|
protected $loggedUser; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Guard |
29
|
|
|
*/ |
30
|
|
|
protected $auth; |
31
|
|
|
|
32
|
|
|
protected function setAuth(Guard $auth) |
33
|
|
|
{ |
34
|
|
|
$this->auth = $auth; |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Return instance of the logged user. |
41
|
|
|
* |
42
|
|
|
* @param mixed $user |
43
|
|
|
* |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
|
|
public function setLoggedUser($user) |
47
|
|
|
{ |
48
|
|
|
$this->loggedUser = $user; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function isLoggedIn() |
54
|
|
|
{ |
55
|
|
|
$this->lazyLoadLoggedUser(); |
56
|
|
|
|
57
|
|
|
return $this->loggedUser instanceof User; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Is current logged in user with role User. |
62
|
|
|
* |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
protected function isLoggedNormalUser() |
66
|
|
|
{ |
67
|
|
|
return $this->isLoggedIn() && $this->getLoggedUser()->isUser(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Inject the instance of logged user. |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
protected function lazyLoadLoggedUser() |
76
|
|
|
{ |
77
|
|
|
if (!$this->auth instanceof Guard) { |
78
|
|
|
$this->setAuth(auth()); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (null === $this->loggedUser) { |
82
|
|
|
$this->loggedUser = $this->auth->user(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return instance of the logged user. |
88
|
|
|
* |
89
|
|
|
* @return User |
90
|
|
|
*/ |
91
|
|
|
public function getLoggedUser() |
92
|
|
|
{ |
93
|
|
|
$this->lazyLoadLoggedUser(); |
94
|
|
|
|
95
|
|
|
/* @var \Tinyissue\Services\SettingsManager $settings */ |
96
|
|
|
if (!$this->isLoggedIn() && !app('tinyissue.settings')->isPublicProjectsEnabled()) { |
97
|
|
|
throw new \DomainException('Unable to find a valid instance of logged user.'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->loggedUser; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.