-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
UserRepository.php
126 lines (105 loc) · 3.34 KB
/
UserRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
* @template T of User
*/
class UserRepository
{
/**
* @return Builder<User>
*/
public function query(): Builder
{
return User::query();
}
/**
* Find a user by ID, optionally making sure it is visible to a certain
* user, or throw an exception.
*
* @return T
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function findOrFail(int|string $id, User $actor = null): Model
{
$query = $this->query()->where('id', $id);
return $this->scopeVisibleTo($query, $actor)->firstOrFail();
}
/**
* Find a user by username, optionally making sure it is visible to a certain
* user, or throw an exception.
*
* @return T
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function findOrFailByUsername(string $username, User $actor = null): Model
{
$query = $this->query()->where('username', $username);
return $this->scopeVisibleTo($query, $actor)->firstOrFail();
}
/**
* Find a user by an identification (username or email).
*
* @return ?T
*/
public function findByIdentification(string $identification): ?Model
{
$field = filter_var($identification, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
return $this->query()->where($field, $identification)->first();
}
/**
* @return ?T
*/
public function findByEmail(string $email): ?Model
{
return $this->query()->where('email', $email)->first();
}
public function getIdForUsername(string $username, User $actor = null): ?int
{
$query = $this->query()->where('username', $username);
return $this->scopeVisibleTo($query, $actor)->value('id');
}
public function getIdsForUsernames(array $usernames, User $actor = null): array
{
$query = $this->query()->whereIn('username', $usernames);
return $this->scopeVisibleTo($query, $actor)->pluck('id')->all();
}
/**
* Find users by matching a string of words against their username,
* optionally making sure they are visible to a certain user.
*/
public function getIdsForUsername(string $string, User $actor = null): array
{
$string = $this->escapeLikeString($string);
$query = $this->query()->where('username', 'like', '%'.$string.'%')
->orderByRaw('username = ? desc', [$string])
->orderByRaw('username like ? desc', [$string.'%']);
return $this->scopeVisibleTo($query, $actor)->pluck('id')->all();
}
/**
* @return Builder<User>
*/
protected function scopeVisibleTo(Builder $query, User $actor = null): Builder
{
if ($actor !== null) {
$query->whereVisibleTo($actor);
}
return $query;
}
/**
* Escape special characters that can be used as wildcards in a LIKE query.
*/
private function escapeLikeString(string $string): string
{
return str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], $string);
}
}