Skip to content

Commit d189215

Browse files
committed
refactor: Consolidate dbproxy and user models
With this refactor we migrate User model to the root micropowermanager database. Following, we consolidate database proxy model by resolving company id from user table directly from database proxy services. Consideration for this refactor is that, admin users can see every user on the users table from the user management admin page. This might have the effect or leaking admin accounts to other admin user.
1 parent 50fa75d commit d189215

File tree

12 files changed

+109
-58
lines changed

12 files changed

+109
-58
lines changed

src/backend/app/Http/Controllers/AgentWebController.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ public function store(CreateAgentRequest $request): ApiResource {
5050
'connection' => ' ', // TODO: solve this. //auth('api')->user()->company->database->database_name
5151
];
5252
$companyId = auth('api')->payload()->get('companyId');
53-
$companyDatabase = CompanyDatabase::query()->where('company_id', $companyId)->firstOrFail();
54-
$databaseProxyData = [
55-
'email' => $request['email'],
56-
'fk_company_id' => $companyId,
57-
'fk_company_database_id' => $companyDatabase->getId(),
58-
];
59-
$this->databaseProxyService->create($databaseProxyData);
53+
// $companyDatabase = CompanyDatabase::query()->where('company_id', $companyId)->firstOrFail();
54+
// $userData = [
55+
// 'email' => $request['email'],
56+
// 'company_id' => $companyId,
57+
// 'password' => $request['password'],
58+
// 'fk_company_database_id' => $companyDatabase->getId(),
59+
// ];
60+
// $this->databaseProxyService->create($databaseProxyData);
6061

6162
return ApiResource::make($this->agentService->create(
6263
$agentData,

src/backend/app/Http/Controllers/Reports.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use App\Models\City;
77
use App\Models\ConnectionGroup;
88
use App\Models\ConnectionType;
9-
use App\Models\DatabaseProxy;
109
use App\Models\Meter\MeterParameter;
1110
use App\Models\PaymentHistory;
1211
use App\Models\Report;
@@ -580,8 +579,7 @@ static function ($q) {
580579
$writer = new Xlsx($this->spreadsheet);
581580
$dirPath = storage_path('./'.$reportType);
582581
$user = User::query()->first();
583-
$databaseProxy = app()->make(DatabaseProxy::class);
584-
$companyId = $databaseProxy->findByEmail($user->email)->getCompanyId();
582+
$companyId = $user->getCompanyId();
585583

586584
if (!file_exists($dirPath) && !mkdir($dirPath, 0774, true) && !is_dir($dirPath)) {
587585
throw new \RuntimeException(sprintf('Directory "%s" was not created', $dirPath));

src/backend/app/Http/Controllers/UserController.php

-9
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
use App\Http\Resources\ApiResource;
77
use App\Models\User;
88
use App\Services\CompanyDatabaseService;
9-
use App\Services\DatabaseProxyService;
109
use App\Services\UserService;
1110
use Illuminate\Http\Request;
1211

1312
class UserController extends Controller {
1413
public function __construct(
1514
private UserService $userService,
16-
private DatabaseProxyService $databaseProxyService,
1715
private CompanyDatabaseService $companyDatabaseService,
1816
) {}
1917

@@ -25,13 +23,6 @@ public function index(Request $request): ApiResource {
2523

2624
public function store(CreateAdminRequest $request) {
2725
$user = $this->userService->create($request->only(['name', 'password', 'email']));
28-
$companyDatabase = $this->companyDatabaseService->findByCompanyId($user->getCompanyId());
29-
$databaseProxyData = [
30-
'email' => $user->getEmail(),
31-
'fk_company_id' => $user->getCompanyId(),
32-
'fk_company_database_id' => $companyDatabase->getId(),
33-
];
34-
$this->databaseProxyService->create($databaseProxyData);
3526

3627
return ApiResource::make($user->toArray());
3728
}

src/backend/app/Http/Middleware/UserDefaultDatabaseConnectionMiddleware.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ private function handleApiRequest(Request $request, \Closure $next) {
7070

7171
// webclient login
7272
if ($request->path() === 'api/auth/login' || $request->path() === 'api/app/login') {
73-
$databaseProxy = $this->databaseProxyManager->findByEmail($request->input('email'));
74-
$companyId = $databaseProxy->getCompanyId();
73+
$user = $this->databaseProxyManager->findByEmail($request->input('email'));
74+
$companyId = $user->getCompanyId();
7575
} elseif ($this->isAgentApp($request->path()) && Str::contains($request->path(), 'login')) { // agent app login
76-
$databaseProxy = $this->databaseProxyManager->findByEmail($request->input('email'));
77-
$companyId = $databaseProxy->getCompanyId();
76+
$user = $this->databaseProxyManager->findByEmail($request->input('email'));
77+
$companyId = $user->getCompanyId();
7878
} elseif ($this->isAgentApp($request->path())) { // agent app authenticated user requests
7979
$companyId = auth('agent_api')->payload()->get('companyId');
8080
if (!is_numeric($companyId)) {

src/backend/app/Models/User.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class User extends Authenticatable implements JWTSubject {
3131
public const COL_COMPANY_ID = 'company_id';
3232

3333
public function __construct(array $attributes = []) {
34-
$this->setConnection('shard');
34+
// $this->setConnection('shard');
3535

3636
parent::__construct($attributes);
3737
}
@@ -118,4 +118,8 @@ public function getEmail(): string {
118118
public function relationTicketUser(): HasOne {
119119
return $this->hasOne(TicketUser::class, TicketUser::COL_USER_ID, User::COL_ID);
120120
}
121+
122+
public function findByEmail(string $email): ?User {
123+
return self::where('email', $email)->first();
124+
}
121125
}

src/backend/app/Services/AbstractDashboardCacheDataService.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace App\Services;
44

5-
use App\Models\DatabaseProxy;
65
use App\Models\User;
76
use Illuminate\Support\Facades\Cache;
87

@@ -31,8 +30,8 @@ public function getDataById($id) {
3130

3231
protected function cacheKeyGenerator(): string {
3332
$user = User::query()->first();
34-
$databaseProxy = app()->make(DatabaseProxy::class);
35-
$companyId = $databaseProxy->findByEmail($user->email)->getCompanyId();
33+
34+
$companyId = $user->getCompanyId();
3635

3736
return $this->cacheDataKey.'-'.$companyId;
3837
}

src/backend/app/Services/DatabaseProxyService.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@
22

33
namespace App\Services;
44

5-
use App\Models\DatabaseProxy;
5+
use App\Models\User;
66
use App\Services\Interfaces\IBaseService;
77
use Illuminate\Database\Eloquent\Collection;
88

99
/**
1010
* @implements IBaseService<DatabaseProxy>
1111
*/
1212
class DatabaseProxyService implements IBaseService {
13-
public function __construct(private DatabaseProxy $databaseProxy) {}
13+
public function __construct(private User $user) {}
1414

15-
public function getById($id): DatabaseProxy {
15+
public function getById($id): User {
1616
throw new \Exception('Method getById() not yet implemented.');
1717

18-
return new DatabaseProxy();
18+
return new User();
1919
}
2020

21-
public function create(array $databaseProxyData): DatabaseProxy {
22-
return $this->databaseProxy->newQuery()->create($databaseProxyData);
21+
public function create(array $userData): User {
22+
// return $this->user->newQuery()->create($userData);
23+
throw new \Exception('Method create() should not be used directly ');
2324
}
2425

25-
public function update($model, array $data): DatabaseProxy {
26+
public function update($model, array $data): User {
2627
throw new \Exception('Method update() not yet implemented.');
2728
}
2829

src/backend/app/modules/DatabaseProxy/DatabaseProxyManagerService.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
namespace MPM\DatabaseProxy;
66

77
use App\Models\CompanyDatabase;
8-
use App\Models\DatabaseProxy;
8+
use App\Models\User;
99
use App\Utils\DemoCompany;
1010
use Illuminate\Database\DatabaseManager;
1111
use Illuminate\Database\Eloquent\Builder;
1212

1313
class DatabaseProxyManagerService {
1414
public function __construct(
15-
private DatabaseProxy $databaseProxy,
15+
private User $user,
1616
private DatabaseManager $databaseManager,
1717
private CompanyDatabase $companyDatabase,
1818
) {}
1919

20-
public function findByEmail(string $email): DatabaseProxy {
21-
return $this->databaseProxy->findByEmail($email);
20+
public function findByEmail(string $email): User {
21+
return $this->user->findByEmail($email);
2222
}
2323

2424
public function runForCompany(int $companyId, callable $callable) {

src/backend/app/modules/User/UserListener.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
use App\Helpers\MailHelperInterface;
88
use App\Services\CompanyDatabaseService;
99
use App\Services\CompanyService;
10-
use App\Services\DatabaseProxyService;
10+
// use App\Services\DatabaseProxyService;
1111
use Inensus\Ticket\Services\TicketUserService;
1212
use MPM\User\Events\UserCreatedEvent;
1313

1414
class UserListener {
1515
public function __construct(
16-
private DatabaseProxyService $databaseProxyService,
16+
// private DatabaseProxyService $databaseProxyService,
1717
private CompanyDatabaseService $companyDatabaseService,
1818
private TicketUserService $ticketUserService,
1919
private CompanyService $companyService,
@@ -27,17 +27,17 @@ public function handle($event): void {
2727
}
2828

2929
public function handleUserCreatedEvent(UserCreatedEvent $event): void {
30-
if ($event->shouldSyncUser) {
31-
$companyDatabase = $this->companyDatabaseService->findByCompanyId($event->user->getCompanyId());
30+
// if ($event->shouldSyncUser) {
31+
// $companyDatabase = $this->companyDatabaseService->findByCompanyId($event->user->getCompanyId());
3232

33-
$databaseProxyData = [
34-
'email' => $event->user->getEmail(),
35-
'fk_company_id' => $event->user->getCompanyId(),
36-
'fk_company_database_id' => $companyDatabase->getId(),
37-
];
33+
// $databaseProxyData = [
34+
// 'email' => $event->user->getEmail(),
35+
// 'fk_company_id' => $event->user->getCompanyId(),
36+
// 'fk_company_database_id' => $companyDatabase->getId(),
37+
// ];
3838

39-
$this->databaseProxyService->create($databaseProxyData);
40-
}
39+
// $this->databaseProxyService->create($databaseProxyData);
40+
// }
4141

4242
$company = $this->companyService->getById($event->user->getCompanyId());
4343
$this->ticketUserService->findOrCreateByUser($event->user);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration {
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up() {
14+
Schema::create('users', function (Blueprint $table) {
15+
$table->increments('id');
16+
$table->string('name');
17+
$table->integer('company_id')->unsigned();
18+
$table->string('email')->unique();
19+
$table->string('password');
20+
$table->rememberToken();
21+
$table->timestamps();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down() {
31+
Schema::dropIfExists('users');
32+
}
33+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration {
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up() {
14+
Schema::connection('shard')->dropIfExists('users');
15+
}
16+
17+
/**
18+
* Reverse the migrations.
19+
*
20+
* @return void
21+
*/
22+
public function down() {
23+
Schema::connection('shard')->create('users', function (Blueprint $table) {
24+
$table->increments('id');
25+
$table->string('name');
26+
$table->integer('company_id')->unsigned();
27+
$table->string('email')->unique();
28+
$table->string('password');
29+
$table->rememberToken();
30+
$table->timestamps();
31+
});
32+
}
33+
};

src/backend/packages/inensus/swifta-payment-provider/src/Console/Commands/InstallPackage.php

-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use App\Models\User;
66
use App\Services\CompanyDatabaseService;
77
use App\Services\CompanyService;
8-
use App\Services\DatabaseProxyService;
98
use App\Services\UserService;
109
use Carbon\Carbon;
1110
use Illuminate\Console\Command;
@@ -21,7 +20,6 @@ public function __construct(
2120
private SwiftaAuthentication $authentication,
2221
private CompanyService $companyService,
2322
private CompanyDatabaseService $companyDatabaseService,
24-
private DatabaseProxyService $databaseProxyService,
2523
) {
2624
parent::__construct();
2725
}
@@ -72,13 +70,6 @@ private function generateAuthenticationToken() {
7270
'email' => $company->getName().'-swifta-user-'.Carbon::now()->timestamp,
7371
'company_id' => $companyId,
7472
]);
75-
$companyDatabase = $this->companyDatabaseService->getById($companyId);
76-
$databaseProxyData = [
77-
'email' => $user->getEmail(),
78-
'fk_company_id' => $user->getCompanyId(),
79-
'fk_company_database_id' => $companyDatabase->getId(),
80-
];
81-
$this->databaseProxyService->create($databaseProxyData);
8273
$customClaims = ['usr' => 'swifta-token', 'exp' => Carbon::now()->addYears(3)->timestamp];
8374
$token = JWTAuth::customClaims($customClaims)->fromUser($user);
8475
$payload = JWTAuth::setToken($token)->getPayload();

0 commit comments

Comments
 (0)