예제 #1
0
    /**
     * 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;
    }
예제 #2
0
 /**
  * @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;
 }
 /**
  * Call the failed method on the job instance.
  *
  * @param  array  $data
  * @return void
  */
 public function failed(array $data)
 {
     $handler = $this->dispatcher->resolveHandler($command = unserialize($data['command']));
     if (method_exists($handler, 'failed')) {
         call_user_func([$handler, 'failed'], $command);
     }
 }
 /**
  * {@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));
 }
예제 #5
0
 /**
  * {@inheritdoc}
  */
 protected function delete(Request $request)
 {
     $id = $request->get('id');
     $actor = $request->actor;
     $input = $request->all();
     $this->bus->dispatch(new DeleteDiscussion($id, $actor, $input));
 }
예제 #6
0
 /**
  * {@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));
 }
예제 #7
0
 /**
  * {@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));
 }
예제 #8
0
 /**
  * Unfallow a User
  *
  * @param $userIdToUnfallow
  * @param Request $request
  * @return \Illuminate\Http\RedirectResponse
  */
 public function destroy($userIdToUnfallow, Request $request)
 {
     $request->replace(array_add($request->all(), 'userId', Auth::id()));
     $this->dispatcher->dispatchFrom(UnfallowUser::class, $request);
     Flash::success('You have now unfallowed this user');
     return Redirect::back();
 }
 /**
  * {@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;
 }
 /**
  * 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));
 }
 /**
  * @param Requests\SignUpRequest $request
  * @param CommandDispatcher $commandDispatcher
  *
  * @return
  */
 public function store(Requests\SignUpRequest $request, CommandDispatcher $commandDispatcher)
 {
     $commandDispatcher->dispatchFrom(RegisterUser::class, $request);
     \Auth::login(User::where('username', $request['username'])->first());
     Flash::overlay('Welcome!!');
     return Redirect::home();
 }
예제 #12
0
 /**
  * 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));
 }
예제 #13
0
 /**
  * Handle the queued job.
  *
  * @param  \Illuminate\Contracts\Queue\Job  $job
  * @param  array  $data
  * @return void
  */
 public function call(Job $job, array $data)
 {
     $command = $this->setJobInstanceIfNecessary($job, unserialize($data['command']));
     $this->dispatcher->dispatchNow($command);
     if (!$job->isDeletedOrReleased()) {
         $job->delete();
     }
 }
예제 #14
0
 /**
  * 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.");
 }
예제 #15
0
 public function boot(Dispatcher $dispatcher)
 {
     $dispatcher->mapUsing(function ($command) {
         $command = str_replace('Commands\\', 'Commands\\Handlers\\', get_class($command));
         return trim($command, '\\') . 'Handler@handle';
     });
     $this->registerMiddleware($this->app['router']);
     $this->registerModuleResourceNamespaces();
     $this->setLocalesConfigurations();
 }
예제 #16
0
 public function execute($files)
 {
     if (empty($this->dir)) {
         throw new UploadSettingsException();
     }
     $this->dispatcher->pipeThrough(['EscapeWork\\LaravelSteroids\\Upload\\NormalizeJob', 'EscapeWork\\LaravelSteroids\\Upload\\ValidateJob', 'EscapeWork\\LaravelSteroids\\Upload\\MoveJob']);
     $dispatched = $this->dispatch(new UploadJob($files, $this->dir));
     $this->dispatcher->pipeThrough([]);
     return $dispatched;
 }
예제 #17
0
 /**
  * @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);
 }
예제 #18
0
 /**
  * @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);
 }
예제 #19
0
 /**
  * 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;
 }
 /**
  * {@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;
 }
예제 #21
0
 /**
  * @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());
 }
예제 #22
0
 /**
  * Handle the queued job.
  *
  * @param  \Illuminate\Contracts\Queue\Job  $job
  * @param  array  $data
  * @return void
  */
 public function call(Job $job, array $data)
 {
     $command = $this->setJobInstanceIfNecessary($job, unserialize($data['command']));
     $handler = $this->dispatcher->getCommandHandler($command) ?: null;
     if ($handler) {
         $this->setJobInstanceIfNecessary($job, $handler);
     }
     $this->dispatcher->dispatchNow($command, $handler);
     if (!$job->isDeletedOrReleased()) {
         $job->delete();
     }
 }
예제 #23
0
 /**
  * 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;
 }
예제 #24
0
 /**
  * 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;
 }
예제 #25
0
 /**
  * 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;
 }
예제 #26
0
 /**
  * {@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));
 }
예제 #27
0
 /**
  * {@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;
 }
예제 #28
0
 /**
  * @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;
 }
예제 #29
0
 /**
  * {@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;
 }
예제 #30
0
 /**
  * {@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]);
 }