1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ArcherZdip\LaravelApiAuth\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
|
|
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
|
|
|
8
|
|
|
|
9
|
|
|
class AppClient extends Model |
10
|
|
|
{ |
11
|
|
|
use SoftDeletes; |
12
|
|
|
|
13
|
|
|
const EVENT_NAME_CREATED = 'created'; |
14
|
|
|
const EVENT_NAME_ACTIVATED = 'activated'; |
15
|
|
|
const EVENT_NAME_DEACTIVATED = 'deactivated'; |
16
|
|
|
const EVENT_NAME_DELETED = 'deleted'; |
17
|
|
|
|
18
|
|
|
/** @var int activate status */ |
19
|
|
|
const ACTIVATE = 1; |
20
|
|
|
const DEACTIVATE = 0; |
21
|
|
|
|
22
|
|
|
protected static $nameRegex = '/^[a-z0-9-]{1,255}$/'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The attributes that are mass assignable. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $fillable = [ |
30
|
|
|
'name', |
31
|
|
|
'appid', |
32
|
|
|
'secret', |
33
|
|
|
'active' |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The attributes that should be hidden for arrays. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $hidden = []; |
42
|
|
|
|
43
|
|
|
public static function boot() |
44
|
|
|
{ |
45
|
|
|
parent::boot(); |
46
|
|
|
|
47
|
|
|
static::created(function(AppClient $appClient) { |
48
|
|
|
self::logApiAuthOprateEvent($appClient, self::EVENT_NAME_CREATED); |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
static::updated(function(AppClient $appClient) { |
52
|
|
|
|
53
|
|
|
$changed = $appClient->getDirty(); |
54
|
|
|
|
55
|
|
|
if (isset($changed) && $changed['active'] === self::ACTIVATE) { |
56
|
|
|
self::logApiAuthOprateEvent($appClient, self::EVENT_NAME_ACTIVATED); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (isset($changed) && $changed['active'] === self::DEACTIVATE) { |
60
|
|
|
self::logApiAuthOprateEvent($appClient, self::EVENT_NAME_DEACTIVATED); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
static::deleted(function(AppClient $appClient) { |
66
|
|
|
self::logApiAuthOprateEvent($appClient, self::EVENT_NAME_DELETED); |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* AppClient constructor. |
73
|
|
|
* @param array $attributes |
74
|
|
|
*/ |
75
|
|
|
public function __construct(array $attributes = []) |
76
|
|
|
{ |
77
|
|
|
parent::__construct($attributes); |
78
|
|
|
|
79
|
|
|
$this->setTable(config('apikey.table_name.app_clients')); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get secret by appid |
84
|
|
|
* |
85
|
|
|
* @param $appid |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
public static function getSecretByAppId($appid) |
89
|
|
|
{ |
90
|
|
|
return self::where([ |
91
|
|
|
'active' => self::ACTIVATE, |
92
|
|
|
'appid' => $appid |
93
|
|
|
])->first(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Generate AppId |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public static function generateAppId() |
102
|
|
|
{ |
103
|
|
|
do { |
104
|
|
|
$appId = Str::random(config('apikey.appid_length', 16)); |
|
|
|
|
105
|
|
|
} while (self::appIdExists($appId)); |
106
|
|
|
|
107
|
|
|
return $appId; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Generate secret |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public static function generateSecret() |
116
|
|
|
{ |
117
|
|
|
return Str::random(config('apikey.secret_length', 64)); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Check name is valid format |
122
|
|
|
* |
123
|
|
|
* @param $name |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public static function isValidName($name) |
127
|
|
|
{ |
128
|
|
|
return (bool)preg_match(self::$nameRegex, $name); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Check name is or not exists |
133
|
|
|
* |
134
|
|
|
* @param $name |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public static function nameExists($name) |
138
|
|
|
{ |
139
|
|
|
return self::where('name', '=', $name)->first() instanceof self; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Check AppId is or not exists |
144
|
|
|
* |
145
|
|
|
* @param $secret |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public static function appIdExists($appId) |
149
|
|
|
{ |
150
|
|
|
return self::where('appid', $appId)->withTrashed()->first() instanceof self; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Log an app oprate admin event |
155
|
|
|
* |
156
|
|
|
* @param AppClient $appClient |
157
|
|
|
* @param string $eventName |
158
|
|
|
*/ |
159
|
|
|
protected static function logApiAuthOprateEvent(AppClient $appClient, $eventName) |
160
|
|
|
{ |
161
|
|
|
$event = new ApiAuthOprateEvent(); |
162
|
|
|
$event->app_client_id = $appClient->id; |
163
|
|
|
$event->ip_address = request()->ip(); |
|
|
|
|
164
|
|
|
$event->event = $eventName; |
165
|
|
|
$event->save(); |
166
|
|
|
} |
167
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths