/** * Execute the console command. * * @return mixed */ public function handle() { // we are assuming that the sysop is always the first user $this->info('Creating administrator, default section and first topic...'); $user = \Nexus\User::first(); if (!$user) { $this->info("Please enter in values for the administrator account. Don't worry You can change this later."); $username = $this->ask('Username'); $email = $this->ask('Email Address'); $password = $this->ask('Password'); $administrator = new \Nexus\User(); $administrator->username = $username; $administrator->name = 'Administrator'; $administrator->email = $email; $administrator->password = \Hash::make($password); $administrator->administrator = true; try { $administrator->save(); } catch (\Exception $e) { $this->error('Failed to add administrator ' . $e); } } else { $this->error('There is already a user account'); } $section = \Nexus\Section::first(); if (!$section) { $this->info("Please enter in values for the main menu. Don't worry You can change this later."); $title = $this->ask('Title'); $mainmenu = new \Nexus\Section(); $mainmenu->title = $title; $mainmenu->user_id = $administrator->id; try { $mainmenu->save(); } catch (\Exception $e) { $this->error('Failed to add main menu ' . $e); } } else { $this->error('There is already a main menu'); } $topic = \Nexus\Topic::first(); if (!$topic) { $this->info("Please enter in values for the first topic. Don't worry You can change this later."); $title = $this->ask('Title'); $firstTopic = new \Nexus\Topic(); $firstTopic->title = $title; $firstTopic->section_id = $mainmenu->id; try { $firstTopic->save(); } catch (\Exception $e) { $this->error('Failed to add first topic ' . $e); } } else { $this->error('There is already a topic'); } }
/** * a section can only be archived by the * the moderator of the parent section * * @return bool */ public function authorize() { $return = false; $section = \Nexus\Section::findOrFail($this->section); $parent = \Nexus\Section::findOrFail($section->parent_id); if (\Auth::user()->id === $parent->moderator->id) { $return = true; } else { $return = false; } return $return; }
/** * Determine if the user is authorized to make this request. * * For the Current Section * user must be the moderator * * For Subsections all these must be true! * [1] a subsection can be edited by the moderator of its parent section * [2] a subsection cannot be moved into a decedent section * [3] a subsection can only be moved into a section the parent section moderator moderates * * @return bool */ public function authorize() { $return = false; $formName = key($this::input('form')); $formValues = $this::input('form')[$formName]; // dd($formValues, $formName); $this->session()->flash('form', $formName); if (!\Auth::check()) { $return = false; } // if $formValues['parent_id'] is null then we are editing the main menu $section = \Nexus\Section::findOrFail($formValues['id']); // are we editing the current section OR a sub section if ($formValues['id'] === $formValues['current_section']) { // current section if ($section->moderator->id == \Auth::user()->id) { $return = true; } else { $return = false; } } else { // sub section $destination = \Nexus\Section::findOrFail($formValues['parent_id']); // [1] a subsection can be edited by the moderator of its parent section if ($section->parent->moderator->id == \Auth::user()->id) { $updated_by_parent_moderator = true; } else { $updated_by_parent_moderator = false; } // [2] a subsection cannot be moved into a decedent section $decedents = \Nexus\Helpers\SectionHelper::allChildSections($section); if ($decedents->where('id', $destination->id)->count() > 0) { $destination_not_child = false; } else { $destination_not_child = true; } // [3] a subsection can only be moved into a section the moderator moderates if ($destination->moderator->id == \Auth::user()->id) { $destination_moderated_by_editor = true; } else { $destination_moderated_by_editor = false; } // if all conditions are matched then this is allowed if ($updated_by_parent_moderator && $destination_not_child && $destination_moderated_by_editor) { $return = true; } else { $return = false; } } return $return; }
/** * a topic can only be restored by * the moderator of the section and destination section * * @return bool */ public function authorize() { $return = false; $currentUserID = \Auth::user()->id; $trashedTopic = \Nexus\Topic::onlyTrashed()->findOrFail($this->topic); $originalSection = \Nexus\Section::withTrashed()->findOrFail($trashedTopic->section_id); $destinationSection = \Nexus\Section::findOrFail($this->destination); if ($destinationSection->moderator->id === $currentUserID && $originalSection->moderator->id === $currentUserID) { $return = true; } else { $return = false; } return $return; }
/** * a user can create a section if they * moderate the current section * * @return bool */ public function authorize() { $return = false; $formName = "sectionCreate"; $formValues = $this::input('form')[$formName]; $this->session()->flash('form', $formName); $parentSection = \Nexus\Section::findOrFail($formValues['parent_id']); if (\Auth::user()->id == $parentSection->moderator->id) { $return = true; } else { $return = false; } return $return; }
public function test_deleting_section_soft_deletes_its_subsections() { // given we have a user with a section and that sub section $user = factory(User::class, 1)->create(); // AND we have a section $section = factory(Section::class, 1)->create(['parent_id' => null, 'user_id' => $user->id]); // with subsections factory(Section::class, 6)->create(['parent_id' => $section->id, 'user_id' => $user->id]); $subsectionCount = Section::where('parent_id', $section->id)->count(); // when we delete that section $section->delete(); // then section and subsections are soft deleted $this->assertTrue($section->trashed()); // we have no subsections $this->assertEquals(Section::where('parent_id', $section->id)->count(), 0); // we have the right amount of soft deleted subsections $this->assertEquals(Section::withTrashed()->where('parent_id', $section->id)->count(), $subsectionCount); }
/** * a section can only be restored by * [1] the moderator of both the parent and destination sections * or * [2] the moderator of the section and destination section * * @return bool */ public function authorize() { $return = false; $currentUserID = \Auth::user()->id; $trashedSection = \Nexus\Section::onlyTrashed()->findOrFail($this->section); $destinationSection = \Nexus\Section::findOrFail($this->destination); if ($destinationSection->moderator->id === $currentUserID) { // case [1] if ($trashedSection->parent != null) { if ($trashedSection->parent->moderator->id === $currentUserID) { $return = true; } } // case [2] if ($trashedSection->moderator->id === $currentUserID) { $return = true; } } return $return; }
/** * topic can be created by moderators of the current section or bbs administrators * * user should be * logged in * user administrator | user moderator * * @return bool */ public function authorize() { $return = false; $formName = "topicCreate"; $formValues = $this::input('form')[$formName]; $this->session()->flash('form', $formName); if (\Auth::check()) { $authUser = \Auth::user(); $section = Section::findOrFail($formValues['section_id']); // is the user an administrator if ($authUser->administrator) { $return = true; } // or the user is the section moderator if ($authUser->id === $section->moderator->id) { $return = true; } } else { $return = false; } return $return; }
public function authorize() { $return = false; $topic = Topic::findOrFail($this::input('topic_id')); $section = Section::findOrFail($topic->section_id); if (\Auth::check()) { $authUser = \Auth::user(); // is the user an administrator if ($authUser->administrator) { $return = true; } // OR is the user the moderator if ($authUser->id === $section->moderator->id) { $return = true; } // OR is the topic NOT ready only if (!$topic->readonly) { $return = true; } } else { $return = false; } return $return; }
/** * generates a breadcrumb trail for a user profile * * @param Nexus\User * @return an array of links to go in a breadcrumb trail */ public static function breadcrumbForUser(\Nexus\User $user) { $breadcrumbs = array(); $crumb['title'] = $user->username; $crumb['route'] = null; $breadcrumbs[] = $crumb; $crumb['title'] = 'Users'; $crumb['route'] = action('Nexus\\UserController@index'); $breadcrumbs[] = $crumb; $section = \Nexus\Section::first(); $crumb['title'] = $section->title; $crumb['route'] = action('Nexus\\SectionController@show', ['section_id' => $section->id]); $breadcrumbs[] = $crumb; return array_reverse($breadcrumbs); }
private function migrateSections() { $this->info('Importing Sections'); $errorCount = 0; if (!\Nexus\Section::first()) { $count = \DB::select('select count(section_id) as count from sectiontable')[0]->count; $this->line("Found {$count} sections "); $this->line("Migrating Sections "); $bar = $this->output->createProgressBar($count); $classicSections = \DB::table('sectiontable')->get(); foreach ($classicSections as $classicSection) { try { $newSection = new \Nexus\Section(); $newSection->id = $classicSection->section_id; $newSection->title = $classicSection->section_title; $newSection->intro = $classicSection->section_intro; $newSection->user_id = $classicSection->user_id; $newSection->weight = $classicSection->section_weight; $newSection->save(); $bar->advance(); } catch (\Exception $e) { $errorCount++; \Log::error('Nexus:upgrade - Failed to add section ' . $e); } } $bar->finish(); $this->line("\nMigration Complete"); $this->line("Jumbling Sections into Subsections"); $bar = $this->output->createProgressBar($count); foreach ($classicSections as $classicSection) { try { $newSection = \Nexus\Section::findOrFail($classicSection->section_id); $newSection->parent_id = $classicSection->parent_id; $newSection->save(); $bar->advance(); } catch (\Exception $e) { $errorCount++; \Log::error('Nexus:upgrade - Failed to add parent to section ' . $e); } } unset($classicSections); $bar->finish(); if ($errorCount) { $this->error("\nEncountered {$errorCount} errors. See log for details"); } $this->info("\nSections Complete\n"); } else { $this->error('Upgrade: found existing sections - skipping Sections'); } }
/** * Remove the specified resource from storage. * * @param section id $id * @param Section\Destroy $request * @return Response */ public function destroy(Requests\Section\DestroyRequest $request, $id) { $section = \Nexus\Section::findOrFail($id); $parent_id = $section->parent_id; $section->delete(); $redirect = action('Nexus\\SectionController@show', ['id' => $parent_id]); return redirect($redirect); }
public function test_restoreSectionToSection_restores_a_section() { // given we have a section with topics $number_of_topics = 10; $section = factory(Section::class, 1)->create(); $section_id = $section->id; factory(Topic::class, $number_of_topics)->create(['section_id' => $section_id]); // and we have another section $anotherSection = factory(Section::class, 1)->create(); // and we delete the section $section->delete(); // the section is deleted $this->assertTrue($section->trashed()); // and so are the topics $this->assertEquals(Section::withTrashed()->find($section_id)->topics->count(), 0); $this->assertEquals(Section::withTrashed()->find($section_id)->trashedTopics->count(), $number_of_topics); // when the section is restored to another section \Nexus\Helpers\RestoreHelper::restoreSectionToSection($section, $anotherSection); // then the section is not trashed $this->assertFalse($section->trashed()); // and neither are the topics $this->assertEquals(Section::find($section_id)->topics->count(), $number_of_topics); $this->assertEquals(Section::find($section_id)->trashedTopics->count(), 0); // and its parent is the other section $this->assertEquals($section->parent_id, $anotherSection->id); }
public function topic(Requests\topic\RestoreRequest $request, $id) { $trashedTopic = \Nexus\Topic::onlyTrashed()->findOrFail($id); $destinationSection = \Nexus\Section::findOrFail($request->destination); \Nexus\Helpers\RestoreHelper::restoreTopicToSection($trashedTopic, $destinationSection); $redirect = action('Nexus\\SectionController@show', ['id' => $destinationSection->id]); return redirect($redirect); }
public function getSectionCountAttribute() { return Section::select(\DB::raw('count(id) as count'))->where('parent_id', $this->id)->value('count'); }