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)); }
public function testShouldDeleteGroup() { // 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(); // prove first that the blog group exist // and that user "jane" is in blog group $blog = $res['blogger']; $jane = $res['user']; $this->assertEquals('blogger', Group::find($blog->id)->name); $this->assertTrue($jane->inGroup($blog), "Jane should be in blogger group"); // now lets delete the blogger group $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\User\\Commands\\DeleteGroupCommand', array('id' => $blog->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('Group successfully deleted.', $result->getMessage()); // blog should not exist now $this->assertInternalType('null', Group::find($blog->id)); // user "jane" should not be in group "blogger" now $j = User::find($jane->id); $this->assertFalse($j->inGroup($blog), "Jane should not be in blogger group anymore"); }
/** * @param User $user * @param Dispatcher $dispatcher * @param Group $group * @return CommandResult */ public function handle(User $user, Dispatcher $dispatcher, Group $group) { // check user permission if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission(['user.manage'])) { return new CommandResult(false, CommandResult::$responseForbiddenMessage, null, 403); } } // fire creating $dispatcher->fire('user.updating', array($this->args)); // find the user to be updated if (!($userToBeUpdated = $user->find($this->id))) { return new CommandResult(false, "User not found.", null, 404); } // begin update $userToBeUpdated->first_name = $this->firstName ? $this->firstName : $userToBeUpdated->first_name; $userToBeUpdated->last_name = $this->lastName ? $this->lastName : $userToBeUpdated->last_name; $userToBeUpdated->permissions = $this->isPermissionsProvided($this->permissions) ? $this->transform($this->permissions) : $userToBeUpdated->permissions; if ($this->isEmailIsProvidedAndIsNew($userToBeUpdated->email, $this->email)) { if ($this->isEmailAlreadyInUsed($user, $this->email)) { return new CommandResult(false, "Email already in used.", null, 400); } else { $userToBeUpdated->email = $this->email; } } if ($this->isPasswordIsProvided()) { $userToBeUpdated->password = $this->password; } // save $userToBeUpdated->save(); // add to group if there's any if ($this->isGroupIdsAreProvided($this->groups)) { // delete all groups first $userToBeUpdated->groups()->detach(); // re attach foreach ($this->groups as $groupId) { $g = $group->find($groupId); if ($g) { $userToBeUpdated->groups()->attach($g); } } } // fire created user $dispatcher->fire('user.updated', array($userToBeUpdated)); // return response return new CommandResult(true, "User successfully updated.", $userToBeUpdated, 200); }
public function testShouldAssociateTheUserToTheGroupIfThereIsAnyProvided() { // create user and logged in $user = User::create(array('first_name' => 'darryl', 'email' => '*****@*****.**', 'password' => 'pass$darryl', 'permissions' => array('superuser' => 1))); $this->application['auth']->loginUsingId($user->id); // create dummy groups $blogger = Group::create(array('name' => 'blogger', 'permissions' => array('blog.list' => 1, 'blog.create' => 1, 'blog.edit' => 1, 'blog.delete' => -1))); $artist = Group::create(array('name' => 'artist', 'permissions' => array('blog.list' => -1, 'art.create' => 1, 'art.edit' => 1, 'art.delete' => -1))); $moderator = Group::create(array('name' => 'moderator', 'permissions' => array('blog.list' => -1, 'art.create' => 1, 'art.edit' => 1, 'art.delete' => -1))); // dummy request, required first name $request = Request::create('', 'GET', array('firstName' => 'Darryl', 'lastName' => 'Fernandez', 'email' => '*****@*****.**', 'password' => 'password', 'permissions' => array(), 'groups' => array($blogger->id, $artist->id))); // begin $result = $this->commandDispatcher->dispatchFrom('Darryldecode\\Backend\\Components\\User\\Commands\\CreateUserCommand', $request); $createdUser = User::find($result->getData()->id); $this->assertTrue($createdUser->inGroup($blogger), 'User should be in blogger group'); $this->assertTrue($createdUser->inGroup($artist), 'User should be in artist group'); $this->assertFalse($createdUser->inGroup($moderator), 'User should not be in moderator group'); }