Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 2.x: Embedding a login form as an element using CakeDC's User plugin and Security component

newish CakePHP user here. I haven't been able to find a solution to this issue.

I would like to embed a login form as an element in an existing page. I am using the CakeDC user plugin. The goal is to embed the login form in the page and then, when they log in, update some content on the page without refreshing the page. Before we get to worrying about AJAX and SecurityComponent, I would like to at least get the form embedded on some other page as an element.

For starters, I tried to straight up copy and paste the contents of '/Views/Users/login.ctp' into the element. This gave me an warning of 'undefined variable "$Model"', so I hardcoded the model as 'Users.User" (because I assumed that the notation for accessing a model inside a plugin was 'plugin.model' like they said in the manual). This failed with the error message Error: Users.usersController could not be found.

So, I adjusted the form to take 'User' as the model instead of $model and the request now gets blackholed. If I disable the security component, I can log in successfully. This isn't quite a satisfactory solution (I mean, I could opt to not validate the form or define 'login' as an unlocked action, but if I do that, then I may as well not have security enabled for login, right?).

Here is the code for app/Plugin/Users/View/Elements/login.ctp:

<div class="users index">
<h2><?php echo __d('users', 'Login'); ?></h2>
<fieldset>
    <?php
        echo $this->Form->create('User', array(
            'action' => 'login',
            'id' => 'LoginForm'));
        echo $this->Form->input('User.email', array(
            'label' => __d('users', 'Email')));
        echo $this->Form->input('User.password',  array(
            'label' => __d('users', 'Password')));

        echo '<p>' . __d('users', 'Remember Me') . $this->Form->checkbox('remember_me') . '</p>';
        echo '<p>' . $this->Html->link(__d('users', 'I forgot my password'), array('action' => 'reset_password')) . '</p>';

        echo $this->Form->end(__d('users', 'Submit'));
    ?>
</fieldset>

Following is the code for the View that is calling the element:

<div id="loginform">
<?php echo $this->element('Users.login'); ?>
</div>

If I copy and paste the element code into a View (say, in apps/Plugin/Users/View/Users/login.ctp), it works regardless of whether Security is on or off, so I suspect some monkeying around with the form or Security calls is in order.

Any ideas or help would be greatly appreciated.

like image 924
user1750992 Avatar asked Oct 16 '12 19:10

user1750992


1 Answers

I think the problem is that elements are essentially dumb views - they aren't meant to know about models. You'll have to pass the data in yourself.

I usually put $this->set("authUser", $this->Auth->user()); in my AppController after setting up auth, this lets me check the data on the currently logged in user anywhere, including elements.

You can also pass data directly like so:

<?php echo $this->element("Users.login", array("user"=>$user));?>

Also, I'm not quit clear on your wording there, but login should be an unlocked action, otherwise how will users first log on?

like image 110
Daniel Avatar answered Sep 28 '22 09:09

Daniel