1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Repositories/UserRepository.php
David Bomba 4c23d43138
Working on Setup workflow (#3509)
* Refactor designs to remove whitespace

* enable dummy data for templating

* Insert faker data into templates

* Fixes for user deletion

* Documentation on User controller:

* Working on app setup

* Files for app setup

* Working on Setup

* Final fixes for setup controller

* Fixes for setup

* Fixes for first install

* Minor fixes
2020-03-18 20:40:15 +11:00

128 lines
3.1 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Repositories;
use App\DataMapper\CompanySettings;
use App\Factory\CompanyUserFactory;
use App\Models\CompanyUser;
use App\Models\User;
use Illuminate\Http\Request;
/**
* UserRepository
*/
class UserRepository extends BaseRepository
{
public function __construct()
{
}
/**
* Gets the class name.
*
* @return string The class name.
*/
public function getClassName()
{
return User::class;
}
/**
* Saves the user and its contacts
*
* @param array $data The data
* @param \App\Models\user $user The user
*
* @return user|\App\Models\user|null user Object
*/
public function save(array $data, User $user)
{
$user->fill($data);
$user->save();
if (isset($data['company_user'])) {
$company = auth()->user()->company();
$account_id = $company->account->id;
$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 {
$cu->fill($data['company_user']);
$cu->restore();
$cu->tokens()->restore();
$cu->save();
}
$user->with(['company_users' => function ($query) use($company, $user){
$query->whereCompanyId($company->id)
->whereUserId($user->id);
}])->first();
//return $user->with('company_user')->whereCompanyId($company->id)->first();
}
return $user;
}
public function destroy(array $data, User $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();
}
$user->delete();
return $user->fresh();
}
public function delete($user)
{
$company = auth()->user()->company();
$cu = CompanyUser::whereUserId($user->id)
->whereCompanyId($company->id)
->first();
if($cu)
{
$cu->tokens()->delete();
$cu->delete();
}
$user->is_deleted=true;
$user->save();
$user->delete();
return $user->fresh();
}
}