Cachet/app/Models/User.php

225 lines
4.9 KiB
PHP
Raw Normal View History

2014-11-16 22:26:08 +00:00
<?php
/*
* This file is part of Cachet.
*
2015-07-06 17:37:01 +01:00
* (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;
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-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,
];
/**
* The attributes that should be casted to native types.
*
* @var string[]
*/
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',
];
2014-11-16 22:26:08 +00:00
/**
* The properties that cannot be mass assigned.
*
* @var string[]
*/
protected $guarded = [];
/**
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'];
/**
* The validation rules.
2014-12-30 18:19:22 +00:00
*
* @var string[]
*/
public $rules = [
'username' => ['required', 'regex:/\A(?!.*[:;]-\))[ -~]+\z/'],
'email' => 'required|email',
'password' => 'required',
];
2015-01-03 17:51:35 +00:00
/**
* Overrides the models boot method.
*/
public static function boot()
{
parent::boot();
self::creating(function ($user) {
if (!$user->api_key) {
$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.
*
* @param string $token
2015-01-03 17:51:35 +00:00
* @param string[] $columns
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*
* @return \CachetHQ\Cachet\Models\User
*/
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);
}
/**
* 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;
}
/**
* 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 14:21:53 -06:00
return trim($this->google_2fa_secret) !== '';
}
}