Translated using Weblate (Turkish)
[phpmyadmin.git] / server_privileges.php
blob9a1fd540ffffc4ea449f790307630d6fb9ed559c
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Server privileges and users manipulations
6 * @package PhpMyAdmin
7 */
9 use PhpMyAdmin\Core;
10 use PhpMyAdmin\Message;
11 use PhpMyAdmin\Relation;
12 use PhpMyAdmin\Response;
13 use PhpMyAdmin\Server\Common;
14 use PhpMyAdmin\Server\Privileges;
15 use PhpMyAdmin\Server\Users;
17 /**
18 * include common file
20 require_once 'libraries/common.inc.php';
22 /**
23 * functions implementation for this script
25 require_once 'libraries/check_user_privileges.inc.php';
27 $cfgRelation = Relation::getRelationsParam();
29 /**
30 * Does the common work
32 $response = Response::getInstance();
33 $header = $response->getHeader();
34 $scripts = $header->getScripts();
35 $scripts->addFile('server_privileges.js');
36 $scripts->addFile('vendor/zxcvbn.js');
38 if ((isset($_REQUEST['viewing_mode'])
39 && $_REQUEST['viewing_mode'] == 'server')
40 && $GLOBALS['cfgRelation']['menuswork']
41 ) {
42 $response->addHTML('<div>');
43 $response->addHTML(Users::getHtmlForSubMenusOnUsersPage('server_privileges.php'));
46 /**
47 * Sets globals from $_POST patterns, for privileges and max_* vars
50 $post_patterns = array(
51 '/_priv$/i',
52 '/^max_/i'
55 Core::setPostAsGlobal($post_patterns);
57 require 'libraries/server_common.inc.php';
59 /**
60 * Messages are built using the message name
62 $strPrivDescAllPrivileges = __('Includes all privileges except GRANT.');
63 $strPrivDescAlter = __('Allows altering the structure of existing tables.');
64 $strPrivDescAlterRoutine = __('Allows altering and dropping stored routines.');
65 $strPrivDescCreateDb = __('Allows creating new databases and tables.');
66 $strPrivDescCreateRoutine = __('Allows creating stored routines.');
67 $strPrivDescCreateTbl = __('Allows creating new tables.');
68 $strPrivDescCreateTmpTable = __('Allows creating temporary tables.');
69 $strPrivDescCreateUser = __('Allows creating, dropping and renaming user accounts.');
70 $strPrivDescCreateView = __('Allows creating new views.');
71 $strPrivDescDelete = __('Allows deleting data.');
72 $strPrivDescDropDb = __('Allows dropping databases and tables.');
73 $strPrivDescDropTbl = __('Allows dropping tables.');
74 $strPrivDescEvent = __('Allows to set up events for the event scheduler.');
75 $strPrivDescExecute = __('Allows executing stored routines.');
76 $strPrivDescFile = __('Allows importing data from and exporting data into files.');
77 $strPrivDescGrantTbl = __(
78 'Allows user to give to other users or remove from other users the privileges '
79 . 'that user possess yourself.'
81 $strPrivDescIndex = __('Allows creating and dropping indexes.');
82 $strPrivDescInsert = __('Allows inserting and replacing data.');
83 $strPrivDescLockTables = __('Allows locking tables for the current thread.');
84 $strPrivDescMaxConnections = __(
85 'Limits the number of new connections the user may open per hour.'
87 $strPrivDescMaxQuestions = __(
88 'Limits the number of queries the user may send to the server per hour.'
90 $strPrivDescMaxUpdates = __(
91 'Limits the number of commands that change any table or database '
92 . 'the user may execute per hour.'
94 $strPrivDescMaxUserConnections = __(
95 'Limits the number of simultaneous connections the user may have.'
97 $strPrivDescProcess = __('Allows viewing processes of all users.');
98 $strPrivDescReferences = __('Has no effect in this MySQL version.');
99 $strPrivDescReload = __(
100 'Allows reloading server settings and flushing the server\'s caches.'
102 $strPrivDescReplClient = __(
103 'Allows the user to ask where the slaves / masters are.'
105 $strPrivDescReplSlave = __('Needed for the replication slaves.');
106 $strPrivDescSelect = __('Allows reading data.');
107 $strPrivDescShowDb = __('Gives access to the complete list of databases.');
108 $strPrivDescShowView = __('Allows performing SHOW CREATE VIEW queries.');
109 $strPrivDescShutdown = __('Allows shutting down the server.');
110 $strPrivDescSuper = __(
111 'Allows connecting, even if maximum number of connections is reached; '
112 . 'required for most administrative operations like setting global variables '
113 . 'or killing threads of other users.'
115 $strPrivDescTrigger = __('Allows creating and dropping triggers.');
116 $strPrivDescUpdate = __('Allows changing data.');
117 $strPrivDescUsage = __('No privileges.');
119 $_add_user_error = false;
121 * Get DB information: username, hostname, dbname,
122 * tablename, db_and_table, dbname_is_wildcard
124 list(
125 $username, $hostname, $dbname, $tablename, $routinename,
126 $db_and_table, $dbname_is_wildcard
127 ) = Privileges::getDataForDBInfo();
130 * Checks if the user is allowed to do what he tries to...
132 if (!$GLOBALS['dbi']->isSuperuser() && !$GLOBALS['is_grantuser']
133 && !$GLOBALS['is_createuser']
135 $response->addHTML(Common::getHtmlForSubPageHeader('privileges', '', false));
136 $response->addHTML(
137 Message::error(__('No Privileges'))
138 ->getDisplay()
140 exit;
142 if (! $GLOBALS['is_grantuser'] && !$GLOBALS['is_createuser']) {
143 $response->addHTML(Message::notice(
144 __('You do not have privileges to manipulate with the users!')
145 )->getDisplay());
149 * Checks if the user is using "Change Login Information / Copy User" dialog
150 * only to update the password
152 if (isset($_REQUEST['change_copy']) && $username == $_REQUEST['old_username']
153 && $hostname == $_REQUEST['old_hostname']
155 $response->addHTML(
156 Message::error(
158 "Username and hostname didn't change. "
159 . "If you only want to change the password, "
160 . "'Change password' tab should be used."
162 )->getDisplay()
164 $response->setRequestStatus(false);
165 exit;
169 * Changes / copies a user, part I
171 list($queries, $password) = Privileges::getDataForChangeOrCopyUser();
174 * Adds a user
175 * (Changes / copies a user, part II)
177 list($ret_message, $ret_queries, $queries_for_display, $sql_query, $_add_user_error)
178 = Privileges::addUser(
179 isset($dbname)? $dbname : null,
180 isset($username)? $username : null,
181 isset($hostname)? $hostname : null,
182 isset($password)? $password : null,
183 $cfgRelation['menuswork']
185 //update the old variables
186 if (isset($ret_queries)) {
187 $queries = $ret_queries;
188 unset($ret_queries);
190 if (isset($ret_message)) {
191 $message = $ret_message;
192 unset($ret_message);
196 * Changes / copies a user, part III
198 if (isset($_REQUEST['change_copy'])) {
199 $queries = Privileges::getDbSpecificPrivsQueriesForChangeOrCopyUser(
200 $queries, $username, $hostname
204 $itemType = '';
205 if (! empty($routinename)) {
206 $itemType = Privileges::getRoutineType($dbname, $routinename);
210 * Updates privileges
212 if (! empty($_POST['update_privs'])) {
213 if (is_array($dbname)) {
214 foreach ($dbname as $key => $db_name) {
215 list($sql_query[$key], $message) = Privileges::updatePrivileges(
216 (isset($username) ? $username : ''),
217 (isset($hostname) ? $hostname : ''),
218 (isset($tablename)
219 ? $tablename
220 : (isset($routinename) ? $routinename : '')),
221 (isset($db_name) ? $db_name : ''),
222 $itemType
226 $sql_query = implode("\n", $sql_query);
227 } else {
228 list($sql_query, $message) = Privileges::updatePrivileges(
229 (isset($username) ? $username : ''),
230 (isset($hostname) ? $hostname : ''),
231 (isset($tablename)
232 ? $tablename
233 : (isset($routinename) ? $routinename : '')),
234 (isset($dbname) ? $dbname : ''),
235 $itemType
241 * Assign users to user groups
243 if (! empty($_REQUEST['changeUserGroup']) && $cfgRelation['menuswork']
244 && $GLOBALS['dbi']->isSuperuser() && $GLOBALS['is_createuser']
246 Privileges::setUserGroup($username, $_REQUEST['userGroup']);
247 $message = Message::success();
251 * Revokes Privileges
253 if (isset($_REQUEST['revokeall'])) {
254 list ($message, $sql_query) = Privileges::getMessageAndSqlQueryForPrivilegesRevoke(
255 (isset($dbname) ? $dbname : ''),
256 (isset($tablename)
257 ? $tablename
258 : (isset($routinename) ? $routinename : '')),
259 $username,
260 $hostname,
261 $itemType
266 * Updates the password
268 if (isset($_REQUEST['change_pw'])) {
269 $message = Privileges::updatePassword(
270 $err_url, $username, $hostname
275 * Deletes users
276 * (Changes / copies a user, part IV)
278 if (isset($_REQUEST['delete'])
279 || (isset($_REQUEST['change_copy']) && $_REQUEST['mode'] < 4)
281 $queries = Privileges::getDataForDeleteUsers($queries);
282 if (empty($_REQUEST['change_copy'])) {
283 list($sql_query, $message) = Privileges::deleteUser($queries);
288 * Changes / copies a user, part V
290 if (isset($_REQUEST['change_copy'])) {
291 $queries = Privileges::getDataForQueries($queries, $queries_for_display);
292 $message = Message::success();
293 $sql_query = join("\n", $queries);
297 * Reloads the privilege tables into memory
299 $message_ret = Privileges::updateMessageForReload();
300 if (isset($message_ret)) {
301 $message = $message_ret;
302 unset($message_ret);
306 * If we are in an Ajax request for Create User/Edit User/Revoke User/
307 * Flush Privileges, show $message and exit.
309 if ($response->isAjax()
310 && empty($_REQUEST['ajax_page_request'])
311 && ! isset($_REQUEST['export'])
312 && (! isset($_REQUEST['submit_mult']) || $_REQUEST['submit_mult'] != 'export')
313 && ((! isset($_REQUEST['initial']) || $_REQUEST['initial'] === null
314 || $_REQUEST['initial'] === '')
315 || (isset($_REQUEST['delete']) && $_REQUEST['delete'] === __('Go')))
316 && ! isset($_REQUEST['showall'])
317 && ! isset($_REQUEST['edit_user_group_dialog'])
318 && ! isset($_REQUEST['db_specific'])
320 $extra_data = Privileges::getExtraDataForAjaxBehavior(
321 (isset($password) ? $password : ''),
322 (isset($sql_query) ? $sql_query : ''),
323 (isset($hostname) ? $hostname : ''),
324 (isset($username) ? $username : '')
327 if (! empty($message) && $message instanceof Message) {
328 $response->setRequestStatus($message->isSuccess());
329 $response->addJSON('message', $message);
330 $response->addJSON($extra_data);
331 exit;
336 * Displays the links
338 if (isset($_REQUEST['viewing_mode']) && $_REQUEST['viewing_mode'] == 'db') {
339 $GLOBALS['db'] = $_REQUEST['db'] = $_REQUEST['checkprivsdb'];
341 $url_query .= '&amp;goto=db_operations.php';
343 // Gets the database structure
344 $sub_part = '_structure';
345 ob_start();
347 list(
348 $tables,
349 $num_tables,
350 $total_num_tables,
351 $sub_part,
352 $is_show_stats,
353 $db_is_system_schema,
354 $tooltip_truename,
355 $tooltip_aliasname,
356 $pos
357 ) = PhpMyAdmin\Util::getDbInfo($db, isset($sub_part) ? $sub_part : '');
359 $content = ob_get_contents();
360 ob_end_clean();
361 $response->addHTML($content . "\n");
362 } else {
363 if (! empty($GLOBALS['message'])) {
364 $response->addHTML(PhpMyAdmin\Util::getMessage($GLOBALS['message']));
365 unset($GLOBALS['message']);
370 * Displays the page
372 $response->addHTML(
373 Privileges::getHtmlForUserGroupDialog(
374 isset($username)? $username : null,
375 $cfgRelation['menuswork']
379 // export user definition
380 if (isset($_REQUEST['export'])
381 || (isset($_REQUEST['submit_mult']) && $_REQUEST['submit_mult'] == 'export')
383 list($title, $export) = Privileges::getListForExportUserDefinition(
384 isset($username) ? $username : null,
385 isset($hostname) ? $hostname : null
388 unset($username, $hostname, $grants, $one_grant);
390 if ($response->isAjax()) {
391 $response->addJSON('message', $export);
392 $response->addJSON('title', $title);
393 exit;
394 } else {
395 $response->addHTML("<h2>$title</h2>$export");
399 if (isset($_REQUEST['adduser'])) {
400 // Add user
401 $response->addHTML(
402 Privileges::getHtmlForAddUser((isset($dbname) ? $dbname : ''))
404 } elseif (isset($_REQUEST['checkprivsdb'])) {
405 if (isset($_REQUEST['checkprivstable'])) {
406 // check the privileges for a particular table.
407 $response->addHTML(
408 Privileges::getHtmlForSpecificTablePrivileges(
409 $_REQUEST['checkprivsdb'], $_REQUEST['checkprivstable']
412 } else {
413 // check the privileges for a particular database.
414 $response->addHTML(
415 Privileges::getHtmlForSpecificDbPrivileges($_REQUEST['checkprivsdb'])
418 } else {
419 if (isset($dbname) && ! is_array($dbname)) {
420 $url_dbname = urlencode(
421 str_replace(
422 array('\_', '\%'),
423 array('_', '%'),
424 $dbname
429 if (! isset($username)) {
430 // No username is given --> display the overview
431 $response->addHTML(
432 Privileges::getHtmlForUserOverview($pmaThemeImage, $text_dir)
434 } else if (!empty($routinename)) {
435 $response->addHTML(
436 Privileges::getHtmlForRoutineSpecificPrivileges(
437 $username, $hostname, $dbname, $routinename,
438 (isset($url_dbname) ? $url_dbname : '')
441 } else {
442 // A user was selected -> display the user's properties
443 // In an Ajax request, prevent cached values from showing
444 if ($response->isAjax()) {
445 header('Cache-Control: no-cache');
448 $response->addHTML(
449 Privileges::getHtmlForUserProperties(
450 (isset($dbname_is_wildcard) ? $dbname_is_wildcard : ''),
451 (isset($url_dbname) ? $url_dbname : ''),
452 $username, $hostname,
453 (isset($dbname) ? $dbname : ''),
454 (isset($tablename) ? $tablename : '')
460 if ((isset($_REQUEST['viewing_mode']) && $_REQUEST['viewing_mode'] == 'server')
461 && $GLOBALS['cfgRelation']['menuswork']
463 $response->addHTML('</div>');