/** * {@inheritdoc} */ protected function data(ServerRequestInterface $request, Document $document) { $actor = $request->getAttribute('actor'); $discussionId = array_get($request->getQueryParams(), 'id'); $data = array_get($request->getParsedBody(), 'data', []); $discussion = $this->bus->dispatch(new EditDiscussion($discussionId, $actor, $data)); // TODO: Refactor the ReadDiscussion (state) command into EditDiscussion? // That's what extensions will do anyway. if ($readNumber = array_get($data, 'attributes.readNumber')) { $state = $this->bus->dispatch(new ReadDiscussion($discussionId, $actor, $readNumber)); $discussion = $state->discussion; } if ($posts = $discussion->getModifiedPosts()) { $discussionPosts = $discussion->postsVisibleTo($actor)->orderBy('time')->lists('id')->all(); foreach ($discussionPosts as &$id) { foreach ($posts as $post) { if ($id == $post->id) { $id = $post; $post->discussion = $post->discussion_id; $post->user = $post->user_id; } } } $discussion->setRelation('posts', $discussionPosts); $this->include = array_merge($this->include, ['posts', 'posts.discussion', 'posts.user']); } return $discussion; }
/** * {@inheritdoc} */ protected function delete(Request $request) { $id = $request->get('id'); $actor = $request->actor; $input = $request->all(); $this->bus->dispatch(new DeleteDiscussion($id, $actor, $input)); }
/** * {@inheritdoc} */ protected function data(ServerRequestInterface $request, Document $document) { $id = array_get($request->getQueryParams(), 'id'); $actor = $request->getAttribute('actor'); $data = array_get($request->getParsedBody(), 'data'); return $this->bus->dispatch(new EditLink($id, $actor, $data)); }
/** * Get the data to be serialized and assigned to the response document. * * @param ServerRequestInterface $request * @param Document $document * @return mixed */ protected function data(ServerRequestInterface $request, Document $document) { $postId = array_get($request->getQueryParams(), 'post'); $actor = $request->getAttribute('actor'); $file = array_get($request->getParsedBody(), 'image'); return $this->bus->dispatch(new UploadImage($postId, base64_decode($file), $actor)); }
/** * {@inheritdoc} */ protected function data(ServerRequestInterface $request, Document $document) { $id = array_get($request->getQueryParams(), 'id'); $actor = $request->getAttribute('actor'); $file = array_get($request->getUploadedFiles(), 'avatar'); return $this->bus->dispatch(new UploadAvatar($id, $file, $actor)); }
/** * @param StartDiscussion $command * @return mixed * @throws Exception */ public function handle(StartDiscussion $command) { $actor = $command->actor; $data = $command->data; $this->assertCan($actor, 'startDiscussion'); // Create a new Discussion entity, persist it, and dispatch domain // events. Before persistence, though, fire an event to give plugins // an opportunity to alter the discussion entity based on data in the // command they may have passed through in the controller. $discussion = Discussion::start(array_get($data, 'attributes.title'), $actor); $this->events->fire(new DiscussionWillBeSaved($discussion, $actor, $data)); $this->validator->assertValid($discussion->getAttributes()); $discussion->save(); // Now that the discussion has been created, we can add the first post. // We will do this by running the PostReply command. try { $post = $this->bus->dispatch(new PostReply($discussion->id, $actor, $data)); } catch (Exception $e) { $discussion->delete(); throw $e; } // Before we dispatch events, refresh our discussion instance's // attributes as posting the reply will have changed some of them (e.g. // last_time.) $discussion->setRawAttributes($post->discussion->getAttributes(), true); $discussion->setStartPost($post); $discussion->setLastPost($post); $this->dispatchEventsFor($discussion, $actor); $discussion->save(); return $discussion; }
/** * {@inheritdoc} */ protected function delete(ServerRequestInterface $request) { $id = array_get($request->getQueryParams(), 'id'); $actor = $request->getAttribute('actor'); $input = $request->getParsedBody(); $this->bus->dispatch(new DeleteDiscussion($id, $actor, $input)); }
/** * Respond with JavaScript to inform the Flarum app about the user's * authentication status. * * An array of identification attributes must be passed as the first * argument. These are checked against existing user accounts; if a match is * found, then the user is authenticated and logged into that account via * cookie. The Flarum app will then simply refresh the page. * * If no matching account is found, then an AuthToken will be generated to * store the identification attributes. This token, along with an optional * array of suggestions, will be passed into the Flarum app's sign up modal. * This results in the user not having to choose a password. When they * complete their registration, the identification attributes will be * set on their new user account. * * @param array $identification * @param array $suggestions * @return HtmlResponse */ protected function authenticated(array $identification, array $suggestions = []) { $user = User::where($identification)->first(); // If a user with these attributes already exists, then we will log them // in by generating an access token. Otherwise, we will generate a // unique token for these attributes and add it to the response, along // with the suggested account information. if ($user) { $accessToken = $this->bus->dispatch(new GenerateAccessToken($user->id)); $payload = ['authenticated' => true]; } else { $token = AuthToken::generate($identification); $token->save(); $payload = array_merge($identification, $suggestions, ['token' => $token->id]); } $content = sprintf('<script> window.opener.app.authenticationComplete(%s); window.close(); </script>', json_encode($payload)); $response = new HtmlResponse($content); if (isset($accessToken)) { // Extend the token's expiry to 2 weeks so that we can set a // remember cookie $accessToken::unguard(); $accessToken->update(['expires_at' => new DateTime('+2 weeks')]); $response = $this->withRememberCookie($response, $accessToken->id); } return $response; }
/** * Get the data to be serialized and assigned to the response document. * * @param ServerRequestInterface $request * @param Document $document * @return mixed */ protected function data(ServerRequestInterface $request, Document $document) { $title = Arr::get($request->getParsedBody(), 'title'); $start_post_id = Arr::get($request->getParsedBody(), 'start_post_id'); $end_post_id = Arr::get($request->getParsedBody(), 'end_post_id'); $actor = $request->getAttribute('actor'); return $this->bus->dispatch(new SplitDiscussion($title, $start_post_id, $end_post_id, $actor)); }
/** * @param Request $request * @param array $routeParams * @return \Psr\Http\Message\ResponseInterface */ public function handle(Request $request, array $routeParams = []) { try { $token = array_get($routeParams, 'token'); $user = $this->bus->dispatch(new ConfirmEmail($token)); } catch (InvalidConfirmationTokenException $e) { return new HtmlResponse('Invalid confirmation token'); } $token = $this->bus->dispatch(new GenerateAccessToken($user->id)); return $this->withRememberCookie($this->redirectTo('/'), $token->id); }
/** * @param Request $request * @return \Psr\Http\Message\ResponseInterface */ public function handle(Request $request) { try { $token = array_get($request->getQueryParams(), 'token'); $user = $this->bus->dispatch(new ConfirmEmail($token)); } catch (InvalidConfirmationTokenException $e) { return new HtmlResponse('Invalid confirmation token'); } $token = $this->bus->dispatch(new GenerateAccessToken($user->id)); return $this->withRememberCookie(new RedirectResponse($this->app->url()), $token->id); }
/** * Create a discussion according to input from the API request. * * @param JsonApiRequest $request * @return \Flarum\Core\Discussions\Discussion */ protected function create(JsonApiRequest $request) { $actor = $request->actor; $discussion = $this->bus->dispatch(new StartDiscussion($actor, $request->get('data'))); // After creating the discussion, we assume that the user has seen all // of the posts in the discussion; thus, we will mark the discussion // as read if they are logged in. if ($actor->exists) { $this->bus->dispatch(new ReadDiscussion($discussion->id, $actor, 1)); } return $discussion; }
/** * @param Request $request * @return \Psr\Http\Message\ResponseInterface */ public function handle(Request $request) { try { $token = array_get($request->getQueryParams(), 'token'); $user = $this->bus->dispatch(new ConfirmEmail($token)); } catch (InvalidConfirmationTokenException $e) { return new HtmlResponse('Invalid confirmation token'); } $session = $request->getAttribute('session'); $this->authenticator->logIn($session, $user->id); return new RedirectResponse($this->app->url()); }
/** * {@inheritdoc} */ protected function data(ServerRequestInterface $request, Document $document) { $actor = $request->getAttribute('actor'); $discussion = $this->bus->dispatch(new StartDiscussion($actor, array_get($request->getParsedBody(), 'data'))); // After creating the discussion, we assume that the user has seen all // of the posts in the discussion; thus, we will mark the discussion // as read if they are logged in. if ($actor->exists) { $this->bus->dispatch(new ReadDiscussion($discussion->id, $actor, 1)); } return $discussion; }
/** * Update a discussion according to input from the API request, and return * it ready to be serialized and assigned to the JsonApi response. * * @param \Flarum\Api\JsonApiRequest $request * @param \Flarum\Api\JsonApiResponse $response * @return \Flarum\Core\Models\Discussion */ protected function data(JsonApiRequest $request, JsonApiResponse $response) { $user = $request->actor->getUser(); $discussionId = $request->get('id'); if ($data = array_except($request->get('data'), ['readNumber'])) { $discussion = $this->bus->dispatch(new EditDiscussionCommand($discussionId, $user, $data)); } if ($readNumber = $request->get('data.readNumber')) { $state = $this->bus->dispatch(new ReadDiscussionCommand($discussionId, $user, $readNumber)); $discussion = $state->discussion; } return $discussion; }
/** * Reply to a discussion according to input from the API request. * * @param \Flarum\Api\JsonApiRequest $request * @param \Flarum\Api\JsonApiResponse $response * @return \Flarum\Core\Models\Post */ protected function create(JsonApiRequest $request, JsonApiResponse $response) { $user = $request->actor->getUser(); $discussionId = $request->get('data.links.discussion.linkage.id'); $post = $this->bus->dispatch(new PostReplyCommand($discussionId, $user, $request->get('data'))); // After replying, we assume that the user has seen all of the posts // in the discussion; thus, we will mark the discussion as read if // they are logged in. if ($user->exists) { $this->bus->dispatch(new ReadDiscussionCommand($discussionId, $user, $post->number)); } return $post; }
/** * {@inheritdoc} */ protected function data(ServerRequestInterface $request, Document $document) { $id = array_get($request->getQueryParams(), 'id'); $actor = $request->getAttribute('actor'); $data = array_get($request->getParsedBody(), 'data', []); // Require the user's current password if they are attempting to change // their own email address. if (isset($data['attributes']['email']) && $actor->id == $id) { $password = array_get($request->getParsedBody(), 'meta.password'); if (!$actor->checkPassword($password)) { throw new PermissionDeniedException(); } } return $this->bus->dispatch(new EditUser($id, $actor, $data)); }
/** * Reply to a discussion according to input from the API request. * * @param JsonApiRequest $request * @return \Flarum\Core\Posts\Post */ protected function create(JsonApiRequest $request) { $actor = $request->actor; $discussionId = $request->get('data.relationships.discussion.data.id'); $post = $this->bus->dispatch(new PostReply($discussionId, $actor, $request->get('data'))); // After replying, we assume that the user has seen all of the posts // in the discussion; thus, we will mark the discussion as read if // they are logged in. if ($actor->exists) { $this->bus->dispatch(new ReadDiscussion($discussionId, $actor, $post->number)); } $discussion = $post->discussion; $discussion->posts_ids = $discussion->postsVisibleTo($actor)->orderBy('time')->lists('id'); return $post; }
/** * {@inheritdoc} */ public function handle(ServerRequestInterface $request) { $body = $request->getParsedBody(); $identification = array_get($body, 'identification'); $password = array_get($body, 'password'); $user = $this->users->findByIdentification($identification); if (!$user || !$user->checkPassword($password)) { throw new PermissionDeniedException(); } if (!$user->is_activated) { $this->events->fire(new UserEmailChangeWasRequested($user, $user->email)); return new JsonResponse(['emailConfirmationRequired' => $user->email], 401); } $token = $this->bus->dispatch(new GenerateAccessToken($user->id)); return new JsonResponse(['token' => $token->id, 'userId' => $user->id]); }
/** * @param Request $request * @param array $routeParams * * @return JsonResponse */ public function handle(Request $request, array $routeParams = []) { $params = ['data' => ['attributes' => $request->getAttributes()]]; $apiResponse = $this->apiClient->send(app('flarum.actor'), 'Flarum\\Api\\Actions\\Users\\CreateAction', $params); $body = $apiResponse->getBody(); $statusCode = $apiResponse->getStatusCode(); $response = new JsonResponse($body, $statusCode); if (!empty($body->data->attributes->isActivated)) { $token = $this->bus->dispatch(new GenerateAccessToken($body->data->id)); // Extend the token's expiry to 2 weeks so that we can set a // remember cookie AccessToken::where('id', $token->id)->update(['expires_at' => new DateTime('+2 weeks')]); return $this->withRememberCookie($response, $token->id); } return $response; }
/** * {@inheritdoc} */ protected function data(ServerRequestInterface $request, Document $document) { $actor = $request->getAttribute('actor'); $ipAddress = array_get($request->getServerParams(), 'REMOTE_ADDR', '127.0.0.1'); if (!$request->getAttribute('bypassFloodgate')) { $this->floodgate->assertNotFlooding($actor); } $discussion = $this->bus->dispatch(new StartDiscussion($actor, array_get($request->getParsedBody(), 'data', []), $ipAddress)); // After creating the discussion, we assume that the user has seen all // of the posts in the discussion; thus, we will mark the discussion // as read if they are logged in. if ($actor->exists) { $this->bus->dispatch(new ReadDiscussion($discussion->id, $actor, 1)); } return $discussion; }
/** * {@inheritdoc} */ protected function data(ServerRequestInterface $request, Document $document) { $actor = $request->getAttribute('actor'); $data = array_get($request->getParsedBody(), 'data'); $discussionId = array_get($data, 'relationships.discussion.data.id'); $post = $this->bus->dispatch(new PostReply($discussionId, $actor, $data)); // After replying, we assume that the user has seen all of the posts // in the discussion; thus, we will mark the discussion as read if // they are logged in. if ($actor->exists) { $this->bus->dispatch(new ReadDiscussion($discussionId, $actor, $post->number)); } $discussion = $post->discussion; $discussion->posts = $discussion->postsVisibleTo($actor)->orderBy('time')->lists('id'); return $post; }
/** * Run the database seeds. * * @return void */ public function run() { $faker = Faker\Factory::create(); $tags = Tag::all(); $skills = Skill::all(); for ($i = 0; $i < 50; $i++) { $user = User::register($faker->unique()->userName, $faker->unique()->email, bcrypt('password'), 'talent'); $this->userRepository->save($user); $profileData = ['first_name' => $faker->firstName, 'last_name' => $faker->lastName, 'location' => $faker->city, 'describe' => $faker->numberBetween(1, count($skills) - 1), 'about' => $faker->sentence(), 'facebook' => $faker->userName, 'linked_in' => $faker->userName, 'twitter' => $faker->userName, 'meetup' => $faker->userName, 'published' => $faker->boolean()]; $userSkills = ''; foreach (range(1, rand(2, 4)) as $x) { $id = rand(1, count($tags) - 1); $userSkills .= $tags[$id]->name . ","; } $profileData['skills'] = $userSkills; $this->dispatcher->dispatch(new UpdateProfile($user, $profileData)); } }
/** * Execute the console command. * * @return mixed */ public function handle(Dispatcher $dispatcher) { $id = \App\Domain\ValueObject\UUID::make(); $title = new \App\Domain\ValueObject\String\NonBlank($this->argument("title")); $author = new \App\Domain\ValueObject\String\NonBlank($this->argument("github_username")); $post = \App\Domain\ValueObject\Post::make($id, $title, $author); $dispatcher->dispatch(new \App\Commands\CreatePost($post)); $this->info("Post {$id} created."); }
/** * @param Request $request * @param array $routeParams * * @return JsonResponse */ public function handle(Request $request, array $routeParams = []) { $controller = 'Flarum\\Api\\Controller\\CreateUserController'; $actor = $request->getAttribute('actor'); $body = ['data' => ['attributes' => $request->getParsedBody()]]; $response = $this->api->send($controller, $actor, [], $body); $body = json_decode($response->getBody()); $statusCode = $response->getStatusCode(); $response = new JsonResponse($body, $statusCode); if (!empty($body->data->attributes->isActivated)) { $token = $this->bus->dispatch(new GenerateAccessToken($body->data->id)); // Extend the token's expiry to 2 weeks so that we can set a // remember cookie AccessToken::where('id', $token->id)->update(['expires_at' => new DateTime('+2 weeks')]); return $this->withRememberCookie($response, $token->id); } return $response; }
/** * Update a discussion according to input from the API request, and return * it ready to be serialized and assigned to the JsonApi response. * * @param JsonApiRequest $request * @param Document $document * @return \Flarum\Core\Discussions\Discussion */ protected function data(JsonApiRequest $request, Document $document) { $actor = $request->actor; $discussionId = $request->get('id'); $data = $request->get('data'); $discussion = $this->bus->dispatch(new EditDiscussion($discussionId, $actor, $data)); // TODO: Refactor the ReadDiscussion (state) command into EditDiscussion? // That's what extensions will do anyway. if ($readNumber = array_get($data, 'attributes.readNumber')) { $state = $this->bus->dispatch(new ReadDiscussion($discussionId, $actor, $readNumber)); $discussion = $state->discussion; } if ($posts = $discussion->getModifiedPosts()) { $discussion->posts_ids = $discussion->postsVisibleTo($actor)->orderBy('time')->lists('id'); $discussion->posts = array_filter($posts, function ($post) { return $post->exists; }); $request->include = array_merge($request->include, ['posts']); $request->link = array_merge($request->include, ['posts', 'posts.discussion', 'posts.user']); } return $discussion; }
/** * @param Request $request * @param array $routeParams * @return \Psr\Http\Message\ResponseInterface */ public function handle(Request $request, array $routeParams = []) { $input = $request->getParsedBody(); $data = new DefaultData(); $data->setDatabaseConfiguration(['driver' => 'mysql', 'host' => array_get($input, 'mysqlHost'), 'database' => array_get($input, 'mysqlDatabase'), 'username' => array_get($input, 'mysqlUsername'), 'password' => array_get($input, 'mysqlPassword'), 'prefix' => array_get($input, 'tablePrefix')]); $data->setAdminUser(['username' => array_get($input, 'adminUsername'), 'password' => array_get($input, 'adminPassword'), 'password_confirmation' => array_get($input, 'adminPasswordConfirmation'), 'email' => array_get($input, 'adminEmail')]); $baseUrl = rtrim((string) $request->getAttribute('originalUri'), '/'); $data->setBaseUrl($baseUrl); $data->setSetting('forum_title', array_get($input, 'forumTitle')); $data->setSetting('mail_from', 'noreply@' . preg_replace('/^www\\./i', '', parse_url($baseUrl, PHP_URL_HOST))); $data->setSetting('welcome_title', 'Welcome to ' . array_get($input, 'forumTitle')); $body = fopen('php://temp', 'wb+'); $input = new StringInput(''); $output = new StreamOutput($body); $this->command->setDataSource($data); try { $this->command->run($input, $output); } catch (Exception $e) { return new JsonResponse(['error' => $e->getMessage()], 500); } $token = $this->bus->dispatch(new GenerateAccessToken(1)); $token->update(['expires_at' => new DateTime('+2 weeks')]); return $this->withRememberCookie(new Response($body, 200), $token->id); }
/** * @param JsonApiRequest $request * @param Document $document * @return \Flarum\Core\Tags\Tag */ protected function data(JsonApiRequest $request, Document $document) { return $this->bus->dispatch(new EditTag($request->get('id'), $request->actor, $request->get('data'))); }
/** * Parse the file in chunks and queues the processing of each chunk * * @param int $size * @param callable $callback * @param bool $shouldQueue */ public function chunk($size = 10, callable $callback, $shouldQueue = true) { // Get total rows $totalRows = $this->getTotalRowsOfFile(); $break = false; for ($startRow = 0; $startRow < $totalRows; $startRow += $chunkSize) { // Set start index $startIndex = $startRow == 0 ? $startRow : $startRow - 1; $chunkSize = $startRow == 0 ? $size + 1 : $size; $job = new ChunkedReadJob($this->file, $this->reader->getLoadSheetsOnly(), $startRow, $startIndex, $chunkSize, $callback, $shouldQueue); if ($shouldQueue) { $this->dispatcher->dispatch($job); } else { $break = $job->handle(); } if ($break === true) { break; } } }
/** * {@inheritdoc} */ protected function delete(ServerRequestInterface $request) { $this->bus->dispatch(new DeleteGroup(array_get($request->getQueryParams(), 'id'), $request->getAttribute('actor'))); }