Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Develop into Master #160

Merged
merged 7 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 17 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ MS_HUB_BASE_URI="https://api-dev-hub.nave.dev"
MS_HUB_PREFIX="/api"
```

## Change permission and role model from spatie/laravel-permissions

You should change the default spatie/laravel-permissions models to ours, as we have some substantial changes to the use of Role and Permission.

```php
// config/permission.php

return [
'models' = [
'permission' => \BildVitta\Hub\Entities\HubPermission::class,
'role' => \BildVitta\Hub\Entities\HubRole::class,
]
];
```

If you already have a change to these models, just extend our classes to have the correct functionalities.

## Add Trait on User Model

And remember to add the `BildVitta\Hub\Traits\User\HasCompanyLinks` Trait in the Users model.
Expand Down Expand Up @@ -200,48 +217,6 @@ return (new RealEstateDevelopmentResource('index', $query->get()))->count($count

Remembering that the scope name has to be permission, if not, it doesn't work <3

## Logging

To generate the log, just put the settings in the `config/logging.php` file

```php
[


'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'stderr', 'satelliteX9'], # Add satelliteX9 on channels
'ignore_exceptions' => false,
],

...

'satelliteX9' => [
'driver' => 'custom',
'via' => BildVitta\Hub\Logging\SatelliteX9Logger::class,
'with' => [
'app_url' => env('SATELLITE_X9_URL', ''),
'app_endpoint' => env('SATELLITE_X9_ENDPOINT', ''),
'app_module' => env('SATELLITE_X9_MODULE', '')
]
]
]
]
```

After the configuration, just use the normal Laravel Log. Passing the entity, entity uuid and action attributes as arguments.

```php
use Log;

Log::info(__('Created user'), [
'entity' => 'user',
'entity_uuid' => $user->uuid,
'action' => 'store'
]);
```

## Testing

coming soon...
Expand Down
8 changes: 1 addition & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@
],
"require": {
"php": "^7.4|^8.0|^8.1|^8.2|^8.3",
"illuminate/cache": "^7.0|^8.49|^9.0|^10.0",
"illuminate/config": "^7.0|^8.50|^9.0|^10.0",
"illuminate/contracts": "^7.0|^8.0|^9.0|^10.0",
"illuminate/log": "^7.0|^8.65|^9.0|^10.0",
"illuminate/routing": "^7.0|^8.49|^9.0|^10.0",
"illuminate/support": "^7.0|^8.0|^9.0|^10.0",
"illuminate/view": "^7.0|^8.49|^9.0|^10.0",
"illuminate/contracts": "^7.0|^8.0|^9.0|^10.0|^11.0",
"spatie/laravel-permission": "^4.0|^5.0|^6.0"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$role = app(config('permission.models.role'));
$hubCompany = app(config('hub.model_company'));

Schema::table($role->getTable(), function (Blueprint $table) use ($hubCompany) {
$table->uuid('uuid')->unique();
$table->string('description')->nullable();
$table->foreignId('hub_company_id')->constrained($hubCompany->getTable())->cascadeOnDelete()->index('roles_hub_company_id_foreign');
$table->boolean('has_all_real_estate_developments')->default(false);
$table->boolean('is_post_construction')->default(false);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$role = app(config('permission.models.role'));

Schema::table($role->getTable(), function (Blueprint $table) {
$table->dropColumn('uuid');
$table->dropColumn('description');
$table->dropForeign(['hub_company_id']);
$table->dropColumn('hub_company_id');
$table->dropColumn('has_all_real_estate_developments');
$table->dropColumn('is_post_construction');
});
}
};
43 changes: 43 additions & 0 deletions src/Console/CleanPermissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace BildVitta\Hub\Console;

use DB;
use Illuminate\Console\Command;

class CleanPermissions extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'hub:clean-permissions';

/**
* The console command description.
*
* @var string|null
*/
protected $description = 'Clean permissions from the database';

public function handle()
{
$this->info('Cleaning permissions...');

$this->info('Cleaning permissions from the database...');

$this->cleanPermissions();

$this->info('Permissions cleaned');
}

public function cleanPermissions()
{
DB::table('model_has_permissions')->delete();

DB::table('model_has_roles')->whereNotIn('model_type', [
'model_type' => 'BildVitta\\Hub\\Entities\\HubUserCompany',
])->delete();
}
}
8 changes: 8 additions & 0 deletions src/Entities/HubPermission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace BildVitta\Hub\Entities;

class HubPermission extends \Spatie\Permission\Models\Permission
{
//
}
38 changes: 38 additions & 0 deletions src/Entities/HubRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace BildVitta\Hub\Entities;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class HubRole extends \Spatie\Permission\Models\Role
{
protected $fillable = [
'uuid',
'name',
'description',
'hub_company_id',
'has_all_real_estate_developments',
'is_post_construction',
];

protected $casts = [
'has_all_real_estate_developments' => 'boolean',
'is_post_construction' => 'boolean',
];

public function company(): BelongsTo
{
return $this->belongsTo(HubCompany::class, 'hub_company_id', 'id');
}

public function positions(): BelongsToMany
{
return $this->morphedByMany(
HubPosition::class,
'model',
config('permission.table_names.model_has_roles'),
config('permission.column_names.model_morph_key'),
);
}
}
3 changes: 2 additions & 1 deletion src/HubServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace BildVitta\Hub;

use BildVitta\Hub\Console\CleanPermissions;
use BildVitta\Hub\Console\InstallHub;
use BildVitta\Hub\Middleware\AuthenticateCheckHubMiddleware;
use BildVitta\Hub\Middleware\AuthenticateHubMiddleware;
Expand Down Expand Up @@ -44,7 +45,7 @@ public function boot(): void

$this->loadRoutesFrom(__DIR__.'/../routes/api.php');
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
$this->commands([InstallHub::class]);
$this->commands([InstallHub::class, CleanPermissions::class]);

$router = $this->app->make(Router::class);
$router->aliasMiddleware('hub.auth', AuthenticateHubMiddleware::class);
Expand Down
20 changes: 20 additions & 0 deletions src/Scopes/Companies/PublicListScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Scopes\Companies;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class PublicListScope implements Scope
{
public function __construct(
protected bool $public_list
) {
}

public function apply(Builder $builder, Model $model)
{
$builder->whereIn('public_list', $this->public_list);
}
}
8 changes: 4 additions & 4 deletions src/Services/UserCompanyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function clearCacheByUserCompany($userCompany)
* @param string $parentUserUuid
* @param string $companyUuid
* @param bool $allBelow
* @param int $onlyPositionOrder null|0|1|2
* @param int $onlyPositionOrder null|0|1|2
* @param array $attributes
* @return User
*/
Expand Down Expand Up @@ -235,7 +235,7 @@ private static function getAllUserParentsByUserCompanyChildren($userCompanyChild
* Get users by companyUuid and positionUuid
*
* @param string $companyUuid
* @param int $positionOrder 0|1|2
* @param int $positionOrder 0|1|2
* @param array $filter
* @param array $attributes
* @return User
Expand Down Expand Up @@ -350,7 +350,7 @@ private static function sortPositions($positions)
*
* @param string $companyUuid
* @param string $userUuid
* @param int $positionOrder 0|1|2
* @param int $positionOrder 0|1|2
* @return bool
*/
public static function checkPositionUser($companyUuid, $userUuid, $positionOrder)
Expand Down Expand Up @@ -393,7 +393,7 @@ public static function checkPositionUser($companyUuid, $userUuid, $positionOrder
*
* @param string $userUuid
* @param string $companyUuid
* @param int $onlyPositionOrder null|0|1
* @param int $onlyPositionOrder null|0|1
* @param array $attributes
* @return User
*/
Expand Down
31 changes: 26 additions & 5 deletions src/Traits/User/HasCompanyLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Collection;
use Spatie\Permission\Contracts\Permission;
use Spatie\Permission\Contracts\Role;
use Spatie\Permission\Traits\HasRoles;

trait HasCompanyLinks
Expand Down Expand Up @@ -61,11 +63,6 @@ public function company_link()
return $this->company();
}

public function getAllPermissions(): Collection
{
return $this->user_company?->getAllPermissions() ?? collect([]);
}

public function realEstateDevelopments(): Attribute
{
return Attribute::get(function () {
Expand All @@ -92,4 +89,28 @@ public function hasMainLinkedCompanies(): bool
{
return $this->company_links()->whereNull('main_company_id')->exists();
}

// Overwrite Spatie\Permission\Traits\HasRoles
public function getAllPermissions(): Collection
{
return $this->user_company?->getAllPermissions() ?? collect([]);
}

public function getRoleNames(): Collection
{
$this->loadMissing('user_company');

return $this->user_company?->roles->pluck('name') ?? collect([]);
}

public function getPermissionsViaRoles(): Collection
{
if (is_a($this, Role::class) || is_a($this, Permission::class)) {
return collect();
}

return $this->loadMissing('user_company', 'user_company.roles')
->user_company?->roles->flatMap(fn ($role) => $role->permissions)
->sort()->values() ?? collect([]);
}
}