Permalink
Browse files

Add an ApiKey model, extend User model

  • Loading branch information...
insom committed Mar 22, 2016
1 parent 62bba0f commit dd8de115625caa01cad2b53a606f3100eb5a6412
Showing with 63 additions and 0 deletions.
  1. +53 −0 app/Models/ApiKey.php
  2. +10 −0 app/Models/User.php
@@ -0,0 +1,53 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Models;
use AltThree\Validator\ValidatingTrait;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class ApiKey extends Model
{
use ValidatingTrait;
/**
* The attributes that should be casted to native types.
*
* @var string[]
*/
protected $casts = [
'active' => 'bool',
'api_key' => 'string',
];
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'user_id' => 'int',
'api_key' => 'required',
'active' => 'required|bool',
];
/**
* An API key belongs to a user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
}
@@ -140,6 +140,16 @@ public static function findByApiToken($token, $columns = ['*'])
return $user;
}
/**
* Lookup all of the API keys related to this user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function apiKeys()
{
return $this->hasMany(ApiKey::class, 'user_id', 'id');
}
/**
* Returns an API key.
*

0 comments on commit dd8de11

Please sign in to comment.