adrianmak Posted January 20, 2015 Share Posted January 20, 2015 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 More sharing options...
Soma Posted January 20, 2015 Share Posted January 20, 2015 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 More sharing options...
adrianmak Posted January 20, 2015 Author Share Posted January 20, 2015 you're right, Soma. Handle redirect in template directly is much more flexible than in the access tab Link to comment Share on other sites More sharing options...
adrianmak Posted January 20, 2015 Author Share Posted January 20, 2015 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 More sharing options...
adrianmak Posted January 20, 2015 Author Share Posted January 20, 2015 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 More sharing options...
Nico Knoll Posted January 21, 2015 Share Posted January 21, 2015 Just put a variable in the Session I guess (http://processwire.com/api/variables/session/): $session->customstring = 'Your account do not have enough access privileges to view this page.'; // and then just $page->body = $session->customstring; Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now