Skip to content

Commit

Permalink
* change usage novauser.gates.user.model to auth.providers.users.model
Browse files Browse the repository at this point in the history
* make custom ActionResource
* change CustomAuthorize to NovauserAuthorize.
* patch NovaServiceProvider
* add route web to test permission on local
* add action to navigation.
  • Loading branch information
anditsung committed Apr 30, 2020
1 parent d919f1a commit 1bf692f
Show file tree
Hide file tree
Showing 16 changed files with 812 additions and 40 deletions.
9 changes: 7 additions & 2 deletions config/novauser.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
'binds' => [
'login' => \Tsung\NovaUserManagement\Http\Controllers\Auth\LoginController::class,

'authorize' => \Tsung\NovaUserManagement\Http\Middleware\CustomAuthorize::class,
]
'authorize' => \Tsung\NovaUserManagement\Http\Middleware\NovauserAuthorize::class,
],

/*
* set true to show actions resource on navigation
*/
'show-actions' => false,
];
2 changes: 1 addition & 1 deletion dist/js/tool.js

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions resources/js/components/permission-checkbox/form/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
:key="group"
class="flex flex-col mb-4 pr-4 w-1/2"
>
<div class="p-3 bg-40 border-l border-t border-r border-60">
<div class="flex p-3 bg-40 border-l border-t border-r border-60">
<h4>{{ group }}</h4>
</div>

Expand Down Expand Up @@ -116,6 +116,14 @@
}
this.check(option);
},
}
selectAllGroupName(group) {
return "Select all " + group
},
toggleGroup(group) {
}
},
}
</script>
13 changes: 13 additions & 0 deletions resources/views/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@
</li>
@endcan

@if(Config::get('novauser.show-actions') == true)
<li class="leading-wide mb-4 text-sm">
<router-link :to="{
name: 'index',
params: {
resourceName: 'action-events'
}
}" class="text-white ml-8 no-underline dim">
{{ __("Actions") }}
</router-link>
</li>
@endif

</template>
</nova-sidebar>
@endcanany
32 changes: 32 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,35 @@
// Route::get('/password/reset/{token}', \Laravel\Nova\Http\Controllers\ResetPasswordController::class . '@showResetForm')->name('password.reset');
// Route::post('/password/reset', \Laravel\Nova\Http\Controllers\ResetPasswordController::class . '@reset');
//});

Route::group(['middleware'=> 'web'], function() {

// to check user permissions
if(app()->environment('local')) {
Route::get('/perm', function() {
$user = Auth()->user();
if($user) {
$permissionModel = config('novauser.gates.permission.model');
$permissions = $permissionModel::all();
echo "PERMISSION FOR {$user->name}<br>";
foreach($permissions as $permission) {
$text = "";
if($user->can($permission->name)) {
$text .= "<b style='color: green'>allow</b>";
}
else {
$text .= "<b style='color: red'>not allow</b>";
}
$text .= " to {$permission->name}<br>";
echo $text;
}
}
else {
echo "NO USER DETECTED";
}
die();
});
}
});


2 changes: 1 addition & 1 deletion src/Commands/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Init extends Command
public function handle()
{
$guard = config('nova.guard') ?: config('auth.defaults.guard');
$userModel = config('novauser.gates.user.model');
$userModel = config('auth.providers.users.model');
$roleModel = config('novauser.gates.role.model');
$permissionModel = config('novauser.gates.permission.model');

Expand Down
27 changes: 21 additions & 6 deletions src/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,46 @@ class Install extends Command

public function handle()
{
$this->info('Replacing Default User Model');
$this->replaceUserModel();
$this->info("Done");

$this->info('Replacing Default User Nova');
$this->replaceUserNova();
$this->info("Done");

$this->info("Publish Novauser Config");
$this->publishConfig();
$this->info('Done');

$this->patchingNovaServiceProviderGate();
}

private function replaceUserModel()
{
$this->info('Replacing Default User Model');
copy(__DIR__.'/../Stub/Models/User.stub', app_path('User.php'));
$this->info("Done");
}

private function replaceUserNova()
{
$this->info('Replacing Default User Nova');
copy(__DIR__.'/../Stub/Nova/User.stub', app_path('Nova/User.php'));
$this->info("Done");
}

private function publishConfig()
{
$this->info("Publish Novauser Config");
$this->call('vendor:publish', ['--tag' => 'novauser-config']);
$this->info('Done');
}

private function patchingNovaServiceProviderGate()
{
$novaServiceProviderPath = app_path('Providers/NovaServiceProvider.php');

$this->info("Patching NovaServiceProvider gate method");
$gate_regex = "/in_array[\(\$\w\-\>\,\[\s\/]+.+/";
$patchGate = '$user->hasPermissionTo(\'viewNova\');';
$novaServiceProviderContent = file_get_contents($novaServiceProviderPath);
$novaServiceProviderContent = preg_replace($gate_regex, $patchGate, $novaServiceProviderContent);
file_put_contents($novaServiceProviderPath, $novaServiceProviderContent);
$this->info("Done");
}
}
2 changes: 1 addition & 1 deletion src/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected function authenticated(Request $request, $user)

private function isActive($user)
{
$userModel = config('novauser.gates.user.model');
$userModel = config('auth.providers.users.model');
$user = $userModel::where($this->username(), $user)->first();
if($user) {
return $user->is_active;
Expand Down
18 changes: 0 additions & 18 deletions src/Http/Middleware/CustomAuthorize.php

This file was deleted.

70 changes: 70 additions & 0 deletions src/Http/Middleware/NovauserAuthorize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php


namespace Tsung\NovaUserManagement\Http\Middleware;


use Laravel\Nova\Nova;
use Spatie\Permission\PermissionRegistrar;

class NovauserAuthorize
{
public function handle($request, $next)
{
if(Nova::check($request)) {

//$this->logonUserStillActive($request);

$this->forgetCachedPermissions($request);

return $next($request);
}
else {
// if the user dont have viewNova permissions then redirect to '/'
// return abort(403);
return redirect('/');
}
}

/**
* @param $request
*
* this method will check user is still active, if not logout the user,
* note: nova will redirect the user if dont have viewNova permission but the user still login
* dont need this cause web user and admin user using the same model
*/
private function logonUserStillActive($request)
{
$user = $request->user();
if ($user) {
if( ! $user = auth()->user()->is_active) {
auth()->logout();
}
}
}

/**
* @param $request
*
* this method will reset cache for permission after adding
* laravel nova using cache permission, so it need to be reset before useable
*/
private function forgetCachedPermissions($request)
{
if ( $request->is('nova-api/*/detach') || $request->is('nova-api/*/*/attach*/*') ) {
// $permissionKey = Nova::resourceForModel(app(PermissionRegistrar::class)->getPermissionClass())::uriKey();
//
// if ($request->viaRelationship === $permissionKey) {
// app(PermissionRegistrar::class)->forgetCachedPermissions();
// }

/*
* if the request->viaRelationship is roles / permissions will reset permission cache
*/
if( $request->viaRelationship === "roles" || $request->viaRelationship === "permissions" ) {
app(PermissionRegistrar::class)->forgetCachedPermissions();
}
}
}

}
Loading

0 comments on commit 1bf692f

Please sign in to comment.