|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Query\QueryException; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Keygen\Keygen; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Token. |
|
11
|
|
|
* |
|
12
|
|
|
* @property int $id |
|
13
|
|
|
* @property string $token |
|
14
|
|
|
* @property int $credits |
|
15
|
|
|
* @property bool $active |
|
16
|
|
|
* @property int $acl_level |
|
17
|
|
|
* @property \Carbon\Carbon|null $created_at |
|
18
|
|
|
* @property \Carbon\Carbon|null $updated_at |
|
19
|
|
|
* |
|
20
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Token whereAclLevel($value) |
|
21
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Token whereActive($value) |
|
22
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Token whereCreatedAt($value) |
|
23
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Token whereCredits($value) |
|
24
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Token whereId($value) |
|
25
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Token whereToken($value) |
|
26
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Token whereUpdatedAt($value) |
|
27
|
|
|
* @mixin \Eloquent |
|
28
|
|
|
* |
|
29
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Domain[] $domains |
|
30
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Scan[] $scan |
|
31
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Scan[] $scans |
|
32
|
|
|
*/ |
|
33
|
|
|
class Token extends Model |
|
34
|
|
|
{ |
|
35
|
|
|
protected $fillable = ['credits', 'token']; |
|
36
|
|
|
|
|
37
|
|
|
protected $table = 'tokens'; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(array $attributes = []) |
|
40
|
|
|
{ |
|
41
|
|
|
parent::__construct($attributes); |
|
42
|
|
|
// Generate token by package gladcodes/keygen |
|
43
|
|
|
$this->token = Keygen::token(24)->generate(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param int $credits |
|
48
|
|
|
* |
|
49
|
|
|
* @return bool |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setTokenCredits(int $credits) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->credits = $credits; |
|
54
|
|
|
|
|
55
|
|
|
try { |
|
56
|
|
|
$this->save(); |
|
57
|
|
|
|
|
58
|
|
|
return true; |
|
59
|
|
|
} catch (QueryException $queryException) { |
|
60
|
|
|
//TODO Log error to Papertrail with Token |
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function setAclLevel(int $aclLevel) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->acl_level = $aclLevel; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function reduceCredits($amount = 1) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->credits -= $amount; |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
|
|
$this->save(); |
|
76
|
|
|
|
|
77
|
|
|
return true; |
|
78
|
|
|
} catch (\Illuminate\Database\QueryException $queryException) { |
|
79
|
|
|
// TODO: Log error to Papertrail with Token |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
86
|
|
|
*/ |
|
87
|
|
|
public function scans() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->hasMany(Scan::class); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function domains() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->hasMany(Domain::class); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public static function reduceToken(string $token, $amount = 1) |
|
98
|
|
|
{ |
|
99
|
|
|
$token = self::getTokenByString($token); |
|
100
|
|
|
if ($token instanceof self) { |
|
101
|
|
|
$token->credits -= $amount; |
|
102
|
|
|
|
|
103
|
|
|
try { |
|
104
|
|
|
$token->save(); |
|
105
|
|
|
|
|
106
|
|
|
return true; |
|
107
|
|
|
} catch (\Illuminate\Database\QueryException $queryException) { |
|
108
|
|
|
//TODO Log error to Papertrail with Token |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public static function getTokenByString(string $token) |
|
115
|
|
|
{ |
|
116
|
|
|
return self::where('token', $token)->first(); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|