PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/UsersManager/Model.php

https://github.com/CodeYellowBV/piwik
PHP | 274 lines | 185 code | 35 blank | 54 comment | 5 complexity | 8d84269bbaec9c40549924b800ecc231 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Plugins\UsersManager;
  10. use Piwik\Common;
  11. use Piwik\Db;
  12. use Piwik\Piwik;
  13. /**
  14. * The UsersManager API lets you Manage Users and their permissions to access specific websites.
  15. *
  16. * You can create users via "addUser", update existing users via "updateUser" and delete users via "deleteUser".
  17. * There are many ways to list users based on their login "getUser" and "getUsers", their email "getUserByEmail",
  18. * or which users have permission (view or admin) to access the specified websites "getUsersWithSiteAccess".
  19. *
  20. * Existing Permissions are listed given a login via "getSitesAccessFromUser", or a website ID via "getUsersAccessFromSite",
  21. * or you can list all users and websites for a given permission via "getUsersSitesFromAccess". Permissions are set and updated
  22. * via the method "setUserAccess".
  23. * See also the documentation about <a href='http://piwik.org/docs/manage-users/' target='_blank'>Managing Users</a> in Piwik.
  24. */
  25. class Model
  26. {
  27. /**
  28. * Returns the list of all the users
  29. *
  30. * @param string[] $userLogins List of users to select. If empty, will return all users
  31. * @return array the list of all the users
  32. */
  33. public function getUsers(array $userLogins)
  34. {
  35. $where = '';
  36. $bind = array();
  37. if (!empty($userLogins)) {
  38. $where = 'WHERE login IN (' . Common::getSqlStringFieldsArray($userLogins) . ')';
  39. $bind = $userLogins;
  40. }
  41. $users = Db::get()->fetchAll("SELECT *
  42. FROM " . Common::prefixTable("user") . "
  43. $where
  44. ORDER BY login ASC", $bind);
  45. return $users;
  46. }
  47. /**
  48. * Returns the list of all the users login
  49. *
  50. * @return array the list of all the users login
  51. */
  52. public function getUsersLogin()
  53. {
  54. $users = Db::get()->fetchAll("SELECT login
  55. FROM " . Common::prefixTable("user") . "
  56. ORDER BY login ASC");
  57. $return = array();
  58. foreach ($users as $login) {
  59. $return[] = $login['login'];
  60. }
  61. return $return;
  62. }
  63. public function getUsersSitesFromAccess($access)
  64. {
  65. $users = Db::get()->fetchAll("SELECT login,idsite
  66. FROM " . Common::prefixTable("access")
  67. . " WHERE access = ?
  68. ORDER BY login, idsite", $access);
  69. $return = array();
  70. foreach ($users as $user) {
  71. $return[$user['login']][] = $user['idsite'];
  72. }
  73. return $return;
  74. }
  75. public function getUsersAccessFromSite($idSite)
  76. {
  77. $users = Db::get()->fetchAll("SELECT login,access
  78. FROM " . Common::prefixTable("access")
  79. . " WHERE idsite = ?", $idSite);
  80. $return = array();
  81. foreach ($users as $user) {
  82. $return[$user['login']] = $user['access'];
  83. }
  84. return $return;
  85. }
  86. public function getUsersLoginWithSiteAccess($idSite, $access)
  87. {
  88. $users = Db::get()->fetchAll("SELECT login
  89. FROM " . Common::prefixTable("access")
  90. . " WHERE idsite = ? AND access = ?", array($idSite, $access));
  91. $logins = array();
  92. foreach ($users as $user) {
  93. $logins[] = $user['login'];
  94. }
  95. return $logins;
  96. }
  97. /**
  98. * For each website ID, returns the access level of the given $userLogin.
  99. * If the user doesn't have any access to a website ('noaccess'),
  100. * this website will not be in the returned array.
  101. * If the user doesn't have any access, the returned array will be an empty array.
  102. *
  103. * @param string $userLogin User that has to be valid
  104. *
  105. * @return array The returned array has the format
  106. * array(
  107. * idsite1 => 'view',
  108. * idsite2 => 'admin',
  109. * idsite3 => 'view',
  110. * ...
  111. * )
  112. */
  113. public function getSitesAccessFromUser($userLogin)
  114. {
  115. $users = Db::get()->fetchAll("SELECT idsite,access
  116. FROM " . Common::prefixTable("access")
  117. . " WHERE login = ?", $userLogin);
  118. $return = array();
  119. foreach ($users as $user) {
  120. $return[] = array(
  121. 'site' => $user['idsite'],
  122. 'access' => $user['access'],
  123. );
  124. }
  125. return $return;
  126. }
  127. public function getUser($userLogin)
  128. {
  129. return Db::get()->fetchRow("SELECT *
  130. FROM " . Common::prefixTable("user")
  131. . " WHERE login = ?", $userLogin);
  132. }
  133. public function getUserByEmail($userEmail)
  134. {
  135. return Db::get()->fetchRow("SELECT *
  136. FROM " . Common::prefixTable("user")
  137. . " WHERE email = ?", $userEmail);
  138. }
  139. public function getUserByTokenAuth($tokenAuth)
  140. {
  141. return Db::get()->fetchRow('SELECT *
  142. FROM ' . Common::prefixTable('user') . '
  143. WHERE token_auth = ?', $tokenAuth);
  144. }
  145. public function addUser($userLogin, $passwordTransformed, $email, $alias, $tokenAuth, $dateRegistered)
  146. {
  147. $user = array(
  148. 'login' => $userLogin,
  149. 'password' => $passwordTransformed,
  150. 'alias' => $alias,
  151. 'email' => $email,
  152. 'token_auth' => $tokenAuth,
  153. 'date_registered' => $dateRegistered,
  154. 'superuser_access' => 0
  155. );
  156. Db::get()->insert(Common::prefixTable("user"), $user);
  157. }
  158. public function setSuperUserAccess($userLogin, $hasSuperUserAccess)
  159. {
  160. Db::get()->update(Common::prefixTable("user"),
  161. array(
  162. 'superuser_access' => $hasSuperUserAccess ? 1 : 0
  163. ),
  164. "login = '$userLogin'"
  165. );
  166. }
  167. public function getUsersHavingSuperUserAccess()
  168. {
  169. $users = Db::get()->fetchAll("SELECT login, email
  170. FROM " . Common::prefixTable("user") . "
  171. WHERE superuser_access = 1
  172. ORDER BY date_registered ASC");
  173. return $users;
  174. }
  175. public function updateUser($userLogin, $password, $email, $alias, $tokenAuth)
  176. {
  177. Db::get()->update(Common::prefixTable("user"),
  178. array(
  179. 'password' => $password,
  180. 'alias' => $alias,
  181. 'email' => $email,
  182. 'token_auth' => $tokenAuth
  183. ),
  184. "login = '$userLogin'"
  185. );
  186. }
  187. public function userExists($userLogin)
  188. {
  189. $count = Db::get()->fetchOne("SELECT count(*)
  190. FROM " . Common::prefixTable("user") . "
  191. WHERE login = ?", $userLogin);
  192. return $count != 0;
  193. }
  194. public function userEmailExists($userEmail)
  195. {
  196. $count = Db::get()->fetchOne("SELECT count(*)
  197. FROM " . Common::prefixTable("user") . "
  198. WHERE email = ?", $userEmail);
  199. return $count != 0;
  200. }
  201. public function addUserAccess($userLogin, $access, $idSites)
  202. {
  203. foreach ($idSites as $idsite) {
  204. Db::get()->insert(Common::prefixTable("access"),
  205. array("idsite" => $idsite,
  206. "login" => $userLogin,
  207. "access" => $access)
  208. );
  209. }
  210. }
  211. public function deleteUserOnly($userLogin)
  212. {
  213. Db::get()->query("DELETE FROM " . Common::prefixTable("user") . " WHERE login = ?", $userLogin);
  214. /**
  215. * Triggered after a user has been deleted.
  216. *
  217. * This event should be used to clean up any data that is related to the now deleted user.
  218. * The **Dashboard** plugin, for example, uses this event to remove the user's dashboards.
  219. *
  220. * @param string $userLogin The login handle of the deleted user.
  221. */
  222. Piwik::postEvent('UsersManager.deleteUser', array($userLogin));
  223. }
  224. public function deleteUserAccess($userLogin, $idSites = null)
  225. {
  226. if (is_null($idSites)) {
  227. Db::get()->query("DELETE FROM " . Common::prefixTable("access") .
  228. " WHERE login = ?",
  229. array($userLogin));
  230. } else {
  231. foreach ($idSites as $idsite) {
  232. Db::get()->query("DELETE FROM " . Common::prefixTable("access") .
  233. " WHERE idsite = ? AND login = ?",
  234. array($idsite, $userLogin)
  235. );
  236. }
  237. }
  238. }
  239. }