Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Table
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
4 / 4
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getTable
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
 authenticate
100.00% covered (success)
100.00%
11 / 11
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-2024 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-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    4.0.0
25 */
26class Table extends AbstractAuth
27{
28
29    use AdapterUserTrait;
30
31    /**
32     * DB table name/class name
33     * @var ?string
34     */
35    protected ?string $table = null;
36
37    /**
38     * Constructor
39     *
40     * Instantiate the File auth adapter object
41     *
42     * @param  string $table
43     * @param  string $usernameField
44     * @param  string $passwordField
45     */
46    public function __construct(string $table, string $usernameField = 'username', string $passwordField = 'password')
47    {
48        $this->table         = $table;
49        if (!empty($usernameField)) {
50            $this->setUsernameField($usernameField);
51        }
52        if (!empty($passwordField)) {
53            $this->setPasswordField($passwordField);
54        }
55    }
56
57    /**
58     * Get the table name
59     *
60     * @return string
61     */
62    public function getTable(): string
63    {
64        return $this->table;
65    }
66
67    /**
68     * Set the table name
69     *
70     * @param  string $table
71     * @return Table
72     */
73    public function setTable(string $table): Table
74    {
75        $this->table = $table;
76        return $this;
77    }
78
79    /**
80     * Method to authenticate
81     *
82     * @param  string $username
83     * @param  string $password
84     * @return int
85     */
86    public function authenticate(string $username, string $password): int
87    {
88        $this->setUsername($username);
89        $this->setPassword($password);
90
91        $table        = $this->table;
92        $this->result = 0;
93        $this->user   = $table::findOne([
94            $this->usernameField => $this->username
95        ]);
96
97        if (($this->password !== null) && isset($this->user->{$this->passwordField}) &&
98            ($this->user->{$this->passwordField} !== null)) {
99            $this->result = (int)$this->verify($this->password, $this->user->{$this->passwordField});
100        }
101
102        return $this->result;
103    }
104
105}