Debian Codegraph @

classes/Users/UserList.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2023 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This file contains the class UserList
25 *
26 * @category API
27 * @package DmarcSrg
28 * @author Aleksey Andreev (liuch)
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30 */
31
32 namespace Liuch\DmarcSrg\Users;
33
34 use Liuch\DmarcSrg\Core;
35 use Liuch\DmarcSrg\Exception\SoftException;
36
37 /**
38 * This class is designed to work with the list of users
39 */
40 class UserList
41 {
42 private $db = null;
43
44 /**
45 * The constructor
46 *
47 * @param \Liuch\DmarcSrg\Database\DatabaseController $db The database controller
48 */
49 public function __construct($db = null)
50 {
51 $this->db = $db ?? Core::instance()->database();
52 }
53
54 /**
55 * Returns a list of users from the database
56 *
57 * @return array Array with instances of User class
58 */
59 public function getList(): array
60 {
61 $list = array_map(function ($udata) {
62 return new DbUser($udata, $this->db);
63 }, $this->db->getMapper('user')->list());
64 return [
65 'users' => $list,
66 'more' => false
67 ];
68 }
69
70 /**
71 * Returns an instance of the User class with the username from $username if such a user exists
72 *
73 * @param string $username User name
74 * @param Core|null $core Instance of the Core class
75 *
76 * @return User
77 */
78 public static function getUserByName(string $username, $core = null)
79 {
80 if ($username === 'admin') {
81 return new AdminUser($core);
82 }
83 $user = new DbUser($username, ($core ?? Core::instance())->database());
84 if (!$user->exists()) {
85 throw new SoftException('Unknown user: ' . $username);
86 }
87 return $user;
88 }
89 }