xref: /webtrees/app/Services/UserService.php (revision 0b5fd0a636fa959f5279ee28ebd2f27e921c091e)
1e5a6b4d4SGreg Roach<?php
2e5a6b4d4SGreg Roach/**
3e5a6b4d4SGreg Roach * webtrees: online genealogy
4e5a6b4d4SGreg Roach * Copyright (C) 2019 webtrees development team
5e5a6b4d4SGreg Roach * This program is free software: you can redistribute it and/or modify
6e5a6b4d4SGreg Roach * it under the terms of the GNU General Public License as published by
7e5a6b4d4SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8e5a6b4d4SGreg Roach * (at your option) any later version.
9e5a6b4d4SGreg Roach * This program is distributed in the hope that it will be useful,
10e5a6b4d4SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11e5a6b4d4SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12e5a6b4d4SGreg Roach * GNU General Public License for more details.
13e5a6b4d4SGreg Roach * You should have received a copy of the GNU General Public License
14e5a6b4d4SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15e5a6b4d4SGreg Roach */
16e5a6b4d4SGreg Roachdeclare(strict_types=1);
17e5a6b4d4SGreg Roach
18e5a6b4d4SGreg Roachnamespace Fisharebest\Webtrees\Services;
19e5a6b4d4SGreg Roach
20e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Auth;
2186730b84SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
22e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Individual;
2386730b84SGreg Roachuse Fisharebest\Webtrees\Tree;
24e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\User;
25e5a6b4d4SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
26e5a6b4d4SGreg Roachuse Illuminate\Database\Query\JoinClause;
27e5a6b4d4SGreg Roachuse Illuminate\Support\Collection;
2886730b84SGreg Roachuse Symfony\Component\HttpFoundation\Request;
29e5a6b4d4SGreg Roach
30e5a6b4d4SGreg Roach/**
31e5a6b4d4SGreg Roach * Functions for managing users.
32e5a6b4d4SGreg Roach */
33e5a6b4d4SGreg Roachclass UserService
34e5a6b4d4SGreg Roach{
35e5a6b4d4SGreg Roach    /**
36e5a6b4d4SGreg Roach     * Find the user with a specified user_id.
37e5a6b4d4SGreg Roach     *
38e5a6b4d4SGreg Roach     * @param int|null $user_id
39e5a6b4d4SGreg Roach     *
40e5a6b4d4SGreg Roach     * @return User|null
41e5a6b4d4SGreg Roach     */
4225d7fe95SGreg Roach    public function find($user_id): ?User
43e5a6b4d4SGreg Roach    {
44*0b5fd0a6SGreg Roach        return app('cache.array')->rememberForever(__CLASS__ . $user_id, static function () use ($user_id): ?User {
45e5a6b4d4SGreg Roach            return DB::table('user')
46e5a6b4d4SGreg Roach                ->where('user_id', '=', $user_id)
47e5a6b4d4SGreg Roach                ->get()
48e5a6b4d4SGreg Roach                ->map(User::rowMapper())
49e5a6b4d4SGreg Roach                ->first();
50e5a6b4d4SGreg Roach        });
51e5a6b4d4SGreg Roach    }
52e5a6b4d4SGreg Roach
53e5a6b4d4SGreg Roach    /**
54e5a6b4d4SGreg Roach     * Find the user with a specified email address.
55e5a6b4d4SGreg Roach     *
56e5a6b4d4SGreg Roach     * @param string $email
57e5a6b4d4SGreg Roach     *
58e5a6b4d4SGreg Roach     * @return User|null
59e5a6b4d4SGreg Roach     */
60e364afe4SGreg Roach    public function findByEmail($email): ?User
61e5a6b4d4SGreg Roach    {
62e5a6b4d4SGreg Roach        return DB::table('user')
63e5a6b4d4SGreg Roach            ->where('email', '=', $email)
64e5a6b4d4SGreg Roach            ->get()
65e5a6b4d4SGreg Roach            ->map(User::rowMapper())
66e5a6b4d4SGreg Roach            ->first();
67e5a6b4d4SGreg Roach    }
68e5a6b4d4SGreg Roach
69e5a6b4d4SGreg Roach    /**
70e5a6b4d4SGreg Roach     * Find the user with a specified user_name or email address.
71e5a6b4d4SGreg Roach     *
72e5a6b4d4SGreg Roach     * @param string $identifier
73e5a6b4d4SGreg Roach     *
74e5a6b4d4SGreg Roach     * @return User|null
75e5a6b4d4SGreg Roach     */
76e364afe4SGreg Roach    public function findByIdentifier($identifier): ?User
77e5a6b4d4SGreg Roach    {
78e5a6b4d4SGreg Roach        return DB::table('user')
79e5a6b4d4SGreg Roach            ->where('user_name', '=', $identifier)
80e5a6b4d4SGreg Roach            ->orWhere('email', '=', $identifier)
81e5a6b4d4SGreg Roach            ->get()
82e5a6b4d4SGreg Roach            ->map(User::rowMapper())
83e5a6b4d4SGreg Roach            ->first();
84e5a6b4d4SGreg Roach    }
85e5a6b4d4SGreg Roach
86e5a6b4d4SGreg Roach    /**
87e5a6b4d4SGreg Roach     * Find the user(s) with a specified genealogy record.
88e5a6b4d4SGreg Roach     *
89e5a6b4d4SGreg Roach     * @param Individual $individual
90e5a6b4d4SGreg Roach     *
9154c7f8dfSGreg Roach     * @return Collection
9254c7f8dfSGreg Roach     * @return User[]
93e5a6b4d4SGreg Roach     */
94e5a6b4d4SGreg Roach    public function findByIndividual(Individual $individual): Collection
95e5a6b4d4SGreg Roach    {
96e5a6b4d4SGreg Roach        return DB::table('user')
97e5a6b4d4SGreg Roach            ->join('user_gedcom_setting', 'user_gedcom_setting.user_id', '=', 'user.user_id')
98e5a6b4d4SGreg Roach            ->where('gedcom_id', '=', $individual->tree()->id())
99e5a6b4d4SGreg Roach            ->where('setting_value', '=', $individual->xref())
100e5a6b4d4SGreg Roach            ->where('setting_name', '=', 'gedcomid')
101e5a6b4d4SGreg Roach            ->select(['user.*'])
102e5a6b4d4SGreg Roach            ->get()
103e5a6b4d4SGreg Roach            ->map(User::rowMapper());
104e5a6b4d4SGreg Roach    }
105e5a6b4d4SGreg Roach
106e5a6b4d4SGreg Roach    /**
107e5a6b4d4SGreg Roach     * Find the user with a specified user_name.
108e5a6b4d4SGreg Roach     *
109e5a6b4d4SGreg Roach     * @param string $user_name
110e5a6b4d4SGreg Roach     *
111e5a6b4d4SGreg Roach     * @return User|null
112e5a6b4d4SGreg Roach     */
113e364afe4SGreg Roach    public function findByUserName($user_name): ?User
114e5a6b4d4SGreg Roach    {
115e5a6b4d4SGreg Roach        return DB::table('user')
116e5a6b4d4SGreg Roach            ->where('user_name', '=', $user_name)
117e5a6b4d4SGreg Roach            ->get()
118e5a6b4d4SGreg Roach            ->map(User::rowMapper())
119e5a6b4d4SGreg Roach            ->first();
120e5a6b4d4SGreg Roach    }
121e5a6b4d4SGreg Roach
122e5a6b4d4SGreg Roach    /**
123e5a6b4d4SGreg Roach     * Get a list of all users.
124e5a6b4d4SGreg Roach     *
12554c7f8dfSGreg Roach     * @return Collection
12654c7f8dfSGreg Roach     * @return User[]
127e5a6b4d4SGreg Roach     */
128e5a6b4d4SGreg Roach    public function all(): Collection
129e5a6b4d4SGreg Roach    {
130e5a6b4d4SGreg Roach        return DB::table('user')
131e5a6b4d4SGreg Roach            ->where('user_id', '>', 0)
132e5a6b4d4SGreg Roach            ->orderBy('real_name')
133e5a6b4d4SGreg Roach            ->get()
134e5a6b4d4SGreg Roach            ->map(User::rowMapper());
135e5a6b4d4SGreg Roach    }
136e5a6b4d4SGreg Roach
137e5a6b4d4SGreg Roach    /**
138e5a6b4d4SGreg Roach     * Get a list of all administrators.
139e5a6b4d4SGreg Roach     *
14054c7f8dfSGreg Roach     * @return Collection
14154c7f8dfSGreg Roach     * @return User[]
142e5a6b4d4SGreg Roach     */
143e5a6b4d4SGreg Roach    public function administrators(): Collection
144e5a6b4d4SGreg Roach    {
145e5a6b4d4SGreg Roach        return DB::table('user')
146*0b5fd0a6SGreg Roach            ->join('user_setting', static function (JoinClause $join): void {
147e5a6b4d4SGreg Roach                $join
148e5a6b4d4SGreg Roach                    ->on('user_setting.user_id', '=', 'user.user_id')
149e5a6b4d4SGreg Roach                    ->where('user_setting.setting_name', '=', 'canadmin')
150e5a6b4d4SGreg Roach                    ->where('user_setting.setting_value', '=', '1');
151e5a6b4d4SGreg Roach            })
152e5a6b4d4SGreg Roach            ->where('user.user_id', '>', 0)
153e5a6b4d4SGreg Roach            ->orderBy('real_name')
154e5a6b4d4SGreg Roach            ->select(['user.*'])
155e5a6b4d4SGreg Roach            ->get()
156e5a6b4d4SGreg Roach            ->map(User::rowMapper());
157e5a6b4d4SGreg Roach    }
158e5a6b4d4SGreg Roach
159e5a6b4d4SGreg Roach    /**
160e5a6b4d4SGreg Roach     * Get a list of all managers.
161e5a6b4d4SGreg Roach     *
16254c7f8dfSGreg Roach     * @return Collection
16354c7f8dfSGreg Roach     * @return User[]
164e5a6b4d4SGreg Roach     */
165e5a6b4d4SGreg Roach    public function managers(): Collection
166e5a6b4d4SGreg Roach    {
167e5a6b4d4SGreg Roach        return DB::table('user')
168*0b5fd0a6SGreg Roach            ->join('user_gedcom_setting', static function (JoinClause $join): void {
169e5a6b4d4SGreg Roach                $join
170e5a6b4d4SGreg Roach                    ->on('user_gedcom_setting.user_id', '=', 'user.user_id')
171e5a6b4d4SGreg Roach                    ->where('user_gedcom_setting.setting_name', '=', 'canedit')
172e5a6b4d4SGreg Roach                    ->where('user_gedcom_setting.setting_value', '=', 'admin');
173e5a6b4d4SGreg Roach            })
174e5a6b4d4SGreg Roach            ->where('user.user_id', '>', 0)
175e5a6b4d4SGreg Roach            ->orderBy('real_name')
176e5a6b4d4SGreg Roach            ->select(['user.*'])
177e5a6b4d4SGreg Roach            ->get()
178e5a6b4d4SGreg Roach            ->map(User::rowMapper());
179e5a6b4d4SGreg Roach    }
180e5a6b4d4SGreg Roach
181e5a6b4d4SGreg Roach    /**
182e5a6b4d4SGreg Roach     * Get a list of all moderators.
183e5a6b4d4SGreg Roach     *
18454c7f8dfSGreg Roach     * @return Collection
18554c7f8dfSGreg Roach     * @return User[]
186e5a6b4d4SGreg Roach     */
187e5a6b4d4SGreg Roach    public function moderators(): Collection
188e5a6b4d4SGreg Roach    {
189e5a6b4d4SGreg Roach        return DB::table('user')
190*0b5fd0a6SGreg Roach            ->join('user_gedcom_setting', static function (JoinClause $join): void {
191e5a6b4d4SGreg Roach                $join
192e5a6b4d4SGreg Roach                    ->on('user_gedcom_setting.user_id', '=', 'user.user_id')
193e5a6b4d4SGreg Roach                    ->where('user_gedcom_setting.setting_name', '=', 'canedit')
194e5a6b4d4SGreg Roach                    ->where('user_gedcom_setting.setting_value', '=', 'accept');
195e5a6b4d4SGreg Roach            })
196e5a6b4d4SGreg Roach            ->where('user.user_id', '>', 0)
197e5a6b4d4SGreg Roach            ->orderBy('real_name')
198e5a6b4d4SGreg Roach            ->select(['user.*'])
199e5a6b4d4SGreg Roach            ->get()
200e5a6b4d4SGreg Roach            ->map(User::rowMapper());
201e5a6b4d4SGreg Roach    }
202e5a6b4d4SGreg Roach
203e5a6b4d4SGreg Roach    /**
204e5a6b4d4SGreg Roach     * Get a list of all verified users.
205e5a6b4d4SGreg Roach     *
20654c7f8dfSGreg Roach     * @return Collection
20754c7f8dfSGreg Roach     * @return User[]
208e5a6b4d4SGreg Roach     */
209e5a6b4d4SGreg Roach    public function unapproved(): Collection
210e5a6b4d4SGreg Roach    {
211e5a6b4d4SGreg Roach        return DB::table('user')
212*0b5fd0a6SGreg Roach            ->join('user_setting', static function (JoinClause $join): void {
213e5a6b4d4SGreg Roach                $join
214e5a6b4d4SGreg Roach                    ->on('user_setting.user_id', '=', 'user.user_id')
215e5a6b4d4SGreg Roach                    ->where('user_setting.setting_name', '=', 'verified_by_admin')
216e5a6b4d4SGreg Roach                    ->where('user_setting.setting_value', '=', '0');
217e5a6b4d4SGreg Roach            })
218e5a6b4d4SGreg Roach            ->where('user.user_id', '>', 0)
219e5a6b4d4SGreg Roach            ->orderBy('real_name')
220e5a6b4d4SGreg Roach            ->select(['user.*'])
221e5a6b4d4SGreg Roach            ->get()
222e5a6b4d4SGreg Roach            ->map(User::rowMapper());
223e5a6b4d4SGreg Roach    }
224e5a6b4d4SGreg Roach
225e5a6b4d4SGreg Roach    /**
226e5a6b4d4SGreg Roach     * Get a list of all verified users.
227e5a6b4d4SGreg Roach     *
22854c7f8dfSGreg Roach     * @return Collection
22954c7f8dfSGreg Roach     * @return User[]
230e5a6b4d4SGreg Roach     */
231e5a6b4d4SGreg Roach    public function unverified(): Collection
232e5a6b4d4SGreg Roach    {
233e5a6b4d4SGreg Roach        return DB::table('user')
234*0b5fd0a6SGreg Roach            ->join('user_setting', static function (JoinClause $join): void {
235e5a6b4d4SGreg Roach                $join
236e5a6b4d4SGreg Roach                    ->on('user_setting.user_id', '=', 'user.user_id')
237e5a6b4d4SGreg Roach                    ->where('user_setting.setting_name', '=', 'verified')
238e5a6b4d4SGreg Roach                    ->where('user_setting.setting_value', '=', '0');
239e5a6b4d4SGreg Roach            })
240e5a6b4d4SGreg Roach            ->where('user.user_id', '>', 0)
241e5a6b4d4SGreg Roach            ->orderBy('real_name')
242e5a6b4d4SGreg Roach            ->select(['user.*'])
243e5a6b4d4SGreg Roach            ->get()
244e5a6b4d4SGreg Roach            ->map(User::rowMapper());
245e5a6b4d4SGreg Roach    }
246e5a6b4d4SGreg Roach
247e5a6b4d4SGreg Roach    /**
248e5a6b4d4SGreg Roach     * Get a list of all users who are currently logged in.
249e5a6b4d4SGreg Roach     *
25054c7f8dfSGreg Roach     * @return Collection
25154c7f8dfSGreg Roach     * @return User[]
252e5a6b4d4SGreg Roach     */
253e5a6b4d4SGreg Roach    public function allLoggedIn(): Collection
254e5a6b4d4SGreg Roach    {
255e5a6b4d4SGreg Roach        return DB::table('user')
256e5a6b4d4SGreg Roach            ->join('session', 'session.user_id', '=', 'user.user_id')
257e5a6b4d4SGreg Roach            ->where('user.user_id', '>', 0)
258e5a6b4d4SGreg Roach            ->orderBy('real_name')
259e5a6b4d4SGreg Roach            ->select(['user.*'])
260e5a6b4d4SGreg Roach            ->distinct()
261e5a6b4d4SGreg Roach            ->get()
262e5a6b4d4SGreg Roach            ->map(User::rowMapper());
263e5a6b4d4SGreg Roach    }
264e5a6b4d4SGreg Roach
265e5a6b4d4SGreg Roach    /**
266e5a6b4d4SGreg Roach     * Create a new user.
267e5a6b4d4SGreg Roach     * The calling code needs to check for duplicates identifiers before calling
268e5a6b4d4SGreg Roach     * this function.
269e5a6b4d4SGreg Roach     *
270e5a6b4d4SGreg Roach     * @param string $user_name
271e5a6b4d4SGreg Roach     * @param string $real_name
272e5a6b4d4SGreg Roach     * @param string $email
273e5a6b4d4SGreg Roach     * @param string $password
274e5a6b4d4SGreg Roach     *
275e5a6b4d4SGreg Roach     * @return User
276e5a6b4d4SGreg Roach     */
2776be338f5SGreg Roach    public function create(string $user_name, string $real_name, string $email, string $password): User
278e5a6b4d4SGreg Roach    {
279e5a6b4d4SGreg Roach        DB::table('user')->insert([
280e5a6b4d4SGreg Roach            'user_name' => $user_name,
281e5a6b4d4SGreg Roach            'real_name' => $real_name,
282e5a6b4d4SGreg Roach            'email'     => $email,
283e5a6b4d4SGreg Roach            'password'  => password_hash($password, PASSWORD_DEFAULT),
284e5a6b4d4SGreg Roach        ]);
285e5a6b4d4SGreg Roach
286e5a6b4d4SGreg Roach        $user_id = (int) DB::connection()->getPdo()->lastInsertId();
287e5a6b4d4SGreg Roach
288e5a6b4d4SGreg Roach        return new User($user_id, $user_name, $real_name, $email);
289e5a6b4d4SGreg Roach    }
290e5a6b4d4SGreg Roach
291e5a6b4d4SGreg Roach    /**
292e5a6b4d4SGreg Roach     * Delete a user
293e5a6b4d4SGreg Roach     *
294e5a6b4d4SGreg Roach     * @param User $user
295e5a6b4d4SGreg Roach     *
296e5a6b4d4SGreg Roach     * @return void
297e5a6b4d4SGreg Roach     */
298e364afe4SGreg Roach    public function delete(User $user): void
299e5a6b4d4SGreg Roach    {
300e5a6b4d4SGreg Roach        // Don't delete the logs, just set the user to null.
301e5a6b4d4SGreg Roach        DB::table('log')
302e5a6b4d4SGreg Roach            ->where('user_id', '=', $user->id())
303e5a6b4d4SGreg Roach            ->update(['user_id' => null]);
304e5a6b4d4SGreg Roach
305e5a6b4d4SGreg Roach        // Take over the user’s pending changes. (What else could we do with them?)
306e5a6b4d4SGreg Roach        DB::table('change')
307e5a6b4d4SGreg Roach            ->where('user_id', '=', $user->id())
308e5a6b4d4SGreg Roach            ->where('status', '=', 'rejected')
309e5a6b4d4SGreg Roach            ->delete();
310e5a6b4d4SGreg Roach
311e5a6b4d4SGreg Roach        DB::table('change')
312e5a6b4d4SGreg Roach            ->where('user_id', '=', $user->id())
313e5a6b4d4SGreg Roach            ->update(['user_id' => Auth::id()]);
314e5a6b4d4SGreg Roach
315e5a6b4d4SGreg Roach        // Delete settings and preferences
316e5a6b4d4SGreg Roach        DB::table('block_setting')
317e5a6b4d4SGreg Roach            ->join('block', 'block_setting.block_id', '=', 'block.block_id')
318e5a6b4d4SGreg Roach            ->where('user_id', '=', $user->id())
319e5a6b4d4SGreg Roach            ->delete();
320e5a6b4d4SGreg Roach
321e5a6b4d4SGreg Roach        DB::table('block')->where('user_id', '=', $user->id())->delete();
322e5a6b4d4SGreg Roach        DB::table('user_gedcom_setting')->where('user_id', '=', $user->id())->delete();
323e5a6b4d4SGreg Roach        DB::table('user_setting')->where('user_id', '=', $user->id())->delete();
324e5a6b4d4SGreg Roach        DB::table('message')->where('user_id', '=', $user->id())->delete();
325e5a6b4d4SGreg Roach        DB::table('user')->where('user_id', '=', $user->id())->delete();
326e5a6b4d4SGreg Roach    }
32786730b84SGreg Roach
32886730b84SGreg Roach    /**
3294db4b4a9SGreg Roach     * @param User $contact_user
33086730b84SGreg Roach     *
33186730b84SGreg Roach     * @return string
33286730b84SGreg Roach     */
333dcbe9044SGreg Roach    public function contactLink(User $contact_user): string
334dcbe9044SGreg Roach    {
335cab242e7SGreg Roach        $tree    = app(Tree::class);
336cab242e7SGreg Roach        $user    = app(UserInterface::class);
337cab242e7SGreg Roach        $request = app(Request::class);
33886730b84SGreg Roach
33986730b84SGreg Roach        if ($contact_user->getPreference('contactmethod') === 'mailto') {
34086730b84SGreg Roach            $url = 'mailto:' . $contact_user->email();
34186730b84SGreg Roach        } elseif ($user instanceof User) {
34286730b84SGreg Roach            // Logged-in users send direct messages
3435d284d5dSGreg Roach            $url = route('message', ['to' => $contact_user->userName()]);
34486730b84SGreg Roach        } else {
34586730b84SGreg Roach            // Visitors use the contact form.
34686730b84SGreg Roach            $url = route('contact', [
34786730b84SGreg Roach                'ged' => $tree ? $tree->name() : '',
34886730b84SGreg Roach                'to'  => $contact_user->userName(),
34986730b84SGreg Roach                'url' => $request->getRequestUri(),
35086730b84SGreg Roach            ]);
35186730b84SGreg Roach        }
35286730b84SGreg Roach
35386730b84SGreg Roach        return '<a href="' . e($url) . '" dir="auto">' . e($contact_user->realName()) . '</a>';
35486730b84SGreg Roach    }
355e5a6b4d4SGreg Roach}
356