Passed
Push — develop ( 55d6dd...809f15 )
by Pieter van der
06:46
created

Tiqr_UserStorage_Pdo::getDisplayName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 3
c 3
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the tiqr project.
4
 * 
5
 * The tiqr project aims to provide an open implementation for 
6
 * authentication using mobile devices. It was initiated by 
7
 * SURFnet and developed by Egeniq.
8
 *
9
 * More information: http://www.tiqr.org
10
 *
11
 * @author Patrick Honing <[email protected]>
12
 * 
13
 * @package tiqr
14
 *
15
 * @license New BSD License - See LICENSE file for details.
16
 *
17
 * @copyright (C) 2010-2012 SURFnet BV
18
 * 
19
 * Create SQL table (MySQL):
20
 * CREATE TABLE `tiqruser` (`userid` varchar(10) PRIMARY KEY, `displayname` varchar(45),`blocked` int,`loginattempts` int,
21
 * `tmpblockattempts` int,`tmpblocktimestamp` varchar(45) default NULL,`notificationtype` varchar(10),`notificationaddress` varchar(45))
22
 * 
23
 */
24
25
26
/**
27
 * This user storage implementation implements a user storage using PDO.
28
 * It is usable for any database with a PDO driver
29
 * 
30
 * @author Patrick Honing <[email protected]>
31
 */
32
class Tiqr_UserStorage_Pdo extends Tiqr_UserStorage_Abstract
33
{
34
    protected $handle = null;
35
    protected $tablename;
36
    
37
    /**
38
     * Create an instance
39
     * @param array $config
40
     * @param array $secretconfig
41
     */
42 2
    public function __construct($config, $secretconfig = array())
43
    {
44 2
        parent::__construct($config, $secretconfig);
45 2
        $this->tablename = isset($config['table']) ? $config['table'] : 'tiqruser';
46
        try {
47 2
            $this->handle = new PDO($config['dsn'],$config['username'],$config['password']);
48
        } catch (PDOException $e) {
49
            return false;
50
        }
51 2
    }
52
53 2
    public function createUser($userId, $displayName)
54
    {
55 2
        if ($this->userExists($userId)) {
56
            $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET displayname = ? WHERE userid = ?");
57
        } else {
58 2
            $sth = $this->handle->prepare("INSERT INTO ".$this->tablename." (displayname,userid) VALUES (?,?)");
59
        }
60 2
        $sth->execute(array($displayName,$userId));
61 2
        return $this->userExists($userId);
62
    }
63
    
64 2
    public function userExists($userId)
65
    {
66 2
        $sth = $this->handle->prepare("SELECT userid FROM ".$this->tablename." WHERE userid = ?");
67 2
        $sth->execute(array($userId));
68 2
        return $sth->fetchColumn();
69
    }
70
    
71 2
    public function getDisplayName($userId)
72
    {
73 2
        $sth = $this->handle->prepare("SELECT displayname FROM ".$this->tablename." WHERE userid = ?");
74 2
        $sth->execute(array($userId));
75 2
        return $sth->fetchColumn();
76
    }
77
78 2
    public function getNotificationType($userId)
79
    {
80 2
        $sth = $this->handle->prepare("SELECT notificationtype FROM ".$this->tablename." WHERE userid = ?");
81 2
        $sth->execute(array($userId));
82 2
        return $sth->fetchColumn();
83
    }
84
    
85 2
    public function setNotificationType($userId, $type)
86
    {
87 2
        $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET notificationtype = ? WHERE userid = ?");
88 2
        $sth->execute(array($type,$userId));
89 2
    }
90
    
91 2
    public function getNotificationAddress($userId)
92
    {
93 2
        $sth = $this->handle->prepare("SELECT notificationaddress FROM ".$this->tablename." WHERE userid = ?");
94 2
        $sth->execute(array($userId));
95 2
        return $sth->fetchColumn();
96
    }
97
    
98
    public function setNotificationAddress($userId, $address)
99
    {
100
        $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET notificationaddress = ?  WHERE userid = ?");
101
        $sth->execute(array($address,$userId));
102
    }
103
    
104 2
    public function getLoginAttempts($userId)
105
    {
106 2
        $sth = $this->handle->prepare("SELECT loginattempts FROM ".$this->tablename." WHERE userid = ?");
107 2
        $sth->execute(array($userId));
108 2
        return $sth->fetchColumn();
109
    }
110
    
111 2
    public function setLoginAttempts($userId, $amount)
112
    {
113 2
        $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET loginattempts = ? WHERE userid = ?");
114 2
        $sth->execute(array($amount,$userId));
115 2
    }
116
    
117 2
    public function isBlocked($userId, $duration)
118
    {
119 2
        if ($this->userExists($userId)) {
120 2
            $sth = $this->handle->prepare("SELECT blocked FROM ".$this->tablename." WHERE userid = ?");
121 2
            $sth->execute(array($userId));
122 2
            $blocked = ($sth->fetchColumn() == 1);
123 2
            $timestamp = $this->getTemporaryBlockTimestamp($userId);
124
            // if not blocked or block is expired, return false
125 2
            if (!$blocked || (false !== $timestamp && false != $duration && (strtotime($timestamp) + $duration * 60) < time())) {
126 2
                return false;
127
            }
128 2
            return true;
129
        } else {
130
            return false;
131
        }
132
    }
133
    
134 2
    public function setBlocked($userId, $blocked)
135
    {
136 2
        $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET blocked = ? WHERE userid = ?");
137 2
        $sth->execute(array(
138 2
                ($blocked) ? "1" : "0",
139 2
                $userId
140
        ));
141 2
    }
142
    
143 2
    public function setTemporaryBlockAttempts($userId, $amount) {
144 2
        $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET tmpblockattempts = ? WHERE userid = ?");
145 2
        $sth->execute(array($amount,$userId));
146 2
    }
147
    
148 2
    public function getTemporaryBlockAttempts($userId) {
149 2
        if ($this->userExists($userId)) {
150 2
            $sth = $this->handle->prepare("SELECT tmpblockattempts FROM ".$this->tablename." WHERE userid = ?");
151 2
            $sth->execute(array($userId));
152 2
            return $sth->fetchColumn();
153
        }
154
        return 0;
155
    }
156
    
157 2
    public function setTemporaryBlockTimestamp($userId, $timestamp)
158
    {
159 2
        $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET tmpblocktimestamp = ? WHERE userid = ?");
160 2
        $sth->execute(array($timestamp,$userId));
161 2
    }
162
            
163 2
    public function getTemporaryBlockTimestamp($userId)
164
    {
165 2
        if ($this->userExists($userId)) {
166 2
            $sth = $this->handle->prepare("SELECT tmpblocktimestamp FROM ".$this->tablename." WHERE userid = ?");
167 2
            $sth->execute(array($userId));
168 2
            $timestamp = $sth->fetchColumn(); 
169 2
            if (null !== $timestamp) {
170 2
                return $timestamp;
171
            }
172
        }
173 2
        return false;
174
    }
175
    
176
}
177