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); //allow users to change only their passwords - not others! if (auth()->user()->id == $user->id && array_key_exists('password', $data) && isset($data['password'])) { $user->password = Hash::make($data['password']); } if (!$user->confirmation_code) { $user->confirmation_code = $this->createDbHash(config('database.default')); } $user->account_id = $account->id; if(strlen($user->password) >=1) $user->has_password = true; $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 { $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(); } $user->restore(); return $user; } 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, $company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); $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(); } event(new UserWasDeleted($user, auth()->user(), $company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); $user->is_deleted = true; $user->save(); $user->delete(); return $user->fresh(); } public function archive($user) { if ($user->trashed()) { return; } $user->delete(); event(new UserWasArchived($user, auth()->user(), auth()->user()->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); } /** * @param $entity */ public function restore($user) { if (! $user->trashed()) { return; } $user->is_deleted = false; $user->save(); $user->restore(); // $user->company_user->restore(); $cu = CompanyUser::withTrashed() ->where('user_id', $user->id) ->where('company_id', auth()->user()->company()->id) ->first(); $cu->restore(); event(new UserWasRestored($user, auth()->user(), auth()->user()->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); } }