1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Repositories/UserRepository.php

210 lines
6.0 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
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. 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;
2022-08-05 09:27:17 +02:00
use App\Jobs\Company\CreateCompanyToken;
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;
/**
* 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(array_key_exists('oauth_provider_id', $details))
unset($details['oauth_provider_id']);
$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']);
}
if (! $user->confirmation_code) {
2021-11-06 01:46:12 +01:00
$user->confirmation_code = $this->createDbHash($company->db);
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 {
if (auth()->user()->isAdmin()) {
2021-05-06 23:41:37 +02:00
$cu->fill($data['company_user']);
$cu->restore();
$cu->tokens()->restore();
$cu->save();
2022-08-05 09:27:17 +02:00
2022-08-05 09:42:54 +02:00
//05-08-2022
2022-08-05 09:27:17 +02:00
if($cu->tokens()->count() == 0){
(new CreateCompanyToken($cu->company, $cu->user, 'restored_user'))->handle();
}
} else {
2021-05-06 23:41:37 +02:00
$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();
}
event(new UserWasDeleted($user, auth()->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
}
/**
2021-01-14 04:44:52 +01:00
* @param $entity
*/
public function restore($user)
{
if (! $user->trashed()) {
return;
}
if (Ninja::isHosted()) {
$count = User::where('account_id', auth()->user()->account_id)->count();
if ($count >= auth()->user()->account->num_users) {
return;
}
}
$user->is_deleted = false;
$user->save();
2021-01-14 04:44:52 +01:00
$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
}
}