Added an option to force visitors to log-in themselves
authorAlexander Ebert <ebert@woltlab.com>
Wed, 2 Aug 2017 10:45:17 +0000 (12:45 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 2 Aug 2017 10:45:24 +0000 (12:45 +0200)
Closes #2329

com.woltlab.wcf/option.xml
com.woltlab.wcf/templates/login.tpl
constants.php
wcfsetup/install/files/lib/form/LoginForm.class.php
wcfsetup/install/files/lib/page/AbstractPage.class.php
wcfsetup/install/files/options.inc.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index e608a251f32e4406ccd77d0c207ed618b0341a70..9c49adafa55762422fa32708f231516e7b7d1e67 100644 (file)
@@ -1122,6 +1122,10 @@ Pinterest</defaultvalue>
                        <!-- /user.ban -->
                        
                        <!-- user.register -->
+                       <option name="force_login">
+                               <categoryname>user.register</categoryname>
+                               <optiontype>boolean</optiontype>
+                       </option>
                        <option name="register_disabled">
                                <categoryname>user.register</categoryname>
                                <optiontype>boolean</optiontype>
index 95a986f749e8cf3da9f637cf67a8a189fbc320f2..c96360d7bb5ad0cc4e10377d47aa512d3f5fa630 100644 (file)
@@ -1,5 +1,7 @@
 {include file='header' __disableLoginLink=true __disableAds=true}
 
+{if $forceLoginRedirect}<p class="info">{lang}wcf.user.login.forceLogin{/lang}</p>{/if}
+
 {if !$errorField|empty && $errorField == 'cookie'}
        <p class="error">{lang}wcf.user.login.error.cookieRequired{/lang}</p>
 {else}
index 72aad40baf9313edf21284cbd91fedb5c3512f02..dac81c311b32161fdb4d82d76dacf358af220547 100644 (file)
@@ -228,3 +228,4 @@ define('MODULE_CONTACT_FORM', 0);
 define('SITEMAP_INDEX_TIME_FRAME', 365);
 define('MODULE_TROPHY', 1);
 define('ENABLE_DEVELOPER_TOOLS', 0);
+define('FORCE_LOGIN', 0);
index 95e471422ceea9cda39f95009ad130272b24f31e..b66e6e5347dabddf0f9067bc7e3d69ef91450708 100644 (file)
@@ -43,6 +43,8 @@ class LoginForm extends \wcf\acp\form\LoginForm {
                        UserAuthenticationFactory::getInstance()->getUserAuthentication()->storeAccessData($this->user, $this->username, $this->password);
                }
                
+               if (FORCE_LOGIN) WCF::getSession()->unregister('__wsc_forceLoginRedirect');
+               
                // change user
                WCF::getSession()->changeUser($this->user);
                
@@ -65,7 +67,8 @@ class LoginForm extends \wcf\acp\form\LoginForm {
                WCF::getTPL()->assign([
                        'useCookies' => $this->useCookies,
                        'supportsPersistentLogins' => UserAuthenticationFactory::getInstance()->getUserAuthentication()->supportsPersistentLogins(),
-                       'loginController' => LinkHandler::getInstance()->getLink('Login')
+                       'loginController' => LinkHandler::getInstance()->getLink('Login'),
+                       'forceLoginRedirect' => (FORCE_LOGIN && WCF::getSession()->getVar('__wsc_forceLoginRedirect') !== null)
                ]);
        }
        
index 2a74ece8db95ae05631572a2db2d37c8c0811236..b361ca71e5736484b1ca4bbc86859135fac8fccb 100644 (file)
@@ -1,9 +1,19 @@
 <?php
 namespace wcf\page;
+use wcf\form\DisclaimerForm;
+use wcf\form\EmailActivationForm;
+use wcf\form\EmailNewActivationCodeForm;
+use wcf\form\LoginForm;
+use wcf\form\LostPasswordForm;
+use wcf\form\NewPasswordForm;
+use wcf\form\RegisterActivationForm;
+use wcf\form\RegisterForm;
+use wcf\form\RegisterNewActivationCodeForm;
 use wcf\system\event\EventHandler;
 use wcf\system\exception\IllegalLinkException;
 use wcf\system\exception\PermissionDeniedException;
 use wcf\system\menu\acp\ACPMenu;
+use wcf\system\request\LinkHandler;
 use wcf\system\request\RequestHandler;
 use wcf\system\WCF;
 use wcf\util\HeaderUtil;
@@ -181,6 +191,10 @@ abstract class AbstractPage implements IPage {
         * @inheritDoc
         */
        public function show() {
+               if (FORCE_LOGIN && !RequestHandler::getInstance()->isACPRequest() && !WCF::getUser()->userID) {
+                       $this->forceLogin();
+               }
+               
                // check if active user is logged in
                if ($this->loginRequired && !WCF::getUser()->userID) {
                        throw new PermissionDeniedException();
@@ -320,4 +334,39 @@ abstract class AbstractPage implements IPage {
                        }
                }
        }
+       
+       /**
+        * Forces visitors to log-in themselves to access the site.
+        */
+       protected function forceLogin() {
+               $allowedControllers = [
+                       DisclaimerForm::class,
+                       EmailActivationForm::class,
+                       EmailNewActivationCodeForm::class,
+                       LoginForm::class,
+                       LostPasswordForm::class,
+                       NewPasswordForm::class,
+                       RegisterActivationForm::class,
+                       RegisterForm::class,
+                       RegisterNewActivationCodeForm::class
+               ];
+               if (in_array(get_class($this), $allowedControllers)) {
+                       // controller is allowed
+                       return;
+               }
+               
+               if (WCF::getActiveRequest()->isAvailableDuringOfflineMode()) {
+                       // allow access to those pages that should be always available
+                       return;
+               }
+               
+               // force redirect to login form
+               WCF::getSession()->register('__wsc_forceLoginRedirect', true);
+               HeaderUtil::redirect(
+                       LinkHandler::getInstance()->getLink('Login', [
+                               'url' => WCF::getRequestURI()
+                       ])
+               );
+               exit;
+       }
 }
index 03e28eb503d8130e8ef1928761330c47cc3626e3..bf82bacb90b8681dcb3b2080e70fee88b066cfff 100644 (file)
@@ -40,5 +40,6 @@ define('LANGUAGE_USE_INFORMAL_VARIANT', 0);
 define('URL_OMIT_INDEX_PHP', 0);
 define('VISITOR_USE_TINY_BUILD', 0);
 define('ENABLE_DEVELOPER_TOOLS', 0);
+define('FORCE_LOGIN', 0);
 
 define('WCF_OPTION_INC_PHP_SUCCESS', true);
index d7d043637a95a34bf830ee375de4d851862e424c..cd8b27476a119b812a8a3ba75949055d883429eb 100644 (file)
@@ -1401,6 +1401,8 @@ Bitte {if LANGUAGE_USE_INFORMAL_VARIANT}befolge{else}befolgen Sie{/if} die Anlei
                <item name="wcf.acp.option.category.module.development.notice"><![CDATA[Diese Option werden exklusiv für die Entwicklung und Fehlersuche verwendet, und sind nicht für den produktiven Einsatz geeignet.]]></item>
                <item name="wcf.acp.option.enable_developer_tools"><![CDATA[Entwickler-Werkzeuge aktivieren]]></item>
                <item name="wcf.acp.option.enable_developer_tools.description"><![CDATA[Aktiviert spezielle Werkzeuge die für die Plugin-Entwicklung verwendet werden. Diese Option sollte im Live-Betrieb abgeschaltet werden.]]></item>
+               <item name="wcf.acp.option.force_login"><![CDATA[Anmeldung erzwingen]]></item>
+               <item name="wcf.acp.option.force_login.description"><![CDATA[Besucher werden aufgefordert sich anzumelden, um auf die Inhalte zugreifen zu können.]]></item>
        </category>
        
        <category name="wcf.acp.customOption">
@@ -3358,6 +3360,7 @@ Fehler sind beispielsweise:
                <item name="wcf.user.userAgent"><![CDATA[Browser-Kennung]]></item>
                <item name="wcf.user.login"><![CDATA[Anmeldung]]></item>
                <item name="wcf.user.login.error.cookieRequired"><![CDATA[Die Anmeldung erfordert den Einsatz von Cookies, bitte {if LANGUAGE_USE_INFORMAL_VARIANT}aktiviere{else}aktivieren Sie{/if} diese, um die Anmeldung durchzuführen.]]></item>
+               <item name="wcf.user.login.forceLogin"><![CDATA[Sie müssen angemeldet sein, um diese Seite aufrufen zu können.]]></item>
                <item name="wcf.user.login.login"><![CDATA[Anmeldung]]></item>
                <item name="wcf.user.login.register"><![CDATA[Registrierung]]></item>
                <item name="wcf.user.login.register.teaser"><![CDATA[{if LANGUAGE_USE_INFORMAL_VARIANT}Du hast{else}Sie haben{/if} noch kein Benutzerkonto auf unserer Seite? <a href="{link controller='Register'}{/link}">{if LANGUAGE_USE_INFORMAL_VARIANT}Registriere dich{else}Registrieren Sie sich{/if} kostenlos</a> und {if LANGUAGE_USE_INFORMAL_VARIANT}nimm{else}nehmen Sie{/if} an unserer Community teil!]]></item>
index 0c166e9d8b4994e69f3ffe60645e84c8b28d416e..1c8b7977bba753cdef0247f3f21b3ebb5f8ed58b 100644 (file)
@@ -1395,6 +1395,8 @@ Please follow the instructions described in <a href="{@$__wcf->getPath()}acp/der
                <item name="wcf.acp.option.category.module.development.notice"><![CDATA[These options are used exclusively for development and debugging, they’re not suitable for use in production environments.]]></item>
                <item name="wcf.acp.option.enable_developer_tools"><![CDATA[Enable developer tools]]></item>
                <item name="wcf.acp.option.enable_developer_tools.description"><![CDATA[Enables a set of specialized tools that are used for plugin development. Should always be disabled in production environments.]]></item>
+               <item name="wcf.acp.option.force_login"><![CDATA[Force login]]></item>
+               <item name="wcf.acp.option.force_login.description"><![CDATA[Visitors are required to log-in themselves to access the page.]]></item>
        </category>
        
        <category name="wcf.acp.customOption">
@@ -3355,6 +3357,7 @@ Errors are:
                <item name="wcf.user.userAgent"><![CDATA[User Agent]]></item>
                <item name="wcf.user.login"><![CDATA[Login]]></item>
                <item name="wcf.user.login.error.cookieRequired"><![CDATA[The login requires the acceptance of cookies, please enable cookies to proceed.]]></item>
+               <item name="wcf.user.login.forceLogin"><![CDATA[You must be logged-in to access to this page.]]></item>
                <item name="wcf.user.login.login"><![CDATA[Login]]></item>
                <item name="wcf.user.login.register"><![CDATA[Register]]></item>
                <item name="wcf.user.login.register.teaser"><![CDATA[Don’t have an account yet? <a href="{link controller='Register'}{/link}">Register yourself now</a> and be a part of our community!]]></item>