2019-06-06 06:51:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-06-06 06:51:28 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-06-06 06:51:28 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2020-03-09 10:38:15 +01:00
|
|
|
use App\DataMapper\CompanySettings;
|
2021-01-14 04:44:52 +01:00
|
|
|
use App\Events\User\UserWasArchived;
|
2020-03-24 10:15:30 +01:00
|
|
|
use App\Events\User\UserWasDeleted;
|
2021-01-14 04:44:52 +01:00
|
|
|
use App\Events\User\UserWasRestored;
|
2020-03-09 10:38:15 +01:00
|
|
|
use App\Models\CompanyUser;
|
|
|
|
use App\Models\User;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-09-16 01:56:10 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-06-06 06:51:28 +02:00
|
|
|
use Illuminate\Http\Request;
|
2021-01-01 10:11:21 +01:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2019-06-06 06:51:28 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* UserRepository.
|
2019-06-06 06:51:28 +02:00
|
|
|
*/
|
|
|
|
class UserRepository extends BaseRepository
|
|
|
|
{
|
2020-09-16 01:56:10 +02:00
|
|
|
use MakesHash;
|
|
|
|
|
2019-06-06 06:51:28 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Saves the user and its contacts.
|
2019-06-06 06:51:28 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param array $data The data
|
2020-11-03 14:27:41 +01:00
|
|
|
* @param \App\Models\User $user The user
|
2019-06-06 06:51:28 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param bool $unset_company_user
|
2020-11-03 14:27:41 +01:00
|
|
|
* @return \App\Models\User user Object
|
2019-06-06 06:51:28 +02:00
|
|
|
*/
|
2020-11-01 04:19:03 +01:00
|
|
|
public function save(array $data, User $user, $unset_company_user = false)
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2020-04-07 22:43:44 +02:00
|
|
|
$details = $data;
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
/*
|
2020-04-07 22:43:44 +02:00
|
|
|
* Getting: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'company_user'
|
|
|
|
* because of User::unguard().
|
|
|
|
* Solution. Unset company_user per request.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ($unset_company_user) {
|
|
|
|
unset($details['company_user']);
|
|
|
|
}
|
|
|
|
|
2020-03-24 10:15:30 +01:00
|
|
|
$company = auth()->user()->company();
|
2020-09-06 11:38:10 +02:00
|
|
|
$account = $company->account;
|
2020-04-23 00:54:10 +02:00
|
|
|
|
|
|
|
/* If hosted and Enterprise we need to increment the num_users field on the accounts table*/
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $user->id && $account->isEnterpriseClient()) {
|
2020-04-23 00:54:10 +02:00
|
|
|
$account->num_users++;
|
|
|
|
$account->save();
|
|
|
|
}
|
2020-03-24 10:15:30 +01:00
|
|
|
|
2020-04-07 22:43:44 +02:00
|
|
|
$user->fill($details);
|
2020-09-16 01:56:10 +02:00
|
|
|
|
2021-01-01 10:11:21 +01:00
|
|
|
//allow users to change only their passwords - not others!
|
2021-01-04 13:38:00 +01:00
|
|
|
if (auth()->user()->id == $user->id && array_key_exists('password', $data) && isset($data['password'])) {
|
2021-01-01 10:11:21 +01:00
|
|
|
$user->password = Hash::make($data['password']);
|
|
|
|
}
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if (!$user->confirmation_code) {
|
2020-09-16 01:56:10 +02:00
|
|
|
$user->confirmation_code = $this->createDbHash(config('database.default'));
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-09-16 01:56:10 +02:00
|
|
|
|
2020-04-23 00:54:10 +02:00
|
|
|
$user->account_id = $account->id;
|
2021-03-04 06:03:28 +01:00
|
|
|
|
|
|
|
if(strlen($user->password) >=1)
|
|
|
|
$user->has_password = true;
|
|
|
|
|
2019-06-06 06:51:28 +02:00
|
|
|
$user->save();
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (isset($data['company_user'])) {
|
2020-03-02 11:22:37 +01:00
|
|
|
$cu = CompanyUser::whereUserId($user->id)->whereCompanyId($company->id)->withTrashed()->first();
|
2019-11-21 09:38:57 +01:00
|
|
|
|
2019-12-04 02:06:14 +01:00
|
|
|
/*No company user exists - attach the user*/
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $cu) {
|
2020-04-23 00:54:10 +02:00
|
|
|
$data['company_user']['account_id'] = $account->id;
|
2020-03-09 10:38:15 +01:00
|
|
|
$data['company_user']['notifications'] = CompanySettings::notificationDefaults();
|
2019-11-22 22:10:53 +01:00
|
|
|
$user->companies()->attach($company->id, $data['company_user']);
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2021-05-06 23:41:37 +02:00
|
|
|
|
|
|
|
if(auth()->user()->isAdmin())
|
|
|
|
{
|
|
|
|
$cu->fill($data['company_user']);
|
|
|
|
$cu->restore();
|
|
|
|
$cu->tokens()->restore();
|
|
|
|
$cu->save();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
$cu->notifications = $data['company_user']['notifications'];
|
|
|
|
$cu->settings = $data['company_user']['settings'];
|
|
|
|
$cu->save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-04 02:06:14 +01:00
|
|
|
}
|
2020-03-09 10:38:15 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$user->with(['company_users' => function ($query) use ($company, $user) {
|
2020-03-09 10:38:15 +01:00
|
|
|
$query->whereCompanyId($company->id)
|
|
|
|
->whereUserId($user->id);
|
|
|
|
}])->first();
|
2019-11-21 09:38:57 +01:00
|
|
|
}
|
2020-03-25 00:20:42 +01:00
|
|
|
$user->restore();
|
2019-11-21 09:38:57 +01:00
|
|
|
|
2021-05-24 02:53:04 +02:00
|
|
|
return $user->fresh();
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-03-02 11:22:37 +01:00
|
|
|
|
|
|
|
public function destroy(array $data, User $user)
|
|
|
|
{
|
2021-03-07 07:27:44 +01:00
|
|
|
if($user->isOwner())
|
|
|
|
return $user;
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (array_key_exists('company_user', $data)) {
|
2020-03-02 11:22:37 +01:00
|
|
|
$this->forced_includes = 'company_users';
|
|
|
|
|
|
|
|
$company = auth()->user()->company();
|
|
|
|
|
|
|
|
$cu = CompanyUser::whereUserId($user->id)
|
|
|
|
->whereCompanyId($company->id)
|
|
|
|
->first();
|
|
|
|
|
2020-03-11 12:05:05 +01:00
|
|
|
$cu->tokens()->forceDelete();
|
|
|
|
$cu->forceDelete();
|
2020-03-02 11:22:37 +01:00
|
|
|
}
|
2020-03-11 12:05:05 +01:00
|
|
|
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new UserWasDeleted($user, $company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2020-03-24 10:15:30 +01:00
|
|
|
|
2020-09-28 04:56:11 +02:00
|
|
|
$user->delete();
|
|
|
|
|
2020-03-02 11:22:37 +01:00
|
|
|
return $user->fresh();
|
|
|
|
}
|
2020-03-11 12:05:05 +01:00
|
|
|
|
2020-03-24 10:15:30 +01:00
|
|
|
/*
|
|
|
|
* Soft deletes the user and the company user
|
|
|
|
*/
|
2020-03-18 10:40:15 +01:00
|
|
|
public function delete($user)
|
|
|
|
{
|
|
|
|
$company = auth()->user()->company();
|
|
|
|
|
|
|
|
$cu = CompanyUser::whereUserId($user->id)
|
|
|
|
->whereCompanyId($company->id)
|
|
|
|
->first();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($cu) {
|
2020-03-18 10:40:15 +01:00
|
|
|
$cu->tokens()->delete();
|
|
|
|
$cu->delete();
|
|
|
|
}
|
|
|
|
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new UserWasDeleted($user, auth()->user(), $company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2020-09-28 04:56:11 +02:00
|
|
|
|
2021-03-26 09:07:43 +01:00
|
|
|
$user->is_deleted = true;
|
|
|
|
$user->save();
|
|
|
|
$user->delete();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-24 10:15:30 +01:00
|
|
|
|
2020-03-18 10:40:15 +01:00
|
|
|
return $user->fresh();
|
|
|
|
}
|
2021-01-14 04:44:52 +01:00
|
|
|
|
|
|
|
public function archive($user)
|
|
|
|
{
|
|
|
|
if ($user->trashed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->delete();
|
|
|
|
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new UserWasArchived($user, auth()->user(), auth()->user()->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2021-01-14 04:44:52 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $entity
|
|
|
|
*/
|
|
|
|
public function restore($user)
|
|
|
|
{
|
|
|
|
if (! $user->trashed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:07:43 +01:00
|
|
|
$user->is_deleted = false;
|
|
|
|
$user->save();
|
2021-01-14 04:44:52 +01:00
|
|
|
$user->restore();
|
2021-03-26 09:07:43 +01:00
|
|
|
// $user->company_user->restore();
|
|
|
|
|
|
|
|
$cu = CompanyUser::withTrashed()
|
|
|
|
->where('user_id', $user->id)
|
|
|
|
->where('company_id', auth()->user()->company()->id)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
$cu->restore();
|
2021-01-14 04:44:52 +01:00
|
|
|
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new UserWasRestored($user, auth()->user(), auth()->user()->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2021-01-14 04:44:52 +01:00
|
|
|
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|