1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Repositories/UserRepository.php

208 lines
5.8 KiB
PHP
Raw Normal View History

2019-06-06 06:51:28 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2019-06-06 06:51:28 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-06-06 06:51:28 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-06-06 06:51:28 +02:00
*/
namespace App\Repositories;
use App\DataMapper\CompanySettings;
2021-01-14 04:44:52 +01:00
use App\Events\User\UserWasArchived;
use App\Events\User\UserWasDeleted;
2021-01-14 04:44:52 +01:00
use App\Events\User\UserWasRestored;
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;
use Illuminate\Support\Facades\Hash;
2019-06-06 06:51:28 +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
/**
* 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)
{
$details = $data;
/*
* 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']);
}
$company = auth()->user()->company();
$account = $company->account;
/* If hosted and Enterprise we need to increment the num_users field on the accounts table*/
if (! $user->id && $account->isEnterpriseClient()) {
$account->num_users++;
$account->save();
}
$user->fill($details);
2020-09-16 01:56:10 +02: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'])) {
$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
$user->account_id = $account->id;
if(strlen($user->password) >=1)
$user->has_password = true;
2019-06-06 06:51:28 +02:00
$user->save();
if (isset($data['company_user'])) {
$cu = CompanyUser::whereUserId($user->id)->whereCompanyId($company->id)->withTrashed()->first();
/*No company user exists - attach the user*/
if (! $cu) {
$data['company_user']['account_id'] = $account->id;
$data['company_user']['notifications'] = CompanySettings::notificationDefaults();
$user->companies()->attach($company->id, $data['company_user']);
} 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();
}
}
$user->with(['company_users' => function ($query) use ($company, $user) {
$query->whereCompanyId($company->id)
->whereUserId($user->id);
}])->first();
}
2020-03-25 00:20:42 +01:00
$user->restore();
2021-05-24 02:53:04 +02:00
return $user->fresh();
}
public function destroy(array $data, User $user)
{
if($user->isOwner())
return $user;
if (array_key_exists('company_user', $data)) {
$this->forced_includes = 'company_users';
$company = auth()->user()->company();
$cu = CompanyUser::whereUserId($user->id)
->whereCompanyId($company->id)
->first();
$cu->tokens()->forceDelete();
$cu->forceDelete();
}
2021-05-06 23:12:07 +02:00
event(new UserWasDeleted($user, $company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2020-09-28 04:56:11 +02:00
$user->delete();
return $user->fresh();
}
/*
* Soft deletes the user and the company user
*/
public function delete($user)
{
$company = auth()->user()->company();
$cu = CompanyUser::whereUserId($user->id)
->whereCompanyId($company->id)
->first();
if ($cu) {
$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
$user->is_deleted = true;
$user->save();
$user->delete();
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;
}
$user->is_deleted = false;
$user->save();
2021-01-14 04:44:52 +01:00
$user->restore();
// $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
}
}