Skip to content

Commit

Permalink
add caffeinated#46, add caffeinated#58, fix 'can' permissions from di…
Browse files Browse the repository at this point in the history
…fferent roles bug, added tests
  • Loading branch information
pimlie committed Oct 17, 2017
1 parent 850bb84 commit 2d08033
Show file tree
Hide file tree
Showing 11 changed files with 538 additions and 119 deletions.
16 changes: 15 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,19 @@
}
}
},
"minimum-stability": "dev"
"minimum-stability": "dev",
"require-dev": {
"phpunit/phpunit": "^6.4",
"squizlabs/php_codesniffer": "3.*",
"orchestra/testbench": "~3.0"
},
"autoload-dev": {
"psr-4": {
"Caffeinated\\Shinobi\\Tests\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"cs": "vendor/bin/phpcs src/*"
}
}
31 changes: 31 additions & 0 deletions migrations/2017_10_17_170735_create_permission_user_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePermissionUserTable extends Migration
{
public function up()
{
Schema::create('permission_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('permission_id')->unsigned()->index();
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('roles')->onDelete('cascade');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('permission_user');
}

}
10 changes: 10 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<ruleset name="Shinobi">

<description>A basic coding standard.</description>

<exclude-pattern>vendor/*</exclude-pattern>

<rule ref="PSR2" />

</ruleset>
23 changes: 23 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<blacklist>
<directory suffix=".php">./vendor</directory>
</blacklist>
</filter>
</phpunit>
111 changes: 9 additions & 102 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use Config;
use Illuminate\Database\Eloquent\Model;
use Caffeinated\Shinobi\Traits\PermissionTrait;

class Role extends Model
{
use PermissionTrait;

/**
* The attributes that are fillable via mass assignment.
*
Expand All @@ -22,11 +25,11 @@ class Role extends Model
protected $table = 'roles';

/**
* The cache tag used by the model.
* The shinobi cache tag used by the model.
*
* @var string
*/
protected $tag = 'shinobi.roles';
protected static $shinobi_tag = 'shinobi.roles';

/**
* Roles can belong to many users.
Expand All @@ -38,32 +41,13 @@ public function users()
return $this->belongsToMany(config('auth.model') ?: config('auth.providers.users.model'))->withTimestamps();
}

/**
* Roles can have many permissions.
*
* @return Model
*/
public function permissions()
{
return $this->belongsToMany('\Caffeinated\Shinobi\Models\Permission')->withTimestamps();
}

/**
* Get permission slugs assigned to role.
*
* @return array
*/
public function getPermissions()
protected function allPermissions()
{
$primaryKey = $this[$this->primaryKey];
$cacheKey = 'caffeinated.shinobi.permissions.'.$primaryKey;

if (method_exists(app()->make('cache')->getStore(), 'tags')) {
return app()->make('cache')->tags($this->tag)->remember($cacheKey, 60, function () {
return $this->permissions->pluck('slug')->all();
});
}

return $this->permissions->pluck('slug')->all();
}

Expand All @@ -74,9 +58,7 @@ public function getPermissions()
*/
public function flushPermissionCache()
{
if (method_exists(app()->make('cache')->getStore(), 'tags')) {
app()->make('cache')->tags($this->tag)->flush();
}
parent::flushPermissionCache([static::$shinobi_tag, \Caffeinated\Shinobi\Traits\ShinobiTrait::$shinobi_tag]);
}

/**
Expand All @@ -96,17 +78,7 @@ public function can($permission)
return true;
}

$permissions = $this->getPermissions();

if (is_array($permission)) {
$permissionCount = count($permission);
$intersection = array_intersect($permissions, $permission);
$intersectionCount = count($intersection);

return ($permissionCount == $intersectionCount) ? true : false;
} else {
return in_array($permission, $permissions);
}
return $this->hasAllPermissions($permission, $this->getPermissions());
}

/**
Expand All @@ -126,71 +98,6 @@ public function canAtLeast(array $permission = [])
return true;
}

$permissions = $this->getPermissions();

$intersection = array_intersect($permissions, $permission);
$intersectionCount = count($intersection);

return ($intersectionCount > 0) ? true : false;
}

/**
* Assigns the given permission to the role.
*
* @param int $permissionId
*
* @return bool
*/
public function assignPermission($permissionId = null)
{
$permissions = $this->permissions;

if (!$permissions->contains($permissionId)) {
$this->flushPermissionCache();

return $this->permissions()->attach($permissionId);
}

return false;
}

/**
* Revokes the given permission from the role.
*
* @param int $permissionId
*
* @return bool
*/
public function revokePermission($permissionId = '')
{
$this->flushPermissionCache();

return $this->permissions()->detach($permissionId);
}

/**
* Syncs the given permission(s) with the role.
*
* @param array $permissionIds
*
* @return bool
*/
public function syncPermissions(array $permissionIds = [])
{
$this->flushPermissionCache();

return $this->permissions()->sync($permissionIds);
}

/**
* Revokes all permissions from the role.
*
* @return bool
*/
public function revokeAllPermissions()
{
$this->flushPermissionCache();

return $this->permissions()->detach();
return $this->hasAnyPermission($permission, $this->getPermissions());
}
}
11 changes: 8 additions & 3 deletions src/ShinobiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Caffeinated\Shinobi;

use Blade;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;

class ShinobiServiceProvider extends ServiceProvider
Expand All @@ -21,9 +22,13 @@ class ShinobiServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->publishes([
__DIR__.'/../migrations' => $this->app->databasePath().'/migrations',
], 'migrations');
if (version_compare(Application::VERSION, '5.3.0', '<')) {
$this->publishes([
__DIR__.'/../migrations' => $this->app->databasePath().'/migrations',
], 'migrations');
} else {
$this->loadMigrationsFrom(__DIR__.'/../migrations');
}

$this->registerBladeDirectives();
}
Expand Down
Loading

0 comments on commit 2d08033

Please sign in to comment.