1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Models/User.php

541 lines
12 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Models;
2015-03-16 22:45:25 +01:00
2015-08-14 14:04:33 +02:00
use App\Events\UserSettingsChanged;
2015-09-25 11:57:40 +02:00
use App\Events\UserSignedUp;
2017-01-30 20:40:43 +01:00
use App\Libraries\Utils;
use Event;
2015-03-31 11:38:24 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
2017-01-30 20:40:43 +01:00
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laracasts\Presenter\PresentableTrait;
2017-01-30 20:40:43 +01:00
use Session;
2017-04-30 21:18:17 +02:00
use App\Models\LookupUser;
2017-11-14 09:58:08 +01:00
use Illuminate\Notifications\Notifiable;
2015-03-23 07:52:01 +01:00
/**
2017-01-30 20:40:43 +01:00
* Class User.
*/
2016-10-22 20:13:37 +02:00
class User extends Authenticatable
{
use PresentableTrait;
2017-02-20 11:44:11 +01:00
use SoftDeletes;
2017-11-14 09:58:08 +01:00
use Notifiable;
/**
* @var string
*/
protected $presenter = 'App\Ninja\Presenters\UserPresenter';
/**
* @var array
*/
public static $all_permissions = [
2016-03-16 00:08:00 +01:00
'create_all' => 0b0001,
'view_all' => 0b0010,
'edit_all' => 0b0100,
];
2016-05-23 18:52:20 +02:00
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
*/
2016-02-04 21:35:28 +01:00
protected $fillable = [
'first_name',
'last_name',
'email',
'password',
'phone',
];
2015-03-18 00:39:03 +01:00
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
'confirmation_code',
'oauth_user_id',
'oauth_provider_id',
'google_2fa_secret',
'google_2fa_phone',
2017-12-13 15:27:49 +01:00
'remember_2fa_token',
2018-03-08 16:37:25 +01:00
'slack_webhook_url',
];
2015-03-18 00:39:03 +01:00
/**
* @var array
*/
2015-03-31 11:38:24 +02:00
protected $dates = ['deleted_at'];
2015-03-18 00:39:03 +01:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
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
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
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
}
/**
* @param $value
*/
2016-02-04 21:35:28 +01:00
public function setEmailAttribute($value)
{
$this->attributes['email'] = $this->attributes['username'] = $value;
}
/**
* @return mixed|string
*/
2015-07-02 22:21:29 +02:00
public function getName()
{
return $this->getDisplayName();
}
/**
* @return mixed
*/
2015-03-16 22:45:25 +01:00
public function getPersonType()
{
return PERSON_USER;
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
/**
* @return mixed
*/
2015-03-16 22:45:25 +01:00
public function isPro()
{
return $this->account->isPro();
}
2018-04-11 22:18:01 +02:00
/**
* @return mixed
*/
public function isEnterprise()
{
return $this->account->isEnterprise();
}
2018-02-04 09:31:52 +01:00
/**
* @return mixed
*/
2018-02-04 09:43:14 +01:00
public function isTrusted()
2018-02-04 09:31:52 +01:00
{
2018-02-04 09:43:14 +01:00
if (Utils::isSelfHost()) {
true;
}
2018-02-04 09:31:52 +01:00
return $this->account->isPro() && ! $this->account->isTrial();
}
2017-12-01 10:48:13 +01:00
/**
* @return mixed
*/
public function hasActivePromo()
{
return $this->account->hasActivePromo();
}
/**
* @param $feature
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
public function hasFeature($feature)
{
return $this->account->hasFeature($feature);
}
/**
* @return mixed
*/
public function isTrial()
{
return $this->account->isTrial();
}
/**
* @return int
*/
2015-03-16 22:45:25 +01:00
public function maxInvoiceDesignId()
{
2017-06-01 18:13:13 +02:00
return $this->hasFeature(FEATURE_MORE_INVOICE_DESIGNS) ? 13 : COUNT_FREE_DESIGNS;
2015-03-16 22:45:25 +01:00
}
/**
* @return mixed|string
*/
2015-03-16 22:45:25 +01:00
public function getDisplayName()
{
if ($this->getFullName()) {
return $this->getFullName();
} elseif ($this->email) {
return $this->email;
} else {
2017-12-07 11:33:46 +01:00
return trans('texts.guest');
2015-03-16 22:45:25 +01:00
}
}
/**
* @return string
*/
2015-03-16 22:45:25 +01:00
public function getFullName()
{
if ($this->first_name || $this->last_name) {
return $this->first_name.' '.$this->last_name;
} else {
return '';
}
}
/**
* @return bool
*/
2015-03-16 22:45:25 +01:00
public function showGreyBackground()
{
2017-01-30 20:40:43 +01:00
return ! $this->theme_id || in_array($this->theme_id, [2, 3, 5, 6, 7, 8, 10, 11, 12]);
2015-03-16 22:45:25 +01:00
}
/**
* @return mixed
*/
2015-03-16 22:45:25 +01:00
public function getRequestsCount()
{
return Session::get(SESSION_COUNTER, 0);
}
2016-05-23 18:52:20 +02:00
/**
* @param bool $success
* @param bool $forced
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
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;
}
}
/**
* @return mixed
*/
2015-03-16 22:45:25 +01:00
public function getMaxNumClients()
{
if ($this->hasFeature(FEATURE_MORE_CLIENTS)) {
2015-10-18 09:30:28 +02:00
return MAX_NUM_CLIENTS_PRO;
}
if ($this->id < LEGACY_CUTOFF) {
return MAX_NUM_CLIENTS_LEGACY;
}
return MAX_NUM_CLIENTS;
2015-03-16 22:45:25 +01:00
}
/**
* @return mixed
*/
2016-01-06 15:23:58 +01:00
public function getMaxNumVendors()
{
if ($this->hasFeature(FEATURE_MORE_CLIENTS)) {
2016-01-06 15:23:58 +01:00
return MAX_NUM_VENDORS_PRO;
}
return MAX_NUM_VENDORS;
}
2016-05-23 18:52:20 +02:00
2015-06-16 21:35:35 +02:00
public function clearSession()
{
$keys = [
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);
}
}
/**
* @param $user
*/
2015-09-25 11:57:40 +02:00
public static function onUpdatingUser($user)
2015-07-07 22:08:16 +02:00
{
2015-09-25 11:57:40 +02:00
if ($user->password != $user->getOriginal('password')) {
2015-07-07 22:08:16 +02:00
$user->failed_logins = 0;
}
// if the user changes their email then they need to reconfirm it
if ($user->isEmailBeingChanged()) {
$user->confirmed = 0;
2017-04-02 19:46:01 +02:00
$user->confirmation_code = strtolower(str_random(RANDOM_KEY_LENGTH));
}
2015-07-07 22:08:16 +02:00
}
/**
* @param $user
*/
2015-09-25 11:57:40 +02:00
public static function onUpdatedUser($user)
{
2017-01-30 20:40:43 +01:00
if (! $user->getOriginal('email')
2015-09-25 11:57:40 +02:00
|| $user->getOriginal('email') == TEST_USERNAME
2015-10-20 10:23:38 +02:00
|| $user->getOriginal('username') == TEST_USERNAME
|| $user->getOriginal('email') == 'tests@bitrock.com') {
2015-09-25 11:57:40 +02:00
event(new UserSignedUp());
}
event(new UserSettingsChanged($user));
}
/**
* @return bool
*/
public function isEmailBeingChanged()
{
return Utils::isNinjaProd() && $this->email != $this->getOriginal('email');
2015-09-25 11:57:40 +02:00
}
2016-05-23 18:52:20 +02:00
2016-03-16 00:08:00 +01:00
/**
2017-01-30 20:40:43 +01:00
* Checks to see if the user has the required permission.
*
* @param mixed $permission Either a single permission or an array of possible permissions
* @param mixed $requireAll - True to require all permissions, false to require only one
2016-03-16 00:08:00 +01:00
*
2017-01-30 20:40:43 +01:00
* @return bool
2016-03-16 00:08:00 +01:00
*/
2017-01-30 17:05:31 +01:00
public function hasPermission($permission, $requireAll = false)
{
2016-03-16 00:08:00 +01:00
if ($this->is_admin) {
return true;
2017-01-30 17:05:31 +01:00
} elseif (is_string($permission)) {
if( is_array(json_decode($this->permissions,1)) && in_array($permission, json_decode($this->permissions,1)) ) {
return true;
2016-03-16 00:08:00 +01:00
}
} elseif (is_array($permission)) {
if ($requireAll)
return count(array_intersect($permission, json_decode($this->permissions,1))) == count( $permission );
else
return count(array_intersect($permission, json_decode($this->permissions,1))) > 0;
2016-03-16 00:08:00 +01:00
}
2016-05-23 18:52:20 +02:00
2016-03-16 00:08:00 +01:00
return false;
}
2016-05-23 18:52:20 +02:00
public function viewModel($model, $entityType)
{
if($this->hasPermission('view_'.$entityType))
return true;
elseif($model->user_id == $this->id)
return true;
else
return false;
}
/**
* @param $entity
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2017-01-30 17:05:31 +01:00
public function owns($entity)
{
2017-01-30 20:40:43 +01:00
return ! empty($entity->user_id) && $entity->user_id == $this->id;
2016-04-23 17:52:36 +02:00
}
2016-05-23 18:52:20 +02:00
/**
* @return bool|mixed
*/
2017-01-30 17:05:31 +01:00
public function filterId()
{ //todo permissions
2016-05-23 18:52:20 +02:00
return $this->hasPermission('view_all') ? false : $this->id;
}
2016-07-11 19:08:43 +02:00
public function filterIdByEntity($entity)
{
return $this->hasPermission('view_' . $entity) ? false : $this->id;
}
2016-07-14 11:46:00 +02:00
public function caddAddUsers()
{
2017-01-30 17:05:31 +01:00
if (! Utils::isNinjaProd()) {
2016-07-14 11:46:00 +02:00
return true;
2017-01-30 17:05:31 +01:00
} elseif (! $this->hasFeature(FEATURE_USERS)) {
2016-07-11 19:08:43 +02:00
return false;
}
$account = $this->account;
$company = $account->company;
$numUsers = 1;
foreach ($company->accounts as $account) {
$numUsers += $account->users->count() - 1;
}
return $numUsers < $company->num_users;
}
public function canCreateOrEdit($entityType, $entity = false)
{
2017-01-30 20:40:43 +01:00
return ($entity && $this->can('edit', $entity))
|| (! $entity && $this->can('create', $entityType));
}
2016-12-27 22:56:55 +01:00
public function primaryAccount()
{
return $this->account->company->accounts->sortBy('id')->first();
}
2017-11-14 21:34:56 +01:00
public function sendPasswordResetNotification($token)
{
//$this->notify(new ResetPasswordNotification($token));
app('App\Ninja\Mailers\UserMailer')->sendPasswordReset($this, $token);
}
2018-03-08 16:37:25 +01:00
public function routeNotificationForSlack()
{
return $this->slack_webhook_url;
}
2018-03-12 14:37:05 +01:00
public function hasAcceptedLatestTerms()
{
if (! NINJA_TERMS_VERSION) {
return true;
}
return $this->accepted_terms_version == NINJA_TERMS_VERSION;
}
2018-03-12 15:17:27 +01:00
public function acceptLatestTerms($ip)
2018-03-12 14:37:05 +01:00
{
$this->accepted_terms_version = NINJA_TERMS_VERSION;
$this->accepted_terms_timestamp = date('Y-m-d H:i:s');
$this->accepted_terms_ip = $ip;
2018-03-12 15:17:27 +01:00
return $this;
2018-03-12 14:37:05 +01:00
}
2018-05-14 19:08:32 +02:00
public function ownsEntity($entity)
{
return $entity->user_id == $this->id;
}
public function shouldNotify($invoice)
{
if (! $this->email || ! $this->confirmed) {
return false;
}
if ($this->cannot('view', $invoice)) {
return false;
}
if ($this->only_notify_owned && ! $this->ownsEntity($invoice)) {
return false;
}
return true;
}
2018-08-17 07:13:32 +02:00
public function permissionsMap()
{
$data = [];
$permissions = json_decode($this->permissions);
if (! $permissions) {
return $data;
}
$keys = array_values((array) $permissions);
$values = array_fill(0, count($keys), true);
return array_combine($keys, $values);
}
public function eligibleForMigration()
{
return is_null($this->public_id) || $this->public_id == 0;
}
2015-03-16 22:45:25 +01:00
}
2015-07-07 22:08:16 +02:00
2017-05-01 10:13:15 +02:00
User::created(function ($user)
2017-04-30 21:18:17 +02:00
{
LookupUser::createNew($user->account->account_key, [
'email' => $user->email,
2017-04-30 21:29:15 +02:00
'user_id' => $user->id,
2017-05-10 12:02:20 +02:00
'confirmation_code' => $user->confirmation_code,
2017-04-30 21:18:17 +02:00
]);
});
2015-07-07 22:08:16 +02:00
User::updating(function ($user) {
2015-09-25 11:57:40 +02:00
User::onUpdatingUser($user);
2017-05-01 09:19:27 +02:00
$dirty = $user->getDirty();
2017-05-10 17:18:48 +02:00
if (array_key_exists('email', $dirty)
|| array_key_exists('confirmation_code', $dirty)
|| array_key_exists('oauth_user_id', $dirty)
2017-05-14 11:11:38 +02:00
|| array_key_exists('oauth_provider_id', $dirty)
|| array_key_exists('referral_code', $dirty)) {
2017-05-10 17:18:48 +02:00
LookupUser::updateUser($user->account->account_key, $user);
2017-05-01 09:19:27 +02:00
}
2015-07-07 22:08:16 +02:00
});
2015-08-14 14:04:33 +02:00
User::updated(function ($user) {
2015-09-25 11:57:40 +02:00
User::onUpdatedUser($user);
});
2017-05-01 11:29:45 +02:00
User::deleted(function ($user)
{
if (! $user->email) {
return;
}
2017-05-10 12:02:20 +02:00
if ($user->forceDeleting) {
LookupUser::deleteWhere([
'email' => $user->email
]);
}
2017-05-01 11:29:45 +02:00
});