Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
csrui committed Jul 12, 2022
0 parents commit 4d5185f
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
indent_size = 4
indent_style = space
trim_trailing_whitespace = false

[*.php]
indent_size = 4
indent_style = tab
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 26B

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "26b/laravel-permissions",
"description": "Laravel package to manage permissions",
"type": "library",
"require": {
"php": "^7.4 || ^8.0",
"spatie/laravel-package-tools": "^1.11",
"illuminate/config": "^8.0 || ^9.0",
"illuminate/support": "^8.0 || ^9.0",
"livewire/livewire": "^2.5"
},
"license": "MIT",
"autoload": {
"psr-4": {
"TwentySixB\\LaravelPermissions\\": "src/"
}
},
"authors": [
{
"name": "26B",
"email": "[email protected]"
}
],
"extra": {
"laravel": {
"providers": [
"TwentySixB\\LaravelPermissions\\PermissionsServiceProvider"
]
}
}
}
5 changes: 5 additions & 0 deletions config/permissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
//
];
24 changes: 24 additions & 0 deletions src/Actions/AbstractQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace TwentySixB\LaravelPermissions\Actions;

use Illuminate\Contracts\Database\Eloquent\Builder;

abstract class AbstractQuery
{

/**
* Returns a query for users eligeble for permission granting.
*
* @param string $query
* @return Builder
*/
abstract public function search(string $query) : Builder;

/**
* Returns a query for users with given permission.
*
* @return Builder
*/
abstract public function list() : Builder;
}
45 changes: 45 additions & 0 deletions src/Events/PermissionGranted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace TwentySixB\LaravelPermissions\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Auth\User;
use Illuminate\Database\Eloquent\Model;

class PermissionGranted
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public function __construct(
protected Model $model,
protected string $identifier,
protected string $grantee_id,
protected User $granter,
) {}

public function getModel() : Model
{
return $this->model;
}

public function getIdentifier() : string
{
return $this->identifier;
}

public function getGranteeId() : string
{
return $this->grantee_id;
}

public function getGranter() : User
{
return $this->granter;
}
}
45 changes: 45 additions & 0 deletions src/Events/PermissionRevoked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace TwentySixB\LaravelPermissions\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Auth\User;
use Illuminate\Database\Eloquent\Model;

class PermissionRevoked
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public function __construct(
protected Model $model,
protected string $identifier,
protected string $revokee_id,
protected User $revoker,
) {}

public function getModel() : Model
{
return $this->model;
}

public function getIdentifier() : string
{
return $this->identifier;
}

public function getRevokeeId() : string
{
return $this->revokee_id;
}

public function getRevoker() : User
{
return $this->revoker;
}
}
165 changes: 165 additions & 0 deletions src/Http/Livewire/Editor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace TwentySixB\LaravelPermissions\Http\Livewire;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use TwentySixB\LaravelPermissions\Actions\AbstractQuery;
use TwentySixB\LaravelPermissions\Events\PermissionGranted;
use TwentySixB\LaravelPermissions\Events\PermissionRevoked;

/**
* Permission editor.
*
*/
class Editor extends Component
{

/**
* Model where permission applies to.
*
* @var Model
*/
public Model $model;

/**
* Name / Indentifier of the permission.
*
* @var string
*/
public string $identifier;

/**
* Search query.
*
* @var string
*/
public string $query = '';

/**
* Class that will handle searching and listing.
*
* @var string
*/
public string $searchAction;

/**
* Users with current permission.
*
* @var Collection
*/
public Collection $usersWithPermission;

/**
* Users eligeble for the current permission.
*
* @var Collection
*/
public Collection $eligebleForPermission;

/**
* @inheritDoc
*
* @return void
*/
public function mount()
{
$this->usersWithPermission = collect();
$this->eligebleForPermission = collect();

$this->fetchUsersWithPermission();
}

/**
* Returns the action responsible for searching and listing.
*
* @return AbstractQuery
*/
protected function getAction() : AbstractQuery
{
return (new ($this->searchAction)(
$this->model,
$this->identifier,
));
}

/**
* Performs a search for users without permission.
*
* @return void
*/
public function search() : void
{
$this->eligebleForPermission = $this->getAction()->search($this->query)->get();
}

/**
* Resets the search form.
*
* @return void
*/
public function resetSearch() : void
{
$this->eligebleForPermission = collect();
$this->query = '';
}

/**
* Populate the list of users with the current permission.
*
* @return void
*/
public function fetchUsersWithPermission() : void
{
$this->usersWithPermission = $this->getAction()->list()->get();
}

/**
* Grant permissions for a given user.
*
* @param string $user_id
* @return void
*/
public function grant(string $user_id) : void
{
PermissionGranted::dispatch(
$this->model,
$this->identifier,
$user_id,
Auth::user(),
);

$this->fetchUsersWithPermission();
$this->resetSearch();
}

/**
* Revoke permissions for a given user.
*
* @param string $user_id
* @return void
*/
public function revoke(string $user_id) : void
{
PermissionRevoked::dispatch(
$this->model,
$this->identifier,
$user_id,
Auth::user(),
);

$this->fetchUsersWithPermission();
}

/**
* @inheritDoc
*
* @return void
*/
public function render()
{
return view('permissions::livewire.editor');
}
}
Loading

0 comments on commit 4d5185f

Please sign in to comment.