1+ <?php namespace Illuminate \Auth ;
2+
3+ use Illuminate \Database \Connection ;
4+ use Illuminate \Hashing \HasherInterface ;
5+
6+ class DatabaseUserProvider implements UserProviderInterface {
7+
8+ /**
9+ * The active database connection.
10+ *
11+ * @param Illuminate\Database\Connection
12+ */
13+ protected $ conn ;
14+
15+ /**
16+ * The hasher implementation.
17+ *
18+ * @var Illuminate\Hashing\HasherInterface
19+ */
20+ protected $ hasher ;
21+
22+ /**
23+ * The table containing the users.
24+ *
25+ * @var string
26+ */
27+ protected $ table ;
28+
29+ /**
30+ * Create a new database user provider.
31+ *
32+ * @param Illuminate\Database\Connection $conn
33+ * @param Illuminate\Hashing\HasherInterface $hasher
34+ * @param string $table
35+ * @return void
36+ */
37+ public function __construct (Connection $ conn , HasherInterface $ hasher , $ table )
38+ {
39+ $ this ->conn = $ conn ;
40+ $ this ->table = $ table ;
41+ $ this ->hasher = $ hasher ;
42+ }
43+
44+ /**
45+ * Retrieve a user by their unique idenetifier.
46+ *
47+ * @param mixed $identifier
48+ * @return Illuminate\Auth\UserInterface|null
49+ */
50+ public function retrieveByID ($ identifier )
51+ {
52+ $ user = $ this ->conn ->table ($ this ->table )->find ($ identifier );
53+
54+ if ( ! is_null ($ user ))
55+ {
56+ return new GenericUser ((array ) $ user );
57+ }
58+ }
59+
60+ /**
61+ * Retrieve a user by the given credentials.
62+ *
63+ * @param array $credentials
64+ * @return Illuminate\Auth\UserInterface|null
65+ */
66+ public function retrieveByCredentials (array $ credentials )
67+ {
68+ // First we will add each credential element to the query as a where clause.
69+ // Then we can execute the query and, if we found a user, return it in a
70+ // generic "user" object that will be utilized by the Guard instances.
71+ $ query = $ this ->conn ->table ($ this ->table );
72+
73+ foreach ($ credentials as $ key => $ value )
74+ {
75+ if ( ! str_contains ($ key , 'password ' ))
76+ {
77+ $ query ->where ($ key , $ value );
78+ }
79+ }
80+
81+ // Now we are ready to execute the query to see if we have an user matching
82+ // the given credentials. If not, we will just return nulls and indicate
83+ // that there are no matching users for these given credential arrays.
84+ $ user = $ query ->first ();
85+
86+ if ( ! is_null ($ user ))
87+ {
88+ return new GenericUser ((array ) $ user );
89+ }
90+ }
91+
92+ /**
93+ * Validate a user against the given credentials.
94+ *
95+ * @param Illuminate\Auth\UserInterface $user
96+ * @param array $credentials
97+ * @return bool
98+ */
99+ public function validateCredentials (UserInterface $ user , array $ credentials )
100+ {
101+ $ plain = $ credentials ['password ' ];
102+
103+ return $ this ->hasher ->check ($ plain , $ user ->getAuthPassword ());
104+ }
105+
106+ }
0 commit comments