Skip to content

Commit

Permalink
Merge pull request vcian#5 from vcian/main
Browse files Browse the repository at this point in the history
Pull from main repository.
  • Loading branch information
ruchit288 authored Jun 25, 2023
2 parents 41ae94f + 3e347f4 commit 56b71b6
Show file tree
Hide file tree
Showing 17 changed files with 734 additions and 7 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
],
"require": {
"php": "^8.0",
"doctrine/dbal": "^3.6"
"doctrine/dbal": "^3.6",
"yajra/laravel-datatables-oracle": "^10.4"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand Down Expand Up @@ -68,4 +69,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
4 changes: 4 additions & 0 deletions config/db-auditor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

use Vcian\LaravelDBAuditor\Traits\DBConnection;

// config for Vcian/LaravelDbAuditor
return [

'db_name' => $this->getDatabaseName()

];
11 changes: 11 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Illuminate\Support\Facades\Route;
use Vcian\LaravelDBAuditor\Controllers\DisplayController;

Route::prefix('api')->group(function() {
Route::get('getAudit', [DisplayController::class, 'getAudit']);
Route::get('getTableData/{table}', [DisplayController::class, 'getTableData']);
Route::get('gettableconstraint/{table}', [DisplayController::class, 'getTableConstraint']);
});

6 changes: 6 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

use Illuminate\Support\Facades\Route;
use Vcian\LaravelDBAuditor\Controllers\DisplayController;

Route::get('laravel-db-auditor', [DisplayController::class, 'index']);
121 changes: 121 additions & 0 deletions src/Controllers/DisplayController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Vcian\LaravelDBAuditor\Controllers;

use Illuminate\Http\JsonResponse;
use Vcian\LaravelDBAuditor\Constants\Constant;
use Vcian\LaravelDBAuditor\Traits\Audit;
use Vcian\LaravelDBAuditor\Traits\Rules;

class DisplayController
{
use Rules, Audit;

/**
* Return view for audit
* @return view
*/
public function index()
{
$tables = $this->getTableList();
return view('DBAuditor::auditor.pages.audit', compact('tables'));
}

/**
* Get audit table list
* @return JsonResponse
*/
public function getAudit() : JsonResponse
{
$columnName = 'status';
$noConstraint = '<img src=' . asset("auditor/icon/close.svg") . ' alt="key" class="m-auto" />';
$constraint = '<img src=' . asset("auditor/icon/check.svg") . ' alt="key" class="m-auto" />';

$tableRules = array_map(function ($value) use ($columnName, $noConstraint, $constraint) {
if ($value[$columnName] === "") {
$value[$columnName] = $noConstraint;
} else {
$value[$columnName] = $constraint;
}
return $value;
}, $this->tablesRule());

return response()->json(
array(
"data" => $tableRules
)
);
}

/**
* Get table data
* @param string $tableName
* @return JsonResponse
*/
public function getTableData(string $tableName) : JsonResponse
{
return response()->json(array(
"data" => $this->tableRules($tableName)
));
}

/**
* Get Constraint list
* @param string $tableName
* @return JsonResponse
*/
public function getTableConstraint(string $tableName) : JsonResponse
{

$data = [
"fields" => $this->getFieldsDetails($tableName),
'constrain' => [
'primary' => $this->getConstraintField($tableName, Constant::CONSTRAINT_PRIMARY_KEY),
'unique' => $this->getConstraintField($tableName, Constant::CONSTRAINT_UNIQUE_KEY),
'foreign' => $this->getConstraintField($tableName, Constant::CONSTRAINT_FOREIGN_KEY),
'index' => $this->getConstraintField($tableName, Constant::CONSTRAINT_INDEX_KEY)
]
];

$response = [];
$greenKey = '<img src=' . asset("auditor/icon/green-key.svg") . ' alt="key" class="m-auto" />';
$grayKey = '<img src=' . asset("auditor/icon/gray-key.svg") . ' alt="key" class="m-auto" />';

foreach ($data['fields'] as $table) {

$primaryKey = $indexing = $uniqueKey = $foreignKey = "-";

if (in_array($table->COLUMN_NAME, $data['constrain']['primary'])) {
$primaryKey = $greenKey;
} else if (in_array($table->COLUMN_NAME, $data['constrain']['unique'])) {
$uniqueKey = $grayKey;
} else if (in_array($table->COLUMN_NAME, $data['constrain']['index'])) {
$indexing = $grayKey;
}

foreach ($data['constrain']['foreign'] as $foreign) {
if ($table->COLUMN_NAME === $foreign['column_name']) {
$foreignKeyToottip = '<div class="inline-flex">
<img src=' . asset("auditor/icon/gray-key.svg") . ' alt="key" class="mr-2">
<div class="relative flex flex-col items-center group">
<svg class="w-5 h-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-8-3a1 1 0 00-.867.5 1 1 0 11-1.731-1A3 3 0 0113 8a3.001 3.001 0 01-2 2.83V11a1 1 0 11-2 0v-1a1 1 0 011-1 1 1 0 100-2zm0 8a1 1 0 100-2 1 1 0 000 2z" clip-rule="evenodd" />
</svg>
<div class="absolute bottom-0 flex flex-col items-center hidden group-hover:flex">
<span class="relative z-10 p-2 text-xs leading-none text-white whitespace-no-wrap bg-black shadow-lg">Foreign Table Name : ' . $foreign['foreign_table_name'] . ' and Foreign Column Name : ' . $foreign['foreign_column_name'] . '</span>
<div class="w-3 h-3 -mt-2 rotate-45 bg-black"></div>
</div>
</div>
</div>';
$foreignKey = $foreignKeyToottip;
}
}

$response[] = ["column" => $table->COLUMN_NAME, "primaryKey" => $primaryKey, "indexing" => $indexing, "uniqueKey" => $uniqueKey, "foreignKey" => $foreignKey];
}

return response()->json(array(
"data" => $response
));
}
}
27 changes: 27 additions & 0 deletions src/Datatable/AuditDatatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Vcian\LaravelDBAuditor\Datatable;

use Yajra\DataTables\Services\DataTable;

class AuditDatatable extends DataTable
{
public function dataTable($query)
{
return datatables()
->eloquent($query)
->addColumn('action', function ($row) {
// Add custom action column
});
}

public function query()
{
// Define your data source query here
}

public function html()
{
// Define the HTML structure of your DataTable
}
}
7 changes: 7 additions & 0 deletions src/Providers/DBAuditorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@ class DBAuditorServiceProvider extends ServiceProvider
public function register(): void
{
$this->commands($this->commands);

}

/**
* Bootstrap services.
*/
public function boot(): void
{
$this->publishes([
__DIR__.'/../resource/images' => public_path('auditor/icon'),
], 'public');

$this->loadViewsFrom(__DIR__ . '/../views', 'DBAuditor');
$this->loadRoutesFrom(__DIR__.'/../../routes/web.php');
$this->loadRoutesFrom(__DIR__.'/../../routes/api.php');
$this->loadTranslationsFrom(__DIR__ . '/../Lang/', 'Lang');
}
}
15 changes: 10 additions & 5 deletions src/Traits/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getNoConstraintFields(string $tableName): array
$fields = Constant::ARRAY_DECLARATION;
try {
$fieldList = DB::select("SELECT * FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`= '" . env('DB_DATABASE') . "' AND `TABLE_NAME`= '" . $tableName . "' AND ( `COLUMN_KEY` = '' OR `COLUMN_KEY` = 'UNI' ) ");
WHERE `TABLE_SCHEMA`= '" . $this->getDatabaseName() . "' AND `TABLE_NAME`= '" . $tableName . "' AND ( `COLUMN_KEY` = '' OR `COLUMN_KEY` = 'UNI' ) ");

foreach ($fieldList as $field) {
if (!in_array($field->DATA_TYPE, Constant::RESTRICT_DATATYPE)) {
Expand Down Expand Up @@ -107,12 +107,17 @@ public function getConstraintField(string $tableName, string $input): array
return [];
}

$result = DB::select("SHOW KEYS FROM {$tableName} WHERE Key_name LIKE '%" . strtolower($input) . "%'");
if($input === Constant::CONSTRAINT_INDEX_KEY) {
$result = DB::select("SHOW INDEX FROM {$tableName}");
} else {
$result = DB::select("SHOW KEYS FROM {$tableName} WHERE Key_name LIKE '%" . strtolower($input) . "%'");
}


if ($input === Constant::CONSTRAINT_FOREIGN_KEY) {
return $this->getForeignKeyDetails($tableName);
}

if ($result) {
foreach ($result as $value) {
$constraintFields[] = $value->Column_name;
Expand All @@ -136,8 +141,8 @@ public function getForeignKeyDetails(string $tableName): array
$resultForeignKey = DB::select("SELECT i.TABLE_SCHEMA, i.TABLE_NAME, i.CONSTRAINT_TYPE,k.COLUMN_NAME, i.CONSTRAINT_NAME,
k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME FROM information_schema.TABLE_CONSTRAINTS i
LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME
WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY' AND i.TABLE_SCHEMA = '" . env('DB_DATABASE') . "' AND i.TABLE_NAME = '" . $tableName . "'");

WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY' AND i.TABLE_SCHEMA = '" . $this->getDatabaseName() . "' AND i.TABLE_NAME = '" . $tableName . "'");
if ($resultForeignKey) {
foreach ($resultForeignKey as $value) {
$foreignFieldDetails[] = [
Expand Down
Binary file added src/resource/images/bg-gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/resource/images/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/resource/images/check.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/resource/images/close.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/resource/images/gray-key.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/resource/images/green-key.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/resource/images/laraveldbauditor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 56b71b6

Please sign in to comment.