This repository has been archived on 2024-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
keksAccount/app/Models/AccessToken.php

62 lines
1.6 KiB
PHP
Raw Normal View History

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
}