Skip to content
Closed
76 changes: 44 additions & 32 deletions controllers/users_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class UsersController extends UsersAppController {
*/
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('register', 'reset', 'verify', 'logout', 'index', 'view', 'reset_password');
$this->Auth->fields = array('username' => 'email', 'password' => 'passwd');
$this->Auth->allow('register', 'reset', 'verify', 'logout', 'index', 'view', 'reset_password','login');

if ($this->action == 'register') {
$this->Auth->enabled = false;
Expand Down Expand Up @@ -191,9 +192,18 @@ public function admin_view($id = null) {
* @return void
*/
public function admin_add() {
if ($this->User->add($this->data)) {
$this->Session->setFlash(__d('users', 'The User has been saved', true));
$this->redirect(array('action' => 'index'));
if($this->data){
if(isset($this->data[$this->modelClass]['temppassword'])){
$this->data[$this->modelClass]['temppassword'] = $this->Auth->password($this->data[$this->modelClass]['temppassword']);
}
if ($this->User->add($this->data)) {
$this->Session->setFlash(__d('users', 'The User has been saved', true));
$this->redirect(array('action' => 'index'));
}else{
$this->Session->setFlash(__d('users', 'The User could not be saved', true));
$this->data[$this->modelClass][$this->Auth->fields['password']] = null;
$this->data[$this->modelClass]['temppassword'] = null;
}
}
}

Expand Down Expand Up @@ -281,36 +291,39 @@ public function register() {
* @return void
*/
public function login() {
if ($this->Auth->user()) {
$this->User->id = $this->Auth->user('id');
$this->User->saveField('last_login', date('Y-m-d H:i:s'));

if ($this->here == $this->Auth->loginRedirect) {
$this->Auth->loginRedirect = '/';
}

$this->Session->setFlash(sprintf(__d('users', '%s you have successfully logged in', true), $this->Auth->user('username')));
if (!empty($this->data)) {
$data = $this->data[$this->modelClass];

$this->Cookie->name = 'rememberMe';
if (!isset($this->data[$this->modelClass]['remember_me'])) {
$this->Cookie->delete($this->modelClass);
} else {
$cookie = array();
$cookie[$this->Auth->fields['username']] = $this->data[$this->modelClass][$this->Auth->fields['username']];
$cookie[$this->Auth->fields['password']] = $this->data[$this->modelClass][$this->Auth->fields['password']];
$this->Cookie->write($this->modelClass, $cookie, true, '1 Month');
if($this->data){
if ($this->Auth->user()) {
$this->User->id = $this->Auth->user('id');
$this->User->saveField('last_login', date('Y-m-d H:i:s'));
if ($this->here == $this->Auth->loginRedirect) {
$this->Auth->loginRedirect = '/';
}
unset($this->data[$this->modelClass]['remember_me']);
}

if (empty($data['return_to'])) {
$data['return_to'] = null;

$this->Session->setFlash(sprintf(__d('users', '%s you have successfully logged in', true), $this->Auth->user('username')));
if (!empty($this->data)) {
$data = $this->data[$this->modelClass];

$this->Cookie->name = 'rememberMe';
if (!isset($this->data[$this->modelClass]['remember_me'])) {
$this->Cookie->delete($this->modelClass);
} else {
$cookie = array();
$cookie[$this->Auth->fields['username']] = $this->data[$this->modelClass][$this->Auth->fields['username']];
$cookie[$this->Auth->fields['password']] = $this->data[$this->modelClass][$this->Auth->fields['password']];
$this->Cookie->write($this->modelClass, $cookie, true, '1 Month');
}
unset($this->data[$this->modelClass]['remember_me']);
}

if (empty($data['return_to'])) {
$data['return_to'] = null;
}
$this->redirect($this->Auth->redirect($data['return_to']));
}else{
$this->Session->setFlash(sprintf(__d('users', 'Login Incorrect', true)));
$this->data[$this->modelClass][$this->Auth->fields['password']] = null;
}
$this->redirect($this->Auth->redirect($data['return_to']));
}

if (isset($this->params['named']['return_to'])) {
$this->set('return_to', urldecode($this->params['named']['return_to']));
} else {
Expand Down Expand Up @@ -364,7 +377,6 @@ public function logout() {
$message = sprintf(__d('users', '%s you have successfully logged out', true), $this->Auth->user('username'));
$this->Session->destroy();
$this->Cookie->destroy();

$this->Session->setFlash($message);
$this->redirect($this->Auth->logout());
}
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ The users plugin is for allowing users to register and login manage their profil

The plugin is thought as a base to extend your app specific users controller and model from.

This fork is modified to work out of the box.

## Installation ##

The plugin is pretty easy to set up, all you need to do is to copy it to you application plugins folder and load the needed tables. You can create database tables using either the schema shell or the [CakeDC Migrations plugin](http://github.com/CakeDC/migrations):
Expand All @@ -16,6 +18,10 @@ or

You will also need the [CakeDC Search plugin](http://github.com/CakeDC/search), just grab it and put it into your application's plugin folder.

If you would like to use admin routing, remember to un-comment the line in app/config/core.php:

Configure::write('Routing.prefixes', array('admin'));

## How to use it ##

You can use the plugin as it comes if you're happy with it or, more common, extend your app specific user implementation from the plugin.
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/controllers/users_controller.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public function testView() {
$this->assertTrue(isset($this->Users->viewVars['user']));

$this->Users->view('INVALID-SLUG');
$this->assertEqual($this->Users->redirectUrl, array('action' => 'index'));
$this->assertEqual($this->Users->redirectUrl, '/');
}

/**
Expand Down
7 changes: 7 additions & 0 deletions views/elements/paging.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
echo $this->Paginator->counter(array(
'format' => 'Page %page% of %pages%,
showing %current% records out of %count% total,
starting on record %start%, ending on %end%'
));
?>
28 changes: 0 additions & 28 deletions views/users/add.ctp

This file was deleted.

13 changes: 11 additions & 2 deletions views/users/admin_add.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
?>

<div class="users form">
<?php echo $this->Form->create($model);?>
<fieldset>
<legend><?php __d('users', 'Add User');?></legend>
<?php

echo $this->Form->input('username');
echo $this->Form->input('passwd');
echo $this->Form->input('temppassword',array('type'=>'password'));
echo $this->Form->input('email');
echo $this->Form->input('tos');
echo $this->Form->input('active');
echo $this->Form->input('is_admin');
echo $this->Form->input('role');
?>
</fieldset>
<?php echo $this->Form->end('Submit');?>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
<div class="actions">
<ul>
<li><?php echo $this->Html->link(__d('users', 'List Users', true), array('action'=>'index'));?></li>
</ul>
</div>
</div>
4 changes: 4 additions & 0 deletions views/users/admin_edit.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<?php
echo $this->Form->input('id');
echo $this->Form->input('username');
echo $this->Form->input('email');
echo $this->Form->input('active');
echo $this->Form->input('is_admin');
echo $this->Form->input('role');
?>
</fieldset>
<?php echo $this->Form->end('Submit');?>
Expand Down
6 changes: 5 additions & 1 deletion views/users/edit.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@
echo $this->Form->input('Addon');
?>
</fieldset>
<?php echo $this->Form->end('Submit');?>
<?php echo $this->Form->end('Submit');?>

<td class="actions">
<?php echo $this->Html->link(__d('users', 'List Users', true), array('action'=>'index')); ?>
</td>
8 changes: 4 additions & 4 deletions views/users/index.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
?>
<div class="users index">
<div class="users index">
<h2><?php __d('users', 'Users');?></h2>
<p>
<?php
Expand Down Expand Up @@ -39,9 +39,9 @@ foreach ($users as $user):
<?php echo $user[$model]['created']; ?>
</td>
<td class="actions">
<?php echo $this->Html->link(__d('users', 'View', true), array('action'=>'view', $user[$model]['id'])); ?>
<?php echo $this->Html->link(__d('users', 'View', true), array('action'=>'view', $user[$model]['slug'])); ?>
<?php echo $this->Html->link(__d('users', 'Edit', true), array('action'=>'edit', $user[$model]['id'])); ?>
<?php echo $this->Html->link(__d('users', 'Delete', true), array('action'=>'delete', $user[$model]['id']), null, sprintf(__d('users', 'Are you sure you want to delete # %s?', true), $user[$model]['id'])); ?>
<?php echo $this->Html->link(__d('users', 'Delete', true), array('action'=>'delete', $user[$model]['id']), null, sprintf(__d('users', 'Are you sure you want to delete # %s?', true), $user[$model]['username'])); ?>
</td>
</tr>
<?php endforeach; ?>
Expand All @@ -54,6 +54,6 @@ foreach ($users as $user):
</div>
<div class="actions">
<ul>
<li><?php echo $this->Html->link(__d('users', 'New User', true), array('action'=>'add')); ?></li>
<li><?php echo $this->Html->link(__d('users', 'New User', true), array('admin'=>true,'action'=>'add')); ?></li>
</ul>
</div>
4 changes: 2 additions & 2 deletions views/users/login.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<fieldset>
<legend><?php __d('users', 'Login') ?></legend>
<?php
echo $this->Form->create($model, array(
'action' => 'login'));
echo $this->Form->create($model, array('url'=>array(
'action' => 'login')));
echo $this->Form->input('email', array(
'label' => __d('users', 'Email', true)));
echo $this->Form->input('passwd', array(
Expand Down