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 Illuminate\Auth\SessionGuard; |
15
|
|
|
use Illuminate\Contracts\Auth\Guard; |
16
|
|
|
use Tinyissue\Model\User; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
trait LoggedUser |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var mixed |
25
|
|
|
*/ |
26
|
|
|
protected $loggedUser; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Illuminate\Contracts\Auth\Factory|\Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard |
30
|
|
|
*/ |
31
|
|
|
protected $auth; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param \Illuminate\Contracts\Auth\Factory|\Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard $auth |
35
|
|
|
* |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
|
|
protected function setAuth($auth) |
39
|
|
|
{ |
40
|
|
|
$this->auth = $auth; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return SessionGuard |
47
|
|
|
*/ |
48
|
|
|
protected function getAuth() |
49
|
|
|
{ |
50
|
|
|
if (!$this->auth instanceof SessionGuard) { |
51
|
|
|
throw new \LogicException('Auth property is not an instance of SessionGuard'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->auth; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return instance of the logged user. |
59
|
|
|
* |
60
|
|
|
* @param mixed $user |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function setLoggedUser($user) |
65
|
|
|
{ |
66
|
|
|
$this->loggedUser = $user; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function isLoggedIn() |
72
|
|
|
{ |
73
|
|
|
$this->lazyLoadLoggedUser(); |
74
|
|
|
|
75
|
|
|
return $this->loggedUser instanceof User; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Is current logged in user with role User. |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
protected function isLoggedNormalUser() |
84
|
|
|
{ |
85
|
|
|
return $this->isLoggedIn() && $this->getLoggedUser()->isUser(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Inject the instance of logged user. |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
protected function lazyLoadLoggedUser() |
94
|
|
|
{ |
95
|
|
|
if (!$this->auth instanceof Guard) { |
96
|
|
|
$this->setAuth(auth()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (null === $this->loggedUser) { |
100
|
|
|
$this->loggedUser = $this->auth->user(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return instance of the logged user. |
106
|
|
|
* |
107
|
|
|
* @return User |
108
|
|
|
*/ |
109
|
|
|
public function getLoggedUser() |
110
|
|
|
{ |
111
|
|
|
$this->lazyLoadLoggedUser(); |
112
|
|
|
|
113
|
|
|
/* @var \Tinyissue\Services\SettingsManager $settings */ |
114
|
|
|
if (!$this->isLoggedIn() && !app('tinyissue.settings')->isPublicProjectsEnabled()) { |
115
|
|
|
throw new \DomainException('Unable to find a valid instance of logged user.'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->loggedUser; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|