2015-03-18 00:39:03 +01:00
|
|
|
<?php namespace App\Models;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-03-30 21:45:10 +02:00
|
|
|
use Session;
|
|
|
|
use Auth;
|
2015-08-14 14:04:33 +02:00
|
|
|
use Event;
|
2015-03-29 14:37:42 +02:00
|
|
|
use App\Libraries\Utils;
|
2015-08-14 14:04:33 +02:00
|
|
|
use App\Events\UserSettingsChanged;
|
2015-03-29 14:37:42 +02:00
|
|
|
use Illuminate\Auth\Authenticatable;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword;
|
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
|
2015-03-31 11:38:24 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2015-03-23 07:52:01 +01:00
|
|
|
|
2015-03-29 14:37:42 +02:00
|
|
|
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
|
2015-03-23 07:52:01 +01:00
|
|
|
|
2015-03-29 14:37:42 +02:00
|
|
|
use Authenticatable, CanResetPassword;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The database table used by the model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'users';
|
|
|
|
|
2015-03-18 00:39:03 +01:00
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-03-29 14:37:42 +02:00
|
|
|
protected $fillable = ['name', 'email', 'password'];
|
2015-03-18 00:39:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes excluded from the model's JSON form.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = ['password', 'remember_token'];
|
|
|
|
|
2015-03-31 11:38:24 +02:00
|
|
|
use SoftDeletes;
|
|
|
|
protected $dates = ['deleted_at'];
|
2015-03-18 00:39:03 +01:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public function account()
|
|
|
|
{
|
2015-03-26 04:52:42 +01:00
|
|
|
return $this->belongsTo('App\Models\Account');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function theme()
|
|
|
|
{
|
2015-03-26 04:52:42 +01:00
|
|
|
return $this->belongsTo('App\Models\Theme');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2015-07-02 22:21:29 +02:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->getDisplayName();
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getPersonType()
|
|
|
|
{
|
|
|
|
return PERSON_USER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the unique identifier for the user.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getAuthIdentifier()
|
|
|
|
{
|
|
|
|
return $this->getKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the password for the user.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAuthPassword()
|
|
|
|
{
|
|
|
|
return $this->password;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the e-mail address where password reminders are sent.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getReminderEmail()
|
|
|
|
{
|
|
|
|
return $this->email;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isPro()
|
|
|
|
{
|
|
|
|
return $this->account->isPro();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isDemo()
|
|
|
|
{
|
|
|
|
return $this->account->id == Utils::getDemoAccountId();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function maxInvoiceDesignId()
|
|
|
|
{
|
2015-07-24 16:13:17 +02:00
|
|
|
return $this->isPro() ? 11 : (Utils::isNinja() ? COUNT_FREE_DESIGNS : COUNT_FREE_DESIGNS_SELF_HOST);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDisplayName()
|
|
|
|
{
|
|
|
|
if ($this->getFullName()) {
|
|
|
|
return $this->getFullName();
|
|
|
|
} elseif ($this->email) {
|
|
|
|
return $this->email;
|
|
|
|
} else {
|
|
|
|
return 'Guest';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFullName()
|
|
|
|
{
|
|
|
|
if ($this->first_name || $this->last_name) {
|
|
|
|
return $this->first_name.' '.$this->last_name;
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function showGreyBackground()
|
|
|
|
{
|
|
|
|
return !$this->theme_id || in_array($this->theme_id, [2, 3, 5, 6, 7, 8, 10, 11, 12]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRequestsCount()
|
|
|
|
{
|
|
|
|
return Session::get(SESSION_COUNTER, 0);
|
|
|
|
}
|
|
|
|
|
2015-06-16 21:35:35 +02:00
|
|
|
/*
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getPopOverText()
|
|
|
|
{
|
|
|
|
if (!Utils::isNinja() || !Auth::check() || Session::has('error')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$count = self::getRequestsCount();
|
|
|
|
|
|
|
|
if ($count == 1 || $count % 5 == 0) {
|
|
|
|
if (!Utils::isRegistered()) {
|
|
|
|
return trans('texts.sign_up_to_save');
|
|
|
|
} elseif (!Auth::user()->account->name) {
|
|
|
|
return trans('texts.set_name');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-16 21:35:35 +02:00
|
|
|
*/
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public function afterSave($success = true, $forced = false)
|
|
|
|
{
|
|
|
|
if ($this->email) {
|
|
|
|
return parent::afterSave($success = true, $forced = false);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMaxNumClients()
|
|
|
|
{
|
|
|
|
return $this->isPro() ? MAX_NUM_CLIENTS_PRO : MAX_NUM_CLIENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRememberToken()
|
|
|
|
{
|
|
|
|
return $this->remember_token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setRememberToken($value)
|
|
|
|
{
|
|
|
|
$this->remember_token = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRememberTokenName()
|
|
|
|
{
|
|
|
|
return 'remember_token';
|
|
|
|
}
|
2015-03-23 07:52:01 +01:00
|
|
|
|
2015-06-16 21:35:35 +02:00
|
|
|
public function clearSession()
|
|
|
|
{
|
|
|
|
$keys = [
|
|
|
|
RECENTLY_VIEWED,
|
|
|
|
SESSION_USER_ACCOUNTS,
|
|
|
|
SESSION_TIMEZONE,
|
|
|
|
SESSION_DATE_FORMAT,
|
|
|
|
SESSION_DATE_PICKER_FORMAT,
|
|
|
|
SESSION_DATETIME_FORMAT,
|
|
|
|
SESSION_CURRENCY,
|
|
|
|
SESSION_LOCALE,
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
Session::forget($key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 22:08:16 +02:00
|
|
|
public static function updateUser($user)
|
|
|
|
{
|
|
|
|
if ($user->password != !$user->getOriginal('password')) {
|
|
|
|
$user->failed_logins = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2015-07-07 22:08:16 +02:00
|
|
|
|
|
|
|
User::updating(function ($user) {
|
|
|
|
User::updateUser($user);
|
|
|
|
});
|
2015-08-14 14:04:33 +02:00
|
|
|
|
|
|
|
User::updated(function ($user) {
|
|
|
|
Event::fire(new UserSettingsChanged());
|
|
|
|
});
|
|
|
|
|