database.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @copyright 2010 Frank Karlitschek karlitschek@kde.org
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /*
  23. *
  24. * The following SQL statement is just a help for developers and will not be
  25. * executed!
  26. *
  27. * CREATE TABLE `users` (
  28. * `uid` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  29. * `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  30. * PRIMARY KEY (`uid`)
  31. * ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  32. *
  33. */
  34. /**
  35. * Class for user management in a SQL Database (e.g. MySQL, SQLite)
  36. */
  37. class OC_User_Database extends OC_User_Backend {
  38. static private $userGroupCache=array();
  39. /**
  40. * @brief Create a new user
  41. * @param $uid The username of the user to create
  42. * @param $password The password of the new user
  43. * @returns true/false
  44. *
  45. * Creates a new user. Basic checking of username is done in OC_User
  46. * itself, not in its subclasses.
  47. */
  48. public function createUser( $uid, $password ){
  49. if( $this->userExists($uid) ){
  50. return false;
  51. }
  52. else{
  53. $query = OC_DB::prepare( "INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )" );
  54. $result = $query->execute( array( $uid, sha1( $password )));
  55. return $result ? true : false;
  56. }
  57. }
  58. /**
  59. * @brief delete a user
  60. * @param $uid The username of the user to delete
  61. * @returns true/false
  62. *
  63. * Deletes a user
  64. */
  65. public function deleteUser( $uid ){
  66. // Delete user-group-relation
  67. $query = OC_DB::prepare( "DELETE FROM `*PREFIX*users` WHERE uid = ?" );
  68. $result = $query->execute( array( $uid ));
  69. return true;
  70. }
  71. /**
  72. * @brief Set password
  73. * @param $uid The username
  74. * @param $password The new password
  75. * @returns true/false
  76. *
  77. * Change the password of a user
  78. */
  79. public function setPassword( $uid, $password ){
  80. if( $this->userExists($uid) ){
  81. $query = OC_DB::prepare( "UPDATE *PREFIX*users SET password = ? WHERE uid = ?" );
  82. $result = $query->execute( array( sha1( $password ), $uid ));
  83. return true;
  84. }
  85. else{
  86. return false;
  87. }
  88. }
  89. /**
  90. * @brief Check if the password is correct
  91. * @param $uid The username
  92. * @param $password The password
  93. * @returns true/false
  94. *
  95. * Check if the password is correct without logging in the user
  96. */
  97. public function checkPassword( $uid, $password ){
  98. $query = OC_DB::prepare( "SELECT uid FROM *PREFIX*users WHERE uid LIKE ? AND password = ?" );
  99. $result = $query->execute( array( $uid, sha1( $password )));
  100. $row=$result->fetchRow();
  101. if($row){
  102. return $row['uid'];
  103. }else{
  104. return false;
  105. }
  106. }
  107. /**
  108. * @brief Get a list of all users
  109. * @returns array with all uids
  110. *
  111. * Get a list of all users.
  112. */
  113. public function getUsers(){
  114. $query = OC_DB::prepare( "SELECT uid FROM *PREFIX*users" );
  115. $result = $query->execute();
  116. $users=array();
  117. while( $row = $result->fetchRow()){
  118. $users[] = $row["uid"];
  119. }
  120. return $users;
  121. }
  122. /**
  123. * @brief check if a user exists
  124. * @param string $uid the username
  125. * @return boolean
  126. */
  127. public function userExists($uid){
  128. $query = OC_DB::prepare( "SELECT * FROM `*PREFIX*users` WHERE uid = ?" );
  129. $result = $query->execute( array( $uid ));
  130. return $result->numRows() > 0;
  131. }
  132. }