Skip to main content
Topic: External Authentication Addon WIP (Updates and Questions) (Read 13727 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

External Authentication Addon WIP (Updates and Questions)

How do I add controller actions using an add-on? I've written a new controller and added it to the package-info.xml file, but it's as if the controller dispatcher is unaware of it. How do I make it aware so that ?action=blah leads to my action?
Last Edit: October 24, 2014, 11:26:46 am by ant59

Re: How do I add actions?

Reply #1

There are at least a couple of ways.

The first and most "future-proof" is to name it "properly" and put it in the "correct" place.
For example, if you want to add a non-admin action, let's say something like index.php?action=myaction you can store the file in sources/controllers (a.k.a. CONTROLLERDIR), the name of the file should be Myaction.controller.php, and it should be inside a class name Myaction_Controller that should extend Action_Controller.
So:
Code: (sources/controllers/Myaction.controller.php) [Select]
<?php

class Myaction_Controller extends Action_Controller
{
    public function action_index()
    {
        //This method is required and should forward the the default (sub)action.
    }
}

Then, for each sub-action (e.g. index.php?action=myaction;sa=something) you can have a method like action_something:
Code: (sources/controllers/Myaction.controller.php) [Select]
<?php

class Myaction_Controller extends Action_Controller
{
    public function action_index()
    {
        //This method is required and should forward the the default (sub)action.
        $this->action_something();
    }

    public function action_something()
    {
        //This takes care of the sub action "something"
    }
}

The other way, is to use the $actionArray and the corresponding hook integrate_actions, but this method should be used only if it is not possible to use the naming pattern (in other words it's there just because some internal actions are not yet mapped to the new way).

The dispatcher supports as well the use of functions instead of classes, but I wouldn't suggest that method, because, as proposed here, it may be removed in the "close" future. ;)
Bugs creator.
Features destroyer.
Template killer.

Re: How do I add actions?

Reply #2

Hmmm, I've tried all of that correctly as far as I can tell. Could you check-over my work?

ExtAuth.controller.php
Code: [Select]
<?php

/**
 * @package External Authentication
 *
 * @author Antony Derham
 * @copyright 2014 Antony Derham
 *
 * @version 1.0
 */

if (!defined('ELK'))
die('No access...');

/**
 * ExtAuth_Controller class, deals with authenticating external accounts
 */
class ExtAuth_Controller extends Action_Controller
{
/**
* Entry point in ExtAuth controller
*/
public function action_index()
{
// Only action, try and authenticate
$this->action_extlogin();
}

/**
* Attempt to authenticate them with HybridAuth
*
* What it does:
*  - takes the provider ID from GET var and attempts HybridAuth login.
*/
public function action_extlogin()
{
// Include the HybridAuth external libaray and configuration
   $config = EXTDIR . '/hybridauth/config.php';
    require_once(EXTDIR . '/hybridauth/Hybrid/Auth.php');

I'm calling "index.php?action=extauth", but it presents the board index.

Re: How do I add actions?

Reply #3

Unix is case sensitive?
Extauth.controller.php
Bugs creator.
Features destroyer.
Template killer.

Re: How do I add actions?

Reply #4

Well that worked perfectly :D Thanks. How does MergeTopics.controller.php and the others get away with camel case then?

Re: How do I add actions?

Reply #5

That's one of those that still require the $actionArray. O:-)

ETA:
 emanuele feels some awesomeness coming from this question! :D
Bugs creator.
Features destroyer.
Template killer.

Re: How do I add actions?

Reply #6

I've got to admit, it's not easy to tip-toe around the core Elkarte code. I want to try and build the add-on without changing too much, but I might be submitting some PRs.

From what I can work out, the easiest way to enter into registration/login is through the OpenID handling code that already exists.

Re: How do I add actions?

Reply #7

heh...
I know the feeling.

TBH last time I tried to look into this, the idea that came to my mind was rewrite the registration/authentication/login code entirely, because as it is now, even the OpenID implementation seems hacked into the normal registration so that "it works".

If you have ideas that would improve the situation, feel free to send PRs. And if you feel comfortable, don't be shy on changing large chunks. ;)
Bugs creator.
Features destroyer.
Template killer.

Re: How do I add actions?

Reply #8

Yeah, I did think about rewriting it entirely, but it annoyed me when I was writing my theme how the login page is already duplicated: one template for error and one for no error.

I'll see how it goes integrating it into the OpenID code, and if needs be, I'll make changes where I need.

Re: How do I add actions?

Reply #9

Quote from: ant59 – Yeah, I did think about rewriting it entirely, but it annoyed me when I was writing my theme how the login page is already duplicated: one template for error and one for no error.
When I noticed it, it was kind of late (I try not to enter into the themes dir lol) to merge them into one...
It's on the list for 1.1 though.
Bugs creator.
Features destroyer.
Template killer.

Re: How do I add actions?

Reply #10

Okay, that idea sank like a lead balloon xD Back to a separate register function and page in the ExtAuth controller.
Last Edit: October 24, 2014, 09:37:10 am by ant59

Re: How do I add actions?

Reply #11

After about an hour of steaming before finally realising I hadn't added the correct globals (fml), I've just successfully logged into Elkarte with Twitter.
Last Edit: October 24, 2014, 10:37:32 am by ant59

Re: How do I add actions?

Reply #12

YAY! :D
Bugs creator.
Features destroyer.
Template killer.

Re: How do I add actions?

Reply #13

Facebook working. Steam working. Google failing. Something on the HybridAuth-side. Google's APIs have gotten really complex.

Re: How do I add actions?

Reply #14

Google working too now.

It's designed (hopefully) in such a way that you can take existing HybridAuth-compatible provider plugins and just drop them straight into this addon.