Skip to content

Commit be9ce9b

Browse files
Merge pull request #78 from mostafamaklad/v1.9
V1.10.0
2 parents 277238f + f2615ec commit be9ce9b

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,23 @@ In Laravel 5.5 the service provider will automatically get registered. In older
8181
];
8282
```
8383

84+
You can publish [the migration](database/migrations/create_permission_collections.php.stub) with:
85+
86+
```bash
87+
php artisan vendor:publish --provider="Maklad\Permission\PermissionServiceProvider" --tag="migrations"
88+
```
89+
90+
```bash
91+
php artisan migrate
92+
```
93+
8494
You can publish the config file with:
8595

8696
```bash
8797
php artisan vendor:publish --provider="Maklad\Permission\PermissionServiceProvider" --tag="config"
8898
```
8999

90-
When published, the [`config/permission.php`](https://github.com/mostafamaklad/laravel-permission-mongodb/blob/master/config/permission.php) config file contains:
100+
When published, the [`config/permission.php`](config/permission.php) config file contains:
91101

92102
```php
93103
return [
@@ -175,6 +185,7 @@ Copy the required files:
175185

176186
```bash
177187
cp vendor/mostafamaklad/laravel-permission-mongodb/config/permission.php config/permission.php
188+
cp vendor/mostafamaklad/laravel-permission-mongodb/database/migrations/create_permission_collections.php.stub database/migrations/2018_01_01_000000_create_permission_collections.php
178189
```
179190

180191
You will also need to create another configuration file at `config/auth.php`. Get it on the Laravel repository or just run the following command:
@@ -200,6 +211,12 @@ $app->configure('permission');
200211
$app->register(Maklad\Permission\PermissionServiceProvider::class);
201212
```
202213

214+
Now, run your migrations:
215+
216+
```bash
217+
php artisan migrate
218+
```
219+
203220
## Usage
204221

205222
First, add the `Maklad\Permission\Traits\HasRoles` trait to your `User` model(s):
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Support\Facades\Schema;
5+
use Jenssegers\Mongodb\Schema\Blueprint;
6+
7+
class CreatePermissionCollections extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
$collectionNames = config('permission.collection_names');
17+
18+
Schema::table($collectionNames['roles'], function (Blueprint $collection) {
19+
$collection->unique(['name', 'guard_name']);
20+
});
21+
22+
Schema::table($collectionNames['permissions'], function (Blueprint $collection) {
23+
$collection->unique(['name', 'guard_name']);
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
$collectionNames = config('permission.collection_names');
35+
36+
Schema::drop($collectionNames['roles']);
37+
Schema::drop($collectionNames['permissions']);
38+
}
39+
}

src/PermissionServiceProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public function boot(PermissionRegistrar $permissionLoader)
2121
$this->publishes([
2222
__DIR__ . '/../config/permission.php' => $this->app->configPath() . '/permission.php',
2323
], 'config');
24+
25+
if (!class_exists('CreatePermissionTables')) {
26+
$timestamp = date('Y_m_d_His');
27+
$mFilePath = $this->app->databasePath() . "/migrations/{$timestamp}_create_permission_collections.php";
28+
$this->publishes([
29+
__DIR__ . '/../database/migrations/create_permission_collections.php.stub' => $mFilePath,
30+
], 'migrations');
31+
}
2432
}
2533

2634
if ($this->app->runningInConsole()) {

tests/TestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ protected function getEnvironmentSetUp($app)
113113
*/
114114
protected function setUpDatabase($app)
115115
{
116+
include_once __DIR__.'/../database/migrations/create_permission_collections.php.stub';
117+
(new \CreatePermissionCollections())->up();
118+
116119
User::create(['email' => 'test@user.com']);
117120
Admin::create(['email' => 'admin@user.com']);
118121
$app[Role::class]->create(['name' => 'testRole']);

0 commit comments

Comments
 (0)