Search code examples
expressionengine

Password protect an ExpressionEngine template group


I'm building a site where staff will have their own section of the site: example.com/jones, example.com/smith, etc. jones and smith are template groups with the same templates inside (using Stash and Low Variables to keep it all DRY). Some users will have different needs for privacy. On one end their section will be public. On the other end some users will need to administer who can access their content (using Solspace friends).

However in the middle of that range are some who just want to protect against any random person seeing their content. I don't want to use members/member groups to manage this. I don't want visitors to need to register to see the content. A shared member account is an option, but we ruled that out because of other issues (what if the password is reset, comments being left under the same account, etc.

What we would like is to password protect the template group. The staff can let people know where to see their page, and let users know what the password is. This is all possible on a server level, but is is possible to allow the user to directly manage the password? Anything we can do to minimize how much we need to have hands on admin of this the better. A custom field and an add on that allows for this kind of security? I didn't see anything on Devot-ee and the methods on the forums don't do this. Bit of a longshot, but figured I'd ask.


Solution

  • Since you said you didn't want to be tied to actual member accounts and were OK with using a custom field to store an editable password...

    I just recently did something similar that protected a group of entries using a custom field. It is similar to the approach outlined in this "Password Protected Content Made Simple" article. But instead of using PHP in the template I used Mo' Variables. And instead of using url_title I used a custom field (called client_password below).

    In addition, I used the Session Variables plugin to check if the user was already "logged in" on subsequent page loads, preventing them having to enter the password again and again.

    {!-- PASSWORD REQUIRED --}
    {if client_password != ""}
    
        {!-- if passed show content and set session --}
        {if post:password == client_password}
    
            {!-- protected content here --}
            {!-- set session --}
            {embed='embeds/_set_session' entry_id="{entry_id}"}
    
        {!-- if session is valid show content --}
        {if:elseif "{exp:session_variables:get name='logged_in'}" == "{entry_id}"}
    
            {!-- protected content here --}
    
        {!-- if failed show login --}   
        {if:elseif post:password != "" AND post:password != client_password}
    
            <div id="protected">
                <p>Incorrect password. Please try again.</p>
                <br>
                <form action="" method="post">
                    <strong>Password</strong><br />
                    <div>
                        <input name="password">
                    </div>
                    <input type="submit" class="submit" value="submit">
                </form>             
            </div>
    
        {!-- if first attempt show login and prompt --}
        {if:else}
    
            <div id="protected">
                <p>This page is password protected. Please provide the password.</p>
                <br>
                <form action="" method="post">
                    <strong>Password</strong><br />
                    <div>
                        <input name="password">
                    </div>
                    <input type="submit" class="submit" value="submit">
                </form>             
            </div>
    
        {/if}
    
    {!-- NO PASSWORD REQUIRED --}
    {if:else}
    
        {!-- protected content here --}
    
    {/if}