Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modified: README.md added additional documentation #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@ After that, you need to run the migration files:
```
$ php artisan migrate
```
On User Model add

```php
use Amir\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;



```
### How to authorize user
This package adds a `role_id` to the `users` table.
Roles are stored in the `roles` table. You can assign a role to a user in your administrator panel or by creating a seed file.
Expand Down Expand Up @@ -111,6 +121,12 @@ Also, you can use these options in combination:
```
$ php artisan permissions:clear --roles admin --tables permission_role
```
On routes you can now
```
@can_access('home.index')
<a href="{{ route("home.index") }}">Home</a>
@endcan_access
```


## About
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
{
"name": "Amir Yousefi",
"email": "[email protected]"
},
{
"name": "Dennis K Kiptugen",
"email": "[email protected]"
}
],
"extra": {
Expand Down
6 changes: 4 additions & 2 deletions src/Commands/PermissionsGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ public function handle()
foreach ($routes as $route){
$action = $route->getActionname();

if ($action == "Closure") {
continue;
if ($action == "Closure")
{
continue;
}
}

$name = $route->getName();
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/UnauthorizedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UnauthorizedException extends HttpException

public static function noPermission()
{
return new static(403, 'User don\'t have permission', null, []);
return new static(403, 'User doesn\'t have permission', null, []);
}

}
8 changes: 8 additions & 0 deletions src/LaravelPermissionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Amir\Permission\Middleware\AuthRoles;
use Illuminate\Support\ServiceProvider;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Auth;

class LaravelPermissionServiceProvider extends ServiceProvider {
public function boot(Router $router)
Expand All @@ -17,6 +18,13 @@ public function boot(Router $router)
}

$router->aliasMiddleware('auth.role', AuthRoles::class);
Blade::directive('can_access', function ($expression) {
return "&lt;?php if ({Auth::user()->permission->contains('name',$expression)}) : ?&gt;";
});

Blade::directive('endcan_access', function ($expression) {
return '&lt;?php endif; ?&gt;';
});
}
public function register()
{
Expand Down
7 changes: 5 additions & 2 deletions src/Middleware/AuthRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function handle($request, Closure $next)

throw_if(!auth($authGuard)->check(), UnauthenticatedException::notLoggedIn());

$action = $request->route()->getActionname();
/*$action = $request->route()->getActionname();
$name = $request->route()->getActionname();

$role_id = auth($authGuard)->user()->role_id;
Expand All @@ -39,7 +39,10 @@ public function handle($request, Closure $next)
$query->orWhere('action', $action);
})->whereHas('roles', function ($query) use($role_id){
$query->where('id',$role_id);
})->first();
})->first();*/
$action = $request->route()->getActionname();
$name = $request->route()->getName();
$permission = auth($authGuard)->user()->permission->where('name',$name)->where('action',$action);

throw_if(is_null($permission), UnauthorizedException::noPermission());

Expand Down
21 changes: 21 additions & 0 deletions src/Models/PermissionRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Amir\Permission\Models;


use Illuminate\Database\Eloquent\Model;


class PermissionRole extends Model
{
use HasFactory;
public $timestamps = FALSE;
public function role()
{
return $this->belongsTo(Role::class);
}
public function permission()
{
return $this->belongsTo(Permission::class) ;
}
}
17 changes: 11 additions & 6 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
trait HasRoles
{
public function role()
{
return $this->belongsTo(Role::class);
}
{
return $this->belongsTo(Role::class);
}

public function getRoleNameAttribute(){
return $this->role()->first()->name ?? null ;
}
public function getRoleNameAttribute()
{
return $this->role()->first()->name ?? null ;
}
public function permission()
{
return $this->hasManyThrough(Permission::class,Permission_Role::class,'role_id','id','role_id','permission_id');
}
}