public function run()
 {
     \DB::table('roles')->truncate();
     $roles = [['name' => 'admin', 'display' => 'Administrator'], ['name' => 'moderator', 'display' => 'Moderator'], ['name' => 'user', 'display' => 'User']];
     foreach ($roles as $role) {
         \App\Clusters\AuthCluster\Models\AccessControl\Role::create($role);
     }
 }
 public function run()
 {
     \DB::table('user_role')->truncate();
     $usersRoles = [['user_id' => \App\Clusters\AuthCluster\Models\User::where('username', '=', 'admin')->firstOrFail()->id, 'role_id' => \App\Clusters\AuthCluster\Models\AccessControl\Role::where('name', '=', 'admin')->firstOrFail()->id], ['user_id' => \App\Clusters\AuthCluster\Models\User::where('username', '=', 'user')->firstOrFail()->id, 'role_id' => \App\Clusters\AuthCluster\Models\AccessControl\Role::where('name', '=', 'user')->firstOrFail()->id]];
     foreach ($usersRoles as $permission) {
         \App\Clusters\AuthCluster\Models\AccessControl\UserRole::create($permission);
     }
 }
 public function it_has_role_in_db_after_registration(FunctionalTester $I)
 {
     $I->amOnRoute(RegisterPage::$ROUTE);
     $userData = $this->userActor->fillRegisterPageFields($I);
     $I->submitForm(RegisterPage::$formId, [], 'Register');
     $user = $I->grabRecord('users', ['username' => $userData['username']]);
     $role = Role::where('name', '=', Role::$defaultRole)->first();
     $I->seeRecord(UserRole::$tableName, ['user_id' => $user->id, 'role_id' => $role->id]);
 }
 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'));
 }
 protected function seedDataGetter($seedName)
 {
     $seedName = strtolower($seedName);
     $role = Role::where('name', '=', $seedName)->firstOrFail();
     $permissions = [];
     $data = [];
     foreach ($this->seedFilesIterator(DIRECTORY_SEPARATOR . $seedName) as $line) {
         $extraData = ['role_id' => $role->id];
         $permissions[] = $this->seedDataComposer($line, $extraData);
     }
     foreach ($permissions as $permission) {
         $permissionId = Permission::where('name', '=', $permission['name'])->firstOrFail()->id;
         $data[] = ['role_id' => $permission['role_id'], 'permission_id' => $permissionId];
     }
     return $data;
 }
 public function setRole($role)
 {
     $cacheKey = $this->getPermissionsCacheKey();
     if (is_numeric($role)) {
         $role = Role::findOrFail($role);
         goto modelSave;
     }
     $role = Role::where('name', '=', $role)->firstOrFail();
     modelSave:
     $this->permissionsRelation()->delete();
     //Delete all permissions
     Cache::forget($cacheKey);
     $authUsername = \Auth::user() ? \Auth::user()->username : '******';
     \Log::info('SET: Role of "' . $role->name . '" to user "' . $this->username . '" by user "' . $authUsername . '".');
     if (!$this->roleRelation) {
         $userRole = new UserRole(['user_id' => $this->id, 'role_id' => $role->id]);
         $this->roleRelation()->save($userRole);
         return;
     }
     $this->roleRelation->role_id = $role->id;
     $this->roleRelation->save();
 }
 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.');
     }
 }