1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwooleTW\Http\Websocket; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait Authenticatable |
10
|
|
|
* |
11
|
|
|
* @property-read \SwooleTW\Http\Websocket\Rooms\RoomContract $room |
12
|
|
|
*/ |
13
|
|
|
trait Authenticatable |
14
|
|
|
{ |
15
|
|
|
protected $userId; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Login using current user. |
19
|
|
|
* |
20
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
21
|
|
|
* |
22
|
|
|
* @return mixed |
23
|
|
|
*/ |
24
|
|
|
public function loginUsing(AuthenticatableContract $user) |
25
|
|
|
{ |
26
|
|
|
return $this->loginUsingId($user->getAuthIdentifier()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Login using current userId. |
31
|
|
|
* |
32
|
|
|
* @param $userId |
33
|
|
|
* |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function loginUsingId($userId) |
37
|
|
|
{ |
38
|
|
|
return $this->join(static::USER_PREFIX . $userId); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Logout with current sender's fd. |
43
|
|
|
* |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function logout() |
47
|
|
|
{ |
48
|
|
|
if (is_null($userId = $this->getUserId())) { |
49
|
|
|
return null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->leave(static::USER_PREFIX . $userId); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set multiple recepients' fds by users. |
57
|
|
|
* |
58
|
|
|
* @param $users |
59
|
|
|
* |
60
|
|
|
* @return \SwooleTW\Http\Websocket\Authenticatable |
61
|
|
|
*/ |
62
|
|
|
public function toUser($users) |
63
|
|
|
{ |
64
|
|
|
$users = is_object($users) ? func_get_args() : $users; |
65
|
|
|
|
66
|
|
|
$userIds = array_map(function (AuthenticatableContract $user) { |
67
|
|
|
$this->checkUser($user); |
68
|
|
|
|
69
|
|
|
return $user->getAuthIdentifier(); |
70
|
|
|
}, $users); |
71
|
|
|
|
72
|
|
|
return $this->toUserId($userIds); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set multiple recepients' fds by userIds. |
77
|
|
|
* |
78
|
|
|
* @param $userIds |
79
|
|
|
* |
80
|
|
|
* @return \SwooleTW\Http\Websocket\Authenticatable |
81
|
|
|
*/ |
82
|
|
|
public function toUserId($userIds) |
83
|
|
|
{ |
84
|
|
|
$userIds = is_string($userIds) || is_integer($userIds) ? func_get_args() : $userIds; |
85
|
|
|
|
86
|
|
|
foreach ($userIds as $userId) { |
87
|
|
|
$fds = $this->room->getClients(static::USER_PREFIX . $userId); |
|
|
|
|
88
|
|
|
$this->to($fds); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get current auth user id by sender's fd. |
96
|
|
|
*/ |
97
|
|
|
public function getUserId() |
98
|
|
|
{ |
99
|
|
|
if (! is_null($this->userId)) { |
100
|
|
|
return $this->userId; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$rooms = $this->room->getRooms($this->getSender()); |
|
|
|
|
104
|
|
|
|
105
|
|
|
foreach ($rooms as $room) { |
106
|
|
|
if (count($explode = explode(static::USER_PREFIX, $room)) === 2) { |
|
|
|
|
107
|
|
|
$this->userId = $explode[1]; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->userId; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Check if a user is online by given userId. |
116
|
|
|
* |
117
|
|
|
* @param $userId |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function isUserIdOnline($userId) |
122
|
|
|
{ |
123
|
|
|
return ! empty($this->room->getClients(static::USER_PREFIX . $userId)); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Check if user object implements AuthenticatableContract. |
128
|
|
|
* |
129
|
|
|
* @param $user |
130
|
|
|
*/ |
131
|
|
|
protected function checkUser($user) |
132
|
|
|
{ |
133
|
|
|
if (! $user instanceOf AuthenticatableContract) { |
134
|
|
|
throw new InvalidArgumentException('user object must implement ' . AuthenticatableContract::class); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|