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/AppAccess.php
2019-04-24 18:46:41 +00:00

72 lines
1.8 KiB
PHP

<?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 AppAccess extends Model
{
protected $table = 'app_access';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'app_id', 'user_id', 'status_id'
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'',
];
static function createCode(User $user, App $app) {
$characters = '0123456789';
$randstring = '';
for ($i = 0; $i < 20; $i++) {
$randstring .= $characters[rand(0, strlen($characters)-1)];
}
$code = $randstring;
$appCode = new AppCode();
$appCode->user_id = $user->id;
$appCode->app_id = $app->id;
$appCode->code = $code;
$appCode->saveOrFail();
return $appCode;
}
public function getUser() {
return User::query()->where("id", "=", $this->user_id)->first();
}
public function getApp() {
return App::query()->where("id", "=", $this->app_id)->first();
}
static public function getOrCreate($userId, $appId) {
$access = AppAccess::query()->where("user_id", "=", $userId)->where("app_id", "=", $appId)->first();
if($access == null) {
$access = new AppAccess();
$access->user_id = $userId;
$access->app_id = $appId;
$access->status = "created";
$access->saveOrFail();
}
return $access;
}
}