oscer-io /
oscer
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Oscer\Cms\Core\Commands; |
||
| 4 | |||
| 5 | use Faker\Generator; |
||
| 6 | use Illuminate\Console\Command; |
||
| 7 | use Illuminate\Database\Eloquent\Factory; |
||
| 8 | use Illuminate\Support\Collection; |
||
| 9 | use Illuminate\Support\Str; |
||
| 10 | use Oscer\Cms\Core\Models\Menu; |
||
| 11 | use Oscer\Cms\Core\Models\Option; |
||
| 12 | use Oscer\Cms\Core\Models\Page; |
||
| 13 | use Oscer\Cms\Core\Models\Permission; |
||
| 14 | use Oscer\Cms\Core\Models\Post; |
||
| 15 | use Oscer\Cms\Core\Models\Role; |
||
| 16 | use Oscer\Cms\Core\Models\Tag; |
||
| 17 | use Oscer\Cms\Core\Models\User; |
||
| 18 | use Symfony\Component\Console\Helper\Table; |
||
| 19 | |||
| 20 | class InstallCommand extends Command |
||
| 21 | { |
||
| 22 | protected $signature = 'cms:install {--dev} {--fresh}'; |
||
| 23 | |||
| 24 | protected $description = 'Install Oscer'; |
||
| 25 | |||
| 26 | protected User $admin; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 27 | |||
| 28 | protected Generator $faker; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Execute the console command. |
||
| 32 | * |
||
| 33 | * @return void |
||
| 34 | */ |
||
| 35 | public function handle() |
||
| 36 | { |
||
| 37 | // If --fresh option is enabled the database will be migrated fresh |
||
| 38 | $this->option('fresh') |
||
| 39 | ? $this->call('migrate:fresh') |
||
| 40 | : $this->call('migrate'); |
||
| 41 | |||
| 42 | $this->registerCmsServiceProvider(); |
||
| 43 | $this->comment('Publishing Oscer Service Provider...'); |
||
| 44 | $this->callSilent('vendor:publish', ['--tag' => 'cms-provider']); |
||
| 45 | |||
| 46 | $this->comment('Publishing Oscer assets...'); |
||
| 47 | $this->callSilent('vendor:publish', ['--tag' => 'cms-assets']); |
||
| 48 | |||
| 49 | $this->comment('Publishing Oscer configuration...'); |
||
| 50 | $this->callSilent('vendor:publish', ['--tag' => 'cms-config']); |
||
| 51 | |||
| 52 | $this->createAdminUser(); |
||
| 53 | $this->createRoles(); |
||
| 54 | |||
| 55 | $this->callSilent('cms:options:resolve'); |
||
| 56 | |||
| 57 | $this->seedDummyContent(); |
||
| 58 | |||
| 59 | $this->info('Oscer is installed successfully.'); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Register the Cms service provider in the application configuration file. |
||
| 64 | * Thanks to laravel/nova for inspiration. |
||
| 65 | */ |
||
| 66 | protected function registerCmsServiceProvider(): void |
||
| 67 | { |
||
| 68 | if (! file_exists(app_path('Providers/CmsServiceProvider.php'))) { |
||
| 69 | $namespace = Str::replaceLast('\\', '', $this->laravel->getNamespace()); |
||
| 70 | |||
| 71 | file_put_contents(config_path('app.php'), str_replace( |
||
| 72 | "{$namespace}\\Providers\RouteServiceProvider::class,".PHP_EOL, |
||
| 73 | "{$namespace}\\Providers\RouteServiceProvider::class,".PHP_EOL." {$namespace}\Providers\CmsServiceProvider::class,".PHP_EOL, |
||
| 74 | file_get_contents(config_path('app.php')) |
||
| 75 | )); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | public function createAdminUser(): void |
||
| 80 | { |
||
| 81 | // admin user |
||
| 82 | if ($this->option('dev')) { |
||
| 83 | $admin = User::query()->create([ |
||
| 84 | 'name' => 'admin', |
||
| 85 | 'email' => '[email protected]', |
||
| 86 | 'password' => 'password', |
||
| 87 | ]); |
||
| 88 | $this->line('User email: <info>[email protected]</info>'); |
||
| 89 | $this->line('Password: <info>password</info>'); |
||
| 90 | } else { |
||
| 91 | $admin = User::query()->create([ |
||
| 92 | 'name' => $this->ask('Name for the first admin user?', 'admin'), |
||
| 93 | 'email' => $this->ask('Email for the first admin user?', '[email protected]'), |
||
| 94 | 'password' => $this->secret('Password for the first admin user?', 'password'), |
||
| 95 | ]); |
||
| 96 | } |
||
| 97 | $admin->assignRole(Role::SUPER_ADMIN_ROLE); |
||
| 98 | |||
| 99 | $this->admin = $admin; |
||
| 100 | |||
| 101 | $this->comment('Super Admin created:'); |
||
| 102 | } |
||
| 103 | |||
| 104 | protected function createRoles() |
||
| 105 | { |
||
| 106 | if ( |
||
| 107 | $this->option('dev') |
||
| 108 | || $this->confirm('Should we create default roles for you?') |
||
| 109 | ) { |
||
| 110 | $this->comment('Create Roles'); |
||
| 111 | $roles = collect([ |
||
| 112 | [ |
||
| 113 | 'name' => 'admin', |
||
| 114 | 'permissions' => [ |
||
| 115 | Permission::all(), |
||
| 116 | ], |
||
| 117 | ], |
||
| 118 | [ |
||
| 119 | 'name' => 'editor', |
||
| 120 | 'permissions' => [ |
||
| 121 | 'posts.*', // all permissions in posts |
||
| 122 | 'pages.*', // all permissions in pages |
||
| 123 | 'menus.*', // all permissions in menus |
||
| 124 | 'options.*', // all permissions in options |
||
| 125 | ], |
||
| 126 | ], |
||
| 127 | [ |
||
| 128 | 'name' => 'publisher', |
||
| 129 | 'permissions' => [ |
||
| 130 | 'posts.*', // all permissions in posts |
||
| 131 | 'pages.*', // all permissions in pages |
||
| 132 | 'menus.*', // all permissions in menus |
||
| 133 | ], |
||
| 134 | ], |
||
| 135 | [ |
||
| 136 | 'name' => 'author', |
||
| 137 | 'permissions' => [ |
||
| 138 | 'posts.view', |
||
| 139 | 'posts.create', |
||
| 140 | 'posts.update', |
||
| 141 | 'pages.view', |
||
| 142 | 'pages.create', |
||
| 143 | 'pages.update', |
||
| 144 | ], |
||
| 145 | ], |
||
| 146 | [ |
||
| 147 | 'name' => 'subscriber', |
||
| 148 | 'permissions' => [ |
||
| 149 | 'posts.view', |
||
| 150 | 'pages.view', |
||
| 151 | ], |
||
| 152 | ], |
||
| 153 | ]) |
||
| 154 | ->map(function ($role) { |
||
| 155 | return Role::query() |
||
| 156 | ->create(['name' => $role['name']]) |
||
| 157 | ->givePermissionTo($role['permissions']); |
||
| 158 | })->map(function (Role $role) { |
||
| 159 | return [ |
||
| 160 | 'name' => $role->name, |
||
| 161 | 'permissions' => $role->permissions->pluck('name')->implode(', '), |
||
| 162 | ]; |
||
| 163 | })->toArray(); |
||
| 164 | |||
| 165 | $table = new Table($this->output); |
||
| 166 | $table |
||
| 167 | ->setHeaders(['role name', 'active permissions']) |
||
| 168 | ->setRows($roles) |
||
| 169 | ->setStyle('default') |
||
| 170 | ->setColumnMaxWidth(1, 150) |
||
| 171 | ->render(); |
||
| 172 | |||
| 173 | $this->info('Roles created'); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | protected function seedDummyContent() |
||
| 178 | { |
||
| 179 | if ( |
||
| 180 | $this->option('dev') |
||
| 181 | || $this->confirm('Should we seed dummy content for you?') |
||
| 182 | ) { |
||
| 183 | $this->comment('Register factories'); |
||
| 184 | $this->faker = app()->make(Generator::class); |
||
| 185 | app()->singleton(Factory::class, function ($app) { |
||
| 186 | $faker = $app->make(Generator::class); |
||
| 187 | |||
| 188 | return Factory::construct($faker, base_path('vendor/oscer-io/oscer/tests/factories')); |
||
| 189 | }); |
||
| 190 | |||
| 191 | $this->seedTagsAndPosts(); |
||
| 192 | $this->seedPages(); |
||
| 193 | $this->seedOptions(); |
||
| 194 | $this->seedMenuItems(); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | protected function seedTagsAndPosts() |
||
| 199 | { |
||
| 200 | $this->comment('Seeding tags'); |
||
| 201 | $tags = collect(['General', 'Tech', 'PHP', 'Laravel', 'Vue.js', 'Travel']) |
||
| 202 | ->map(function ($name) { |
||
| 203 | return factory(Tag::class)->create(['name' => $name]); |
||
| 204 | }); |
||
| 205 | $this->info("{$tags->count()} tags seeded"); |
||
| 206 | |||
| 207 | $this->comment('Seeding posts'); |
||
| 208 | $posts = factory(Post::class, 10)->create(['author_id' => $this->admin->id]); |
||
| 209 | $posts->each(function (Post $post) use ($tags) { |
||
| 210 | $post->tags()->sync($tags->random(rand(1, 3))->pluck('id')); |
||
| 211 | }); |
||
| 212 | $this->info("{$posts->count()} posts seeded and random tags assigned"); |
||
| 213 | } |
||
| 214 | |||
| 215 | protected function seedPages() |
||
| 216 | { |
||
| 217 | $this->comment('Seeding pages'); |
||
| 218 | collect([ |
||
| 219 | [ |
||
| 220 | 'name' => 'Home', |
||
| 221 | 'slug' => 'front-page', |
||
| 222 | 'body' => 'Welcome to Oscer', |
||
| 223 | ], |
||
| 224 | [ |
||
| 225 | 'name' => 'About me', |
||
| 226 | 'slug' => 'about', |
||
| 227 | 'body' => $this->faker->paragraphs(rand(3, 5), true), |
||
| 228 | ], |
||
| 229 | [ |
||
| 230 | 'name' => 'Legal Notice', |
||
| 231 | 'slug' => 'legal', |
||
| 232 | 'body' => $this->faker->paragraphs(rand(3, 5), true), |
||
| 233 | ], |
||
| 234 | [ |
||
| 235 | 'name' => 'Privacy', |
||
| 236 | 'slug' => 'privacy', |
||
| 237 | 'body' => $this->faker->paragraphs(rand(3, 5), true), |
||
| 238 | ], |
||
| 239 | ])->map(function ($page) { |
||
| 240 | return factory(Page::class)->create(array_merge($page, ['author_id' => $this->admin->id])); |
||
| 241 | })->tap(function (Collection $pages) { |
||
| 242 | factory(Page::class, 50)->create(['author_id' => $this->admin->id]); |
||
| 243 | $this->info("{$pages->count()} pages seeded"); |
||
| 244 | }); |
||
| 245 | } |
||
| 246 | |||
| 247 | protected function seedOptions() |
||
| 248 | { |
||
| 249 | Option::query() |
||
| 250 | ->where('key', 'pages.front_page') |
||
| 251 | ->first() |
||
| 252 | ->update(['value' => 'front-page']); |
||
| 253 | } |
||
| 254 | |||
| 255 | protected function seedMenuItems() |
||
| 256 | { |
||
| 257 | $this->comment('Seeding menu items'); |
||
| 258 | $menus = collect([ |
||
| 259 | [ |
||
| 260 | 'name' => 'Main Menu', |
||
| 261 | 'location' => 'main', |
||
| 262 | 'items' => [ |
||
| 263 | [ |
||
| 264 | 'name' => 'About me', |
||
| 265 | 'url' => '/about', |
||
| 266 | 'order' => 1, |
||
| 267 | ], |
||
| 268 | [ |
||
| 269 | 'name' => 'Blog', |
||
| 270 | 'url' => '/posts', |
||
| 271 | 'order' => 2, |
||
| 272 | ], |
||
| 273 | ], |
||
| 274 | ], |
||
| 275 | [ |
||
| 276 | 'name' => 'Footer Menu', |
||
| 277 | 'location' => 'footer', |
||
| 278 | 'items' => [ |
||
| 279 | [ |
||
| 280 | 'name' => 'Legal Notice', |
||
| 281 | 'url' => '/legal', |
||
| 282 | 'order' => 1, |
||
| 283 | ], |
||
| 284 | [ |
||
| 285 | 'name' => 'Privacy', |
||
| 286 | 'url' => '/privacy', |
||
| 287 | 'order' => 2, |
||
| 288 | ], |
||
| 289 | ], |
||
| 290 | ], |
||
| 291 | ]); |
||
| 292 | $menus->each(function (array $data) { |
||
| 293 | /** @var Menu $menu */ |
||
| 294 | $menu = factory(Menu::class)->create(['name' => $data['name'], 'location' => $data['location']]); |
||
| 295 | $menu->items()->createMany($data['items']); |
||
| 296 | }); |
||
| 297 | |||
| 298 | $this->info("{$menus->count()} menus seeded."); |
||
| 299 | } |
||
| 300 | } |
||
| 301 |