pingcrm/app/User.php
Burton 9f93e151a8
Hash password only once
When a user resets their password, the password would be double hashed and the user locked out of their account.

Ensure we only hash the password if it needs to be hashed. If it has already been hashed, just return the password as-is.
2020-03-20 21:58:34 -07:00

81 lines
2.4 KiB
PHP

<?php
namespace App;
use League\Glide\Server;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\URL;
use Illuminate\Auth\Authenticatable;
use Illuminate\Support\Facades\Hash;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
class User extends Model implements AuthenticatableContract, AuthorizableContract
{
use SoftDeletes, Authenticatable, Authorizable;
protected $casts = [
'owner' => 'boolean',
];
public function account()
{
return $this->belongsTo(Account::class);
}
public function getNameAttribute()
{
return $this->first_name.' '.$this->last_name;
}
public function setPasswordAttribute($password)
{
$this->attributes['password'] = Hash::needsRehash($password) ? Hash::make($password) : $password;
}
public function photoUrl(array $attributes)
{
if ($this->photo_path) {
return URL::to(App::make(Server::class)->fromPath($this->photo_path, $attributes));
}
}
public function isDemoUser()
{
return $this->email === 'johndoe@example.com';
}
public function scopeOrderByName($query)
{
$query->orderBy('last_name')->orderBy('first_name');
}
public function scopeWhereRole($query, $role)
{
switch ($role) {
case 'user': return $query->where('owner', false);
case 'owner': return $query->where('owner', true);
}
}
public function scopeFilter($query, array $filters)
{
$query->when($filters['search'] ?? null, function ($query, $search) {
$query->where(function ($query) use ($search) {
$query->where('first_name', 'like', '%'.$search.'%')
->orWhere('last_name', 'like', '%'.$search.'%')
->orWhere('email', 'like', '%'.$search.'%');
});
})->when($filters['role'] ?? null, function ($query, $role) {
$query->whereRole($role);
})->when($filters['trashed'] ?? null, function ($query, $trashed) {
if ($trashed === 'with') {
$query->withTrashed();
} elseif ($trashed === 'only') {
$query->onlyTrashed();
}
});
}
}