예제 #1
0
 /**
  * Validate and save changes.
  *
  * @return Response
  */
 public function patchIndex(BoardConfigRequest $request, Board $board)
 {
     $input = $request->all();
     $optionGroups = $request->getBoardOptions();
     foreach ($optionGroups as $optionGroup) {
         foreach ($optionGroup->options as $option) {
             $setting = BoardSetting::firstOrNew(['option_name' => $option->option_name, 'board_uri' => $board->board_uri]);
             $option->option_value = $input[$option->option_name];
             $setting->option_value = $input[$option->option_name];
             $setting->save();
         }
     }
     $board->title = $input['boardBasicTitle'];
     $board->description = $input['boardBasicDesc'];
     $board->is_overboard = isset($input['boardBasicOverboard']) && !!$input['boardBasicOverboard'];
     $board->is_indexed = isset($input['boardBasicIndexed']) && !!$input['boardBasicIndexed'];
     $board->is_worksafe = isset($input['boardBasicWorksafe']) && !!$input['boardBasicWorksafe'];
     $board->save();
     Event::fire(new BoardWasModified($board));
     return $this->view(static::VIEW_CONFIG, ['board' => $board, 'groups' => $optionGroups]);
 }
예제 #2
0
 /**
  * Imports users and creates roles based on addition data in that row.
  *
  * @return void
  */
 public function importInfinityRolesAndBoards()
 {
     # BORROW SEEDERS
     require base_path() . "/database/seeds/OptionSeeder.php";
     require base_path() . "/database/seeds/PermissionSeeder.php";
     require base_path() . "/database/seeds/RoleSeeder.php";
     # DESTROY SEQUENCE
     if (DB::connection() instanceof \Illuminate\Database\PostgresConnection) {
         $this->comment("\tDropping role sequence.");
         DB::statement("DROP SEQUENCE IF EXISTS roles_role_id_seq CASCADE;");
     }
     $PermissionSeeder = new \PermissionSeeder();
     $PermissionSeeder->setCommand($this);
     $PermissionSeeder->run();
     $PermissionGroupSeeder = new \PermissionGroupSeeder();
     $PermissionGroupSeeder->setCommand($this);
     $PermissionGroupSeeder->run();
     $OptionSeeder = new \OptionSeeder();
     $OptionSeeder->setCommand($this);
     $OptionSeeder->run();
     $OptionGroupSeeder = new \OptionGroupSeeder();
     $OptionGroupSeeder->setCommand($this);
     $OptionGroupSeeder->run();
     $RoleSeeder = new \RoleSeeder();
     $RoleSeeder->setCommand($this);
     $RoleSeeder->runMaster();
     $RolePermissionSeeder = new \RolePermissionSeeder();
     $RolePermissionSeeder->setCommand($this);
     $RolePermissionSeeder->run();
     \Artisan::call('cache:clear');
     # REPAIR SEQUENCE
     if (DB::connection() instanceof \Illuminate\Database\PostgresConnection) {
         $this->comment("\tCreating role_id sequence again.");
         DB::statement("CREATE SEQUENCE roles_role_id_seq;");
         $pgSeqNext = DB::table('roles')->select(DB::raw("(MAX(\"role_id\") + 1) AS next"))->pluck("next");
         DB::statement("ALTER SEQUENCE roles_role_id_seq OWNED BY \"roles\".\"role_id\" RESTART WITH {$pgSeqNext};");
         DB::statement("ALTER TABLE roles ALTER COLUMN role_id SET DEFAULT nextval('roles_role_id_seq');");
     }
     # THEIR TABLES
     $tBoardsTable = $this->tcon->table("boards")->join('board_create', 'boards.uri', '=', 'board_create.uri')->select('boards.*', 'board_create.time');
     $tModsTable = $this->tcon->table("mods");
     # BEGIN USER IMPORT
     $this->info("\tImporting Users ...");
     $userAdmin = null;
     $userBoardRelationships = [];
     $usersImported = 0;
     $tModsTable->chunk(100, function ($mods) use(&$userAdmin, &$userBoardRelationships, &$usersImported) {
         $this->line("\t\tHandling 100 users ...");
         foreach ($mods as $mod) {
             # CREATE USER
             $user = new User(['username' => $mod->username, 'email' => property_exists($mod, "email") ? $mod->email ?: null : null, 'password' => null, 'password_legacy' => json_encode(['hasher' => "Vichan", 'hash' => $mod->password, 'salt' => $mod->salt])]);
             // 8chan has an issue with duplicates.
             try {
                 $saved = $user->save();
             } catch (\Exception $e) {
                 $saved = false;
             }
             if ($saved) {
                 ++$usersImported;
                 # REMEMBER ROLES
                 if ($mod->boards) {
                     // TODO
                     // Pull these values from config when formal importer created.
                     switch ($mod->type) {
                         // Janitor (Disabled)
                         case 10:
                             // Disabled
                         // Disabled
                         case 99:
                             $this->comment("\t\tMod {$user->username} is disabled.");
                             break;
                             // Board volunteer
                         // Board volunteer
                         case 19:
                             // Board owner
                         // Board owner
                         case 20:
                             if (!isset($userBoardRelationships[$mod->boards])) {
                                 $userBoardRelationships[$mod->boards] = [];
                             }
                             $userBoardRelationships[$mod->boards][$user->user_id] = $mod->type;
                             break;
                             // Global volunteer
                         // Global volunteer
                         case 25:
                             $this->comment("\t\tSetting {$user->username} to Global Mod.");
                             $user->roles()->attach(Role::ID_MODERATOR);
                             break;
                             // Admin
                         // Admin
                         case 30:
                             $this->comment("\t\tSetting {$user->username} to Global Admin.");
                             $user->roles()->attach(Role::ID_ADMIN);
                             $userAdmin = $userAdmin ?: $user;
                             break;
                             // Not identified!
                         // Not identified!
                         default:
                             $this->error("\t\tMod {$user->username} has invalid mod type {$mod->type}.");
                             break;
                     }
                 }
             }
         }
     });
     $this->info("\tImported {$usersImported} users(s).");
     if (!$userAdmin) {
         $this->comment("\tFailed to import an admin. This may cause problems.");
     }
     unset($mods, $mod, $user);
     # BEGIN BOARD IMPORT
     $this->info("\tImporting Boards ...");
     $boardsImported = 0;
     $tBoardsTable->chunk(100, function ($boards) use(&$boardsImported, $userAdmin, $userBoardRelationships) {
         $this->line("\t\tHandling 100 boards ...");
         foreach ($boards as $tBoard) {
             // We have an array like [board_uri => [ user_id => user_role ]] in the user import.
             // This fetches the first key of the biggest item.
             $boardOwner = [];
             if (isset($userBoardRelationships[$tBoard->uri])) {
                 $boardOwner = array_keys($userBoardRelationships[$tBoard->uri], max($userBoardRelationships[$tBoard->uri]));
             }
             // Or it defaults to our admin.
             if (!isset($boardOwner[0])) {
                 $this->comment("\t\t/{$tBoard->uri}/ has no known owner, assuming it is {$userAdmin->username}.");
                 $boardOwner = $userAdmin->user_id;
             } else {
                 $boardOwner = isset($boardOwner) ? $boardOwner[0] : $userAdmin->user_id;
             }
             $hBoard = new Board(['board_uri' => $tBoard->uri, 'title' => $tBoard->title, 'description' => $tBoard->subtitle, 'created_at' => new Carbon($tBoard->time), 'created_by' => $boardOwner, 'operated_by' => $boardOwner, 'posts_total' => $tBoard->posts_total, 'is_indexed' => !!$tBoard->indexed, 'is_overboard' => !!$tBoard->indexed, 'is_worksafe' => !!$tBoard->sfw]);
             if ($hBoard->save()) {
                 ++$boardsImported;
                 if (!$tBoard->public_bans || !$tBoard->public_logs || $tBoard->public_logs == 2) {
                     $role = Role::getAnonymousRoleForBoard($hBoard);
                     $perms = [];
                     if (!$tBoard->public_bans) {
                         $perms[] = ['permission_id' => "board.bans", 'value' => false];
                     }
                     if (!$tBoard->public_logs || $tBoard->public_logs == 2) {
                         $perms[] = ['permission_id' => "board.logs", 'value' => false];
                     }
                     $role->permissionAssignments()->createMany($perms);
                 }
             } else {
                 $this->error("Failed to save /{$hBoard->board_uri}/.");
             }
         }
     });
     $this->info("\tImported {$boardsImported} board(s).");
     unset($boards, $tBoard, $hBoard);
     # BEGIN ROLE CONFIGURATION
     $this->info("\tCreating roles ...");
     $roleBoardVols = 0;
     $roleBoardOwners = 0;
     $roleBoardSkips = 0;
     // Okay, so apparently sometimes there are users that own boards that don't exist.
     // We cannot crate roles for these boards, so lets prune useless data.
     $boardsWeCareAbout = Board::select('board_uri')->whereIn('board_uri', array_keys($userBoardRelationships))->get()->pluck('board_uri');
     if ($boardsWeCareAbout->count() != count($userBoardRelationships)) {
         $this->comment("\t\tThere are " . (count($userBoardRelationships) - $boardsWeCareAbout->count()) . " board(s) which users own that do not exist!");
     }
     foreach ($boardsWeCareAbout as $board) {
         $ownerRole = null;
         $janitorRole = null;
         $roles = $userBoardRelationships[$board];
         foreach ($roles as $mod => $role) {
             // TODO
             // Pull these values from config when formal importer created.
             switch ($role) {
                 // Board volunteer
                 case 19:
                     $janitorRole = $janitorRole ?: Role::getJanitorRoleForBoard($board);
                     $userRole = new UserRole(['user_id' => $mod, 'role_id' => $janitorRole->role_id]);
                     if ($userRole->save()) {
                         ++$roleBoardVols;
                     }
                     break;
                     // Board owner
                 // Board owner
                 case 20:
                     $ownerRole = $ownerRole ?: Role::getOwnerRoleForBoard($board);
                     $userRole = new UserRole(['user_id' => $mod, 'role_id' => $ownerRole->role_id]);
                     if ($userRole->save()) {
                         ++$roleBoardOwners;
                     }
                     break;
                 default:
                     $this->line("\t\tI don't know what to do with role {$role} for user id {$mod} in {$board}.");
                     break;
             }
         }
     }
     $this->info("\t\tCreated {$roleBoardOwners} owner(s) and {$roleBoardVols} janitor(s) ");
     $this->info("\t\tPulled roles from " . count($userBoardRelationships) . " boards with relationships.");
     unset($board, $roles, $mod, $role, $userRole, $janitorRole, $ownerRole);
 }
예제 #3
0
 /**
  * Allows for the creation of a new board.
  *
  * @return Response
  */
 public function putCreate(Request $request)
 {
     if (!$this->user->canCreateBoard()) {
         return abort(403);
     }
     $configErrors = [];
     // Check time and quantity restraints.
     if (!$this->user->canAdminConfig()) {
         $boardLastCreated = null;
         $boardsOwned = 0;
         $boardCreateTimer = $this->option('boardCreateTimer');
         $boardsCreateMax = $this->option('boardCreateMax');
         if (!$this->user->isAnonymous()) {
             foreach ($this->user->createdBoards as $board) {
                 ++$boardsOwned;
                 if (is_null($boardLastCreated) || $board->created_at->timestamp > $boardLastCreated->timestamp) {
                     $boardLastCreated = $board->created_at;
                 }
             }
         }
         if ($boardsCreateMax > 0 && $boardsOwned >= $boardsCreateMax) {
             $configErrors[] = Lang::choice("panel.error.board.create_more_than_max", $boardsCreateMax, ['boardsCreateMax' => $boardsCreateMax]);
         }
         if ($boardCreateTimer > 0 && (!is_null($boardLastCreated) && $boardLastCreated->diffInMinutes() < $boardCreateTimer)) {
             $configErrors[] = Lang::choice("panel.error.board.create_so_soon", $boardLastCreated->addMinutes($boardCreateTimer)->diffInMinutes() + 1, ['boardCreateTimer' => $boardLastCreated->diffInMinutes()]);
         }
     }
     if (count($configErrors)) {
         return redirect()->back()->withInput()->withErrors($configErrors);
     }
     // Validate input.
     // If the user is anonymous, we must also be creating an account.
     $input = Input::all();
     if ($this->user->isAnonymous()) {
         $validator = $this->registrar->validator($input);
         if ($validator->fails()) {
             $this->throwValidationException($request, $validator);
         }
     }
     // Generate a list of banned URIs.
     $bannedUris = array_filter(explode("\n", $this->option('boardUriBanned')));
     $bannedUris[] = "cp";
     $bannedUris = implode(",", $bannedUris);
     // Validate the basic boardconstraints.
     $input['board_uri'] = strtolower((string) $input['board_uri']);
     $requirements = ['board_uri' => ["required", "unique:boards,board_uri", "not_in:{$bannedUris}", "string", "regex:(" . Board::URI_PATTERN . ")"], 'title' => "required|string|between:1,255", 'description' => "string|between:0,255"];
     $validator = Validator::make($input, $requirements);
     if ($validator->fails()) {
         $this->throwValidationException($request, $validator);
     }
     if ($this->user->isAnonymous()) {
         $this->auth->login($this->registrar->create($request->all()));
         $this->user = $this->auth->user();
     }
     // Create the board.
     $board = new Board(['board_uri' => $input['board_uri'], 'title' => $input['title'], 'description' => $input['description'], 'created_by' => $this->user->user_id, 'operated_by' => $this->user->user_id]);
     $board->save();
     // Seed board ownership permissions.
     $board->setOwner($this->user);
     $this->log("log.board.create", $board->board_uri);
     Event::fire(new BoardWasCreated($board, $this->user));
     return redirect("cp/board/{$board->board_uri}");
 }