2018-10-24 05:50:15 +02:00
< ? php
2019-05-11 05:32:07 +02:00
/**
2020-09-06 11:38:10 +02:00
* Invoice Ninja ( https :// invoiceninja . com ) .
2019-05-11 05:32:07 +02:00
*
* @ link https :// github . com / invoiceninja / invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @ copyright Copyright ( c ) 2024. Invoice Ninja LLC ( https :// invoiceninja . com )
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @ license https :// www . elastic . co / licensing / elastic - license
2019-05-11 05:32:07 +02:00
*/
2018-10-24 05:50:15 +02:00
namespace App\Http\Controllers ;
2023-10-26 04:57:44 +02:00
use App\Models\Account ;
2023-12-23 06:14:26 +01:00
use App\Libraries\MultiDB ;
use App\Utils\TruthSource ;
2019-09-16 23:42:08 +02:00
use App\Models\CompanyUser ;
2023-12-23 06:14:26 +01:00
use Illuminate\Http\Response ;
use App\Helpers\Encrypt\Secure ;
use App\Jobs\Account\CreateAccount ;
2022-06-16 07:58:11 +02:00
use App\Transformers\AccountTransformer ;
2019-09-16 23:42:08 +02:00
use App\Transformers\CompanyUserTransformer ;
2018-10-24 12:24:09 +02:00
use Illuminate\Foundation\Bus\DispatchesJobs ;
2023-12-23 06:14:26 +01:00
use App\Http\Requests\Account\CreateAccountRequest ;
use App\Http\Requests\Account\UpdateAccountRequest ;
2018-10-24 05:50:15 +02:00
2019-03-28 22:34:58 +01:00
class AccountController extends BaseController
2018-10-24 05:50:15 +02:00
{
use DispatchesJobs ;
2019-09-16 23:42:08 +02:00
protected $entity_type = CompanyUser :: class ;
2019-04-18 13:57:22 +02:00
2019-09-16 23:42:08 +02:00
protected $entity_transformer = CompanyUserTransformer :: class ;
2019-04-18 13:57:22 +02:00
2018-10-24 05:50:15 +02:00
public function __construct ()
{
2019-03-28 22:34:58 +01:00
parent :: __construct ();
2018-10-24 05:50:15 +02:00
}
/**
* Display a listing of the resource .
*
2020-10-28 11:10:49 +01:00
* @ return void
2018-10-24 05:50:15 +02:00
*/
public function index ()
{
2019-12-30 22:59:12 +01:00
// return view('signup.index');
2018-10-24 05:50:15 +02:00
}
/**
* Show the form for creating a new resource .
*
2020-10-28 11:10:49 +01:00
* @ return void
2018-10-24 05:50:15 +02:00
*/
public function create ()
{
//
}
/**
* Store a newly created resource in storage .
*
2020-10-28 11:10:49 +01:00
* @ param CreateAccountRequest $request
* @ return Response
2019-10-06 14:24:15 +02:00
*
2018-10-24 05:50:15 +02:00
*/
public function store ( CreateAccountRequest $request )
{
2023-12-23 03:10:15 +01:00
2023-12-23 06:14:26 +01:00
if ( $request -> has ( 'cf-turnstile-response' ) && config ( 'ninja.cloudflare.turnstile.secret' )) {
2023-12-23 03:10:15 +01:00
$r = \Illuminate\Support\Facades\Http :: post ( 'https://challenges.cloudflare.com/turnstile/v0/siteverify' , [
'secret' => config ( 'ninja.cloudflare.turnstile.secret' ),
'response' => $request -> input ( 'cf-turnstile-response' ),
'remoteip' => $request -> getClientIp (),
]);
2024-01-14 05:05:00 +01:00
if ( $r -> successful ()) {
2023-12-23 03:10:15 +01:00
if ( $r -> json ()[ 'success' ] === true ) {
2023-12-23 06:14:26 +01:00
// Captcha passed
2023-12-23 03:10:15 +01:00
} else {
return response () -> json ([ 'message' => 'Captcha Failed' ], 400 );
}
}
}
2023-12-23 06:14:26 +01:00
if ( $request -> has ( 'hash' ) && config ( 'ninja.cloudflare.turnstile.secret' )) { //@todo once all platforms are implemented, we disable access to the rest of this route without a success response.
2024-01-14 05:05:00 +01:00
2023-12-23 06:14:26 +01:00
if ( Secure :: decrypt ( $request -> input ( 'hash' )) !== $request -> input ( 'email' )) {
return response () -> json ([ 'message' => 'Invalid Signup Payload' ], 400 );
}
}
2022-06-24 13:15:14 +02:00
$account = ( new CreateAccount ( $request -> all (), $request -> getClientIp ())) -> handle ();
2020-09-06 11:38:10 +02:00
if ( ! ( $account instanceof Account )) {
2020-03-07 13:46:45 +01:00
return $account ;
2020-03-21 06:37:30 +01:00
}
2024-01-14 05:05:00 +01:00
2023-04-02 07:57:27 +02:00
MultiDB :: findAndSetDbByAccountKey ( $account -> key );
2020-03-07 13:46:45 +01:00
2023-08-27 00:41:27 +02:00
$cu = CompanyUser :: query () -> where ( 'user_id' , $account -> users () -> first () -> id );
2023-03-24 01:22:21 +01:00
$company_user = $cu -> first ();
2020-03-11 12:05:05 +01:00
2022-06-21 11:57:17 +02:00
$truth = app () -> make ( TruthSource :: class );
2023-03-24 01:22:21 +01:00
$truth -> setCompanyUser ( $company_user );
$truth -> setUser ( $company_user -> user );
$truth -> setCompany ( $company_user -> company );
$truth -> setCompanyToken ( $company_user -> tokens () -> where ( 'user_id' , $company_user -> user_id ) -> where ( 'company_id' , $company_user -> company_id ) -> first ());
2020-03-11 12:05:05 +01:00
2023-03-24 01:22:21 +01:00
return $this -> listResponse ( $cu );
2018-10-24 05:50:15 +02:00
}
2022-06-16 07:58:11 +02:00
public function update ( UpdateAccountRequest $request , Account $account )
{
2022-06-16 08:52:43 +02:00
$fi = new \FilesystemIterator ( public_path ( 'react' ), \FilesystemIterator :: SKIP_DOTS );
2022-06-21 11:57:17 +02:00
if ( iterator_count ( $fi ) < 30 ) {
2022-06-16 08:52:43 +02:00
return response () -> json ([ 'message' => 'React App Not Installed, Please install the React app before attempting to switch.' ], 400 );
2022-06-21 11:57:17 +02:00
}
2022-06-16 08:52:43 +02:00
2022-06-16 07:58:11 +02:00
$account -> fill ( $request -> all ());
$account -> save ();
$this -> entity_type = Account :: class ;
$this -> entity_transformer = AccountTransformer :: class ;
return $this -> itemResponse ( $account );
}
2018-10-24 05:50:15 +02:00
}