1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 20:52:56 +01:00

Force DB to be set only on company table

This commit is contained in:
David Bomba 2019-03-28 09:21:28 +11:00
parent 1c3f0c590c
commit caf653ed69
9 changed files with 45 additions and 10 deletions

View File

@ -14,13 +14,32 @@ class SetDb
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (config('ninja.db.multi_db_enabled'))
$error['error'] = ['message' => 'Database could not be set'];
if( $request->header('X-API-TOKEN') && ($user = CompanyToken::whereRaw("BINARY `token`= ?",[$request->header('X-API-TOKEN')])->first()->user ) && config('ninja.db.multi_db_enabled'))
{
MultiDB::setDB(auth()->user()->db);
if(! MultiDB::findAndSetDb($request->header('X-API-TOKEN')))
{
return response()->json(json_encode($error, JSON_PRETTY_PRINT) ,403);
}
}
else {
return response()->json(json_encode($error, JSON_PRETTY_PRINT) ,403);
}
return $next($request);
}
}

View File

@ -42,9 +42,9 @@ class CreateCompany
$company->name = $this->request['first_name'] . ' ' . $this->request['last_name'];
$company->account_id = $this->account->id;
$company->company_key = $this->createHash();
$company->db = config('database.default');
$company->ip = request()->ip();
$company->settings = CompanySettings::defaults();
$company->db = config('database.default');
$company->save();

View File

@ -47,7 +47,6 @@ class CreateUser
$user->password = bcrypt($this->request['password']);
$user->accepted_terms_version = config('ninja.terms_version');
$user->confirmation_code = $this->createDbHash(config('database.default'));
$user->db = config('database.default');
$user->fill($this->request);
$user->save();

View File

@ -2,6 +2,7 @@
namespace App\Libraries;
use App\Models\CompanyToken;
use App\Models\User;
/**
@ -74,6 +75,23 @@ class MultiDB
return false;
}
public static function findAndSetDb($token) :bool
{
foreach (self::$dbs as $db)
{
if($ct = CompanyToken::on($db)->whereRaw("BINARY `token`= ?", [$token])->first())
{
self::setDb($ct->company->db);
return true;
}
}
return false;
}
/**
* @param $database

View File

@ -27,13 +27,14 @@ class SendVerificationNotification
* @return void
*/
public function handle($event)
{
//send confirmation email using $event->user
{//todo handle the change of DB locaiton to Company Token table
/*send confirmation email using $event->user
MultiDB::setDB($event->user->db);
Mail::to($event->user->email)
//->cc('')
//->bcc('')
->queue(new VerifyUser($event->user));
*/
}
}

View File

@ -92,7 +92,6 @@ class User extends Authenticatable implements MustVerifyEmail
return $ct->company;
// return $this->companies()->where('company_id', $this->getCurrentCompanyId())->first();
}
/**

View File

@ -22,6 +22,5 @@ $factory->define(App\Models\User::class, function (Faker $faker) {
'email_verified_at' => now(),
'password' => bcrypt(config('ninja.testvars.password')), // secret
'remember_token' => str_random(10),
'db' => config('database.default')
];
});

View File

@ -204,7 +204,6 @@ class CreateUsersTable extends Migration
$table->unsignedInteger('avatar_width')->nullable();
$table->unsignedInteger('avatar_height')->nullable();
$table->unsignedInteger('avatar_size')->nullable();
$table->string('db', 100);
$table->text('signature');
$table->string('password');
$table->rememberToken();
@ -224,6 +223,7 @@ class CreateUsersTable extends Migration
$table->unsignedInteger('user_id')->index();
$table->string('token')->nullable();
$table->string('name')->nullable();
$table->string('db', 100)->nullable();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');

View File

@ -25,7 +25,7 @@ Route::group(['middleware' => ['api_secret_check']], function () {
});
Route::group(['middleware' => ['api_secret_check','token_auth']], function () {
Route::group(['middleware' => ['db','api_secret_check','token_auth']], function () {
Route::resource('clients', 'ClientController'); // name = (clients. index / create / show / update / destroy / edit