예제 #1
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}");
 }