Cachet/src/Models/User.php

147 lines
3.3 KiB
PHP
Raw Normal View History

2014-11-16 22:26:08 +00:00
<?php
namespace CachetHQ\Cachet\Models;
use Illuminate\Auth\Reminders\RemindableInterface;
2014-12-20 21:20:17 +00:00
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\UserTrait;
2015-01-01 12:23:17 +00:00
use Illuminate\Database\Eloquent\Model;
2015-01-03 17:51:35 +00:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
2015-01-01 12:23:17 +00:00
use Illuminate\Support\Facades\Hash;
use Watson\Validating\ValidatingTrait;
2014-11-16 22:26:08 +00:00
2015-01-02 08:46:08 +00:00
/**
2015-01-02 12:16:28 +00:00
* @property int $id
* @property string $username
* @property string $password
* @property string $remember_token
* @property string $email
* @property int $active
* @property int $level
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
2015-01-02 08:46:08 +00:00
*/
2015-01-01 12:23:17 +00:00
class User extends Model implements UserInterface, RemindableInterface
2014-12-20 21:20:17 +00:00
{
use RemindableTrait, UserTrait, ValidatingTrait;
/**
* The validation rules.
*
* @var string[]
*/
protected $rules = [
'username' => 'required|alpha|unique:users',
'email' => 'required|email|unique:users',
'password' => 'required',
];
2014-11-16 22:26:08 +00:00
/**
2015-01-01 18:57:33 +00:00
* The hidden properties.
*
* These are excluded when we are serializing the model.
2014-12-30 18:19:22 +00:00
*
2015-01-04 13:39:34 +00:00
* @var string[]
*/
protected $hidden = ['password', 'remember_token', 'google_2fa_secret'];
/**
2015-01-01 18:57:33 +00:00
* The properties that cannot be mass assigned.
2014-12-30 18:19:22 +00:00
*
* @var string[]
*/
2014-12-04 23:40:28 +00:00
protected $guarded = [];
2015-01-03 17:51:35 +00:00
/**
* Overrides the models boot method.
*
* @return void
*/
public static function boot()
{
parent::boot();
self::creating(function ($user) {
$user->api_key = self::generateApiKey();
});
}
2014-12-01 16:46:56 +00:00
/**
2014-12-30 18:19:22 +00:00
* Hash any password being inserted by default.
2014-12-01 16:46:56 +00:00
*
2015-01-01 12:23:17 +00:00
* @param string $password
2014-12-30 18:19:22 +00:00
*
2015-01-01 18:57:33 +00:00
* @return $this
2014-12-01 16:46:56 +00:00
*/
2014-12-20 21:20:17 +00:00
public function setPasswordAttribute($password)
{
2014-12-01 16:46:56 +00:00
$this->attributes['password'] = Hash::make($password);
2015-01-01 18:57:33 +00:00
return $this;
2014-12-01 16:46:56 +00:00
}
2014-12-20 20:40:48 +00:00
/**
* Returns a Gravatar URL for the users email address.
2014-12-30 18:19:22 +00:00
*
* @param int $size
*
2014-12-20 20:40:48 +00:00
* @return string
*/
2014-12-20 21:20:17 +00:00
public function getGravatarAttribute($size = 200)
{
2015-01-01 12:23:17 +00:00
return sprintf('https://www.gravatar.com/avatar/%s?size=%d', md5($this->email), $size);
2014-12-04 15:36:19 +00:00
}
2015-01-03 17:51:35 +00:00
/**
* 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);
}
/**
* Returns whether a user is at admin level.
*
* @return bool
*/
public function getIsAdminAttribute()
{
return (bool) $this->level;
}
/**
* Returns if a user has enabled two factor authentication.
*
* @return bool
*/
public function getHasEnabled2FAAttribute()
{
return (bool) (trim($this->google_2fa_secret) !== '');
}
}