xref: /webtrees/app/Services/UserService.php (revision 5d284d5d1c1aebf4add71a7eb2f6c99ce48c8f98)
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     */
42e5a6b4d4SGreg Roach    public function find($user_id)
43e5a6b4d4SGreg Roach    {
44e5a6b4d4SGreg Roach        return app('cache.array')->rememberForever(__CLASS__ . $user_id, function () use ($user_id) {
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     */
60e5a6b4d4SGreg Roach    public function findByEmail($email)
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     */
76e5a6b4d4SGreg Roach    public function findByIdentifier($identifier)
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     *
91e5a6b4d4SGreg Roach     * @return Collection|User[]
92e5a6b4d4SGreg Roach     */
93e5a6b4d4SGreg Roach    public function findByIndividual(Individual $individual): Collection
94e5a6b4d4SGreg Roach    {
95e5a6b4d4SGreg Roach        return DB::table('user')
96e5a6b4d4SGreg Roach            ->join('user_gedcom_setting', 'user_gedcom_setting.user_id', '=', 'user.user_id')
97e5a6b4d4SGreg Roach            ->where('gedcom_id', '=', $individual->tree()->id())
98e5a6b4d4SGreg Roach            ->where('setting_value', '=', $individual->xref())
99e5a6b4d4SGreg Roach            ->where('setting_name', '=', 'gedcomid')
100e5a6b4d4SGreg Roach            ->select(['user.*'])
101e5a6b4d4SGreg Roach            ->get()
102e5a6b4d4SGreg Roach            ->map(User::rowMapper());
103e5a6b4d4SGreg Roach    }
104e5a6b4d4SGreg Roach
105e5a6b4d4SGreg Roach    /**
106e5a6b4d4SGreg Roach     * Find the user with a specified user_name.
107e5a6b4d4SGreg Roach     *
108e5a6b4d4SGreg Roach     * @param string $user_name
109e5a6b4d4SGreg Roach     *
110e5a6b4d4SGreg Roach     * @return User|null
111e5a6b4d4SGreg Roach     */
112e5a6b4d4SGreg Roach    public function findByUserName($user_name)
113e5a6b4d4SGreg Roach    {
114e5a6b4d4SGreg Roach        return DB::table('user')
115e5a6b4d4SGreg Roach            ->where('user_name', '=', $user_name)
116e5a6b4d4SGreg Roach            ->get()
117e5a6b4d4SGreg Roach            ->map(User::rowMapper())
118e5a6b4d4SGreg Roach            ->first();
119e5a6b4d4SGreg Roach    }
120e5a6b4d4SGreg Roach
121e5a6b4d4SGreg Roach    /**
122e5a6b4d4SGreg Roach     * Get a list of all users.
123e5a6b4d4SGreg Roach     *
124e5a6b4d4SGreg Roach     * @return Collection|User[]
125e5a6b4d4SGreg Roach     */
126e5a6b4d4SGreg Roach    public function all(): Collection
127e5a6b4d4SGreg Roach    {
128e5a6b4d4SGreg Roach        return DB::table('user')
129e5a6b4d4SGreg Roach            ->where('user_id', '>', 0)
130e5a6b4d4SGreg Roach            ->orderBy('real_name')
131e5a6b4d4SGreg Roach            ->get()
132e5a6b4d4SGreg Roach            ->map(User::rowMapper());
133e5a6b4d4SGreg Roach    }
134e5a6b4d4SGreg Roach
135e5a6b4d4SGreg Roach    /**
136e5a6b4d4SGreg Roach     * Get a list of all administrators.
137e5a6b4d4SGreg Roach     *
138e5a6b4d4SGreg Roach     * @return Collection|User[]
139e5a6b4d4SGreg Roach     */
140e5a6b4d4SGreg Roach    public function administrators(): Collection
141e5a6b4d4SGreg Roach    {
142e5a6b4d4SGreg Roach        return DB::table('user')
143e5a6b4d4SGreg Roach            ->join('user_setting', function (JoinClause $join): void {
144e5a6b4d4SGreg Roach                $join
145e5a6b4d4SGreg Roach                    ->on('user_setting.user_id', '=', 'user.user_id')
146e5a6b4d4SGreg Roach                    ->where('user_setting.setting_name', '=', 'canadmin')
147e5a6b4d4SGreg Roach                    ->where('user_setting.setting_value', '=', '1');
148e5a6b4d4SGreg Roach            })
149e5a6b4d4SGreg Roach            ->where('user.user_id', '>', 0)
150e5a6b4d4SGreg Roach            ->orderBy('real_name')
151e5a6b4d4SGreg Roach            ->select(['user.*'])
152e5a6b4d4SGreg Roach            ->get()
153e5a6b4d4SGreg Roach            ->map(User::rowMapper());
154e5a6b4d4SGreg Roach    }
155e5a6b4d4SGreg Roach
156e5a6b4d4SGreg Roach    /**
157e5a6b4d4SGreg Roach     * Get a list of all managers.
158e5a6b4d4SGreg Roach     *
159e5a6b4d4SGreg Roach     * @return Collection|User[]
160e5a6b4d4SGreg Roach     */
161e5a6b4d4SGreg Roach    public function managers(): Collection
162e5a6b4d4SGreg Roach    {
163e5a6b4d4SGreg Roach        return DB::table('user')
164e5a6b4d4SGreg Roach            ->join('user_gedcom_setting', function (JoinClause $join): void {
165e5a6b4d4SGreg Roach                $join
166e5a6b4d4SGreg Roach                    ->on('user_gedcom_setting.user_id', '=', 'user.user_id')
167e5a6b4d4SGreg Roach                    ->where('user_gedcom_setting.setting_name', '=', 'canedit')
168e5a6b4d4SGreg Roach                    ->where('user_gedcom_setting.setting_value', '=', 'admin');
169e5a6b4d4SGreg Roach            })
170e5a6b4d4SGreg Roach            ->where('user.user_id', '>', 0)
171e5a6b4d4SGreg Roach            ->orderBy('real_name')
172e5a6b4d4SGreg Roach            ->select(['user.*'])
173e5a6b4d4SGreg Roach            ->get()
174e5a6b4d4SGreg Roach            ->map(User::rowMapper());
175e5a6b4d4SGreg Roach    }
176e5a6b4d4SGreg Roach
177e5a6b4d4SGreg Roach    /**
178e5a6b4d4SGreg Roach     * Get a list of all moderators.
179e5a6b4d4SGreg Roach     *
180e5a6b4d4SGreg Roach     * @return Collection|User[]
181e5a6b4d4SGreg Roach     */
182e5a6b4d4SGreg Roach    public function moderators(): Collection
183e5a6b4d4SGreg Roach    {
184e5a6b4d4SGreg Roach        return DB::table('user')
185e5a6b4d4SGreg Roach            ->join('user_gedcom_setting', function (JoinClause $join): void {
186e5a6b4d4SGreg Roach                $join
187e5a6b4d4SGreg Roach                    ->on('user_gedcom_setting.user_id', '=', 'user.user_id')
188e5a6b4d4SGreg Roach                    ->where('user_gedcom_setting.setting_name', '=', 'canedit')
189e5a6b4d4SGreg Roach                    ->where('user_gedcom_setting.setting_value', '=', 'accept');
190e5a6b4d4SGreg Roach            })
191e5a6b4d4SGreg Roach            ->where('user.user_id', '>', 0)
192e5a6b4d4SGreg Roach            ->orderBy('real_name')
193e5a6b4d4SGreg Roach            ->select(['user.*'])
194e5a6b4d4SGreg Roach            ->get()
195e5a6b4d4SGreg Roach            ->map(User::rowMapper());
196e5a6b4d4SGreg Roach    }
197e5a6b4d4SGreg Roach
198e5a6b4d4SGreg Roach    /**
199e5a6b4d4SGreg Roach     * Get a list of all verified users.
200e5a6b4d4SGreg Roach     *
201e5a6b4d4SGreg Roach     * @return Collection|User[]
202e5a6b4d4SGreg Roach     */
203e5a6b4d4SGreg Roach    public function unapproved(): Collection
204e5a6b4d4SGreg Roach    {
205e5a6b4d4SGreg Roach        return DB::table('user')
206e5a6b4d4SGreg Roach            ->join('user_setting', function (JoinClause $join): void {
207e5a6b4d4SGreg Roach                $join
208e5a6b4d4SGreg Roach                    ->on('user_setting.user_id', '=', 'user.user_id')
209e5a6b4d4SGreg Roach                    ->where('user_setting.setting_name', '=', 'verified_by_admin')
210e5a6b4d4SGreg Roach                    ->where('user_setting.setting_value', '=', '0');
211e5a6b4d4SGreg Roach            })
212e5a6b4d4SGreg Roach            ->where('user.user_id', '>', 0)
213e5a6b4d4SGreg Roach            ->orderBy('real_name')
214e5a6b4d4SGreg Roach            ->select(['user.*'])
215e5a6b4d4SGreg Roach            ->get()
216e5a6b4d4SGreg Roach            ->map(User::rowMapper());
217e5a6b4d4SGreg Roach    }
218e5a6b4d4SGreg Roach
219e5a6b4d4SGreg Roach    /**
220e5a6b4d4SGreg Roach     * Get a list of all verified users.
221e5a6b4d4SGreg Roach     *
222e5a6b4d4SGreg Roach     * @return Collection|User[]
223e5a6b4d4SGreg Roach     */
224e5a6b4d4SGreg Roach    public function unverified(): Collection
225e5a6b4d4SGreg Roach    {
226e5a6b4d4SGreg Roach        return DB::table('user')
227e5a6b4d4SGreg Roach            ->join('user_setting', function (JoinClause $join): void {
228e5a6b4d4SGreg Roach                $join
229e5a6b4d4SGreg Roach                    ->on('user_setting.user_id', '=', 'user.user_id')
230e5a6b4d4SGreg Roach                    ->where('user_setting.setting_name', '=', 'verified')
231e5a6b4d4SGreg Roach                    ->where('user_setting.setting_value', '=', '0');
232e5a6b4d4SGreg Roach            })
233e5a6b4d4SGreg Roach            ->where('user.user_id', '>', 0)
234e5a6b4d4SGreg Roach            ->orderBy('real_name')
235e5a6b4d4SGreg Roach            ->select(['user.*'])
236e5a6b4d4SGreg Roach            ->get()
237e5a6b4d4SGreg Roach            ->map(User::rowMapper());
238e5a6b4d4SGreg Roach    }
239e5a6b4d4SGreg Roach
240e5a6b4d4SGreg Roach    /**
241e5a6b4d4SGreg Roach     * Get a list of all users who are currently logged in.
242e5a6b4d4SGreg Roach     *
243e5a6b4d4SGreg Roach     * @return Collection|User[]
244e5a6b4d4SGreg Roach     */
245e5a6b4d4SGreg Roach    public function allLoggedIn(): Collection
246e5a6b4d4SGreg Roach    {
247e5a6b4d4SGreg Roach        return DB::table('user')
248e5a6b4d4SGreg Roach            ->join('session', 'session.user_id', '=', 'user.user_id')
249e5a6b4d4SGreg Roach            ->where('user.user_id', '>', 0)
250e5a6b4d4SGreg Roach            ->orderBy('real_name')
251e5a6b4d4SGreg Roach            ->select(['user.*'])
252e5a6b4d4SGreg Roach            ->distinct()
253e5a6b4d4SGreg Roach            ->get()
254e5a6b4d4SGreg Roach            ->map(User::rowMapper());
255e5a6b4d4SGreg Roach    }
256e5a6b4d4SGreg Roach
257e5a6b4d4SGreg Roach    /**
258e5a6b4d4SGreg Roach     * Create a new user.
259e5a6b4d4SGreg Roach     * The calling code needs to check for duplicates identifiers before calling
260e5a6b4d4SGreg Roach     * this function.
261e5a6b4d4SGreg Roach     *
262e5a6b4d4SGreg Roach     * @param string $user_name
263e5a6b4d4SGreg Roach     * @param string $real_name
264e5a6b4d4SGreg Roach     * @param string $email
265e5a6b4d4SGreg Roach     * @param string $password
266e5a6b4d4SGreg Roach     *
267e5a6b4d4SGreg Roach     * @return User
268e5a6b4d4SGreg Roach     */
269c65e00b4SGreg Roach    public static function create(string $user_name, string $real_name, string $email, string $password): User
270e5a6b4d4SGreg Roach    {
271e5a6b4d4SGreg Roach        DB::table('user')->insert([
272e5a6b4d4SGreg Roach            'user_name' => $user_name,
273e5a6b4d4SGreg Roach            'real_name' => $real_name,
274e5a6b4d4SGreg Roach            'email'     => $email,
275e5a6b4d4SGreg Roach            'password'  => password_hash($password, PASSWORD_DEFAULT),
276e5a6b4d4SGreg Roach        ]);
277e5a6b4d4SGreg Roach
278e5a6b4d4SGreg Roach        $user_id = (int) DB::connection()->getPdo()->lastInsertId();
279e5a6b4d4SGreg Roach
280e5a6b4d4SGreg Roach        return new User($user_id, $user_name, $real_name, $email);
281e5a6b4d4SGreg Roach    }
282e5a6b4d4SGreg Roach
283e5a6b4d4SGreg Roach    /**
284e5a6b4d4SGreg Roach     * Delete a user
285e5a6b4d4SGreg Roach     *
286e5a6b4d4SGreg Roach     * @param User $user
287e5a6b4d4SGreg Roach     *
288e5a6b4d4SGreg Roach     * @return void
289e5a6b4d4SGreg Roach     */
290e5a6b4d4SGreg Roach    public function delete(User $user)
291e5a6b4d4SGreg Roach    {
292e5a6b4d4SGreg Roach        // Don't delete the logs, just set the user to null.
293e5a6b4d4SGreg Roach        DB::table('log')
294e5a6b4d4SGreg Roach            ->where('user_id', '=', $user->id())
295e5a6b4d4SGreg Roach            ->update(['user_id' => null]);
296e5a6b4d4SGreg Roach
297e5a6b4d4SGreg Roach        // Take over the user’s pending changes. (What else could we do with them?)
298e5a6b4d4SGreg Roach        DB::table('change')
299e5a6b4d4SGreg Roach            ->where('user_id', '=', $user->id())
300e5a6b4d4SGreg Roach            ->where('status', '=', 'rejected')
301e5a6b4d4SGreg Roach            ->delete();
302e5a6b4d4SGreg Roach
303e5a6b4d4SGreg Roach        DB::table('change')
304e5a6b4d4SGreg Roach            ->where('user_id', '=', $user->id())
305e5a6b4d4SGreg Roach            ->update(['user_id' => Auth::id()]);
306e5a6b4d4SGreg Roach
307e5a6b4d4SGreg Roach        // Delete settings and preferences
308e5a6b4d4SGreg Roach        DB::table('block_setting')
309e5a6b4d4SGreg Roach            ->join('block', 'block_setting.block_id', '=', 'block.block_id')
310e5a6b4d4SGreg Roach            ->where('user_id', '=', $user->id())
311e5a6b4d4SGreg Roach            ->delete();
312e5a6b4d4SGreg Roach
313e5a6b4d4SGreg Roach        DB::table('block')->where('user_id', '=', $user->id())->delete();
314e5a6b4d4SGreg Roach        DB::table('user_gedcom_setting')->where('user_id', '=', $user->id())->delete();
315e5a6b4d4SGreg Roach        DB::table('user_setting')->where('user_id', '=', $user->id())->delete();
316e5a6b4d4SGreg Roach        DB::table('message')->where('user_id', '=', $user->id())->delete();
317e5a6b4d4SGreg Roach        DB::table('user')->where('user_id', '=', $user->id())->delete();
318e5a6b4d4SGreg Roach    }
31986730b84SGreg Roach
32086730b84SGreg Roach    /**
32186730b84SGreg Roach     * @param User $user
32286730b84SGreg Roach     *
32386730b84SGreg Roach     * @return string
32486730b84SGreg Roach     */
325dcbe9044SGreg Roach    public function contactLink(User $contact_user): string
326dcbe9044SGreg Roach    {
32786730b84SGreg Roach        $tree    = app()->make(Tree::class);
32886730b84SGreg Roach        $user    = app()->make(UserInterface::class);
32986730b84SGreg Roach        $request = app()->make(Request::class);
33086730b84SGreg Roach
33186730b84SGreg Roach        if ($contact_user->getPreference('contactmethod') === 'mailto') {
33286730b84SGreg Roach            $url = 'mailto:' . $contact_user->email();
33386730b84SGreg Roach        } elseif ($user instanceof User) {
33486730b84SGreg Roach            // Logged-in users send direct messages
335*5d284d5dSGreg Roach            $url = route('message', ['to' => $contact_user->userName()]);
33686730b84SGreg Roach        } else {
33786730b84SGreg Roach            // Visitors use the contact form.
33886730b84SGreg Roach            $url = route('contact', [
33986730b84SGreg Roach                'ged' => $tree ? $tree->name() : '',
34086730b84SGreg Roach                'to'  => $contact_user->userName(),
34186730b84SGreg Roach                'url' => $request->getRequestUri(),
34286730b84SGreg Roach            ]);
34386730b84SGreg Roach        }
34486730b84SGreg Roach
34586730b84SGreg Roach        return '<a href="' . e($url) . '" dir="auto">' . e($contact_user->realName()) . '</a>';
34686730b84SGreg Roach    }
347e5a6b4d4SGreg Roach}
348