Jump to content
doolin

Try to create a Custom Login site

Recommended Posts

Hi guys

I try to create a custom login page. So i read a lot in the forum about this, but i don't understand how i have to implement it. The login page was no problem, i use the code from ryan here:


And customize my login page. At the moment is this site my default admin page: 

/admin. 

So my first idea was..I create a new site called "login" and add my custom Login site as template to this site. This worked fine, but now i have two more problems:

First, when the user logout via the custom logout links, he gets back to the old default login site "/admin". I've tried to change this in the admin template, without success. I use the Default admin theme.

Second: The created page "login" is visible in the site tree, but it should be hidden for non admin user, so i move the "login" site below the admin site, but now the non admin user cannot access the site anymore. I also tied to customize the template access, but the site are still visible.

Can anyone explaine me a better solution for this?

Greats

Share this post


Link to post
Share on other sites

Hi doolin, I would suggest checking this post from Soma, which "corrects" ryan's original code for the custom login.

Concerning the redirection after logging out, I am assuming that you are using the link in the admin area, right? Since the admin doesn't know about your custom login page, it can't redirect to it. You need to hook into the Session::logout function to force redirection to the right login page, for example with a module:

class CustomLogout extends WireData implements Module {
  public static function getModuleInfo() {
    return array(
      'title' => 'Custom Logout',
      'summary' => 'Redirects to a custom login page after logout',
      'singular' => true,
      'autoload' => true
    );
  }

  public function init() {
    $this->addHookAfter('Session::logout', function($event) {
      $this->session->redirect($this->pages->get('/login/')->url);
    });
  }
}

Concerning the login page itself, it should remain accessible for non-admin users, since they need to access it to login. What you could simply do is make it non-editable by non-admin users by configuring the permissions for the login template (in the Access tab). Removing it from the page tree is somewhat of a hassle...

  • Like 3

Share this post


Link to post
Share on other sites
Hi ESRCH

Thanks for the right code.

Yes i user the link in the admin area.

Where i have to put this code? I read in the Docs i can place hooks anywhere i want with the autoload method. I add the code into the default.php in the Default admin theme, but it doesn't work?

yes i see, i can set the site as non editable, but the site are still display, but i have to hide it. Is there no easy way?

greats

Share this post


Link to post
Share on other sites

While you can put hooks anywhere, the cleanest approach is usually to create a separate module for it. In this case, the code I gave you is the code for a complete module, so here's what you should do:

  • Create a CustomLogout.module file in the /site/modules directory (you can rename the file if you want, but it must have the same name as the class that it defines).
  • Paste the code from my previous post.
  • Go to Modules > Refresh. The new module should appear, and you can click on install, to install it just like any Core module.

After doing this, the redirection should work.

To hide the login page, I would suggest using CSS to hide a specific item in the PageList. You will notice when examining the HTML generated by the PageList that each page is actually a div with (among other things) a class of "PageListID1234", where 1234 is the id of the page. So if your login page's id is 1025 for example, you could create a custom.css file (in /site/templates/styles) with the rule

.PageListID1025 { display: none !important; }

You now have to include this CSS file when showing the PageList page, but only when the user is not the superuser (or any another rule based on permissions or roles). You can try using the Admin Custom Files module and checking the different options there. I must admit that I have never used it, so I can't help you much. Otherwise, you can make a module (or include it in the above module in the init function) with this hook:

$this->addHookBefore("ProcessPageList::execute", function($event) {
  if (!$this->user->isSuperUser()) // You can change the rule here
    $this->config-styles->add($this->config->urls->templates . "styles/custom.css"); // Adapt the link to the file
})

As a final note, you can indeed add hooks (the $this->addHookAfter(...) call) more or less anywhere, but the syntax will change:

  • Within a class (such as a module as defined in the code in my previous post), the hook is called on $this, e.g., $this->addHookBefore("Page::render")
  • Outside of a class (such as in a template, or the admin.php file), the hook is called on wire(), so wire()->addHookBefore("Page::render").
  • You can also call addHook on an instance of a class, like $page->addHookBefore("render").

What you tried to do by including my entire module code in the default.php file wouldn't have worked, because the autoload looks for classes in files that have the same name as the class (so the CustomLogout class must be in the CustomLogout.module or CustomLogout.php file).

Learning about hooks requires a little effort, but once you get it, it is very logical and it really opens the door to doing whatever you want with Processwire. The hook system is really well built, and most aspects of the application can be hooked into. I would suggest the following to learn about hooks (that's what I did):

  • Like 1

Share this post


Link to post
Share on other sites
ESRCH, thank you very much for your effort and the great explanation! No, Now i understood how hooks are working and for what they are.
 
Yes, CSS, thank you. I dont know why i did not think about this. Good solution.  :)
 
Ok, i created an module according to your instructions. The module are installed now. If I Logout now, it redirects me to 
 
/admin/login/logout
 
and i get a 
 

"Internal Server Error

 
The server encountered an internal error or misconfiguration and was unable to complete your request.
 
Error has been logged."
 
There are already an site called "Login" by default from processwire. So i create a new site in "/" called "logins", and customize your code. But i still get the error?
 
I created a new module for the css hook:
 
<?php
class SiteHider extends WireData implements Module {
  public static function getModuleInfo() {
    return array(
      'title' => 'SiteHider',
      'summary' => 'Hide Sites in CSS per User Roles',
      'singular' => true,
      'autoload' => true
    );
  }

  public function init() {
    $this->addHookBefore("ProcessPageList::execute", function($event) {
  if (!$this->user->isSelina())
    $this->config-styles->add($this->config->urls->templates . "css/sitehide.css");
})
  }
}

 
And get this error:
 

"Parse Error: syntax error, unexpected T_OBJECT_OPERATOR (line 15..."

I also get the same error if i combine both hooks like you sayed

 
What im doing wrong? 
I have to say i've added in the first line of both modules "<?php".

Share this post


Link to post
Share on other sites

The problem is most probably the $this->user->isSelena() function. I understand the confusion, since $this->user->isSuperuser() gives the impression that you can use any user name, but it's not the case. isSuperuser() is a built-in method of the User class (you can find it on the cheatsheet under $user / User methods). If you want to allow only a specific user with the username "selina", you would do

if (!$this->user->name === 'selina')

This should hopefully do the trick.

You should also add a semi-colon after the }) on the third line from the bottom, or you'll get another error :)

  • Like 1

Share this post


Link to post
Share on other sites

I didn't realize that your post had two parts with two different questions, sorry :rolleyes:

For the logout problem, I would need to know more about the error. There normally isn't a /login page in ProcessWire from the start, so I'm surprised that this would be an issue. At the end of th error message, you can see the message that the "Error has been logged". This means that you can find this error in /site/assets/logs/errors.txt. So try doing it again, and look at the last error in the file, it should give you some indication on how to solve the issue. If not, just post the error here, as well as the exact code from the module if you adapted it.

Share this post


Link to post
Share on other sites
It's a module:
 
/**
 * ProcessWire Login Process
 *
 * Provides Login capability for ProcessWire Admin 
 * 
 * For more details about how Process modules work, please see: 
 * /wire/core/Process.php 
 * 
 * ProcessWire 2.x 
 * Copyright (C) 2014 by Ryan Cramer 
 * Licensed under GNU/GPL v2, see LICENSE.TXT
 * 
 * http://www.processwire.com
 * http://www.ryancramer.com
 *
 */

class ProcessLogin extends Process {

protected $nameField;
protected $passField; 
protected $submitField; 
protected $form; 
protected $id; 

public static function getModuleInfo() {
return array(
'title' => 'Login',          
'summary' => 'Login to ProcessWire', 
'version' => 101, 
'permanent' => true, 
'permission' => 'page-view',
);
}
.............
 
the error Log:
 

2015-03-13 09:35:54 admin http://SITE.ch/admins/module/ Parse Error:  syntax error, unexpected T_OBJECT_OPERATOR (line 15 of /is/htdocs/wp12205379_U47G28VDDD/www/SITE/site/modules/SiteHider/SiteHider.module)

2015-03-13 09:38:36 guest http://SITE.ch/admins/login/ Error:  Using $this when not in object context (line 14 of /is/htdocs/wp12205379_U47G28VDDD/www/SITE/site/modules/CustomLogout/CustomLogout.module)

Share this post


Link to post
Share on other sites

For ProcessLogin: Did you put your custom login page under the Admin page? You should avoid doing that if you want people who have not logged in to access it so that they are able to login.

I am a bit stumped with your errors. Could you please paste the exact code from the SiteHider.module and the CustomLogout.module files? Also, which version of PHP are you using?

Share this post


Link to post
Share on other sites
No. There are already an "Login" page under /admin that i never created. My created site is here /login. I tried to move the /admin/Login site to / and get an error.

CustomLogout.module



<?php
class CustomLogout extends WireData implements Module {
public static function getModuleInfo() {
return array(
'title' => 'Custom Logout',
'summary' => 'Redirects to a custom login page after logout',
'singular' => true,
'autoload' => true
);
}

public function init() {
$this->addHookAfter('Session::logout', function($event) {
$this->session->redirect($this->pages->get('/login/')->url);
});
}
}


SiteHider.module 



<?php
class SiteHider extends WireData implements Module {
public static function getModuleInfo() {
return array(
'title' => 'SiteHider',
'summary' => 'Hide Sites in CSS per User Roles',
'singular' => true,
'autoload' => true
);
}

public function init() {
$this->addHookBefore("ProcessPageList::execute", function($event) {
if (!$this->user->name === 'selina');
$this->config-styles->add($this->config->urls->templates . "css/sitehide.css")
})
}
}

I use PHP Version 5.3.29

Share this post


Link to post
Share on other sites

Hmmm... I wonder if it's the inline function. Try this code instead for each module:

<?php
class CustomLogout extends WireData implements Module {
  public static function getModuleInfo() {
    return array(
      'title' => 'Custom Logout',
      'summary' => 'Redirects to a custom login page after logout',
      'singular' => true,
      'autoload' => true
    );
  }

  public function init() {
    $this->addHookAfter('Session::logout', $this, 'hookAfterLogout');
  }

  public function hookAfterLogout($event) {
    $this->session->redirect($this->pages->get('/login/')->url);
  }
}
<?php
class SiteHider extends WireData implements Module {
  public static function getModuleInfo() {
    return array(
      'title' => 'SiteHider',
      'summary' => 'Hide Sites in CSS per User Roles',
      'singular' => true,
      'autoload' => true
    );
  }

  public function init() {
    $this->addHookBefore("ProcessPageList::execute", $this, 'hookBeforePageListExecute');
  }

  public function hookBeforePageListExecute($event) {
    if (!$this->user->name === 'selina')
      $this->config-styles->add($this->config->urls->templates . "css/sitehide.css");
  }
}

Also, be careful with where you put the semi-colons (;). In SiteHider, you had put it just after the if, which is a problem.

  • Like 3

Share this post


Link to post
Share on other sites
Hmmm... I wonder if it's the inline function

Yep, If I remember correctly, you can't refer to "$this" in a closure. But you could use wire('session') to get the session object there.

  • Like 1

Share this post


Link to post
Share on other sites

Indeed, though after some research, they added support for $this in PHP 5.4, which is why it works on my local server (I have PHP 5.4.7).

  • Like 1

Share this post


Link to post
Share on other sites
Ok, the CustomLogout Module works fine now :)
I try to understand. The problem was, you have to define a separate function and call it. Before we try it with an event right? why that didn't work?
 
For the SiteHider i still get this error:
 
2015-03-13 21:23:02 admin http://SITE.ch/admins/module/ Parse Error: syntax error, unexpected T_OBJECT_OPERATOR (line 18 of /is/htdocs/wp12205379_U47G28VDDD/www/SITE/site/modules/SiteHider/SiteHider.module)

I'm not even can install the module, I get the error as soon i transfer the files in the /modules folder

Share this post


Link to post
Share on other sites

The problem with the CustomLogout module was that we were using an anonymous function in the hook (the function($event) {...} is an anonymous function), and according to the PHP manual (here, under Changelog), PHP version 5.3 didn't allow using $this within such anonymous functions. So we had to use the "traditional" syntax for hooks, which uses a method of the module.

The reason why the SiteHider module is called without needing to install it is probably because it wasn't uninstalled correctly the previously, so it is still in the database (if you look in the modules table in your database, you can probably find the SiteHider module). Before deleting a module from the /site/modules folder, you should always uninstall it first by clicking on its name in the modules list, selecting Uninstall and clicking Save. This will remove the module from the database.

I see that I did a typo in my code, there is a > missing: it should be $this->config->styles->add(...); Also, the line above should be

if ($this->user->name !== 'selina')

because the ! operator has a higher precedence as the === operator.

I hope that this solves the problem :)

Share this post


Link to post
Share on other sites
this is embarrassing, because i checked the code for errors but i cant see this! My "Syntax View" is not so good right now, but it gets better. Now,  I've removed the "!" from:



if (!$this->user->name === 'selina')


and now, processwire takes the selected css file! It does what it should do  :)

Very nice, thank you :)

  • Like 1

Share this post


Link to post
Share on other sites

Hey, just noticed right now that this is a swiss powered discussion :)

Have a nice weekend!

Cheers

  • Like 3

Share this post


Link to post
Share on other sites

Haha yeah...really...We could speak Swiss-german...nice weekend!

  • Like 1

Share this post


Link to post
Share on other sites

Glad it solved your problem.

Indeed, it's a pretty Swiss thread :) Though Swiss German would be a little difficult, I'm from Geneva... German would be fine ;)

  • Like 1

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Tyssen
      I have a client who is reporting that in the last couple of days they can no longer login to their site with their normal browser (Chrome). Using another browser or an incognito window works.
      I've tried logging into the site using the same login details in my usual browser (Firefox) and have had no problems.
      The site is a membership site and today other members are reporting the same problem.
      The site is running 3.0.148 and has the session handler DB and login throttle modules installed. It was recently upgraded to 3.x from 2.x. But no changes have been made to the site between the time when they were able to login OK and when the problem started happening.
    • By codevark
      The Minimal Site works fine, but if I try to login as admin, I have no idea what the credentials are.
      I did not do the "install" process myself. The OVA came with everything set up, but there's no mention of the PW admin credentials being set to some initial value.
      Wondering if anyone else has tried the Bitnami OVA and if there's a simple answer.
      --Pete
    • By Robin S
      A new module that hasn't had a lot of testing yet. Please do your own testing before deploying on any production website.
      Custom Paths
      Allows any page to have a custom path/URL.
      Screenshot

      Usage
      The module creates a field named custom_path on install. Add the custom_path field to the template of any page you want to set a custom path for. Whatever path is entered into this field determines the path and URL of the page ($page->path and $page->url). Page numbers and URL segments are supported if these are enabled for the template, and previous custom paths are managed by PagePathHistory if that module is installed.
      The custom_path field appears on the Settings tab in Page Edit by default but there is an option in the module configuration to disable this if you want to position the field among the other template fields.
      If the custom_path field is populated for a page it should be a path that is relative to the site root and that starts with a forward slash. The module prevents the same custom path being set for more than one page.
      The custom_path value takes precedence over any ProcessWire path. You can even override the Home page by setting a custom path of "/" for a page.
      It is highly recommended to set access controls on the custom_path field so that only privileged roles can edit it: superuser-only is recommended.
      It is up to the user to set and maintain suitable custom paths for any pages where the module is in use. Make sure your custom paths are compatible with ProcessWire's $config and .htaccess settings, and if you are basing the custom path on the names of parent pages you will probably want to have a strategy for updating custom paths if parent pages are renamed or moved.
      Example hooks to Pages::saveReady
      You might want to use a Pages::saveReady hook to automatically set the custom path for some pages. Below are a couple of examples.
      1. In this example the start of the custom path is fixed but the end of the path will update dynamically according to the name of the page:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'my_template') { $page->custom_path = "/some-custom/path-segments/$page->name/"; } }); 2. The Custom Paths module adds a new Page::realPath method/property that can be used to get the "real" ProcessWire path to a page that might have a custom path set. In this example the custom path for news items is derived from the real ProcessWire path but a parent named "news-items" is removed:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'news_item') { $page->custom_path = str_replace('/news-items/', '/', $page->realPath); } }); Caveats
      The custom paths will be used automatically for links created in CKEditor fields, but if you have the "link abstraction" option enabled for CKEditor fields (Details > Markup/HTML (Content Type) > HTML Options) then you will see notices from MarkupQA warning you that it is unable to resolve the links.
      Installation
      Install the Custom Paths module.
      Uninstallation
      The custom_path field is not automatically deleted when the module is uninstalled. You can delete it manually if the field is no longer needed.
       
      https://github.com/Toutouwai/CustomPaths
      https://modules.processwire.com/modules/custom-paths/
    • By franciccio-ITALIANO
      Hi, I need to provide a quite complex user registration form: description, subdescription, drop-down lists etc. 
      Through this registration, users will be able to access and comment.
      If someone responds to their comments, I would like a NOTIFICATION to appear when accessing their panel.
      My social interaction project is just that, it seems simple, I don't need more.
      Now I am undecided whether to use buddypress, elgg, or a native processwire system.
      What do you recommend? 
      If you recommend processwire, which modules should I install?
      Do they work or is processwire too immature for that?
      Translated with www.DeepL.com/Translator (free version)
    • By abdulqayyum
      Dear processwire community,
      i have a problem in loginRegister module, i could not add custom field in login and register page.
      i read from plugin documentation. they are saying.
      " By default, the email and password fields are required for both forms. You may want to add more fields. To do this, you’ll need to add fields to your “user” template. You can add fields to your user template in the admin by going to “Setup > Templates > Show system templates > user”.

      but i could not find similar scenario like “Setup > Templates > Show system templates > user”
      i can see just "Setup > Templates" not seeing "Show system templates > user" in my admin panel.

      Please help me in this case that how i can add custom field in these two page.
      Regards AbdulQayyum

×
×
  • Create New...