Skip to content

Commit

Permalink
Add observables to laratrust events
Browse files Browse the repository at this point in the history
  • Loading branch information
santigarcor committed Sep 25, 2017
1 parent 6342410 commit 2c0ee36
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 93 deletions.
26 changes: 26 additions & 0 deletions src/Traits/LaratrustHasEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@

trait LaratrustHasEvents
{
/**
* Register an observer to the Laratrust events.
*
* @param object|string $class
* @return void
*/
public static function laratrustObserve($class)
{
$observables = [
'roleAttached',
'roleDetached',
'permissionAttached',
'permissionDetached',
'roleSynced',
'permissionSynced',
];

$className = is_string($class) ? $class : get_class($class);

foreach ($observables as $event) {
if (method_exists($class, $event)) {
static::registerLaratrustEvent(\Illuminate\Support\Str::snake($event, '.'), $className.'@'.$event);
}
}
}

/**
* Fire the given event for the model.
*
Expand Down
56 changes: 28 additions & 28 deletions src/Traits/LaratrustPermissionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ trait LaratrustPermissionTrait
{
use LaratrustDynamicUserRelationsCalls;

/**
* Boots the permission model and attaches event listener to
* remove the many-to-many records when trying to delete.
* Will NOT delete any records if the permission model uses soft deletes.
*
* @return void|bool
*/
public static function bootLaratrustPermissionTrait()
{
static::deleting(function ($permission) {
if (!method_exists(Config::get('laratrust.models.permission'), 'bootSoftDeletes')) {
$permission->roles()->sync([]);
}
});

static::deleting(function ($permission) {
if (method_exists($permission, 'bootSoftDeletes') && $permission->forceDeleting) {
return;
}

$permission->roles()->sync([]);

foreach (array_keys(Config::get('laratrust.user_models')) as $key) {
$permission->$key()->sync([]);
}
});
}

/**
* Many-to-Many relations with role model.
*
Expand Down Expand Up @@ -48,32 +76,4 @@ public function getMorphByUserRelation($relationship)
Config::get('laratrust.foreign_keys.user')
);
}

/**
* Boots the permission model and attaches event listener to
* remove the many-to-many records when trying to delete.
* Will NOT delete any records if the permission model uses soft deletes.
*
* @return void|bool
*/
public static function bootLaratrustPermissionTrait()
{
static::deleting(function ($permission) {
if (!method_exists(Config::get('laratrust.models.permission'), 'bootSoftDeletes')) {
$permission->roles()->sync([]);
}
});

static::deleting(function ($permission) {
if (method_exists($permission, 'bootSoftDeletes') && $permission->forceDeleting) {
return;
}

$permission->roles()->sync([]);

foreach (array_keys(Config::get('laratrust.user_models')) as $key) {
$permission->$key()->sync([]);
}
});
}
}
68 changes: 34 additions & 34 deletions src/Traits/LaratrustRoleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,40 @@ trait LaratrustRoleTrait
use LaratrustDynamicUserRelationsCalls;
use LaratrustHasEvents;

/**
* Boots the role model and attaches event listener to
* remove the many-to-many records when trying to delete.
* Will NOT delete any records if the role model uses soft deletes.
*
* @return void|bool
*/
public static function bootLaratrustRoleTrait()
{
$flushCache = function ($role) {
$role->flushCache();
};

// If the role doesn't use SoftDeletes.
if (method_exists(static::class, 'restored')) {
static::restored($flushCache);
}

static::deleted($flushCache);
static::saved($flushCache);

static::deleting(function ($role) {
if (method_exists($role, 'bootSoftDeletes') && $role->forceDeleting) {
return;
}

$role->permissions()->sync([]);

foreach (array_keys(Config::get('laratrust.user_models')) as $key) {
$role->$key()->sync([]);
}
});
}

/**
* Tries to return all the cached permissions of the role.
* If it can't bring the permissions from the cache,
Expand Down Expand Up @@ -67,40 +101,6 @@ public function permissions()
);
}

/**
* Boots the role model and attaches event listener to
* remove the many-to-many records when trying to delete.
* Will NOT delete any records if the role model uses soft deletes.
*
* @return void|bool
*/
public static function bootLaratrustRoleTrait()
{
$flushCache = function ($role) {
$role->flushCache();
};

// If the role doesn't use SoftDeletes.
if (method_exists(static::class, 'restored')) {
static::restored($flushCache);
}

static::deleted($flushCache);
static::saved($flushCache);

static::deleting(function ($role) {
if (method_exists($role, 'bootSoftDeletes') && $role->forceDeleting) {
return;
}

$role->permissions()->sync([]);

foreach (array_keys(Config::get('laratrust.user_models')) as $key) {
$role->$key()->sync([]);
}
});
}

/**
* Checks if the role has a permission by its name.
*
Expand Down
62 changes: 31 additions & 31 deletions src/Traits/LaratrustUserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,37 @@ trait LaratrustUserTrait
{
use LaratrustHasEvents;

/**
* Boots the user model and attaches event listener to
* remove the many-to-many records when trying to delete.
* Will NOT delete any records if the user model uses soft deletes.
*
* @return void|bool
*/
public static function bootLaratrustUserTrait()
{
$flushCache = function ($user) {
$user->flushCache();
};

// If the user doesn't use SoftDeletes.
if (method_exists(static::class, 'restored')) {
static::restored($flushCache);
}

static::deleted($flushCache);
static::saved($flushCache);

static::deleting(function ($user) {
if (method_exists($user, 'bootSoftDeletes') && !$user->forceDeleting) {
return;
}

$user->roles()->sync([]);
$user->permissions()->sync([]);
});
}

/**
* Tries to return all the cached roles of the user.
* If it can't bring the roles from the cache,
Expand Down Expand Up @@ -96,37 +127,6 @@ public function permissions()
return $permissions;
}

/**
* Boots the user model and attaches event listener to
* remove the many-to-many records when trying to delete.
* Will NOT delete any records if the user model uses soft deletes.
*
* @return void|bool
*/
public static function bootLaratrustUserTrait()
{
$flushCache = function ($user) {
$user->flushCache();
};

// If the user doesn't use SoftDeletes.
if (method_exists(static::class, 'restored')) {
static::restored($flushCache);
}

static::deleted($flushCache);
static::saved($flushCache);

static::deleting(function ($user) {
if (method_exists($user, 'bootSoftDeletes') && !$user->forceDeleting) {
return;
}

$user->roles()->sync([]);
$user->permissions()->sync([]);
});
}

/**
* Checks if the user has a role by its name.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/LaratrustUserEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,22 @@ public function testAnEventIsFiredWhenPermissionsAreSynced()

$this->user->syncPermissions([]);
}

public function testCanAddObservableClasses()
{
$events = [
'role.attached',
'role.detached',
'permission.attached',
'permission.detached',
'role.synced',
'permission.synced',
];

User::laratrustObserve(\Laratrust\Tests\Models\UserObserver::class);

foreach ($events as $event) {
$this->assertTrue(User::getEventDispatcher()->hasListeners("laratrust.{$event}: " . User::class));
}
}
}
30 changes: 30 additions & 0 deletions tests/Models/UserObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Laratrust\Tests\Models;

class UserObserver
{
public function roleAttached($user, $thing, $team)
{
}

public function roleDetached($user, $thing, $team)
{
}

public function permissionAttached($user, $thing, $team)
{
}

public function permissionDetached($user, $thing, $team)
{
}

public function roleSynced($user, $thing, $team)
{
}

public function permissionSynced($user, $thing, $team)
{
}
}

0 comments on commit 2c0ee36

Please sign in to comment.