/
UserRepository.php
128 lines (111 loc) · 3.14 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
127
128
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\User;
use Illuminate\Database\Eloquent\Builder;
class UserRepository
{
/**
* Get a new query builder for the users table.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function query()
{
return User::query();
}
/**
* Find a user by ID, optionally making sure it is visible to a certain
* user, or throw an exception.
*
* @param int $id
* @param User $actor
* @return User
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function findOrFail($id, User $actor = null)
{
$query = User::where('id', $id);
return $this->scopeVisibleTo($query, $actor)->firstOrFail();
}
/**
* Find a user by an identification (username or email).
*
* @param string $identification
* @return User|null
*/
public function findByIdentification($identification)
{
$field = filter_var($identification, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
return User::where($field, $identification)->first();
}
/**
* Find a user by email.
*
* @param string $email
* @return User|null
*/
public function findByEmail($email)
{
return User::where('email', $email)->first();
}
/**
* Get the ID of a user with the given username.
*
* @param string $username
* @param User|null $actor
* @return int|null
*/
public function getIdForUsername($username, User $actor = null)
{
$query = User::where('username', $username);
return $this->scopeVisibleTo($query, $actor)->value('id');
}
/**
* Find users by matching a string of words against their username,
* optionally making sure they are visible to a certain user.
*
* @param string $string
* @param User|null $actor
* @return array
*/
public function getIdsForUsername($string, User $actor = null)
{
$string = $this->escapeLikeString($string);
$query = User::where('username', 'like', '%'.$string.'%')
->orderByRaw('username = ? desc', [$string])
->orderByRaw('username like ? desc', [$string.'%']);
return $this->scopeVisibleTo($query, $actor)->pluck('id')->all();
}
/**
* Scope a query to only include records that are visible to a user.
*
* @param Builder $query
* @param User $actor
* @return Builder
*/
protected function scopeVisibleTo(Builder $query, User $actor = null)
{
if ($actor !== null) {
$query->whereVisibleTo($actor);
}
return $query;
}
/**
* Escape special characters that can be used as wildcards in a LIKE query.
*
* @param string $string
* @return string
*/
private function escapeLikeString($string)
{
return str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], $string);
}
}