2014-11-16 22:26:08 +00:00
|
|
|
<?php
|
|
|
|
|
2015-04-19 08:52:39 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
2015-07-06 17:37:01 +01:00
|
|
|
* (c) Alt Three Services Limited
|
2015-04-19 08:52:39 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2015-01-02 00:18:19 +00:00
|
|
|
namespace CachetHQ\Cachet\Models;
|
|
|
|
|
2015-08-03 22:32:36 +01:00
|
|
|
use AltThree\Validator\ValidatingTrait;
|
2015-03-20 18:30:45 -06:00
|
|
|
use Illuminate\Auth\Authenticatable;
|
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword;
|
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
|
2016-08-03 17:44:21 +01:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2015-01-01 12:23:17 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
2014-11-16 22:26:08 +00:00
|
|
|
|
2016-08-23 13:09:47 +01:00
|
|
|
/**
|
|
|
|
* This is the user model.
|
|
|
|
*
|
|
|
|
* @author James Brooks <james@alt-three.com>
|
|
|
|
*/
|
2015-03-20 18:30:45 -06:00
|
|
|
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
|
2014-12-20 21:20:17 +00:00
|
|
|
{
|
2016-10-19 07:53:53 +00:00
|
|
|
use Notifiable;
|
2015-03-20 18:30:45 -06:00
|
|
|
use Authenticatable, CanResetPassword, ValidatingTrait;
|
2015-01-04 19:06:08 +00:00
|
|
|
|
2015-12-25 17:52:01 +08:00
|
|
|
/**
|
|
|
|
* The admin level of user.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const LEVEL_ADMIN = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The general level of user.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const LEVEL_USER = 2;
|
|
|
|
|
2016-08-23 13:09:47 +01:00
|
|
|
/**
|
|
|
|
* The model's attributes.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $attributes = [
|
|
|
|
'welcomed' => false,
|
|
|
|
];
|
|
|
|
|
2015-01-04 19:06:08 +00:00
|
|
|
/**
|
2015-08-22 15:57:53 +01:00
|
|
|
* The attributes that should be casted to native types.
|
2015-01-04 19:06:08 +00:00
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2015-08-22 15:57:53 +01:00
|
|
|
protected $casts = [
|
|
|
|
'username' => 'string',
|
|
|
|
'email' => 'string',
|
|
|
|
'google_2fa_secret' => 'string',
|
|
|
|
'api_key' => 'string',
|
|
|
|
'active' => 'bool',
|
|
|
|
'level' => 'int',
|
2016-08-23 13:09:47 +01:00
|
|
|
'welcomed' => 'bool',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The fillable properties.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
'username',
|
|
|
|
'password',
|
|
|
|
'google_2fa_secret',
|
|
|
|
'email',
|
|
|
|
'api_key',
|
|
|
|
'active',
|
|
|
|
'level',
|
|
|
|
'welcomed',
|
2015-01-04 19:06:08 +00:00
|
|
|
];
|
2014-11-16 22:26:08 +00:00
|
|
|
|
2015-08-22 15:57:53 +01:00
|
|
|
/**
|
|
|
|
* The properties that cannot be mass assigned.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $guarded = [];
|
|
|
|
|
2014-11-27 16:05:00 +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[]
|
2014-11-27 16:05:00 +00:00
|
|
|
*/
|
2015-01-09 09:03:07 +00:00
|
|
|
protected $hidden = ['password', 'remember_token', 'google_2fa_secret'];
|
2014-11-27 16:05:00 +00:00
|
|
|
|
2014-12-04 22:33:15 +00:00
|
|
|
/**
|
2015-08-22 15:57:53 +01:00
|
|
|
* The validation rules.
|
2014-12-30 18:19:22 +00:00
|
|
|
*
|
2015-01-04 19:06:08 +00:00
|
|
|
* @var string[]
|
2014-12-04 22:33:15 +00:00
|
|
|
*/
|
2015-08-22 15:57:53 +01:00
|
|
|
public $rules = [
|
2016-10-01 11:55:43 +01:00
|
|
|
'username' => ['required', 'regex:/\A(?!.*[:;]-\))[ -~]+\z/'],
|
|
|
|
'email' => 'required|email',
|
2015-08-22 15:57:53 +01:00
|
|
|
'password' => 'required',
|
|
|
|
];
|
2014-12-04 22:33:15 +00:00
|
|
|
|
2015-01-03 17:51:35 +00:00
|
|
|
/**
|
|
|
|
* Overrides the models boot method.
|
|
|
|
*/
|
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
self::creating(function ($user) {
|
2015-04-19 08:52:39 +01:00
|
|
|
if (!$user->api_key) {
|
2015-03-16 08:47:25 +00:00
|
|
|
$user->api_key = self::generateApiKey();
|
|
|
|
}
|
2015-01-03 17:51:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-03 17:44:21 +01:00
|
|
|
/**
|
|
|
|
* Scope all admin users.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeAdmins(Builder $query)
|
|
|
|
{
|
|
|
|
return $query->where('level', self::LEVEL_ADMIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scope all active users.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeActive(Builder $query)
|
|
|
|
{
|
|
|
|
return $query->where('active', true);
|
|
|
|
}
|
|
|
|
|
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-16 16:10:44 +00:00
|
|
|
* @return \CachetHQ\Cachet\Models\User
|
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)
|
|
|
|
{
|
2016-10-02 01:07:16 +05:30
|
|
|
return sprintf('https://www.gravatar.com/avatar/%s?size=%d', md5(strtolower($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.
|
|
|
|
*
|
2015-01-12 13:34:31 +00:00
|
|
|
* @param string $token
|
2015-01-03 17:51:35 +00:00
|
|
|
* @param string[] $columns
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
|
|
|
*
|
|
|
|
* @return \CachetHQ\Cachet\Models\User
|
|
|
|
*/
|
2015-01-12 13:34:31 +00:00
|
|
|
public static function findByApiToken($token, $columns = ['*'])
|
2015-01-03 17:51:35 +00:00
|
|
|
{
|
2016-10-12 23:31:45 +05:30
|
|
|
$user = static::where('api_key', $token)->firstOrFail($columns);
|
2015-01-03 17:51:35 +00:00
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an API key.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function generateApiKey()
|
|
|
|
{
|
|
|
|
return str_random(20);
|
|
|
|
}
|
2015-01-04 19:06:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether a user is at admin level.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getIsAdminAttribute()
|
|
|
|
{
|
2015-12-25 17:52:01 +08:00
|
|
|
return $this->level == self::LEVEL_ADMIN;
|
2015-01-04 19:06:08 +00:00
|
|
|
}
|
2015-01-09 09:03:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if a user has enabled two factor authentication.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-01-09 14:21:53 -06:00
|
|
|
public function getHasTwoFactorAttribute()
|
2015-01-09 09:03:07 +00:00
|
|
|
{
|
2015-01-09 14:21:53 -06:00
|
|
|
return trim($this->google_2fa_secret) !== '';
|
2015-01-09 09:03:07 +00:00
|
|
|
}
|
2014-11-27 22:08:28 +00:00
|
|
|
}
|