public function run()
 {
     \DB::table('role_permission')->truncate();
     foreach ($this->rolePermissionRepo->getSeeds() as $rolePermission) {
         \App\Clusters\AuthCluster\Models\AccessControl\RolePermission::create($rolePermission);
     }
 }
 public function ajax()
 {
     if (Request::ajax()) {
         $data = Input::all();
         if (isset($data['action']) && $data['action'] == 'role_permissions') {
             if (isset($data['role_id']) && !empty($data['role_id'])) {
                 $user = \Auth::user();
                 if (!$user || !$user->can('updateUsersAccess')) {
                     return response('', 401);
                 }
                 $permissions = RolePermission::where('role_id', '=', $data['role_id'])->with('permission')->get();
                 return response()->json($permissions);
             }
             return response('', 401);
         }
         return response('', 404);
     }
     return response('', 404);
 }
 public function attachPermission($permission)
 {
     $cacheKey = $this->getPermissionsCacheKey();
     $permission = Permission::where('name', '=', $permission)->firstOrFail();
     $rolePermission = RolePermission::where('role_id', '=', $this->role()->id)->where('permission_id', '=', $permission->id)->first();
     //This role does not support this permission
     if (!$rolePermission) {
         return FALSE;
     }
     $record = $this->permissionsRelation()->where('permission_id', '=', $permission->id)->where('user_id', '=', $this->id)->first();
     if ($record) {
         return NULL;
     }
     //User already has permission
     $authUsername = \Auth::user() ? \Auth::user()->username : '******';
     \Log::info('SET: Permission "' . $permission . '" to user "' . $this->username . '" by user "' . $authUsername . '".');
     $this->permissionsRelation()->create(['user_id' => $this->id, 'permission_id' => $permission->id]);
     Cache::forget($cacheKey);
     return TRUE;
 }
 public function edit($id)
 {
     $user = User::findOrFail($id);
     $userPermissionsObj = $user->permissions();
     $userRole = $user->role();
     $roles = Role::orderBy('id', 'desc')->lists('name', 'id')->all();
     $userPermissions = [];
     $permissions = [];
     if ($userRole) {
         $permissions = RolePermission::where('role_id', '=', $userRole->id)->with('permission')->get();
     }
     foreach ($userPermissionsObj as $p) {
         $userPermissions[] = $p->id;
     }
     return view('authcluster.dashboard.users.edit', compact('user', 'roles', 'userPermissions', 'userRole', 'permissions'));
 }
 public function it_creates_default_permission_for_all_roles(FunctionalTester $I)
 {
     $roles = Role::all();
     foreach ($roles as $role) {
         $rolePermissions = RolePermission::where('role_id', '=', $role->id)->with('permission')->get();
         $I->assertTrue($rolePermissions->count() >= 1, 'Each role should have a default permission.');
     }
 }