Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
Table
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
9 / 9
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getTable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUsernameField
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPasswordField
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTable
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setUsernameField
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setPasswordField
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 authenticate
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Auth;
15
16/**
17 * Table auth class
18 *
19 * @category   Pop
20 * @package    Pop\Auth
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    3.3.0
25 */
26class Table extends AbstractAuth
27{
28
29    /**
30     * DB table name/class name
31     * @var string
32     */
33    protected $table = null;
34
35    /**
36     * Username field
37     * @var string
38     */
39    protected $usernameField = 'username';
40
41    /**
42     * Password field
43     * @var string
44     */
45    protected $passwordField = 'password';
46
47    /**
48     * User record
49     * @var mixed
50     */
51    protected $user = null;
52
53    /**
54     * Constructor
55     *
56     * Instantiate the File auth adapter object
57     *
58     * @param  string $table
59     * @param  string $usernameField
60     * @param  string $passwordField
61     */
62    public function __construct($table, $usernameField = 'username', $passwordField = 'password')
63    {
64        $this->table         = $table;
65        $this->usernameField = $usernameField;
66        $this->passwordField = $passwordField;
67    }
68
69    /**
70     * Get the table name
71     *
72     * @return string
73     */
74    public function getTable()
75    {
76        return $this->table;
77    }
78
79    /**
80     * Get the username field
81     *
82     * @return string
83     */
84    public function getUsernameField()
85    {
86        return $this->usernameField;
87    }
88
89    /**
90     * Get the password field
91     *
92     * @return string
93     */
94    public function getPasswordField()
95    {
96        return $this->passwordField;
97    }
98
99    /**
100     * Set the table name
101     *
102     * @param  string $table
103     * @return Table
104     */
105    public function setTable($table)
106    {
107        $this->table = $table;
108        return $this;
109    }
110
111    /**
112     * Set the username field
113     *
114     * @param  string $usernameField
115     * @return Table
116     */
117    public function setUsernameField($usernameField)
118    {
119        $this->usernameField = $usernameField;
120        return $this;
121    }
122
123    /**
124     * Set the password field
125     *
126     * @param  string $passwordField
127     * @return Table
128     */
129    public function setPasswordField($passwordField)
130    {
131        $this->passwordField = $passwordField;
132        return $this;
133    }
134
135    /**
136     * Get the user record
137     *
138     * @return mixed
139     */
140    public function getUser()
141    {
142        return $this->user;
143    }
144
145    /**
146     * Method to authenticate
147     *
148     * @param  string $username
149     * @param  string $password
150     * @return int
151     */
152    public function authenticate($username, $password)
153    {
154        $this->setUsername($username);
155        $this->setPassword($password);
156
157        $table        = $this->table;
158        $this->result = 0;
159        $this->user   = $table::findOne([
160            $this->usernameField => $this->username
161        ]);
162
163        if ((null !== $this->password) && isset($this->user->{$this->passwordField}) &&
164            (null !== $this->user->{$this->passwordField})) {
165            $this->result = (int)$this->verify($this->password, $this->user->{$this->passwordField});
166        }
167
168        return $this->result;
169    }
170
171}