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

Change permission and role model to custom models #159

Merged
merged 2 commits into from
May 23, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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('has_all_real_estate_developments');
$table->dropColumn('is_post_construction');
});
}
};
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'),
);
}
}
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