Skip to content

Commit d589f4b

Browse files
authored
Version 2
- Add default_migrations boolean to configure whether to run default migrations or not - Reference models via the config - Split migration files - Fix tests - Update readme - Add upgrade guide - Optimise role check
1 parent 85cc4af commit d589f4b

File tree

9 files changed

+99
-21
lines changed

9 files changed

+99
-21
lines changed

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ $ composer require codinglabsau/laravel-roles
1515
```
1616

1717
## Usage
18+
19+
### Publish Assets
20+
```
21+
php artisan vendor:publish --provider="Codinglabs\Roles\RolesServiceProvider"
22+
```
23+
##### Or Publish Specific Assets
24+
```
25+
php artisan vendor:publish --tag="roles-config"
26+
php artisan vendor:publish --tag="roles-migrations"
27+
```
28+
### Migrations
29+
You will need to ensure that you have published the migrations and run `php artisan migrate`.
1830
### Add the trait
1931
Add the `HasRoles` trait to your user model:
2032

@@ -139,9 +151,31 @@ Vue.mixin({
139151
}
140152
})
141153
```
142-
```vue
143-
// SomeComponent.vue
154+
```html
155+
<!-- SomeComponent.vue -->
144156
<div v-if="hasRole('manager')">I am a manager</div>
157+
```
158+
## Configuration
159+
160+
```
161+
<?php
162+
163+
return [
164+
/*
165+
|--------------------------------------------------------------------------
166+
| Models
167+
|--------------------------------------------------------------------------
168+
|
169+
| You may replace the models here with your own if you need to use a custom
170+
| model.
171+
|
172+
*/
173+
174+
'models' => [
175+
'role' => \Codinglabs\Roles\Role::class
176+
]
177+
];
178+
145179
```
146180

147181
## Contributing

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"homepage": "https://github.com/codinglabsau/laravel-roles",
1313
"keywords": ["Laravel", "Roles"],
1414
"require": {
15-
"php" : "^7.2",
15+
"php" : "^7.3",
1616
"illuminate/support": "^6.0|^7.0|^8.0"
1717
},
1818
"require-dev": {

config/roles.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
<?php
22

33
return [
4-
//
5-
];
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Models
7+
|--------------------------------------------------------------------------
8+
|
9+
| You may replace the models here with your own if you need to use a custom
10+
| model.
11+
|
12+
*/
13+
14+
'models' => [
15+
'role' => \Codinglabs\Roles\Role::class
16+
]
17+
];
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateRolesTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('roles', function (Blueprint $table) {
12+
$table->increments('id');
13+
$table->string('name');
14+
$table->timestamps();
15+
});
16+
}
17+
18+
public function down()
19+
{
20+
Schema::dropIfExists('roles');
21+
}
22+
}

database/migrations/2020_03_16_055658_create_roles_table.php renamed to database/migrations/2020_03_16_055657_create_role_user_table.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
class CreateRolesTable extends Migration
7+
class CreateRoleUserTable extends Migration
88
{
99
public function up()
1010
{
11-
Schema::create('roles', function (Blueprint $table) {
12-
$table->increments('id');
13-
$table->string('name');
14-
$table->timestamps();
15-
});
16-
1711
Schema::create('role_user', function (Blueprint $table) {
1812
$table->increments('id');
1913
$table->integer('role_id')->unsigned();
@@ -27,6 +21,5 @@ public function up()
2721
public function down()
2822
{
2923
Schema::dropIfExists('role_user');
30-
Schema::dropIfExists('roles');
3124
}
3225
}

src/HasRoles.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ trait HasRoles
88
{
99
public function roles(): BelongsToMany
1010
{
11-
return $this->belongsToMany(Role::class)
11+
return $this->belongsToMany(config('roles.models.role'))
1212
->withTimestamps();
1313
}
1414

1515
public function hasRole($role): bool
1616
{
1717
if (is_array($role)) {
18-
return $this->roles()->whereIn('name', $role)->exists();
18+
return $this->roles->whereIn('name', $role)->isNotEmpty();
1919
}
2020

21-
return $this->roles()->where('name', $role)->exists();
21+
return $this->roles->where('name', $role)->isNotEmpty();
2222
}
2323
}

src/RolesServiceProvider.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ public function boot()
1111
{
1212
$this->publishes([
1313
__DIR__.'/../config/roles.php' => config_path('roles.php'),
14-
]);
14+
], 'roles-config');
1515

1616
$this->publishes([
1717
__DIR__.'/../database/migrations/' => database_path('migrations')
18-
], 'migrations');
19-
20-
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
18+
], 'roles-migrations');
2119

2220
Gate::define('role', function ($user, ...$roles) {
2321
return $user->hasRole($roles);

tests/RolesTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Codinglabs\Roles\RolesServiceProvider;
1010
use Illuminate\Foundation\Auth\User as AuthUser;
1111

12-
class MigrateWithLaravelTest extends TestCase
12+
class RolesTest extends TestCase
1313
{
1414
/** @var User */
1515
protected $user;
@@ -18,6 +18,7 @@ protected function setUp(): void
1818
{
1919
parent::setUp();
2020

21+
$this->artisan('vendor:publish', ['--tag' => 'roles-migrations'])->run();
2122
$this->artisan('migrate', ['--database' => 'testbench'])->run();
2223
$this->loadLaravelMigrations(['--database' => 'testbench']);
2324

upgrade.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Upgrade Guide v1 to v2
2+
3+
### Migrations
4+
###### Impact: High
5+
Migrations will no longer be automatically ran by the packages service provider, the migrations will now need to be published using this command:
6+
```
7+
php artisan publish --tag="roles-migrations"
8+
```
9+
If you haven't already migrated the tables then run `php artisan migrate` to ensure the tables are created.
10+
11+
### Configuration
12+
###### Impact: Low
13+
The config file has been updated to now include the option to define what class is used for the Role model. So if you ever need to customise the role model you can make your own and reference it in the config.
14+
15+
If you wish to customise the model used for roles then you must publish the config using this command:
16+
```
17+
php artisan publish --tag="roles-config"
18+
```

0 commit comments

Comments
 (0)