daily pastebin goal
36%
SHARE
TWEET

Untitled

a guest Aug 13th, 2016 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. use Illuminate\Database\Seeder;
  4. use App\User;
  5. use App\Role;
  6. use App\Permission;
  7.  
  8. class AclDefaultSeeder extends Seeder
  9. {
  10.     /**
  11.      * Run the database seeds.
  12.      *
  13.      * @return void
  14.      */
  15.     public function run()
  16.     {
  17.         User::truncate();
  18.         Role::truncate();
  19.         Permission::truncate();
  20.        
  21.         DB::table('permission_role')->truncate();
  22.         DB::table('role_user')->truncate();
  23.  
  24.         $config = config('laratrust_acl');
  25.  
  26.         $map_permission = [
  27.             'c' => 'create',
  28.             'r' => 'read',
  29.             'u' => 'update',
  30.             'd' => 'delete'
  31.         ];
  32.  
  33.         foreach ($config as $key => $value) {
  34.  
  35.             // Create a new role
  36.             $role = new Role();
  37.             $role->name         = $key;
  38.             $role->display_name = 'User '.ucfirst($key);
  39.             $role->description  = ucfirst($key);
  40.             $role->save();
  41.  
  42.             $this->command->info('Creating Role '. strtoupper($key));
  43.  
  44.             // Reading role permission modules
  45.             $modules = $value;
  46.             foreach ($modules as $module => $permissions) {
  47.  
  48.                 $_permissions = explode(',', $permissions);
  49.  
  50.                 foreach ($_permissions as $p => $perm) {
  51.  
  52.                     $permission = Permission::firstOrCreate([
  53.                         'name' => $module . '-' . $map_permission[$perm],
  54.                         'display_name' => ucfirst($map_permission[$perm]) . ' ' . ucfirst($module),
  55.                         'description' => ucfirst($map_permission[$perm]) . ' ' . ucfirst($module),
  56.                     ]);
  57.  
  58.                     $this->command->info('Creating Permission to '.$map_permission[$perm].' for '. $module);
  59.  
  60.                     $exist = DB::table('permission_role')->where('role_id',$role->id)->where('permission_id', $permission->id)->first();
  61.                    
  62.                     if(!$exist) {
  63.                         $role->attachPermission($permission);
  64.                     } else {
  65.                         $this->command->info($key . ': ' . $p . ' ' . $map_permission[$perm] . ' already exist');
  66.                     }
  67.                 }
  68.             }
  69.  
  70.             // Create default user for each role
  71.             $user = new User();
  72.  
  73.             $user->name = ucfirst($key);
  74.             $user->email = $key.'@app.com';
  75.             $user->password = bcrypt('password');
  76.             $user->remember_token = str_random(10);
  77.             $user->save();
  78.            
  79.             $user->attachRole($role);
  80.         }
  81.     }
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top