Skip to content

Commit

Permalink
Merge pull request #188 from gabriel-caruso/patch-1
Browse files Browse the repository at this point in the history
Fix #180
  • Loading branch information
santigarcor committed Aug 27, 2017
2 parents 079b673 + 91e5d79 commit c67d1c9
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions src/views/generators/seeder.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php echo '<?php' ?>


use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

Expand All @@ -15,90 +14,94 @@ public function run()
{
$this->command->info('Truncating User, Role and Permission tables');
$this->truncateLaratrustTables();

$config = config('laratrust_seeder.role_structure');
$userPermission = config('laratrust_seeder.permission_structure');
$mapPermission = collect(config('laratrust_seeder.permissions_map'));

foreach ($config as $key => $modules) {

// Create a new role
$role = \{{ $role }}::create([
'name' => $key,
'display_name' => ucwords(str_replace("_", " ", $key)),
'description' => ucwords(str_replace("_", " ", $key))
'display_name' => ucwords(str_replace('_', ' ', $key)),
'description' => ucwords(str_replace('_', ' ', $key))
]);
$permissions = [];

$this->command->info('Creating Role '. strtoupper($key));

// Reading role permission modules
foreach ($modules as $module => $value) {
$permissions = explode(',', $value);

foreach ($permissions as $p => $perm) {
foreach (explode(',', $value) as $p => $perm) {

$permissionValue = $mapPermission->get($perm);

$permission = \{{ $permission }}::firstOrCreate([
$permissions[] = \{{ $permission }}::firstOrCreate([
'name' => $permissionValue . '-' . $module,
'display_name' => ucfirst($permissionValue) . ' ' . ucfirst($module),
'description' => ucfirst($permissionValue) . ' ' . ucfirst($module),
]);
])->id;

$this->command->info('Creating Permission to '.$permissionValue.' for '. $module);

if (!$role->hasPermission($permission->name)) {
$role->attachPermission($permission);
} else {
$this->command->info($key . ': ' . $p . ' ' . $permissionValue . ' already exist');
}
}
}

// Attach all permissions to the role
$role->permissions()->sync($permissions);

$this->command->info("Creating '{$key}' user");

// Create default user for each role
$user = \{{ $user }}::create([
'name' => ucwords(str_replace("_", " ", $key)),
'name' => ucwords(str_replace('_', ' ', $key)),
'email' => $key.'@app.com',
'password' => bcrypt('password')
]);

$user->attachRole($role);
}

// creating user with permissions
// Creating user with permissions
if (!empty($userPermission)) {

foreach ($userPermission as $key => $modules) {

foreach ($modules as $module => $value) {
$permissions = explode(',', $value);

// Create default user for each permission set
$user = \{{ $user }}::create([
'name' => ucwords(str_replace("_", " ", $key)),
'name' => ucwords(str_replace('_', ' ', $key)),
'email' => $key.'@app.com',
'password' => bcrypt('password'),
'remember_token' => str_random(10),
]);
foreach ($permissions as $p => $perm) {
$permissions = [];

foreach (explode(',', $value) as $p => $perm) {

$permissionValue = $mapPermission->get($perm);

$permission = \{{ $permission }}::firstOrCreate([
$permissions[] = \{{ $permission }}::firstOrCreate([
'name' => $permissionValue . '-' . $module,
'display_name' => ucfirst($permissionValue) . ' ' . ucfirst($module),
'description' => ucfirst($permissionValue) . ' ' . ucfirst($module),
]);
])->id;

$this->command->info('Creating Permission to '.$permissionValue.' for '. $module);

if (!$user->hasPermission($permission->name)) {
$user->attachPermission($permission);
} else {
$this->command->info($key . ': ' . $p . ' ' . $permissionValue . ' already exist');
}
}
}

// Attach all permissions to the user
$user->permissions()->sync($permissions);
}
}
}

/**
* Truncates all the laratrust tables and the users table
*
* @return void
*/
public function truncateLaratrustTables()
Expand Down

0 comments on commit c67d1c9

Please sign in to comment.