1
|
|
|
<?php namespace Anomaly\UsersModule\User; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Entry\EntryRepository; |
4
|
|
|
use Anomaly\UsersModule\User\Contract\UserInterface; |
5
|
|
|
use Anomaly\UsersModule\User\Contract\UserRepositoryInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class UserRepository |
9
|
|
|
* |
10
|
|
|
* @link http://pyrocms.com/ |
11
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
12
|
|
|
* @author Ryan Thompson <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class UserRepository extends EntryRepository implements UserRepositoryInterface |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The user model. |
19
|
|
|
* |
20
|
|
|
* @var UserModel |
21
|
|
|
*/ |
22
|
|
|
protected $model; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new UserRepository instance. |
26
|
|
|
* |
27
|
|
|
* @param UserModel $model |
28
|
|
|
*/ |
29
|
|
|
function __construct(UserModel $model) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$this->model = $model; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Find a user by their credentials. |
36
|
|
|
* |
37
|
|
|
* @param array $credentials |
38
|
|
|
* @return null|UserInterface |
39
|
|
|
*/ |
40
|
|
|
public function findByCredentials(array $credentials) |
41
|
|
|
{ |
42
|
|
|
if (isset($credentials['email'])) { |
43
|
|
|
$user = $this->findByEmail($credentials['email']); |
44
|
|
|
} elseif (isset($credentials['username'])) { |
45
|
|
|
$user = $this->findByUsername($credentials['username']); |
46
|
|
|
} else { |
47
|
|
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($user && app('hash')->check($credentials['password'], $user->password)) { |
|
|
|
|
51
|
|
|
return $user; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Find a user by their email. |
59
|
|
|
* |
60
|
|
|
* @param $email |
61
|
|
|
* @return null|UserInterface |
62
|
|
|
*/ |
63
|
|
|
public function findByEmail($email) |
64
|
|
|
{ |
65
|
|
|
return $this->model->where('email', $email)->first(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Find a user by their username. |
70
|
|
|
* |
71
|
|
|
* @param $username |
72
|
|
|
* @return null|UserInterface |
73
|
|
|
*/ |
74
|
|
|
public function findByUsername($username) |
75
|
|
|
{ |
76
|
|
|
return $this->model->where('username', $username)->first(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Find a user by their reset code. |
81
|
|
|
* |
82
|
|
|
* @param $code |
83
|
|
|
* @return null|UserInterface |
84
|
|
|
*/ |
85
|
|
|
public function findByResetCode($code) |
86
|
|
|
{ |
87
|
|
|
return $this->model->where('reset_code', $code)->first(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Find a user by their activation code. |
92
|
|
|
* |
93
|
|
|
* @param $code |
94
|
|
|
* @return null|UserInterface |
95
|
|
|
*/ |
96
|
|
|
public function findByActivationCode($code) |
97
|
|
|
{ |
98
|
|
|
return $this->model->where('activation_code', $code)->first(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Touch a user's last activity and IP. |
103
|
|
|
* |
104
|
|
|
* @param UserInterface $user |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function touchLastActivity(UserInterface $user) |
108
|
|
|
{ |
109
|
|
|
$user->last_activity_at = time(); |
|
|
|
|
110
|
|
|
$user->ip_address = $_SERVER['REMOTE_ADDR']; |
|
|
|
|
111
|
|
|
|
112
|
|
|
//return $this->save($user); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Touch a user's last login. |
117
|
|
|
* |
118
|
|
|
* @param UserInterface $user |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function touchLastLogin(UserInterface $user) |
122
|
|
|
{ |
123
|
|
|
$user->last_login_at = time(); |
|
|
|
|
124
|
|
|
|
125
|
|
|
return $this->save($user); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.