2019-04-24 18:46:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Auth\Authenticatable;
|
|
|
|
use Laravel\Lumen\Auth\Authorizable;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
|
|
|
|
|
|
|
class AccessToken extends Model
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
'status', 'expires_at', 'access_id', 'token'
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes excluded from the model's JSON form.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = [
|
|
|
|
'',
|
|
|
|
];
|
|
|
|
|
|
|
|
static function createToken(AppAccess $access) {
|
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
$randstring = '';
|
|
|
|
for ($i = 0; $i < 20; $i++) {
|
|
|
|
$randstring = $characters[rand(0, strlen($characters)-1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = hash("sha512", $access->id.time().$randstring);
|
|
|
|
|
|
|
|
$accessToken = new AccessToken();
|
|
|
|
$accessToken->expires_at = date("Y-m-d H:i:s", time()+3600);
|
|
|
|
$accessToken->access_id = $access->id;
|
|
|
|
$accessToken->token = $token;
|
|
|
|
|
|
|
|
$accessToken->saveOrFail();
|
|
|
|
|
|
|
|
return $accessToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAppAccess(): AppAccess {
|
|
|
|
return AppAccess::query()->where("id", "=", $this->access_id)->firstOrFail();
|
|
|
|
}
|
|
|
|
public function getUser(): User {
|
|
|
|
return User::query()->where("id", "=", $this->getAppAccess()->user_id)->firstOrFail();
|
|
|
|
}
|
|
|
|
|
2019-06-20 14:46:50 +00:00
|
|
|
public function getApp(): App {
|
|
|
|
return $this->getAppAccess()->getApp();
|
|
|
|
}
|
|
|
|
|
2019-04-24 18:46:41 +00:00
|
|
|
}
|