public function testShouldDeleteUser()
 {
     // create user and logged in (the user who will perform the action)
     $user = User::create(array('first_name' => 'darryl', 'email' => '*****@*****.**', 'password' => 'pass$darryl', 'permissions' => array('superuser' => 1)));
     $this->application['auth']->loginUsingId($user->id);
     $res = $this->createDummyUserAndGroup();
     // before deleting the user and detaching to any group
     // lets just prove that the the group the user is being associated with
     // contains 1 user
     $artistGroup = Group::with('users')->find($res['artist']->id);
     $this->assertCount(1, $artistGroup->users->toArray());
     $this->assertEquals('jane', $artistGroup->users->first()->first_name);
     $blogger = Group::with('users')->find($res['blogger']->id);
     $this->assertCount(1, $blogger->users->toArray());
     $this->assertEquals('jane', $blogger->users->first()->first_name);
     // create dummy
     $u = $res['user'];
     // begin deletion
     $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\User\\Commands\\DeleteUserCommand', array('id' => $u->id));
     // prove deletion response should be successful
     $this->assertTrue($result->isSuccessful(), 'Transaction should be successful.');
     $this->assertEquals(200, $result->getStatusCode(), 'Status code should be forbidden.');
     $this->assertEquals('User successfully deleted.', $result->getMessage());
     // now this groups should have no user in it now
     $blogger = Group::with('users')->find($res['blogger']->id);
     $artistGroup = Group::with('users')->find($res['artist']->id);
     $this->assertCount(0, $artistGroup->users->toArray());
     $this->assertCount(0, $blogger->users->toArray());
     // user deleted should not exist
     $this->assertInternalType('null', User::find($u->id));
 }
 /**
  * handle user deletion logic
  *
  * @param User $user
  * @param Group $group
  * @param Dispatcher $dispatcher
  * @return CommandResult
  */
 public function handle(User $user, Group $group, Dispatcher $dispatcher)
 {
     // check user permission
     if (!$this->disablePermissionChecking) {
         if (!$this->user->hasAnyPermission(['user.delete'])) {
             return new CommandResult(false, CommandResult::$responseForbiddenMessage, null, 403);
         }
     }
     // find the group
     if (!($groupToBeDelete = $group->with('users')->find($this->id))) {
         return new CommandResult(false, "Group not found.", null, 404);
     }
     // fire deleting
     $dispatcher->fire('group.deleting', array($this->args));
     // begin deletion
     $groupToBeDelete->users()->detach();
     $groupToBeDelete->delete();
     // fire deleted
     $dispatcher->fire('group.deleted', array($groupToBeDelete));
     // all good
     return new CommandResult(true, "Group successfully deleted.", null, 200);
 }
 /**
  * @param \Darryldecode\Backend\Components\User\Models\Group $group
  * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
  * @return \Darryldecode\Backend\Base\Commands\CommandResult
  */
 public function handle(Group $group, Dispatcher $dispatcher)
 {
     // check user permission
     if (!$this->disablePermissionChecking) {
         if (!$this->user->hasAnyPermission(['user.manage'])) {
             return new CommandResult(false, CommandResult::$responseForbiddenMessage, null, 403);
         }
     }
     // fire before query event
     $dispatcher->fire('groups.beforeQuery', array($this->args));
     // begin
     $q = $group->with(array_merge(array('users'), $this->with))->ofName($this->name);
     // give paginated results if told so
     if ($this->paginate) {
         $results = $q->paginate($this->perPage);
     } else {
         $results = $q->get();
     }
     // fire after query event
     $dispatcher->fire('groups.afterQuery', array($results));
     // all good
     return new CommandResult(true, "Query groups command successful.", $results, 200);
 }