Skip to content
This repository has been archived by the owner on May 19, 2020. It is now read-only.

Commit

Permalink
added several features & fixed a bug (#65)
Browse files Browse the repository at this point in the history
* added features #46, #58 and fixed some bugs

* Rename allPermissions method, add getShinobiTag method
  • Loading branch information
pimlie authored and kaidesu committed Oct 19, 2017
1 parent 850bb84 commit 41cd0ae
Show file tree
Hide file tree
Showing 11 changed files with 563 additions and 124 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('users')->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>
121 changes: 17 additions & 104 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,14 @@ class Role extends Model
protected $table = 'roles';

/**
* The cache tag used by the model.
* The shinobi cache tag used by the model.
*
* @var string
* @return string
*/
protected $tag = 'shinobi.roles';
protected static function getShinobiTag()
{
return 'shinobi.roles';
}

/**
* Roles can belong to many users.
Expand All @@ -39,31 +45,12 @@ public function users()
}

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

/**
* Get permission slugs assigned to role.
* Get fresh permission slugs assigned to role from database.
*
* @return array
*/
public function getPermissions()
protected function getFreshPermissions()
{
$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 +61,10 @@ public function getPermissions()
*/
public function flushPermissionCache()
{
if (method_exists(app()->make('cache')->getStore(), 'tags')) {
app()->make('cache')->tags($this->tag)->flush();
}
parent::flushPermissionCache([
static::getShinobiTag(),
\Caffeinated\Shinobi\Traits\ShinobiTrait::getShinobiTag()
]);
}

/**
Expand All @@ -96,17 +84,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 +104,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 comment has been minimized.

Copy link
@tomvo

tomvo Dec 15, 2017

It seems that after upgrading to this version of the package the migrations run automatically. This breaks our migration policy since we selectively published the migrations before. This might be something you want to check

$this->publishes([
__DIR__.'/../migrations' => $this->app->databasePath().'/migrations',
], 'migrations');
} else {
$this->loadMigrationsFrom(__DIR__.'/../migrations');
}

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

0 comments on commit 41cd0ae

Please sign in to comment.