Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. <?php
  2. namespace UserAccounts\Model;
  3. require 'DBConnect.php';
  4.  
  5. use \PDO;
  6.  
  7. class Account {
  8. private $id;
  9. private $firstName;
  10. private $lastName;
  11. private $username;
  12. private $password;
  13.  
  14. public function __construct($firstName, $lastName, $username, $password, $id=null) {
  15. $this->setFirstName($firstName);
  16. $this->setLastName($lastName);
  17. $this->setUsername($username);
  18. $this->setPassword($password);
  19. $this->setId($id);
  20. }
  21.  
  22. public static function add($account) {
  23. // hash the password
  24. $hashed = password_hash($account->getPassword(),
  25. PASSWORD_DEFAULT);
  26.  
  27. try {
  28. $db = new DBConnect();
  29. $conn = $db->getConnection();
  30. } catch (Exception $ex) {
  31. throw $ex;
  32. }
  33.  
  34. $insertStat = $conn->prepare(
  35. "INSERT INTO Account(username, password, firstName, lastName)
  36. VALUES (:username, :password, :firstName, :lastName)");
  37. $insertStat->bindValue(':username', $account->getUsername());
  38. $insertStat->bindValue(':password', $hashed);
  39. $insertStat->bindValue(':firstName', $account->getFirstName());
  40. $insertStat->bindValue(':lastName', $account->getLastName());
  41.  
  42. try {
  43. $insertStat->execute();
  44. } catch (Exception $ex) {
  45. throw $ex;
  46. }
  47.  
  48. return $conn->lastInsertId();
  49. }
  50.  
  51. public static function getAccount($username, $password) {
  52.  
  53. try {
  54. $db = new DBConnect();
  55. $conn = $db->getConnection();
  56. } catch (Exception $ex) {
  57. throw $ex;
  58. }
  59.  
  60. $queryStat = $conn->prepare(
  61. "SELECT * FROM Account WHERE username = :username");
  62. $queryStat->bindValue(':username', $username);
  63.  
  64. try {
  65. $queryStat->execute();
  66. } catch (Exception $ex) {
  67. throw $ex;
  68. }
  69.  
  70. $result = $queryStat->fetch(PDO::FETCH_OBJ);
  71.  
  72. if ($result && password_verify($password, $result->password)) {
  73. return new Account(
  74. $result->firstName,
  75. $result->lastName,
  76. $result->username,
  77. $result->password);
  78. } else {
  79. return null;
  80. }
  81. }
  82.  
  83. public static function isUsernameAvailable($username) {
  84. try {
  85. $db = new DBConnect();
  86. $conn = $db->getConnection();
  87. } catch (Exception $ex) {
  88. throw $ex;
  89. }
  90.  
  91. $queryStat = $conn->prepare(
  92. "SELECT id FROM Account WHERE username = :username");
  93. $queryStat->bindParam(':username', $username);
  94.  
  95. try {
  96. $queryStat->execute();
  97. } catch (Exception $ex) {
  98. throw $ex;
  99. }
  100.  
  101. $result = $queryStat->fetch(PDO::FETCH_OBJ);
  102.  
  103. if ($result && $result != null) {
  104. return false;
  105. }
  106. return true;
  107. }
  108.  
  109. public function getId(){
  110. return $this->id;
  111. }
  112.  
  113. public function setId($id){
  114. $this->id = $id;
  115. }
  116.  
  117. public function getFirstName(){
  118. return $this->firstName;
  119. }
  120.  
  121. public function setFirstName($firstName){
  122. $this->firstName = $firstName;
  123. }
  124.  
  125. public function getLastName(){
  126. return $this->lastName;
  127. }
  128.  
  129. public function setLastName($lastName){
  130. $this->lastName = $lastName;
  131. }
  132.  
  133. public function getUsername(){
  134. return $this->username;
  135. }
  136.  
  137. public function setUsername($username){
  138. $this->username = $username;
  139. }
  140.  
  141. public function getPassword(){
  142. return $this->password;
  143. }
  144.  
  145. public function setPassword($password){
  146. $this->password = $password;
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement