Jump to content

redirect to a URL in the access tab of a template


adrianmak
 Share

Recommended Posts

i am building a multi-language site with some restricted pages available for a specific user role only.

Under the access tab of a template, here there are three options for pages with no access

- Show a 404 Page

- Show the Login page: /pw1/processwire/login/

- Redirect to another URL

I use the third one, Redirect to another URL, which is a front-end user login (instead of pw login page) /pw1/user/login

That option seems only for a hard-coded url. If a user changed to another language at the front-end, when view a restricited page, he/she will still redirect to that url but won't with a language prefixed url, e.g. /pw1/cht/user/login

Link to comment
Share on other sites

Read the notes there:

Optional: In your URL, you can include the tag '{id}' (perhaps as a GET variable), and it will be replaced by the requested page's ID number, if you want it.

Edit: maybe you can use the id somehow.

But maybe a better option would be to have some code in those template to check for if the user is logged in an redirect with API

if(!$user->isLoggedin()) $session->redirect($pages->get(1230)->url); // 1230 if of login page

and the url will be in the language the user is viewing the site.

Link to comment
Share on other sites

Now I turn off the access control in the template and put following code in this template file

<?php

if (!$user->isLoggedin()) {
    $session->redirect($pages->get("template=login")->url);
} 

if ($user->hasRole("registered")) {
    $page->title;
    $page->body;
} else {
    $page->body = "Your account do not have enough access privileges to view this page.";
}

include('./main.php');

However, a user with that role, after login , it will redirect to home but not display that page

a user without that role, after login, it will redirect to home but not display "Your account do not have enough access privileges to view this page."

This is my login template file

<?php
    $lang = ($user->language->name == 'default') ? '' : $user->language->name . '/';
    if ($user->isLoggedin()) {
        $session->redirect($config->urls->root . $lang);
    }

    if ($input->post->user && $input->post->pass) {
        $username = $sanitizer->username($input->post->user);
        $password = $input->post->pass;

        try {
            if ($session->login($username, $password)) {
                $session->redirect($config->urls->root . $lang);            
            } else {
                $error = "Wrong username or password. Login failed.";
            }
        } catch (Exception $e) {
            $error = $e->getMessage();
        }        
    }

$form_title = __("Sign In");
$form_user = __("User");
$form_pass = __("Password");
$form_submit_btn = __("Sign in");
$form_error =  __("Login failed");

$page->title = "User Login";
$page->body .= "
<div class='container'>
    <div class='col-md-12'>
        <div class='login-box'>
        <div class='panel panel-default'>
            <div class='panel-heading'>
                <h3 class='panel-title'><span class='lock-icon'></span><strong>$form_title</strong></h3>
            </div>
            <div class='panel-body'>
                <form role='form' action='./' method='post'>
                    <div class='message-error'>$error</div>
                    <div class='form-group'>
                        <label for='user'>$form_user</label>
                        <input type='text' name='user' id='user' class='form-control' placeholder=$form_user />
                    </div>
                    <div class='form-group'>
                        <label for='pass'>$form_pass</label>
                        <input type='password' name='pass' id='pass' class='form-control' placeholder=$form_pass />
                    </div>
                    <button type='submit' class='btn btn-sm btn-primary'>$form_submit_btn</button>
                </form>
            </div>
        </div>
        </div>
    </div>
</div>
";

include('./main.php');
?>

I knew that there is something to do in successful login to fulfill my task (current it is all  redirect to home)

But how ?

Link to comment
Share on other sites

I'm thinking of this code

if (!$user->isLoggedin()) {
    $session->redirect($pages->get("template=login")->url);
} 

if ($user->hasRole("registered")) {
    $page->title;
    $page->body;
} else {
    $page->body = "Your account do not have enough access privileges to view this page.";
}

include('./main.php');

Before make a redirection, save current page url and pass direction with saved url to the login page (a custom form)
,  then in the custom form, when a successful login, it will check this passing variable is null, redirect to home otherwirse redirect to the url.

How could i pass other information to another page in a session redirection ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...