public function it_caches_new_data(SettingsRepository $inner) { $this->set('b', 2); $this->get('b')->shouldReturn(2); $inner->all()->shouldNotHaveBeenCalled(); $inner->get('b')->shouldNotHaveBeenCalled(); }
public function addAssets(BuildClientView $event) { if ($event->action instanceof ForumClientAction) { if ($this->settings->get('default_locale')) { $rawJs = file_get_contents(realpath(__DIR__ . '/../../assets/js/google-translate.js')); $js = str_replace("%%DEFAULT_LOCAL%%", $this->settings->get('default_locale'), $rawJs); $event->view->getAssets()->addJs(function () use($js) { return $js; }); } } }
/** * Get the forum, ready to be serialized and assigned to the JsonApi * response. * * @param JsonApiRequest $request * @param Document $document * @return \Flarum\Core\Forum */ protected function data(JsonApiRequest $request, Document $document) { if (!$request->actor->isAdmin()) { throw new PermissionDeniedException(); } $config = $request->get('data.attributes.config'); if (is_array($config)) { foreach ($config as $k => $v) { $this->settings->set($k, $v); } } return app('flarum.forum'); }
/** * Get the data that should be made available to email templates. * * @param User $user * @param string $email * * @return array */ protected function getEmailData(User $user, $email) { $token = $this->generateToken($user, $email); // TODO: Need to use UrlGenerator, but since this is part of core we // don't know that the forum routes will be loaded. Should the confirm // email route be part of core?? return ['username' => $user->username, 'url' => Core::url() . '/confirm/' . $token->id, 'forumTitle' => $this->settings->get('forum_title')]; }
/** * @param RequestPasswordReset $command * @return \Flarum\Core\Users\User * @throws ModelNotFoundException */ public function handle(RequestPasswordReset $command) { $user = $this->users->findByEmail($command->email); if (!$user) { throw new ModelNotFoundException(); } $token = PasswordToken::generate($user->id); $token->save(); // TODO: Need to use UrlGenerator, but since this is part of core we // don't know that the forum routes will be loaded. Should the reset // password route be part of core?? $data = ['username' => $user->username, 'url' => Core::url() . '/reset/' . $token->id, 'forumTitle' => $this->settings->get('forum_title')]; $this->mailer->send(['text' => 'flarum::emails.resetPassword'], $data, function (Message $message) use($user) { $message->to($user->email); $message->subject('Reset Your Password'); }); return $user; }
/** * @param Request $request * @param array $routeParams * @return RedirectResponse|EmptyResponse */ public function handle(Request $request, array $routeParams = []) { session_start(); $provider = new Github(['clientId' => $this->settings->get('github.client_id'), 'clientSecret' => $this->settings->get('github.client_secret'), 'redirectUri' => $this->url->toRoute('github.login')]); if (!isset($_GET['code'])) { $authUrl = $provider->getAuthorizationUrl(['scope' => ['user:email']]); $_SESSION['oauth2state'] = $provider->getState(); return new RedirectResponse($authUrl); } elseif (empty($_GET['state']) || $_GET['state'] !== $_SESSION['oauth2state']) { unset($_SESSION['oauth2state']); echo 'Invalid state.'; exit; } $token = $provider->getAccessToken('authorization_code', ['code' => $_GET['code']]); $owner = $provider->getResourceOwner($token); $email = $owner->getEmail(); $username = preg_replace('/[^a-z0-9-_]/i', '', $owner->getNickname()); return $this->authenticated(compact('email'), compact('username')); }
/** * {@inheritdoc} */ public function handle(Request $request, array $routeParams = []) { if (!$request->actor->isAdmin()) { throw new PermissionDeniedException(); } $config = $request->get('config', []); // TODO: throw HTTP status 400 or 422 if (!is_array($config)) { throw new Exception(); } foreach ($config as $k => $v) { $this->settings->set($k, $v); if (strpos($k, 'theme_') === 0 || $k === 'custom_less') { $forum = app('Flarum\\Forum\\Actions\\ClientAction'); $forum->flushAssets(); $admin = app('Flarum\\Admin\\Actions\\ClientAction'); $admin->flushAssets(); } } return new EmptyResponse(204); }
/** * Get the name of the locale to use. * * @param User $actor * @param Request $request * @return string */ protected function getLocale(User $actor, Request $request) { if ($actor->exists) { $locale = $actor->getPreference('locale'); } else { $locale = array_get($request->getCookieParams(), 'locale'); } if (!$locale || !$this->locales->hasLocale($locale)) { return $this->settings->get('default_locale', 'en'); } return $locale; }
/** * @param RegisterUser $command * * @throws PermissionDeniedException if signup is closed and the actor is * not an administrator. * @throws \Flarum\Core\Exceptions\InvalidConfirmationTokenException if an * email confirmation token is provided but is invalid. * * @return User */ public function handle(RegisterUser $command) { $actor = $command->actor; $data = $command->data; if (!$this->settings->get('allow_sign_up') && !$actor->isAdmin()) { throw new PermissionDeniedException(); } $username = array_get($data, 'attributes.username'); $email = array_get($data, 'attributes.email'); $password = array_get($data, 'attributes.password'); // If a valid authentication token was provided as an attribute, // then we won't require the user to choose a password. if (isset($data['attributes']['token'])) { $token = AuthToken::validOrFail($data['attributes']['token']); $password = $password ?: str_random(20); } $user = User::register($username, $email, $password); // If a valid authentication token was provided, then we will assign // the attributes associated with it to the user's account. If this // includes an email address, then we will activate the user's account // from the get-go. if (isset($token)) { foreach ($token->payload as $k => $v) { $user->{$k} = $v; } if (isset($token->payload['email'])) { $user->activate(); } } event(new UserWillBeSaved($user, $actor, $data)); $user->save(); if (isset($token)) { $token->delete(); } $this->dispatchEventsFor($user); return $user; }
/** * Handle upload requests * @param Request $request * @todo Add upload event * @todo Add upload permissions */ public function handle(Request $request) { $images = $request->http->getUploadedFiles()['images']; $results = []; // get driver name $driver_list = ['local' => '\\S12g\\ImageAttachments\\Drivers\\Local', 'qiniu' => '\\S12g\\ImageAttachments\\Drivers\\Qiniu', 'tietuku' => '\\S12g\\ImageAttachments\\Drivers\\Tietuku', 'imgur' => '\\S12g\\ImageAttachments\\Drivers\\Imgur', 'upyun' => '\\S12g\\ImageAttachments\\Drivers\\Upyun']; // return driver name if in admin and no images if ($request->actor->isAdmin() && !is_array($images)) { foreach ($driver_list as $name => $driver) { $results[$name] = ['config' => $driver::getConfigItems() ?: null, 'title' => $driver::title]; } return new JsonResponse($results); } $driver_name = $this->settings->get('imageattachments.driver') ?: 'local'; $driver = $driver_list[$driver_name]; if (!$driver) { $driver_name = 'local'; $driver = $driver_list['local']; } // get config $config = $this->settings->get('imageattachments.' . $driver_name . '.config'); $config = json_decode($config, true) ?: []; // get driver $fs = new $driver($config); // save images try { foreach ($images as $image_key => $image) { $tmpFile = tempnam(sys_get_temp_dir(), 'image'); $image->moveTo($tmpFile); $results['img_' . $image_key] = $fs->saveImage($tmpFile); @unlink($tmpFile); } } catch (Exception $e) { throw $e; } return new JsonResponse($results); }