-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathUserMutator.php
148 lines (129 loc) · 3.78 KB
/
UserMutator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace App\Mutators;
use Keygen;
trait UserMutator
{
public function setEmailAttribute($email)
{
// Ensure valid email
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \Exception("Invalid email address.");
}
// Ensure email does not exist
elseif (static::whereEmail($email)->count() > 0) {
throw new \Exception("Email already exists.");
}
$this->attributes['email'] = $email;
}
public function setUsernameAttribute($username)
{
if(static::whereUsername($username)->count() > 0){
throw new \Exception("Username already exists.");
}
// Make Username Lowercase plus Use Underscore on spaces
$this->attributes['username'] = strtolower(str_replace(' ', '_', $username));
}
public function setIdAttribute($id)
{
if(static::whereId($id)->count() > 0){
throw new \Exception("Id already exists.");
}
$this->attributes['id'] = $id;
}
public function setCodeAttribute($code)
{
if(static::whereCode($code)->count() > 0){
throw new \Exception("Code already exists.");
}
$this->attributes['code'] = $code;
}
public function setPasswordAttribute($password)
{
$this->attributes['password'] = bcrypt($password);
}
private static function generateID()
{
return Keygen::numeric(14)->prefix(mt_rand(1, 9))->generate(true);
}
public static function generateUniqueID()
{
$id = self::generateID();
// Ensure ID does not exist
// Generate new one if ID already exists
while (self::whereId($id)->count() > 0) {
$id = self::generateID();
}
return $id;
}
public function getFirstNameAttribute($firstname)
{
return ucwords($firstname);
}
public function getLastNameAttribute($lastname)
{
return ucwords($lastname);
}
public static function generateUniqueCode()
{
$code = self::generateCode();
while (self::whereCode($code)->count() > 0) {
$code = self::generateCode();
}
return $code;
}
private static function generateCode()
{
return Keygen::bytes()->generate(
function($key) {
// Generate a random numeric key
$random = Keygen::numeric()->generate();
// Manipulate the random bytes with the numeric key
return substr(md5($key . $random . strrev($key)), mt_rand(0,8), 20);
},
function($key) {
// Add a (-) after every fourth character in the key
return join('-', str_split($key, 4));
},
'strtoupper'
);
}
public static function findByUsername($username)
{
return self::whereUsername($username)->firstOrFail();
}
public static function findByEmail($email)
{
return self::whereEmail($email)->firstOrFail();
}
public static function findByCode($code)
{
return self::whereCode($code)->firstOrFail();
}
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [
'user' => [
'id' => $this->id,
]
];
}
public function scopeWhereCan($query, $ability)
{
$query->where(function ($query) use ($ability) {
// direct
$query->whereHas('abilities', function ($query) use ($ability) {
$query->byName($ability);
});
// through roles
$query->orWhereHas('roles', function ($query) use ($ability) {
$query->whereHas('abilities', function ($query) use ($ability) {
$query->byName($ability);
});
});
});
}
}