UserGroups.php 14 KB

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