2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Server privileges and users manipulations
12 require_once 'libraries/common.inc.php';
15 * functions implementation for this script
17 require_once 'libraries/display_change_password.lib.php';
18 require_once 'libraries/server_privileges.lib.php';
19 require_once 'libraries/check_user_privileges.lib.php';
21 $cfgRelation = PMA_getRelationsParam();
24 * Does the common work
26 $response = PMA\libraries\Response
::getInstance();
27 $header = $response->getHeader();
28 $scripts = $header->getScripts();
29 $scripts->addFile('server_privileges.js');
31 if ((isset($_REQUEST['viewing_mode'])
32 && $_REQUEST['viewing_mode'] == 'server')
33 && $GLOBALS['cfgRelation']['menuswork']
35 include_once 'libraries/server_users.lib.php';
36 $response->addHTML('<div>');
37 $response->addHTML(PMA_getHtmlForSubMenusOnUsersPage('server_privileges.php'));
41 * Sets globals from $_POST patterns, for privileges and max_* vars
44 $post_patterns = array(
49 PMA_setPostAsGlobal($post_patterns);
51 require 'libraries/server_common.inc.php';
54 * Messages are built using the message name
56 $strPrivDescAllPrivileges = __('Includes all privileges except GRANT.');
57 $strPrivDescAlter = __('Allows altering the structure of existing tables.');
58 $strPrivDescAlterRoutine = __('Allows altering and dropping stored routines.');
59 $strPrivDescCreateDb = __('Allows creating new databases and tables.');
60 $strPrivDescCreateRoutine = __('Allows creating stored routines.');
61 $strPrivDescCreateTbl = __('Allows creating new tables.');
62 $strPrivDescCreateTmpTable = __('Allows creating temporary tables.');
63 $strPrivDescCreateUser = __('Allows creating, dropping and renaming user accounts.');
64 $strPrivDescCreateView = __('Allows creating new views.');
65 $strPrivDescDelete = __('Allows deleting data.');
66 $strPrivDescDropDb = __('Allows dropping databases and tables.');
67 $strPrivDescDropTbl = __('Allows dropping tables.');
68 $strPrivDescEvent = __('Allows to set up events for the event scheduler.');
69 $strPrivDescExecute = __('Allows executing stored routines.');
70 $strPrivDescFile = __('Allows importing data from and exporting data into files.');
71 $strPrivDescGrantTbl = __(
72 'Allows user to give to other users or remove from other users the privileges '
73 . 'that user possess yourself.'
75 $strPrivDescIndex = __('Allows creating and dropping indexes.');
76 $strPrivDescInsert = __('Allows inserting and replacing data.');
77 $strPrivDescLockTables = __('Allows locking tables for the current thread.');
78 $strPrivDescMaxConnections = __(
79 'Limits the number of new connections the user may open per hour.'
81 $strPrivDescMaxQuestions = __(
82 'Limits the number of queries the user may send to the server per hour.'
84 $strPrivDescMaxUpdates = __(
85 'Limits the number of commands that change any table or database '
86 . 'the user may execute per hour.'
88 $strPrivDescMaxUserConnections = __(
89 'Limits the number of simultaneous connections the user may have.'
91 $strPrivDescProcess = __('Allows viewing processes of all users.');
92 $strPrivDescReferences = __('Has no effect in this MySQL version.');
93 $strPrivDescReload = __(
94 'Allows reloading server settings and flushing the server\'s caches.'
96 $strPrivDescReplClient = __(
97 'Allows the user to ask where the slaves / masters are.'
99 $strPrivDescReplSlave = __('Needed for the replication slaves.');
100 $strPrivDescSelect = __('Allows reading data.');
101 $strPrivDescShowDb = __('Gives access to the complete list of databases.');
102 $strPrivDescShowView = __('Allows performing SHOW CREATE VIEW queries.');
103 $strPrivDescShutdown = __('Allows shutting down the server.');
104 $strPrivDescSuper = __(
105 'Allows connecting, even if maximum number of connections is reached; '
106 . 'required for most administrative operations like setting global variables '
107 . 'or killing threads of other users.'
109 $strPrivDescTrigger = __('Allows creating and dropping triggers.');
110 $strPrivDescUpdate = __('Allows changing data.');
111 $strPrivDescUsage = __('No privileges.');
113 $_add_user_error = false;
115 * Get DB information: username, hostname, dbname,
116 * tablename, db_and_table, dbname_is_wildcard
119 $username, $hostname, $dbname, $tablename, $routinename,
120 $db_and_table, $dbname_is_wildcard
121 ) = PMA_getDataForDBInfo();
124 * Checks if the user is allowed to do what he tries to...
126 if (!$GLOBALS['is_superuser'] && !$GLOBALS['is_grantuser']
127 && !$GLOBALS['is_createuser']
129 $response->addHTML(PMA_getHtmlForSubPageHeader('privileges', '', false));
131 PMA\libraries\Message
::error(__('No Privileges'))
138 * Checks if the user is using "Change Login Information / Copy User" dialog
139 * only to update the password
141 if (isset($_REQUEST['change_copy']) && $username == $_REQUEST['old_username']
142 && $hostname == $_REQUEST['old_hostname']
145 PMA\libraries\Message
::error(
147 "Username and hostname didn't change. "
148 . "If you only want to change the password, "
149 . "'Change password' tab should be used."
153 $response->setRequestStatus(false);
158 * Changes / copies a user, part I
160 list($queries, $password) = PMA_getDataForChangeOrCopyUser();
164 * (Changes / copies a user, part II)
166 list($ret_message, $ret_queries, $queries_for_display, $sql_query, $_add_user_error)
168 isset($dbname)?
$dbname : null,
169 isset($username)?
$username : null,
170 isset($hostname)?
$hostname : null,
171 isset($password)?
$password : null,
172 $cfgRelation['menuswork']
174 //update the old variables
175 if (isset($ret_queries)) {
176 $queries = $ret_queries;
179 if (isset($ret_message)) {
180 $message = $ret_message;
185 * Changes / copies a user, part III
187 if (isset($_REQUEST['change_copy'])) {
188 $queries = PMA_getDbSpecificPrivsQueriesForChangeOrCopyUser(
189 $queries, $username, $hostname
194 if (! empty($routinename)) {
195 $itemType = PMA_getRoutineType($dbname, $routinename);
201 if (! empty($_POST['update_privs'])) {
202 if (is_array($dbname)) {
203 foreach ($dbname as $key => $db_name) {
204 list($sql_query[$key], $message) = PMA_updatePrivileges(
205 (isset($username) ?
$username : ''),
206 (isset($hostname) ?
$hostname : ''),
209 : (isset($routinename) ?
$routinename : '')),
210 (isset($db_name) ?
$db_name : ''),
215 $sql_query = implode("\n", $sql_query);
217 list($sql_query, $message) = PMA_updatePrivileges(
218 (isset($username) ?
$username : ''),
219 (isset($hostname) ?
$hostname : ''),
222 : (isset($routinename) ?
$routinename : '')),
223 (isset($dbname) ?
$dbname : ''),
230 * Assign users to user groups
232 if (! empty($_REQUEST['changeUserGroup']) && $cfgRelation['menuswork']
233 && $GLOBALS['is_superuser'] && $GLOBALS['is_createuser']
235 PMA_setUserGroup($username, $_REQUEST['userGroup']);
236 $message = PMA\libraries\Message
::success();
242 if (isset($_REQUEST['revokeall'])) {
243 list ($message, $sql_query) = PMA_getMessageAndSqlQueryForPrivilegesRevoke(
244 (isset($dbname) ?
$dbname : ''),
247 : (isset($routinename) ?
$routinename : '')),
255 * Updates the password
257 if (isset($_REQUEST['change_pw'])) {
258 $message = PMA_updatePassword(
259 $err_url, $username, $hostname
265 * (Changes / copies a user, part IV)
267 if (isset($_REQUEST['delete'])
268 ||
(isset($_REQUEST['change_copy']) && $_REQUEST['mode'] < 4)
270 include_once 'libraries/relation_cleanup.lib.php';
271 $queries = PMA_getDataForDeleteUsers($queries);
272 if (empty($_REQUEST['change_copy'])) {
273 list($sql_query, $message) = PMA_deleteUser($queries);
278 * Changes / copies a user, part V
280 if (isset($_REQUEST['change_copy'])) {
281 $queries = PMA_getDataForQueries($queries, $queries_for_display);
282 $message = PMA\libraries\Message
::success();
283 $sql_query = join("\n", $queries);
287 * Reloads the privilege tables into memory
289 $message_ret = PMA_updateMessageForReload();
290 if (isset($message_ret)) {
291 $message = $message_ret;
296 * If we are in an Ajax request for Create User/Edit User/Revoke User/
297 * Flush Privileges, show $message and exit.
299 if ($GLOBALS['is_ajax_request']
300 && empty($_REQUEST['ajax_page_request'])
301 && ! isset($_REQUEST['export'])
302 && (! isset($_REQUEST['submit_mult']) ||
$_REQUEST['submit_mult'] != 'export')
303 && ((! isset($_REQUEST['initial']) ||
$_REQUEST['initial'] === null
304 ||
$_REQUEST['initial'] === '')
305 ||
(isset($_REQUEST['delete']) && $_REQUEST['delete'] === __('Go')))
306 && ! isset($_REQUEST['showall'])
307 && ! isset($_REQUEST['edit_user_group_dialog'])
308 && ! isset($_REQUEST['db_specific'])
310 $extra_data = PMA_getExtraDataForAjaxBehavior(
311 (isset($password) ?
$password : ''),
312 (isset($sql_query) ?
$sql_query : ''),
313 (isset($hostname) ?
$hostname : ''),
314 (isset($username) ?
$username : '')
317 if (! empty($message) && $message instanceof PMA\libraries\Message
) {
318 $response = PMA\libraries\Response
::getInstance();
319 $response->setRequestStatus($message->isSuccess());
320 $response->addJSON('message', $message);
321 $response->addJSON($extra_data);
329 if (isset($_REQUEST['viewing_mode']) && $_REQUEST['viewing_mode'] == 'db') {
330 $GLOBALS['db'] = $_REQUEST['db'] = $_REQUEST['checkprivsdb'];
332 $url_query .= '&goto=db_operations.php';
334 // Gets the database structure
335 $sub_part = '_structure';
344 $db_is_system_schema,
348 ) = PMA\libraries\Util
::getDbInfo($db, isset($sub_part) ?
$sub_part : '');
350 $content = ob_get_contents();
352 $response->addHTML($content . "\n");
354 if (! empty($GLOBALS['message'])) {
355 $response->addHTML(PMA\libraries\Util
::getMessage($GLOBALS['message']));
356 unset($GLOBALS['message']);
364 PMA_getHtmlForUserGroupDialog(
365 isset($username)?
$username : null,
366 $cfgRelation['menuswork']
370 // export user definition
371 if (isset($_REQUEST['export'])
372 ||
(isset($_REQUEST['submit_mult']) && $_REQUEST['submit_mult'] == 'export')
374 list($title, $export) = PMA_getListForExportUserDefinition(
375 isset($username) ?
$username : null,
376 isset($hostname) ?
$hostname : null
379 unset($username, $hostname, $grants, $one_grant);
381 $response = PMA\libraries\Response
::getInstance();
382 if ($GLOBALS['is_ajax_request']) {
383 $response->addJSON('message', $export);
384 $response->addJSON('title', $title);
387 $response->addHTML("<h2>$title</h2>$export");
391 if (isset($_REQUEST['adduser'])) {
394 PMA_getHtmlForAddUser((isset($dbname) ?
$dbname : ''))
396 } elseif (isset($_REQUEST['checkprivsdb'])) {
397 if (isset($_REQUEST['checkprivstable'])) {
398 // check the privileges for a particular table.
400 PMA_getHtmlForSpecificTablePrivileges(
401 $_REQUEST['checkprivsdb'], $_REQUEST['checkprivstable']
405 // check the privileges for a particular database.
407 PMA_getHtmlForSpecificDbPrivileges($_REQUEST['checkprivsdb'])
411 if (isset($dbname) && ! is_array($dbname)) {
412 $url_dbname = urlencode(
421 if (! isset($username)) {
422 // No username is given --> display the overview
424 PMA_getHtmlForUserOverview($pmaThemeImage, $text_dir)
426 } else if (!empty($routinename)) {
428 PMA_getHtmlForRoutineSpecificPrivilges(
429 $username, $hostname, $dbname, $routinename,
430 (isset($url_dbname) ?
$url_dbname : '')
434 // A user was selected -> display the user's properties
435 // In an Ajax request, prevent cached values from showing
436 if ($GLOBALS['is_ajax_request'] == true) {
437 header('Cache-Control: no-cache');
441 PMA_getHtmlForUserProperties(
442 (isset($dbname_is_wildcard) ?
$dbname_is_wildcard : ''),
443 (isset($url_dbname) ?
$url_dbname : ''),
444 $username, $hostname,
445 (isset($dbname) ?
$dbname : ''),
446 (isset($tablename) ?
$tablename : '')
452 if ((isset($_REQUEST['viewing_mode']) && $_REQUEST['viewing_mode'] == 'server')
453 && $GLOBALS['cfgRelation']['menuswork']
455 $response->addHTML('</div>');