PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/User.php

https://bitbucket.org/delpho/tickhub
PHP | 288 lines | 187 code | 48 blank | 53 comment | 39 complexity | 5a9a9d297a3ca730d835eb42cfb713a7 MD5 | raw file
  1. <?php
  2. /**
  3. * Description of User
  4. *
  5. * @author jaraya
  6. */
  7. final class User {
  8. const TABLE = 'user';
  9. const VIEW_TICKSPOT_USERS = 'v_tickhub_users';
  10. /**
  11. *
  12. * @var int
  13. */
  14. private $id;
  15. /**
  16. *
  17. * @var string
  18. */
  19. private $email;
  20. /**
  21. *
  22. * @var string
  23. */
  24. private $password;
  25. /**
  26. *
  27. * @var string
  28. */
  29. private $given_name;
  30. /**
  31. *
  32. * @var int
  33. */
  34. private $tickspot_user_id;
  35. /**
  36. *
  37. * @var string
  38. */
  39. private $tickspot_company;
  40. /**
  41. *
  42. * @var TickSpotUser
  43. */
  44. private $tickspot_user;
  45. /**
  46. *
  47. * @var string
  48. */
  49. private $github_access_token;
  50. /**
  51. *
  52. * @var string
  53. */
  54. private $github_token_type;
  55. /**
  56. *
  57. * @var User
  58. */
  59. private static $_logged_user;
  60. public function getID () {
  61. return $this->id;
  62. }
  63. public function getEmail () {
  64. return $this->email;
  65. }
  66. public function setEmail ( $email ) {
  67. $this->email = $email;
  68. }
  69. public function setPassword ( $password ) {
  70. $this->password = sha1($password);
  71. }
  72. public function getGivenName () {
  73. return $this->given_name;
  74. }
  75. public function setGivenName ( $name ) {
  76. $this->given_name = $name;
  77. }
  78. public function getTickSpotUserID () {
  79. return $this->tickspot_user_id;
  80. }
  81. public function setTickSpotUserID ($userID) {
  82. $this->tickspot_user_id = Utils::enforcePositiveIntegerValue($userID);
  83. }
  84. public function getTickSpotUser () {
  85. return null;
  86. }
  87. public function getTickspotCompany () {
  88. return $this->tickspot_company;
  89. }
  90. public function setTickSpotCompany ( $company ) {
  91. $this->tickspot_company = $company;
  92. }
  93. public function getGithubAccessToken () {
  94. return $this->github_access_token;
  95. }
  96. public function setGithubAccessToken ( $githubAccessToken ) {
  97. $this->github_access_token = $githubAccessToken;
  98. return $this;
  99. }
  100. public function getGithubTokenType () {
  101. return $this->github_token_type;
  102. }
  103. public function setGithubTokenType ( $githubTokenType ) {
  104. $this->github_token_type = $githubTokenType;
  105. return $this;
  106. }
  107. public function __construct($id = null) {
  108. if ($id !== null) {
  109. $id = Utils::enforcePositiveIntegerValue($id);
  110. $this->retrieve($id);
  111. }
  112. }
  113. private function retrieve($userID){
  114. $pdo = Database::getInstance()->getConnection();
  115. if ($pdo) {
  116. $result = $pdo->query('SELECT * FROM ' . self::TABLE . ' WHERE id = ' . $userID);
  117. $row = $result->fetch(PDO::FETCH_ASSOC);
  118. if ($row && count($row) > 0) {
  119. $this->id = (int) $row['id'];
  120. $this->email = $row['email'];
  121. $this->given_name = $row['given_name'];
  122. $this->tickspot_user_id = $row['tickspot_user_id'];
  123. $this->tickspot_company = $row['tickspot_company'];
  124. $this->github_access_token = $row['github_access_token'];
  125. $this->github_token_type = $row['github_token_type'];
  126. } else {
  127. throw new Exception("Invalid user Id");
  128. }
  129. }
  130. return $this;
  131. }
  132. public function save() {
  133. //check mandatory values
  134. if ($this->email === null) {
  135. throw new Exception("The email address is not valid");
  136. } elseif ($this->getID() === null && $this->password == null) {
  137. throw new RoboinvestException("You must specify a password");
  138. }
  139. //check if the username and email are unique
  140. $this->checkEmail();
  141. $pdo = Database::getInstance()->getConnection();
  142. //insert or update?
  143. $sql = $this->getID() ? 'UPDATE ' : 'INSERT INTO ';
  144. $sql .= self::TABLE . ' SET ';
  145. if ($this->getID() == null || ($this->getID() != null && $this->password != null )) {
  146. $sql .= "password = :password, ";
  147. }
  148. $sql .= "email = :email,
  149. given_name = :given_name,
  150. tickspot_user_id = :tickspot_user_id,
  151. tickspot_company = :tickspot_company,
  152. github_access_token = :github_access_token,
  153. github_token_type = :github_token_type ";
  154. $sql .= $this->getID() ? ' WHERE id = :id ' : '';
  155. $stmt = $pdo->prepare($sql);
  156. $params = array(
  157. ':email' => $this->getEmail(),
  158. ':given_name' => $this->getGivenName(),
  159. ':tickspot_user_id' => $this->getTickSpotUserId(),
  160. ':tickspot_company' => $this->getTickspotCompany(),
  161. ':github_access_token' => $this->getGithubAccessToken(),
  162. ':github_token_type' => $this->getGithubTokenType(),
  163. );
  164. if ($this->getID() == null || ($this->getID() != null && $this->password != null )) {
  165. $params[':password'] = $this->password;
  166. }
  167. if ($this->getID()) {
  168. $params[':id'] = $this->getID();
  169. }
  170. $pdo->beginTransaction();
  171. $stmt->execute($params);
  172. $rows_affected = $stmt->rowCount();
  173. if ($this->getID() == null && $rows_affected == 1) {
  174. $this->id = $pdo->lastInsertId();
  175. Log::getInstance()->log("[registration] New user, id: " . $this->_id);
  176. }
  177. $pdo->commit();
  178. return ($rows_affected == 1 || $rows_affected == 0);
  179. }
  180. private function checkEmail() {
  181. $pdo = Database::getInstance()->getConnection();
  182. $email = $pdo->quote($this->getEmail());
  183. $sql = "SELECT count(1) FROM " . self::TABLE . " WHERE email = $email ";
  184. $sql .= $this->getID() ? " AND id <> " . $this->getID() : '';
  185. $result = $pdo->query($sql);
  186. $count = $result->fetch(PDO::FETCH_COLUMN);
  187. if ((int) $count > 0) {
  188. throw new Exception("The email address is already in use");
  189. }
  190. }
  191. public static function login ($email, $password, $remember = true ) {
  192. $email = Utils::enforceEmailValue($email);
  193. $pdo = Database::getInstance()->getConnection();
  194. if ($pdo) {
  195. $sql = "SELECT id FROM " . self::TABLE . " WHERE email = :email AND password = :password ";
  196. $stmt = $pdo->prepare($sql);
  197. $params = array(
  198. ':email' => $email,
  199. ':password' => sha1($password)
  200. );
  201. $sucess = $stmt->execute($params);
  202. if ($sucess) {
  203. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  204. if ($row && count($row) > 0) {
  205. Log::getInstance()->log("[login] $email successfully logged in");
  206. $user = new User($row['id']);
  207. UserSession::initSession($user->getID(), $remember);
  208. return $user;
  209. } else {
  210. Log::getInstance()->log("[login] Invalid username or password for '$email' ");
  211. }
  212. }
  213. }
  214. return null;
  215. }
  216. public static function isLogged() {
  217. return isset($_COOKIE) && isset($_COOKIE['customer']);
  218. }
  219. /**
  220. * Get the current logged user data
  221. *
  222. * @return User
  223. */
  224. public static function getLoggedUser() {
  225. if ( self::isLogged() ) {
  226. if ( self::$_logged_user == null ) {
  227. try {
  228. $cookie = $_COOKIE['customer'];
  229. $session = new UserSession($cookie);
  230. self::$_logged_user = new User($session->getUserID());
  231. } catch (Exception $ex) {
  232. Log::getInstance()->log("[$cookie] Session error... logging out ({$ex->getMessage()})");
  233. UserSession::endSession();
  234. }
  235. }
  236. }
  237. return self::$_logged_user;
  238. }
  239. }
  240. ?>