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-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;
|
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;
|
2015-01-04 19:06:08 +00:00
|
|
|
use Watson\Validating\ValidatingTrait;
|
2014-11-16 22:26:08 +00:00
|
|
|
|
2015-03-20 18:30:45 -06:00
|
|
|
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
|
2014-12-20 21:20:17 +00:00
|
|
|
{
|
2015-03-20 18:30:45 -06:00
|
|
|
use Authenticatable, CanResetPassword, ValidatingTrait;
|
2015-01-04 19:06:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The validation rules.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $rules = [
|
2015-05-20 16:16:25 +01:00
|
|
|
'username' => ['required', 'regex:/\A(?!.*[:;]-\))[ -~]+\z/', 'unique:users'],
|
2015-01-04 19:06:08 +00:00
|
|
|
'email' => 'required|email|unique:users',
|
|
|
|
'password' => 'required',
|
|
|
|
];
|
2014-11-16 22:26:08 +00:00
|
|
|
|
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-01-01 18:57:33 +00:00
|
|
|
* The properties that cannot be mass assigned.
|
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
|
|
|
*/
|
2014-12-04 23:40:28 +00:00
|
|
|
protected $guarded = [];
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
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.
|
|
|
|
*
|
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
|
|
|
{
|
2015-01-12 13:34:31 +00:00
|
|
|
$user = static::where('api_key', $token)->first($columns);
|
2015-01-03 17:51:35 +00:00
|
|
|
|
|
|
|
if (!$user) {
|
|
|
|
throw new ModelNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-23 17:06:46 -06:00
|
|
|
return $this->level == 1;
|
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
|
|
|
}
|