CakeDC User Plugin - Is there Documentation Anywhere? - cakephp

Browsing through GitHub and I found a pretty powerful CakePHP plugin called CakeDC Users that has a lot of features (Account verification, password reset, etc) for a creating a login/authentication system. I like it because it seems to be written by some of the actual CakePHP developers and it gets updated a lot but there seems to be absolutely zero documentation anywhere on it. I've just come across this plugin recently, since I was trying to see if there's a better way than "rolling" with my own solution. So I was wondering if anybody here has had experience with it and if so could point to some decent documentation online.
Edit There is some stuff at the bottom of the readme, but it hasn't been too intuitive for me.
Alternate question, if you don't use this plugin, is there a login/authentication plugin you use in CakePHP that you use for login/authentication?

I have ran into the same problem with using the CakeDC plugins, a lot of them have little/no documentation.
However, there is not "Zero" documentation for it, you can see how to set it up for the most part at the bottom of the github page in the read me. Also you need to put this inside your AppController::beforeFilter() method.
$this->Auth->authorize = 'controller';
$this->Auth->fields = array('username' => 'email', 'password' => 'passwd');
$this->Auth->loginAction = array('plugin' => 'users', 'controller' => 'users', 'action' => 'login', 'admin' => false);
$this->Auth->loginRedirect = '/';
$this->Auth->logoutRedirect = '/';
$this->Auth->authError = __('Sorry, but you need to login to access this location.', true);
$this->Auth->loginError = __('Invalid e-mail / password
combination. Please try again', true);
$this->Auth->autoRedirect = true;
$this->Auth->userModel = 'User';
$this->Auth->userScope = array('User.active' => 1);
if ($this->Auth->user()) {
$this->set('userData', $this->Auth->user());
$this->set('isAuthorized', ($this->Auth->user('id') != ''));
}
Also, you need an isAuthorized() function, something as simple as this will do:
public function isAuthorized() {
return true;
}
Additionally, you will need to allow the 'login' action (this will involve editing the plugin files). Just add 'login' to the $this->Auth->allow() in users_controller.php.

This question is pretty old now, but as it's not marked as resolved and we've been doing a lot on the documentation since then I think it's worth to update:
Documentation can be found here:
For the version 3+ of the framework
https://github.com/CakeDC/users/blob/master/Docs/Home.md
Tutorial > http://www.cakedc.com/jorge_gonzalez/2016/02/21/cakedc_users_plugin_for_cakephp_3_-_update
CakePHP Facebook login tutorial >
http://www.cakedc.com/jorge_gonzalez/2016/02/21/cakephp_facebook_login_using_cakedc_users_plugin_-_update_3_1_5
For the (old) version 2
https://github.com/CakeDC/users/blob/2.x/Docs/Home.md

After exhaustive search I found a tutorial on how to use CakeDC!
Here it is

Related

how to redirect the page after checking the boolean value from database

I am developing a hrm panel in cakephp. I want when employee punchin the page redirect to punchout page,but if employee logout the panel then he goes to punchin page that page must not be redirected until it checks the boolean value from the database.
I disagree with the other answer because it does not show the best way to do it in CakePHP. If you use the router the code provided by the other answer will not work with routing. Also if your app is not in the root of the domain / but for example.com/my-tool/ the string type URL won't work either and the link is wrong, it would go to example.com/punchout instead of example.com/my-tool/punchout
if ($value === true) {
$this->redirect(array('controller' => 'employees', 'action' => 'punchout'));
}
This is the correct way to do links in CakePHP for all links that point to any controller of your application. The string should be only used if it is an external URL. If you use links in your layout and have plugins and routing prefixes you also want to add the plugin and prefix key to the array and set it according to your needs.
Might it be so simple just to put your redirect code into a IF statement?
if($boolean === true) {
$this->redirect(array('controller' => 'employees', 'action' => 'punchout'));
}
//normal code here when boolean is false

add language prefix to URL from cookie on page load in CakePHP

I'm quite new on cakePHP. I'm creating multilanguage page using this tutorial: i18n multilanguage tutorial everything is working fine, but on page load I need to add language prefix from cookie (localhost/eng instead of localhost/), this prefix appears when I select some menu, but I had a headache how to add prefix on pageload. Thanks for advices.
You can do a redirect in your AppController, after you call _setLanguage(). Something like:
$this->_setLanguage();
if( $this->here == '/' )
$this->redirect(array('controller' => 'your_controller', 'action' => 'your_action', 'language' => $this->Session->read('Config.language')));

CakePHP: Reporting Failed Downloads with the Media View

I'm using CakePHP's Media view to force file downloads. My code is pretty much exactly like the example provided in the cookbook, which I'll paste here for your convenience:
<?php
class ExampleController extends AppController {
public function download () {
$this->viewClass = 'Media';
// Download app/outside_webroot_dir/example.zip
$params = array(
'id' => 'example.zip',
'name' => 'example',
'download' => true,
'extension' => 'zip',
'path' => APP . 'outside_webroot_dir' . DS
);
$this->set($params);
}
}
In the database, I have a field that keeps track of how many times the file was downloaded. I'm looking for a way to make sure that this number is as accurate as possible, so if a user's download gets cancelled or times out, the number does not increment. Is there some way for CakePHP's Media view to report that the download was, indeed, successful?
Detecting when a file has finished downloading is no easy task. This is something that would be done on the client side with javascript, but browsers do not give you any hooks for that.
There is a pretty clever solution here (setting a cookie and then looking for it with javascript), but it only tells you when the download has started.

Outputting a hyperlink from a controller in cakePHP

I'm just getting started with cakePHP, and things aren't going so well so far.
I have a controller that handles confirming user emails. On registration the user is sent an email with a confirmcode in a link. Depending on the confirm code they give, the controller gives different text responses. One of these responses includes a hyperlink in order to log in.
I'm trying to use the Html helper, but although I've loaded it in $helpers at the top of the class, I an only make it work if I then use App::import, and then instantiate it.
It all seems overkill to simply make a hyperlink! How many times do I have to load the same class?
Wherever I look on the web it keeps telling me it's a bad idea to use a helper in a controller, but how else am I supposed to get the link made?
So I have
var $helpers = array('Html');
at the top of the controller, and:
if (isset($this->User->id)) { // Check the user's entered it right
// Do some stuff to remember the user has confirmed
// This is to load the html helper - supposedly bad form, but how else do I make the link?
App::import('Helper', 'Html');
$html = new HtmlHelper();
$this->set('message', __("Your email address has been confirmed.", TRUE)." ".$html->link(__("Please log in", TRUE), array('controller' => "users", 'action' => "login" )));
} else {
$this->set('message', __("Please check your mail for the correct URL to confirm your account", TRUE));
}
in the controller's confirm method and
<div>
<?php echo $message;?>
</div>
in the view to output the resulting message
Surely I'm going wrong somewhere - can anyone explain how?
You're not supposed to use Helpers in the Controller. As #Lincoln pointed out, you should construct the link in the View. You may construct the URL in the Controller, since a URL is basically data, but a link is a very medium-specific (HTML) implementation of a URL.
Either way, you'll need to create a full URL (including host) if you want to send it in an Email. The most universal way is to use Router::url:
$fullUrl = Router::url(array('controller' => ...), true); // 'true' for full URL
Do this in either the Controller or the View. For creating a link, use this in the View:
echo $html->link('Title', $fullUrl);
The idea is that all the data you need to render the page is sent to the view with set, then any conditional logic or formatting is done in the view with helpers, so send whole query results when appropriate (suppose you need to alter a link to include the user's screen name, you'll have it handy).
in controller action
$this->set('user', $this->User);
in view (this is slightly different depending on if your in <= 1.2 or 1.3
if ($user->id) //available because of Controller->set
{
//1.2
$link = $html->link(__("Please log in", TRUE), array('controller' => "users", 'action' => "login" ));
//1.3
$link = $this->Html->link(__("Please log in", TRUE), array('controller' => "users", 'action' => "login" ));
echo __("Your email address has been confirmed.", TRUE)." $link";
}
else
{
$this->set('message', __("Please check your mail for the correct URL to confirm your account", TRUE));
}
What you are trying to do should be done with the SessionComponent. $this->Session->setFlash('your message here');
and in your layout with the session helper put $this->Session->flash();
About your wanting urls in the controller, Router::url is correct as deceze said, but there is no use for it there as you should not be building html in a controller.
what you want to do it use the session::setFlash() method above and then redirect them using
$this->redirect(array('controller' => "users", 'action' => "login" ));

cakephp why can't I have an admin route and a superuser route?

In core.php I can define
Configure::write('Routing.admin', 'admin');
and /admin/controller/index will work.
but if I define both
Configure::write('Routing.admin', 'admin');
Configure::write('Routing.superuser', 'superuser');
and try to look at /superuser/blah/index/ instead of it saying the controller doesn't exist it says
Error: SuperuserController could not be found.
instead of saying
Error: BlahController could not be found.
When I first read the documentation I was under the impression I could run both routes, and not just one or the other. Is there something more I need to do?
I believe they are working on this for CakePHP 1.3, but for now, we have to cheat to accomplish additional routing. This is the method I've used in the past.
// SuperUser Routing
Router::connect('/superuser/:controller',
array('prefix' => 'superuser', 'superuser' => true));
Router::connect('/superuser/:controller/:action/*',
array('prefix' => 'superuser', 'superuser' => true));
There were some issues generating URLs using the array('controller' => ...) method, but I haven't touched that project in a few months, so I can't remember all the caveats with it. This should at least give you a starting point though.
The CakePHP document explains this some. The relevant section starts about halfway in talking about multiple prefixes.
If you're using Jason's trick, and having trouble with generating URLs using the array('controller' => ...) syntax, then put this in your appcontroller:
if (isset($this->params['prefix']) && $this->params['prefix'] == 'superuser') {
Configure::write('Routing.admin', 'superuser');
}
This forces the appcontroller to use the correct admin prefix, which in this case is "superuser".

Resources