Debian Codegraph @

public/users.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-2025 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 script returns the users information
25 *
26 * HTTP GET query:
27 * when the header 'Accept' is 'application/json':
28 * It returns a list of the users or data for the user specified in the parameter user.
29 * otherwise:
30 * it returns the content of the index.html file
31 * HTTP POST query:
32 * Inserts or updates data for specified user. The data must be in json format with the following fields:
33 * `name` string User name.
34 * `action` string Must be one of the following values: `add`, `update`, `delete`, `set_password`
35 * If the value is `delete`, all the fields below will be ignored.
36 * `level` integer One of the User::LEVEL_* values
37 * `enabled` boolean Set `false` to temporarily deactivate the user
38 * `domains` array Array of domain names to assign. If undefined, no changes are required.
39 * Example:
40 * { "name": "user194", "action": "update", "enabled": true, "domains": [ "domain1.org", "domain2.org" ] }
41 * Other HTTP methods:
42 * It returns an error
43 *
44 * All the data is in json format.
45 *
46 * @category Web
47 * @package DmarcSrg
48 * @author Aleksey Andreev (liuch)
49 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
50 */
51
52 namespace Liuch\DmarcSrg;
53
54 use Liuch\DmarcSrg\Users\User;
55 use Liuch\DmarcSrg\Users\DbUser;
56 use Liuch\DmarcSrg\Users\UserList;
57 use Liuch\DmarcSrg\Domains\DomainList;
58 use Liuch\DmarcSrg\Exception\SoftException;
59 use Liuch\DmarcSrg\Exception\RuntimeException;
60 use Liuch\DmarcSrg\Exception\ForbiddenException;
61
62 require realpath(__DIR__ . '/..') . '/init.php';
63
64 if (Core::isJson()) {
65 try {
66 $core = Core::instance();
67
68 if (Core::requestMethod() == 'GET') {
69 if (!isset($_GET['user'])) {
70 // Retrieving a list of all users
71 $core->auth()->isAllowed(User::LEVEL_ADMIN);
72
73 $list = array_map(function ($user) {
74 $ua = $user->toArray();
75 $ua['level'] = User::levelToString($ua['level']);
76 return $ua;
77 }, (new UserList())->getList()['users']);
78
79 Core::sendJson([
80 'users' => $list,
81 'more' => false
82 ]);
83 return;
84 }
85
86 $uname = $_GET['user'];
87 if (empty($uname)) {
88 // New user
89 $core->auth()->isAllowed(User::LEVEL_ADMIN);
90
91 Core::sendJson([
92 'domains' => [ 'available' => (new DomainList(UserList::getUserByName('admin')))->names() ]
93 ]);
94 return;
95 }
96
97 $core->auth()->isAllowed(User::LEVEL_USER);
98 if ($core->getCurrentUser()->name() === $uname && $core->getCurrentUser()->level() < User::LEVEL_ADMIN) {
99 // The current user and not Admin
100 $udata = (new DbUser($uname))->toArray();
101 Core::sendJson([
102 'name' => $udata['name'],
103 'level' => User::levelToString($udata['level']),
104 'password' => $udata['password'] // bool value
105 ]);
106 return;
107 }
108
109 // Retrieving user data by Admin
110 $core->auth()->isAllowed(User::LEVEL_ADMIN);
111
112 $user = new DbUser($uname);
113 $res = $user->toArray();
114 $res['level'] = User::levelToString($res['level']);
115 $res['domains'] = [
116 'available' => (new DomainList(UserList::getUserByName('admin')))->names(),
117 'assigned' => (new DomainList($user))->names()
118 ];
119 Core::sendJson($res);
120 return;
121 } elseif (Core::requestMethod() == 'POST') {
122 $data = Core::getJsonData();
123 if ($data) {
124 $uname = $data['name'] ?? null;
125 $action = $data['action'] ?? '';
126 if ($action === 'set_password') {
127 $core->auth()->isAllowed(User::LEVEL_USER);
128
129 $user = new DbUser($uname);
130 $c_user = $core->getCurrentUser();
131 if ($c_user->name() === $user->name()) {
132 if (!$c_user->verifyPassword($data['password'] ?? '')) {
133 throw new SoftException('The current password is incorrect');
134 }
135 } else {
136 $core->auth()->isAllowed(User::LEVEL_ADMIN);
137 if ($c_user->level() <= $user->level()) {
138 throw new ForbiddenException('Forbidden');
139 }
140 }
141 if (empty($data['new_password'])) {
142 throw new SoftException('New password must not be empty');
143 }
144 $user->setPassword($data['new_password']);
145 Core::sendJson([
146 'error_code' => 0,
147 'message' => 'The password has been successfully updated'
148 ]);
149 return;
150 }
151
152 $core->auth()->isAllowed(User::LEVEL_ADMIN);
153
154 if (!empty($data['level']) && gettype($data['level']) == 'string') {
155 $data['level'] = User::stringToLevel($data['level']);
156 }
157 $user = new DbUser([
158 'name' => $uname,
159 'level' => $data['level'] ?? null,
160 'enabled' => $data['enabled'] ?? null
161 ]);
162 $check_level = function () use ($core, $user, $action) {
163 if ($core->getCurrentUser()->level() <= $user->level()) {
164 throw new SoftException("Insufficient access level to {$action} this user");
165 }
166 };
167 switch ($action) {
168 case 'add':
169 $check_level();
170 $user->ensure('nonexist');
171 $user->save();
172 break;
173 case 'update':
174 $check_level();
175 $user->ensure('exist');
176 $user->save();
177 break;
178 case 'delete':
179 $check_level();
180 $user->delete();
181 $user = null;
182 break;
183 default:
184 throw new SoftException(
185 'Unknown action. Valid values are "add", "update", "delete", "set_password".'
186 );
187 }
188
189 $domains = $data['domains'] ?? null;
190 if ($user && is_array($domains)) {
191 $domains = array_values(array_unique(array_filter($domains, function ($val) {
192 return is_string($val);
193 })));
194 $user->assignDomains($domains);
195 }
196
197 $res = [
198 'error_code' => 0,
199 'message' => 'Successfully'
200 ];
201 if (isset($user)) {
202 $ua = $user->toArray();
203 $ua['level'] = User::levelToString($ua['level']);
204 $res['user'] = $ua;
205 }
206 Core::sendJson($res);
207 return;
208 }
209 }
210 } catch (RuntimeException $e) {
211 Core::sendJson(ErrorHandler::exceptionResult($e));
212 return;
213 }
214 } elseif (Core::requestMethod() == 'GET') {
215 Core::instance()->sendHtml();
216 return;
217 }
218
219 Core::sendBad();