/
userModel.php
executable file
·77 lines (66 loc) · 2.43 KB
/
userModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
require_once __DIR__ . '/vendor/autoload.php';
class UserModel extends Database{
private $table = "users";
public function getById(){
return parent::get($this->table);
}
public function getByEmail1( $email1 ){
return $this->query("SELECT * FROM users WHERE email1 = :email1 LIMIT 1"
,[":email1" => $email1])[0];
}
public function insertEmailAndPassword( $bindings ){
$stmt = $this->conn->prepare("INSERT INTO {$this->table} (email1, password) VALUES (:email1, :password)");
$stmt->bindParam(':email1', $bindings['email1'] ,PDO::PARAM_STR);
$stmt->bindParam(':password', create_hash( $bindings['password'] ) ,PDO::PARAM_STR);
if($stmt->execute()){
return $this->conn->lastInsertId('user_id');
}
return false;
}
public function update($bindings){
$stmt = $this->conn->prepare(
"UPDATE {$this->table}
SET name = :name
, furigana = :furigana
, sex = :sex
, tel = :tel
, zipcode = :zipcode
, xmpf = :xmpf
, address = :address
WHERE email1 = :email1"
);
$stmt->bindParam(':email1', $bindings['email1'] ,PDO::PARAM_STR);
$stmt->bindParam(':name', $bindings['name'] ,PDO::PARAM_STR);
$stmt->bindParam(':furigana', $bindings['furigana'] ,PDO::PARAM_STR);
$stmt->bindParam(':sex', $bindings['sex'] ,PDO::PARAM_INT);
$stmt->bindParam(':tel', $bindings['tel'] ,PDO::PARAM_STR);
$stmt->bindParam(':zipcode', $bindings['zipcode'] ,PDO::PARAM_STR);
$stmt->bindParam(':xmpf', $bindings['xmpf'] ,PDO::PARAM_INT);
$stmt->bindParam(':address', $bindings['address'] ,PDO::PARAM_STR);
if($stmt->execute()){
return true;
}
return false;
}
/**
* メールアドレスが登録済みかチェックする
* @param string $email チェック対象のメールアドレス
* @return bool true:登録済み, false:登録されていない
*/
public function emailExists( $email ){
return (bool)$this->getByEmail1( $email );
}
/**
* パスワードが正しいか検証する
* @param string $email メールアドレス
* @param string $inputPassword 入力されたパスワード
* @return bool true:パスワードは正しい, false:パスワードが間違っている
*/
public function validatePassword( $email, $inputPassword ) {
if( !$user = $this->getByEmail1( $email ) ){
return false;
}
return validate_password($inputPassword, $user->password);
}
}