-
composer.json
Open in GitHub{ // "require": { "php": ">=7.4", "ext-curl": "*", "ext-json": "*", // "santigarcor/laratrust": "^6.0" }, // }
-
app/User.php
Open in GitHubuse Laratrust\Traits\LaratrustUserTrait; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use LaratrustUserTrait; // }
-
config/laratrust_seeder.php
Open in GitHubreturn [ 'create_users' => true, 'roles_structure' => [ 'admin' => [ 'dungeons' => 'c,r,u,d', 'expansions' => 'c,r,u,d', 'npcs' => 'c,r,u,d', 'profile' => 'r,u', 'dungeonroute' => 'c,r,u,d' ], 'user' => [ 'profile' => 'r,u', 'dungeonroute' => 'c,r,u,d' ], ], 'permission_structure' => [ 'cru_user' => [ 'profile' => 'c,r,u' ], ], 'permissions_map' => [ 'c' => 'create', 'r' => 'read', 'u' => 'update', 'd' => 'delete' ] ];
-
database/seeders/LaratrustSeeder.php
Open in GitHubuse App\Permission; use App\Role; use App\User; use Illuminate\Support\Facades\Schema; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Config; class LaratrustSeeder extends Seeder { public function run() { $this->command->info('Truncating User, Role and Permission tables'); $this->truncateLaratrustTables(); $config = config('laratrust_seeder.roles_structure'); $mapPermission = collect(config('laratrust_seeder.permissions_map')); foreach ($config as $key => $modules) { // Create a new role $role = Role::firstOrCreate([ 'name' => $key, 'display_name' => ucwords(str_replace('_', ' ', $key)), 'description' => ucwords(str_replace('_', ' ', $key)) ]); $permissions = []; $this->command->info('Creating Role '. strtoupper($key)); foreach ($modules as $module => $value) { foreach (explode(',', $value) as $p => $perm) { $permissionValue = $mapPermission->get($perm); $permissions[] = Permission::firstOrCreate([ 'name' => $permissionValue . '-' . $module, 'display_name' => ucfirst($permissionValue) . ' ' . ucfirst($module), 'description' => ucfirst($permissionValue) . ' ' . ucfirst($module), ])->id; $this->command->info('Creating Permission to '.$permissionValue.' for '. $module); } } $role->permissions()->sync($permissions); if(Config::get('laratrust_seeder.create_users')) { $this->command->info("Creating '{$key}' user"); // Create default user for each role $user = User::create([ 'name' => ucwords(str_replace('_', ' ', $key)), 'email' => $key.'@app.com', 'password' => bcrypt('password') ]); $user->attachRole($role); } } } public function truncateLaratrustTables() { Schema::disableForeignKeyConstraints(); DB::table('permission_role')->truncate(); DB::table('permission_user')->truncate(); DB::table('role_user')->truncate(); if(Config::get('laratrust_seeder.truncate_tables')) { Role::truncate(); Permission::truncate(); } if(Config::get('laratrust_seeder.truncate_tables') && Config::get('laratrust_seeder.create_users')) { User::truncate(); } Schema::enableForeignKeyConstraints(); } }
-
app/Policies/DungeonRoutePolicy.php
Open in GitHubuse App\Models\DungeonRoute; use App\User; use Illuminate\Auth\Access\HandlesAuthorization; class DungeonRoutePolicy { // public function publish(User $user, DungeonRoute $dungeonroute) { if (!$dungeonroute->hasKilledAllUnskippables()) { return $this->deny('Unable to change sharing settings: not all unskippable enemies have been killed'); } return ($dungeonroute->isOwnedByUser($user) || $user->hasRole('admin')); } // }