Skip to content

Commit

Permalink
Merge branch 'new-tests'
Browse files Browse the repository at this point in the history
  • Loading branch information
santigarcor committed Sep 20, 2017
2 parents ccf7b81 + d3a9f1a commit bd0b664
Show file tree
Hide file tree
Showing 23 changed files with 1,254 additions and 1,759 deletions.
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
"kkszymanowski/traitor": "^0.2.0"
},
"require-dev": {
"mockery/mockery": "~0.9.2",
"phpunit/phpunit": "~4.1"
"mockery/mockery": "^0.9.2",
"phpunit/phpunit": "~4.1",
"orchestra/testbench": "~3.2"
},
"autoload": {
"psr-4": {
"Laratrust\\": "src/"
}
},
"autoload-dev": {
"classmap": [
"tests/Middleware/MiddlewareTest.php",
"tests/UserTest.php"
]
"psr-4": {
"Laratrust\\Tests\\": "tests/"
}
},
"config": {
"sort-packages": true
Expand Down
13 changes: 9 additions & 4 deletions src/Middleware/LaratrustMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Redirect;

class LaratrustMiddleware
{
Expand Down Expand Up @@ -40,10 +42,13 @@ protected function authorization($type, $rolesPermissions, $team, $options)
*/
protected function unauthorized()
{
return call_user_func(
Config::get('laratrust.middleware.handling', 'abort'),
Config::get('laratrust.middleware.params', '403')
);
$parameter = Config::get('laratrust.middleware.params');

if (Config::get('laratrust.middleware.handling') == 'abort') {
return App::abort($parameter);
}

return Redirect::to($parameter);
}

/**
Expand Down
29 changes: 16 additions & 13 deletions src/Traits/LaratrustUserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,9 @@ public function isAbleTo($permission, $team = null, $requireAll = false)
public function ability($roles, $permissions, $team = null, $options = [])
{
list($team, $options) = $this->assignRealValuesTo($team, $options, 'is_array');

// Convert string to array if that's what is passed in.
if (!is_array($roles)) {
$roles = explode(',', $roles);
}
if (!is_array($permissions)) {
$permissions = explode(',', $permissions);
}
$roles = $this->standardValue($roles);
$permissions = $this->standardValue($permissions);

// Set up default values and validate options.
$options = $this->checkOrSet('validate_all', $options, [false, true]);
Expand Down Expand Up @@ -409,16 +404,24 @@ private function detachModel($relationship, $objectType, $object, $team)
private function syncModels($relationship, $objectType, $objects, $team, $detaching)
{
$mappedObjects = [];
$team = Config::get('laratrust.use_teams') ? $this->getIdFor($team, 'team') : null;

if (Config::get('laratrust.use_teams')) {
foreach ($objects as $object) {
foreach ($objects as $object) {
if (Config::get('laratrust.use_teams') && $team) {
$mappedObjects[$this->getIdFor($object, $objectType)] = [$this->teamForeignKey() => $team];
} else {
$mappedObjects[] = $this->getIdFor($object, $objectType);
}
} else {
$mappedObjects = $objects;
}

$this->$relationship()->sync($mappedObjects, $detaching);
$relationshipToSync = $this->$relationship();

if (Config::get('laratrust.use_teams') && $team) {
$relationshipToSync->wherePivot($this->teamForeignKey(), $team);
}

$relationshipToSync->sync($mappedObjects, $detaching);

$this->flushCache();

return $this;
Expand Down Expand Up @@ -709,7 +712,7 @@ public function flushCache()

/**
* Checks if the option exists inside the array,
* if not sets a the first option inside the default values array.
* otherwise, sets the first option inside the default values array.
*
* @param string $option
* @param array $array
Expand Down
34 changes: 34 additions & 0 deletions tests/LaratrustPermissionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Laratrust\Test;

use Laratrust\Tests\LaratrustTestCase;
use Laratrust\Tests\Models\Permission;

class LaratrustPermissionTest extends LaratrustTestCase
{
protected $permission;

public function setUp()
{
parent::setUp();

$this->migrate();
$this->permission = new Permission();
}

public function testUsersRelationship()
{
$this->assertInstanceOf('Illuminate\Database\Eloquent\Relations\MorphToMany', $this->permission->users());
}

public function testAccessUsersRelationshipAsAttribute()
{
$this->assertEmpty($this->permission->users);
}

public function testRolesRelationship()
{
$this->assertInstanceOf('\Illuminate\Database\Eloquent\Relations\BelongsToMany', $this->permission->roles());
}
}

0 comments on commit bd0b664

Please sign in to comment.