server_user_groups.lib.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * set of functions for user group handling
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Return HTML to list the users belonging to a given user group
  13. *
  14. * @param string $userGroup user group name
  15. *
  16. * @return string HTML to list the users belonging to a given user group
  17. */
  18. function PMA_getHtmlForListingUsersofAGroup($userGroup)
  19. {
  20. $html_output = '<h2>'
  21. . sprintf(__('Users of \'%s\' user group'), htmlspecialchars($userGroup))
  22. . '</h2>';
  23. $usersTable = PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb'])
  24. . "." . PMA_Util::backquote($GLOBALS['cfg']['Server']['users']);
  25. $sql_query = "SELECT `username` FROM " . $usersTable
  26. . " WHERE `usergroup`='" . PMA_Util::sqlAddSlashes($userGroup) . "'";
  27. $result = PMA_queryAsControlUser($sql_query, false);
  28. if ($result) {
  29. if ($GLOBALS['dbi']->numRows($result) == 0) {
  30. $html_output .= '<p>'
  31. . __('No users were found belonging to this user group.')
  32. . '</p>';
  33. } else {
  34. $html_output .= '<table>'
  35. . '<thead><tr><th>#</th><th>' . __('User') . '</th></tr></thead>'
  36. . '<tbody>';
  37. $i = 0;
  38. while ($row = $GLOBALS['dbi']->fetchRow($result)) {
  39. $i++;
  40. $html_output .= '<tr>'
  41. . '<td>' . $i . ' </td>'
  42. . '<td>' . htmlspecialchars($row[0]) . '</td>'
  43. . '</tr>';
  44. }
  45. $html_output .= '</tbody>'
  46. . '</table>';
  47. }
  48. }
  49. $GLOBALS['dbi']->freeResult($result);
  50. return $html_output;
  51. }
  52. /**
  53. * Returns HTML for the 'user groups' table
  54. *
  55. * @return string HTML for the 'user groups' table
  56. */
  57. function PMA_getHtmlForUserGroupsTable()
  58. {
  59. $tabs = PMA_Util::getMenuTabList();
  60. $html_output = '<h2>' . __('User groups') . '</h2>';
  61. $groupTable = PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb'])
  62. . "." . PMA_Util::backquote($GLOBALS['cfg']['Server']['usergroups']);
  63. $sql_query = "SELECT * FROM " . $groupTable . " ORDER BY `usergroup` ASC";
  64. $result = PMA_queryAsControlUser($sql_query, false);
  65. if ($result && $GLOBALS['dbi']->numRows($result)) {
  66. $html_output .= '<form name="userGroupsForm" id="userGroupsForm"'
  67. . ' action="server_privileges.php" method="post">';
  68. $html_output .= PMA_URL_getHiddenInputs();
  69. $html_output .= '<table id="userGroupsTable">';
  70. $html_output .= '<thead><tr>';
  71. $html_output .= '<th style="white-space: nowrap">'
  72. . __('User group') . '</th>';
  73. $html_output .= '<th>' . __('Server level tabs') . '</th>';
  74. $html_output .= '<th>' . __('Database level tabs') . '</th>';
  75. $html_output .= '<th>' . __('Table level tabs') . '</th>';
  76. $html_output .= '<th>' . __('Action') . '</th>';
  77. $html_output .= '</tr></thead>';
  78. $html_output .= '<tbody>';
  79. $odd = true;
  80. $userGroups = array();
  81. while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
  82. $groupName = $row['usergroup'];
  83. if (! isset($userGroups[$groupName])) {
  84. $userGroups[$groupName] = array();
  85. }
  86. $userGroups[$groupName][$row['tab']] = $row['allowed'];
  87. }
  88. foreach ($userGroups as $groupName => $tabs) {
  89. $html_output .= '<tr class="' . ($odd ? 'odd' : 'even') . '">';
  90. $html_output .= '<td>' . htmlspecialchars($groupName) . '</td>';
  91. $html_output .= '<td>' . _getAllowedTabNames($tabs, 'server') . '</td>';
  92. $html_output .= '<td>' . _getAllowedTabNames($tabs, 'db') . '</td>';
  93. $html_output .= '<td>' . _getAllowedTabNames($tabs, 'table') . '</td>';
  94. $html_output .= '<td>';
  95. $html_output .= '<a class="" href="server_user_groups.php'
  96. . PMA_URL_getCommon(
  97. array(
  98. 'viewUsers' => 1, 'userGroup' => $groupName
  99. )
  100. )
  101. . '">'
  102. . PMA_Util::getIcon('b_usrlist.png', __('View users')) . '</a>';
  103. $html_output .= '&nbsp;&nbsp;';
  104. $html_output .= '<a class="" href="server_user_groups.php'
  105. . PMA_URL_getCommon(
  106. array(
  107. 'editUserGroup' => 1, 'userGroup' => $groupName
  108. )
  109. )
  110. . '">'
  111. . PMA_Util::getIcon('b_edit.png', __('Edit')) . '</a>';
  112. $html_output .= '&nbsp;&nbsp;';
  113. $html_output .= '<a class="deleteUserGroup ajax"'
  114. . ' href="server_user_groups.php'
  115. . PMA_URL_getCommon(
  116. array(
  117. 'deleteUserGroup' => 1, 'userGroup' => $groupName
  118. )
  119. )
  120. . '">'
  121. . PMA_Util::getIcon('b_drop.png', __('Delete')) . '</a>';
  122. $html_output .= '</td>';
  123. $html_output .= '</tr>';
  124. $odd = ! $odd;
  125. }
  126. $html_output .= '</tbody>';
  127. $html_output .= '</table>';
  128. $html_output .= '</form>';
  129. }
  130. $GLOBALS['dbi']->freeResult($result);
  131. $html_output .= '<fieldset id="fieldset_add_user_group">';
  132. $html_output .= '<a href="server_user_groups.php'
  133. . PMA_URL_getCommon(array('addUserGroup' => 1)) . '">'
  134. . PMA_Util::getIcon('b_usradd.png')
  135. . __('Add user group') . '</a>';
  136. $html_output .= '</fieldset>';
  137. return $html_output;
  138. }
  139. /**
  140. * Returns the list of allowed menu tab names
  141. * based on a data row from usergroup table.
  142. *
  143. * @param array $row row of usergroup table
  144. * @param string $level 'server', 'db' or 'table'
  145. *
  146. * @return string comma seperated list of allowed menu tab names
  147. */
  148. function _getAllowedTabNames($row, $level)
  149. {
  150. $tabNames = array();
  151. $tabs = PMA_Util::getMenuTabList($level);
  152. foreach ($tabs as $tab => $tabName) {
  153. if (! isset($row[$level . '_' . $tab])
  154. || $row[$level . '_' . $tab] == 'Y'
  155. ) {
  156. $tabNames[] = $tabName;
  157. }
  158. }
  159. return implode(', ', $tabNames);
  160. }
  161. /**
  162. * Deletes a user group
  163. *
  164. * @param string $userGroup user group name
  165. *
  166. * @return void
  167. */
  168. function PMA_deleteUserGroup($userGroup)
  169. {
  170. $userTable = PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb'])
  171. . "." . PMA_Util::backquote($GLOBALS['cfg']['Server']['users']);
  172. $groupTable = PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb'])
  173. . "." . PMA_Util::backquote($GLOBALS['cfg']['Server']['usergroups']);
  174. $sql_query = "DELETE FROM " . $userTable
  175. . " WHERE `usergroup`='" . PMA_Util::sqlAddSlashes($userGroup) . "'";
  176. PMA_queryAsControlUser($sql_query, true);
  177. $sql_query = "DELETE FROM " . $groupTable
  178. . " WHERE `usergroup`='" . PMA_Util::sqlAddSlashes($userGroup) . "'";
  179. PMA_queryAsControlUser($sql_query, true);
  180. }
  181. /**
  182. * Returns HTML for add/edit user group dialog
  183. *
  184. * @param string $userGroup name of the user group in case of editing
  185. *
  186. * @return string HTML for add/edit user group dialog
  187. */
  188. function PMA_getHtmlToEditUserGroup($userGroup = null)
  189. {
  190. $html_output = '';
  191. if ($userGroup == null) {
  192. $html_output .= '<h2>' . __('Add user group') . '</h2>';
  193. } else {
  194. $html_output .= '<h2>'
  195. . sprintf(__('Edit user group: \'%s\''), htmlspecialchars($userGroup))
  196. . '</h2>';
  197. }
  198. $html_output .= '<form name="userGroupForm" id="userGroupForm"'
  199. . ' action="server_user_groups.php" method="post">';
  200. $urlParams = array();
  201. if ($userGroup != null) {
  202. $urlParams['userGroup'] = $userGroup;
  203. $urlParams['editUserGroupSubmit'] = '1';
  204. } else {
  205. $urlParams['addUserGroupSubmit'] = '1';
  206. }
  207. $html_output .= PMA_URL_getHiddenInputs($urlParams);
  208. $html_output .= '<fieldset id="fieldset_user_group_rights">';
  209. $html_output .= '<legend>' . __('User group menu assignments')
  210. . '&nbsp;&nbsp;&nbsp;'
  211. . '<input type="checkbox" class="checkall_box" title="Check All">'
  212. . '<label for="addUsersForm_checkall">' . __('Check All') . '</label>'
  213. . '</legend>';
  214. if ($userGroup == null) {
  215. $html_output .= '<label for="userGroup">' . __('Group name:') . '</label>';
  216. $html_output .= '<input type="text" name="userGroup" '
  217. . 'autocomplete="off" required="required" />';
  218. $html_output .= '<div class="clearfloat"></div>';
  219. }
  220. $allowedTabs = array(
  221. 'server' => array(),
  222. 'db' => array(),
  223. 'table' => array()
  224. );
  225. if ($userGroup != null) {
  226. $groupTable = PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb'])
  227. . "." . PMA_Util::backquote($GLOBALS['cfg']['Server']['usergroups']);
  228. $sql_query = "SELECT * FROM " . $groupTable
  229. . " WHERE `usergroup`='" . PMA_Util::sqlAddSlashes($userGroup) . "'";
  230. $result = PMA_queryAsControlUser($sql_query, false);
  231. if ($result) {
  232. while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
  233. $key = $row['tab'];
  234. $value = $row['allowed'];
  235. if (substr($key, 0, 7) == 'server_' && $value == 'Y') {
  236. $allowedTabs['server'][] = substr($key, 7);
  237. } elseif (substr($key, 0, 3) == 'db_' && $value == 'Y') {
  238. $allowedTabs['db'][] = substr($key, 3);
  239. } elseif (substr($key, 0, 6) == 'table_' && $value == 'Y') {
  240. $allowedTabs['table'][] = substr($key, 6);
  241. }
  242. }
  243. }
  244. $GLOBALS['dbi']->freeResult($result);
  245. }
  246. $html_output .= _getTabList(
  247. __('Server-level tabs'), 'server', $allowedTabs['server']
  248. );
  249. $html_output .= _getTabList(
  250. __('Database-level tabs'), 'db', $allowedTabs['db']
  251. );
  252. $html_output .= _getTabList(
  253. __('Table-level tabs'), 'table', $allowedTabs['table']
  254. );
  255. $html_output .= '</fieldset>';
  256. $html_output .= '<fieldset id="fieldset_user_group_rights_footer"'
  257. . ' class="tblFooters">';
  258. $html_output .= '<input type="submit" name="update_privs" value="Go">';
  259. $html_output .= '</fieldset>';
  260. return $html_output;
  261. }
  262. /**
  263. * Returns HTML for checkbox groups to choose
  264. * tabs of 'server', 'db' or 'table' levels.
  265. *
  266. * @param string $title title of the checkbox group
  267. * @param string $level 'server', 'db' or 'table'
  268. * @param array $selected array of selected allowed tabs
  269. *
  270. * @return string HTML for checkbox groups
  271. */
  272. function _getTabList($title, $level, $selected)
  273. {
  274. $tabs = PMA_Util::getMenuTabList($level);
  275. $html_output = '<fieldset>';
  276. $html_output .= '<legend>' . $title . '</legend>';
  277. foreach ($tabs as $tab => $tabName) {
  278. $html_output .= '<div class="item">';
  279. $html_output .= '<input type="checkbox" class="checkall"'
  280. . (in_array($tab, $selected) ? ' checked="checked"' : '')
  281. . ' name="' . $level . '_' . $tab . '" value="Y" />';
  282. $html_output .= '<label for="' . $level . '_' . $tab . '">'
  283. . '<code>' . $tabName . '</code>'
  284. . '</label>';
  285. $html_output .= '</div>';
  286. }
  287. $html_output .= '</fieldset>';
  288. return $html_output;
  289. }
  290. /**
  291. * Add/update a user group with allowed menu tabs.
  292. *
  293. * @param string $userGroup user group name
  294. * @param boolean $new whether this is a new user group
  295. *
  296. * @return void
  297. */
  298. function PMA_editUserGroup($userGroup, $new = false)
  299. {
  300. $tabs = PMA_Util::getMenuTabList();
  301. $groupTable = PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb'])
  302. . "." . PMA_Util::backquote($GLOBALS['cfg']['Server']['usergroups']);
  303. if (! $new) {
  304. $sql_query = "DELETE FROM " . $groupTable
  305. . " WHERE `usergroup`='" . PMA_Util::sqlAddSlashes($userGroup) . "';";
  306. PMA_queryAsControlUser($sql_query, true);
  307. }
  308. $sql_query = "INSERT INTO " . $groupTable
  309. . "(`usergroup`, `tab`, `allowed`)"
  310. . " VALUES ";
  311. $first = true;
  312. foreach ($tabs as $tabGroupName => $tabGroup) {
  313. foreach ($tabs[$tabGroupName] as $tab => $tabName) {
  314. if (! $first) {
  315. $sql_query .= ", ";
  316. }
  317. $tabName = $tabGroupName . '_' . $tab;
  318. $allowed = isset($_REQUEST[$tabName]) && $_REQUEST[$tabName] == 'Y';
  319. $sql_query .= "('" . $userGroup . "', '" . $tabName . "', '"
  320. . ($allowed ? "Y" : "N") . "')";
  321. $first = false;
  322. }
  323. }
  324. $sql_query .= ";";
  325. PMA_queryAsControlUser($sql_query, true);
  326. }
  327. ?>