ElkArte Community

Extending Elk => Addons => Topic started by: Ant59 on October 23, 2014, 07:57:58 am

Title: External Authentication Addon WIP (Updates and Questions)
Post by: Ant59 on October 23, 2014, 07:57:58 am
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?
Title: Re: How do I add actions?
Post by: emanuele on October 23, 2014, 08:44:34 am
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 (http://www.elkarte.net/community/index.php?topic=2112.0), it may be removed in the "close" future. ;)
Title: Re: How do I add actions?
Post by: Ant59 on October 23, 2014, 08:52:15 am
Hmmm, I've tried all of that correctly as far as I can tell. Could you check-over my work?

ExtAuth.controller.php
<?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.
Title: Re: How do I add actions?
Post by: emanuele on October 23, 2014, 08:56:27 am
Unix is case sensitive?
Extauth.controller.php
Title: Re: How do I add actions?
Post by: Ant59 on October 23, 2014, 09:00:09 am
Well that worked perfectly :D Thanks. How does MergeTopics.controller.php and the others get away with camel case then?
Title: Re: How do I add actions?
Post by: emanuele on October 23, 2014, 09:02:28 am
That's one of those that still require the $actionArray. O:-)

ETA:
/me feels some awesomeness coming from this question! :D
Title: Re: How do I add actions?
Post by: Ant59 on October 24, 2014, 06:49:52 am
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.
Title: Re: How do I add actions?
Post by: emanuele on October 24, 2014, 07:02:23 am
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. ;)
Title: Re: How do I add actions?
Post by: Ant59 on October 24, 2014, 07:04:53 am
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.
Title: Re: How do I add actions?
Post by: emanuele on October 24, 2014, 07:40:25 am
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.
Title: Re: How do I add actions?
Post by: Ant59 on October 24, 2014, 09:25:50 am
Okay, that idea sank like a lead balloon xD Back to a separate register function and page in the ExtAuth controller.
Title: Re: How do I add actions?
Post by: Ant59 on October 24, 2014, 10:30:29 am
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.
Title: Re: How do I add actions?
Post by: emanuele on October 24, 2014, 10:54:23 am
YAY! :D
Title: Re: How do I add actions?
Post by: Ant59 on October 24, 2014, 11:16:48 am
Facebook working. Steam working. Google failing. Something on the HybridAuth-side. Google's APIs have gotten really complex.
Title: Re: How do I add actions?
Post by: Ant59 on October 24, 2014, 11:18:16 am
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.
Title: Re: How do I add actions?
Post by: Ant59 on October 24, 2014, 11:24:15 am
Next thing: have the user be able to associate and disassociate providers from the account settings page.

I'll leave registration stuff until last since I can then re-use the subs I create for associating.

Not had to touch any of the core Elkarte code yet :)
Title: Re: How do I add actions?
Post by: Ant59 on October 24, 2014, 11:26:16 am
For anyone interested in how the Xydre theme handles the external logins

(http://i.imgur.com/ZD8NXBS.png)
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: emanuele on October 24, 2014, 12:27:15 pm
WOW! :D

/me has a new hero. nods
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: IchBin on October 24, 2014, 01:10:41 pm
That is awesome ant59.  Just for that, you should get a raise to ant60! ;)

Make this part of core when/if you can guys!
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: emanuele on October 24, 2014, 01:27:11 pm
ROFL @ IchBin :P
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Ant59 on October 28, 2014, 08:46:45 am
I'm having trouble loading the tempalte for the profile page. I'm getting this:

QuoteAn Error Has Occurred
Unable to load the 'action_profile' template.
Back

for this:

Code: [Select]
public function action_profile()
{
loadTemplate('Extauth');
}

I've added the link to the profile menu here:

Code: [Select]
'extauth' => array(
'label' => 'Connected Accounts',
'file' => 'Extauth.controller.php',
'controller' => 'Extauth_Controller',
'function' => 'action_profile',
'sc' => 'post',
'token' => 'profile-ea%u',
'password' => true,
'permission' => array(
'own' => array('profile_identity_any', 'profile_identity_own'),
'any' => array('profile_identity_any'),
),
),

Not sure what's going wrong. It's as if it's completely ignoring the loadTemplate call.

I have a "Extauth.template.php" file in themes/default/ with a template_main() function that just echoes 'Test'. But it won't show, and instead throws that error.
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: emanuele on October 28, 2014, 09:36:15 am
template_main is "deprecated" in the sense that it's better not to rely on it and always name explicitly the template you want to use using the sub_template index:
Code: [Select]
$context['sub_template'] = 'my_template';
then, the menu template tries to be smart and sets a "default" sub_template to the name of the sub-action, in that case "action_profile", so in your template file you should have:
Code: [Select]
function template_action_profile ()
{

}

That should do the trick. ;)
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Ant59 on October 28, 2014, 09:42:15 am
Awesome! It works :D Thank you very much @emanuele. You're an encyclopedia of Elkarte :P
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: emanuele on October 28, 2014, 09:54:46 am
LOL

For sure I know the bugs I wrote. :P
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Ant59 on October 28, 2014, 10:28:52 am
A little more progress

(http://i.imgur.com/ncL4xfr.png)
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Ant59 on October 28, 2014, 11:56:15 am
Association and disassociation work perfectly on the fly :D
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: TE on October 28, 2014, 12:38:29 pm
Wonderful, I'd love to see that as part of the ElkArte Core, so where's the pull request? ;)
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Ant59 on October 28, 2014, 12:47:09 pm
Haha! I'll release it as an addon first, but I'd have no objection to it being included in the core. I'd prefer it to be optional though, rather than forcibly included for people that don't want to use it as per my suggestion here: http://www.elkarte.net/community/index.php?topic=2155
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Ant59 on October 29, 2014, 08:00:13 am
Got a working registration template. I'll add the provider they're coming from at some point too.

(http://i.imgur.com/fWQwwmS.png)

EDIT: I might integrate the provider into the Register button, as with the login buttons (give it colour, icon, etc...).
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Ant59 on October 29, 2014, 08:10:04 am
I rolled with that idea and got this. Pretty happy with it. Now I've got to write the handling action for submitting the form and either registering them or telling them they screwed up (oh joy).

(http://i.imgur.com/UvSq2s1.png)

I'm wondering whether I should put it all into a single column though. The agreement requires javascript to scale it's height as-is and I can't think of a CSS-only solution.
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: emanuele on October 29, 2014, 08:18:10 am
Very very nice! :D
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Joshua Dickerson on October 29, 2014, 11:46:17 am
I think you should go with moving it to a single column. Easier to read, especially if you're looking at it on a smaller screen (mobile).
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Ant59 on October 29, 2014, 11:48:07 am
I should have been clearer. It's responsive and does move to a single column on small screens. But on medium and large screens, I'm wondering between multi or single.
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Jorin on October 30, 2014, 12:42:31 am
I think a single column would be better too, even on big screens. It is easier to read long texts (and most of the agreements are long) if the space for the text, the rows are wide enough.
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: emanuele on October 30, 2014, 05:52:50 am
That's true, though there are two points to consider:
1) nobody reads agreements (apart maybe two or three exceptions),
2) have to scroll down to find the only buttons you are looking for ("register using ...") may be annoying to the average user (as an example, I find annoying the gmail registration page lol).
3) (heck, I continue messing with numbers LOL) even Amazon has dropped the "I accept the agreement" for a simpler "signing up you accept the [link]terms and conditions[/link] (not that I remember if it ever had a box to tick).
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Ant59 on October 30, 2014, 06:44:15 am
Quote from: emanuele – 3) (heck, I continue messing with numbers LOL) even Amazon has dropped the "I accept the agreement" for a simpler "signing up you accept the [link]terms and conditions[/link] (not that I remember if it ever had a box to tick).

I am wondering whether it's worth removing the checkbox altogether and opt for the "Agree and Register with Facebook" option instead. That saves having to check for it being ticked too. Guess I could submit a PR for core with that option too as I did with the checkbox.
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Ant59 on October 30, 2014, 07:10:49 am
Quote from: Joshua Dickerson – I think you should go with moving it to a single column. Easier to read, especially if you're looking at it on a smaller screen (mobile).

Quote from: Jorin – I think a single column would be better too, even on big screens. It is easier to read long texts (and most of the agreements are long) if the space for the text, the rows are wide enough.

This is how it currently looks across different sized screens:

https://www.youtube.com/watch?v=3Qgb-_FZluM
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Ant59 on October 31, 2014, 08:13:41 am
I've pushed my code to Github. Feel free to check it over. I've got some more work to do on registration still, but everything else works. I'd appreciate it if someone can check over the security side of things and make sure I've not left out any checks.

https://github.com/Ant59/elkarte-external-authentication

Please don't use it yet. I haven't decided on a license and it doesn't currently work fully.
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: ahrasis on October 31, 2014, 11:33:59 pm
I like it already just by reading this topic. Great job!

Already forked it and will test it.
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Ant59 on November 24, 2014, 09:06:55 am
@emanuele, how should I proceed with this? Would it be preferable:
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: TE on November 24, 2014, 09:14:09 am
I'd vote for "As a PR for the core" -> Developement -> Elk 1.1
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: emanuele on November 24, 2014, 11:07:47 am
If you already have an addon I wouldn't stop you from distributing it! ;D (I would in fact encourage you!)
If you then want to push it to the code... I guess it's fine, 1.1 would be the branch! :D
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: ahrasis on January 11, 2015, 04:22:43 am
Any new development on this addon? Will it be released as addon for 1.0? Will it be added to 1.1 branch? Unpatiently waiting... ;D
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: Jorin on March 05, 2015, 06:43:37 am
Me too!
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: stanstrup on February 27, 2016, 01:28:29 pm
Any news on this? I'd like to have social login on my forum.
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: ahrasis on February 27, 2016, 09:51:20 pm
Not yet, I think. It most probably will be in 1.1.
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: emanuele on February 28, 2016, 06:02:26 am
Not really unless someone takes care of coding it. ;)
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: ahrasis on February 28, 2016, 11:32:17 pm
Ok. I really thought it has been PR to 1.1. What about its license? Public?
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: TE on February 29, 2016, 01:49:59 am
Quote from: ahrasis – Ok. I really thought it has been PR to 1.1. What about its license? Public?
https://github.com/eurich/elkarte-external-authentication
Problem is: there's no licence file available,  The existing files just contain:
Code: [Select]
/**
* @package External Authentication
*
* @author Antony Derham
* @copyright 2014 Antony Derham
*
* @version 1.0
*/
If feel we need @Ant59 's permission to adapt the code or we'd have to rewrite the code..
Title: Re: External Authentication Addon WIP (Updates and Questions)
Post by: emanuele on March 01, 2016, 03:53:35 am
Quote from: Ant59 – Please don't use it yet. I haven't decided on a license and it doesn't currently work fully.