User-based API key. Closes #256.

This commit is contained in:
James Brooks
2015-01-03 17:51:35 +00:00
parent 1d30133851
commit 3d4c38c7ee
9 changed files with 187 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\UserTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Hash;
/**
@@ -47,6 +48,20 @@ class User extends Model implements UserInterface, RemindableInterface
*/
protected $guarded = [];
/**
* Overrides the models boot method.
*
* @return void
*/
public static function boot()
{
parent::boot();
self::creating(function ($user) {
$user->api_key = self::generateApiKey();
});
}
/**
* Hash any password being inserted by default.
*
@@ -72,4 +87,35 @@ class User extends Model implements UserInterface, RemindableInterface
{
return sprintf('https://www.gravatar.com/avatar/%s?size=%d', md5($this->email), $size);
}
/**
* Find by api_key, or throw an exception.
*
* @param string $api_key
* @param string[] $columns
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*
* @return \CachetHQ\Cachet\Models\User
*/
public static function findByApiKey($api_key, $columns = ['*'])
{
$user = static::where('api_key', $api_key)->first($columns);
if (!$user) {
throw new ModelNotFoundException();
}
return $user;
}
/**
* Returns an API key.
*
* @return string
*/
public static function generateApiKey()
{
return str_random(20);
}
}