Closed AngusDV closed 4 years ago
$user->givePermissionTo($permission->name); not work i am understand that before don't give permission to user and i am give permission to role so after do it $user->givePermissionTo($permission->name); but when i am check permission table in mongodb and nothing in user_ids fileds just created.
i am use this version "mostafamaklad/laravel-permission-mongodb": "2.0.x-dev",
i know in vendor\mostafamaklad\laravel-permission-mongodb\src\Traits\HasPermissions method givePermissionTo $this->permissions()->saveMany($permissions); not work correctly
could you send me your composer.json
file
Laravel Framework 6.7.0
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.2",
"barryvdh/laravel-snappy": "^0.4.6",
"browner12/helpers": "^3.0",
"brozot/laravel-fcm": "^1.3",
"ckfinder/ckfinder-laravel-package": "v3.5.1.2",
"darryldecode/cart": "^4.0",
"fideloper/proxy": "^4.0",
"guzzlehttp/guzzle": "6.5",
"hekmatinasser/verta": "^1.10",
"hisorange/browser-detect": "^4.0",
"intervention/image": "^2.5",
"jenssegers/mongodb": "^3.6",
"laravel/framework": "^6.2",
"laravel/passport": "^8.0",
"laravel/tinker": "^2.0",
"laravolt/avatar": "3.0.1",
"morilog/jalali": "3.*",
"mostafamaklad/laravel-permission-mongodb": "2.0.x-dev",
"nwidart/laravel-modules": "^6.2",
"qcod/laravel-settings": "^1.0",
"stevebauman/location": "^5.0",
"uxweb/sweet-alert": "dev-master",
"yajra/laravel-datatables-oracle": "~9.0"
},
"require-dev": {
"facade/ignition": "^1.4",
"fzaninotto/faker": "^1.4",
"laravel/ui": "^1.1",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^3.0",
"phpunit/phpunit": "^8.0"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
}
}
namespace App;
use Illuminate\Notifications\Notifiable; use Jenssegers\Mongodb\Eloquent\SoftDeletes; use Jenssegers\Mongodb\Eloquent\HybridRelations; use Jenssegers\Mongodb\Auth\User as Authenticatable; use App\Traits\UseAutoIncrementID; use Laravel\Passport\HasApiTokens; use Maklad\Permission\Traits\HasRoles; use App\Traits\JalaliDate; use Storage; use App\Traits\UserDeleteTrait; use App\Notifications\ResetPasswordLinkNotification; use DB; use Carbon; use Alert; use Redirect; use App\Cartable; use App\UserOwner;
class User extends Authenticatable {
use HasApiTokens,Notifiable,HasRoles,JalaliDate,SoftDeletes;
public $timestamps = true;
protected $guard_name = 'web';
protected $dates = ['deleted_at'];
protected $collection = 'users';
protected $routeMiddleware = [ 'CustomCKFinderAuth' => \App\Http\Middleware\CustomCKFinderAuth::class, 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 'role' => \Maklad\Permission\Middlewares\RoleMiddleware::class, 'permission' => \Maklad\Permission\Middlewares\PermissionMiddleware::class, ];
try to use "mostafamaklad/laravel-permission-mongodb": "^2.0",
you are have different between permission role and directive permission so i am change in namespace Maklad\Permission; class PermissionRegistrar line 54 to : return $user->permissions->where('name',$permission->name)->count() ?: null; and in namespace Maklad\Permission\Middlewares; class PermissionMiddleware line 34 to : if (! app('auth')->user()->permissions()->whereIn('name',$permissions)->count()>0) { my problem solved but you must when insert permission to role and after assign role to user insert directive permission for user this is true . because directive permission have priority to role permissions. please change plugin and create directive permission when assign role to user otherwise can not revoke permission for a specific user.
sample
$role = Role::create(['name' => 'superadmin','label'=>'مدیر کل','guard_name'=>'web']);
//cartable
$permission = Permission::create(['name' => 'show cartable','label'=>'نمایش سفارش ها']);
$role->givePermissionTo($permission);
$user->assignRole('superadmin');
$user->givePermissionTo(Permission::where('role_ids',Role::whereName('superadmin')->first()->_id)->get()->pluck('name')->toArray());
$user->save();
i am do it after each assignRole but you must do it in assignRole thank you for reply @mostafamaklad
this relation is wrong
$user->givePermissionTo(Permission::where('role_ids',Role::whereName('superadmin')->first()->_id)->get()->pluck('name')->toArray());
Please debug this query
Permission::where('role_ids',Role::whereName('superadmin')->first()->_id)->get()->pluck('name')->toArray()
relation is true i am do it with tinker Maklad\Permission\Models\Permission::where('role_ids',Maklad\Permission\Models\Role::whereName('superadmin')->first()->_id)->get()->pluck('name')->toArray()
and get [ "show cartable", "create cartable", "edit cartable", "delete cartable", "show cartable files", "show timeline", "create timeline", "edit timeline", "delete timeline", "show users", "show all users", "create users", "edit users", "delete users", "permision users", "active users", "create|superadmin|admin|a|b|c|d|supplier|customer", "show reminder", "create reminder", "edit reminder", "delete reminder", "show setting", "show setting group", "create setting group", "edit setting group", "delete setting group", "show setting unit price", "create setting unit price", "edit setting unit price", "delete setting unit price", "show setting scale", "create setting scale", "edit setting scale", "delete setting scale", "show setting primary", "create setting primary", "edit setting primary", "delete setting primary", "show setting companyus", "create setting companyus", "edit setting companyus", "delete setting companyus", "show setting slider", "create setting slider", "edit setting slider", "delete setting slider", "show ticket", "create ticket", "edit ticket", "delete ticket", "show ticket group", "create ticket group", "edit ticket group", "delete ticket group", ]
spatie have same problem. after asignRole can not revoke permission for a specific user
just change
Maklad\Permission\Models\Permission::whereIn('role_ids',Maklad\Permission\Models\Role::whereIn('name',['superadmin','admin'])->get()->pluck('_id')->toArray())->get()->pluck('name')->toArray()
namespace Maklad\Permission\Traits; trait HasRoles public function assignRole(...$roles) must change it if user do it must assign directive permission to user
this is simple just do it with your code :
$role = Role::create(['name' => 'superadmin','label'=>'مدیر کل','guard_name'=>'web']);
//cartable
$permission = Permission::create(['name' => 'show cartable','label'=>'نمایش سفارش ها']);
$role->givePermissionTo($permission);
```
$user=User::where_id(auth()->user()->_id)->first(); $user->assignRole('superadmin'); $user->save();
after that you can not revoke permission because user do't have permission just do it :
$user->revokePermissionTo('show cartable');
and after that just do :
$user->getAllPermissions();
oh get all permission !!!
You cannot revoke a permission assigned by a role you can create another role without this permission then assign this role to the user you want
check this issue in spatie/laravel-permission
https://github.com/spatie/laravel-permission/issues/1230
i am know but this is not correct because if you are want to revoke permission of role then for each user you are must create a role and this is cardinal table and not correct and huge data store in database
$role = Role::create(['name' => 'superadmin','label'=>'مدیر کل','guard_name'=>'web']); //cartable $permission = Permission::create(['name' => 'show cartable']); $role->givePermissionTo($permission);
why $user->revokePermissionTo('show cartable'); not work ?
and after $user->getAllPermissions() get it