Passed
Push — master ( 1c7137...b5ef5e )
by Pieter van der
03:26 queued 14s
created

Tiqr_UserStorage_GenericStore   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 33
eloc 52
c 1
b 0
f 0
dl 0
loc 189
rs 9.76

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setNotificationType() 0 5 1
A setNotificationAddress() 0 5 1
B isBlocked() 0 10 7
A createUser() 0 5 1
A setBlocked() 0 5 1
A setLoginAttempts() 0 5 1
A getLoginAttempts() 0 8 3
A getDisplayName() 0 6 2
A setTemporaryBlockTimestamp() 0 4 1
A getNotificationType() 0 8 3
A getTemporaryBlockTimestamp() 0 7 3
A getTemporaryBlockAttempts() 0 7 3
A userExists() 0 4 2
A setTemporaryBlockAttempts() 0 4 1
A getNotificationAddress() 0 8 3
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 Ivo Jansch <[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
20
require_once 'Tiqr/UserStorage/Abstract.php';
21
22
/**
23
 * This user storage implementation implements a simple user storage using json files.
24
 * This is mostly for demonstration and development purposes. In a production environment
25
 * please supply your own implementation that hosts the data in your user database OR
26
 * in a secure (e.g. hardware encrypted) storage.
27
 * @author ivo
28
 */
29
abstract class Tiqr_UserStorage_GenericStore extends Tiqr_UserStorage_Abstract
30
{
31
32
    abstract protected function _loadUser($userId, $failOnNotFound = TRUE);
33
  
34
    abstract protected function _saveUser($userId, $data);
35
36
    abstract protected function _deleteUser($userId);
37
38
39
    /**
40
     * (non-PHPdoc)
41
     * @see simplesamlphp/modules/authTiqr/lib/User/sspmod_authTiqr_User_Interface::createUser()
42
     */
43
    public function createUser($userId, $displayName) 
44
    {
45
        $user = array("userId"=>$userId,
46
                      "displayName"=>$displayName);
47
        return $this->_saveUser($userId, $user);
48
    }
49
50
    /**
51
     * (non-PHPdoc)
52
     * @see simplesamlphp/modules/authTiqr/lib/User/sspmod_authTiqr_User_Interface::userExists()
53
     */
54
    public function userExists($userId)
55
    {
56
        $user = $this->_loadUser($userId, FALSE);
57
        return (is_array($user)||is_object($user)); 
58
    }
59
60
    /**
61
     * (non-PHPdoc)
62
     * @see simplesamlphp/modules/authTiqr/lib/User/sspmod_authTiqr_User_Interface::getDisplayName()
63
     */
64
    public function getDisplayName($userId) 
65
    {
66
        if ($data = $this->_loadUser($userId)) {
67
            return $data["displayName"];
68
        }
69
        return NULL;
70
    }
71
72
    /**
73
     * (non-PHPdoc)
74
     * @see simplesamlphp/modules/authTiqr/lib/User/sspmod_authTiqr_User_Interface::getNotificationType()
75
     */
76
    public function getNotificationType($userId)
77
    {
78
        if ($data = $this->_loadUser($userId)) {
79
            if (isset($data["notificationType"])) {
80
               return $data["notificationType"];
81
            }
82
        }
83
        return NULL;
84
    }
85
86
    /**
87
     * (non-PHPdoc)
88
     * @see simplesamlphp/modules/authTiqr/lib/User/sspmod_authTiqr_User_Interface::setNotificationType()
89
     */
90
    public function setNotificationType($userId, $type)
91
    {
92
        $data = $this->_loadUser($userId);
93
        $data["notificationType"] = $type;
94
        $this->_saveUser($userId, $data);
95
    }    
96
    
97
    /**
98
     * (non-PHPdoc)
99
     * @see simplesamlphp/modules/authTiqr/lib/User/sspmod_authTiqr_User_Interface::getNotificationAddress()
100
     */
101
    public function getNotificationAddress($userId)
102
    {
103
        if ($data = $this->_loadUser($userId)) {
104
            if (isset($data["notificationAddress"])) {
105
               return $data["notificationAddress"];
106
            }
107
        }
108
        return NULL;
109
    }
110
111
    /**
112
     * (non-PHPdoc)
113
     * @see simplesamlphp/modules/authTiqr/lib/User/sspmod_authTiqr_User_Interface::setNotificationAddress()
114
     */
115
    public function setNotificationAddress($userId, $address)
116
    {
117
        $data = $this->_loadUser($userId);
118
        $data["notificationAddress"] = $address;
119
        $this->_saveUser($userId, $data);
120
    } 
121
122
    /**
123
     * (non-PHPdoc)
124
     * @see simplesamlphp-module/authTiqr/lib/User/sspmod_authTiqr_User_Interface::getFailedLoginAttempts()
125
     */
126
    public function getLoginAttempts($userId)
127
    {
128
        if ($data = $this->_loadUser($userId)) {
129
            if (isset($data["loginattempts"])) {
130
                return $data["loginattempts"];
131
            }
132
        }
133
        return 0;
134
    }
135
    
136
    /**
137
     * (non-PHPdoc)
138
     * @see simplesamlphp-module/authTiqr/lib/User/sspmod_authTiqr_User_Interface::setFailedLoginAttempts()
139
     */
140
    public function setLoginAttempts($userId, $amount)
141
    {
142
        $data = $this->_loadUser($userId);
143
        $data["loginattempts"] = $amount;
144
        $this->_saveUser($userId, $data);
145
    }
146
    
147
    /**
148
     * (non-PHPdoc)
149
     * @see simplesamlphp-module/authTiqr/lib/User/sspmod_authTiqr_User_Interface::isBlocked()
150
     */
151
    public function isBlocked($userId, $duration)
152
    {
153
        if ($data = $this->_loadUser($userId)) {
154
            $timestamp = $this->getTemporaryBlockTimestamp($userId);
155
            // if not blocked or block is expired, return false
156
            if (!isset($data["blocked"]) || $data["blocked"]==false || (false !== $timestamp && false != $duration && (strtotime($timestamp) + $duration * 60) < time())) {
157
                return false;
158
            }
159
        }
160
        return true;
161
    }
162
    
163
    /**
164
     * (non-PHPdoc)
165
     * @see libTiqr/library/tiqr/Tiqr/UserStorage/Tiqr_UserStorage_Interface::setTemporaryBlockAttempts()
166
     */
167
    public function setTemporaryBlockAttempts($userId, $amount) {
168
        $data = $this->_loadUser($userId);
169
        $data["temporaryBlockAttempts"] = $amount;
170
        $this->_saveUser($userId, $data);
171
    }
172
    
173
    /**
174
     * (non-PHPdoc)
175
     * @see libTiqr/library/tiqr/Tiqr/UserStorage/Tiqr_UserStorage_Interface::getTemporaryBlockAttempts()
176
     */
177
    public function getTemporaryBlockAttempts($userId) {
178
        if ($data = $this->_loadUser($userId)) {
179
            if (isset($data["temporaryBlockAttempts"])) {
180
                return $data["temporaryBlockAttempts"];
181
            }
182
        }
183
        return 0;
184
    }
185
    
186
    /**
187
     * (non-PHPdoc)
188
     * @see libTiqr/library/tiqr/Tiqr/UserStorage/Tiqr_UserStorage_Interface::setTemporaryBlockTimestamp()
189
     */
190
    public function setTemporaryBlockTimestamp($userId, $timestamp) {
191
        $data = $this->_loadUser($userId);
192
        $data["temporaryBlockTimestamp"] = $timestamp;
193
        $this->_saveUser($userId, $data);
194
    }
195
    
196
    /**
197
     * (non-PHPdoc)
198
     * @see libTiqr/library/tiqr/Tiqr/UserStorage/Tiqr_UserStorage_Interface::getTemporaryBlockTimestamp()
199
     */
200
    public function getTemporaryBlockTimestamp($userId) {
201
        if ($data = $this->_loadUser($userId)) {
202
            if (isset($data["temporaryBlockTimestamp"])) {
203
                return $data["temporaryBlockTimestamp"];
204
            }
205
        }
206
        return false;
207
    }
208
    
209
    /**
210
     * (non-PHPdoc)
211
     * @see simplesamlphp-module/authTiqr/lib/User/sspmod_authTiqr_User_Interface::block()
212
     */
213
    public function setBlocked($userId, $blocked) 
214
    {
215
        $data = $this->_loadUser($userId);
216
        $data["blocked"] = $blocked;
217
        $this->_saveUser($userId, $data);
218
    }
219
    
220
}
221