Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
27 changes: 26 additions & 1 deletion Controller/UmusersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class UmusersController extends UserminAppController {

public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('login', 'logout', 'loggedout');
$this->Auth->allow('login', 'logout', 'loggedout', 'reset_password');
}

public function login() {
Expand Down Expand Up @@ -128,4 +128,29 @@ public function delete($id = null) {
$this->redirect(array('action' => 'index'));
}

public function reset_password(){
if( isset( $this->request->data["Umuser"]["email"] ) ){
$email= $this->request->data["Umuser"]["email"];

try{
$this->Umuser->reset_password( $email );
$this->Session->setFlash(__('To initiate the reset proccess. please follow the instruction sent it to your email account'));

$this->redirect(array('action' => 'login'), null, false);
return 'redirect to Login';

}catch(NoEmailException $error){
error_log( get_class( $error ) );
$this->Session->setFlash(__('Please Enter a Valid Email'));

}catch(NoUserFound $error){
error_log( get_class( $error ) );
$this->Session->setFlash(__('The Email Was Not Found'));
}

}
}



}
31 changes: 24 additions & 7 deletions Model/Umuser.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,32 @@ public function beforeSave() {
* @var array
*/
public $belongsTo = array(
'Umrole' => array(
'className' => 'Umrole',
'foreignKey' => 'umrole_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
'Umrole' => array(
'className' => 'Umrole',
'foreignKey' => 'umrole_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);


function reset_password( $email=null ){
if( !isset($email) || empty($email) ){
throw new NoEmailException();
}

$user_changed= $this->find( "first", array("conditions"=> array( "email"=>$email ) ) );
if($user_changed==false){
throw new NoUserFound();
}

$token_password= crypt( $email, Configure::read("Security.salt") );
$user_changed["Umuser"]["token_password"]= crypt( "no_password", Configure::read("Security.salt") );
$user_changed["Umuser"]["token_password"]=$token_password;
$this->save( $user_changed );
}

function afterSave($created) {
if ($created && Configure::read('Usermin.sendEmailAfterUserCreated')) {
// send email to newly created user
Expand Down
64 changes: 64 additions & 0 deletions Test/Case/Controller/UmusersControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
App::uses("Controller", "Plugin/Controller");
App::uses("View", "Plugin/View");
App::import("Model", "Usermin.Umuser");

/**
*
**/
class UmusersControllerTest extends ControllerTestCase{
public $fixtures = array('plugin.usermin.umuser');

public function setUp(){
$this->Umuser=& ClassRegistry::init("Umuser");
$_ENV["enviroment"]="test";
}


public function test_it_should_show_form_if_dont_have_email_data(){
$view= $this->testAction("/reset_password",
array("method"=>"get", "return"=>"view"));


$this->assertRegExp("/reset_password/", $view);
$this->assertRegExp("/\[email\]/", $view);
}

public function test_it_should_redirect_to_login_and_show_alert_if_it_has_email(){
$data= array(
"Umuser"=>array( "email"=>"test@test.com" )
);

$contents= $this->testAction('/reset_password',
array("data"=>$data, "method"=>"post", "return"=>"contents") );

$this->assertRegExp("/login/", $this->headers["Location"] );
$this->assertRegExp("/redirect to Login/", $contents);

}

public function test_it_should_and_show_an_error_if_doesnt_exit_email(){
$data=array(
"Umuser"=>array("email"=>"wrong@test.com")
);

$contents= $this->testAction("/reset_password", array("data"=>$data, "method"=>"post", "return"=>"contents"));

$this->assertRegExp("/The Email Was Not Found/", $contents);
}

public function test_it_should_error_if_the_is_blank(){
$data=array(
"Umuser"=>array("email"=>"")
);

$contents= $this->testAction("/reset_password", array("data"=>$data, "method"=>"post", "return"=>"contents"));

$this->assertRegExp("/Please Enter a Valid Email/", $contents);
}


}


?>
55 changes: 55 additions & 0 deletions Test/Case/Model/UmuserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
App::uses("Umuser", "plugin/Model");
App::import("Model", "Usermin.Umuser");
/**
*
**/
class UmuserTest extends CakeTestCase{
public $fixtures =array("plugin.usermin.umuser");

public function setUp(){
$this->Umuser=& ClassRegistry::init("Umuser");
}

public function test_it_should_reset_user_password(){
$email= "test@test.com";
$this->Umuser->reset_password( $email );
$user_changed= $this->Umuser->find("first", array(
"email"=> $email
));
$password= crypt( $email, Configure::read("Security.salt") );

$this->assertEquals( $user_changed["Umuser"]["token_password"], $password);
}

public function test_it_should_send_error_if_email_is_null(){
try{
$this->Umuser->reset_password();
}catch(Exception $exception){
$this->assertInstanceOf( "NoEmailException", $exception );
}
}

public function test_it_should_send_error_if_email_is_empty(){
try{
$this->Umuser->reset_password("");
}catch(Exception $exception){
$this->assertInstanceOf( "NoEmailException", $exception );
}
}


public function test_it_should_send_error_if_email_not_exist(){
try{
$email= "testFail@tet.com";
$this->Umuser->reset_password( $email );
}catch(Exception $exception){
$this->assertInstanceOf( "NoUserFound", $exception );
}
}


}


?>
57 changes: 30 additions & 27 deletions Test/Fixture/UmuserFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,35 @@
*
*/
class UmuserFixture extends CakeTestFixture {
public $name= "Umuser";
public $import = 'Umuser';
/**
* Fields
*
* @var array
*/
public $fields = array(
'id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'primary', 'collate' => 'latin1_swedish_ci', 'comment' => ' ', 'charset' => 'latin1'),
'username' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'latin1_swedish_ci', 'comment' => '', 'charset' => 'latin1'),
'email' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'latin1_swedish_ci', 'comment' => '', 'charset' => 'latin1'),
'password' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'latin1_swedish_ci', 'comment' => '', 'charset' => 'latin1'),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
'reset_password' => array('type'=>"boolean"),
'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'InnoDB')
);

/**
* Fields
*
* @var array
*/
public $fields = array(
'id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'primary', 'collate' => 'latin1_swedish_ci', 'comment' => ' ', 'charset' => 'latin1'),
'username' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'latin1_swedish_ci', 'comment' => '', 'charset' => 'latin1'),
'email' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'latin1_swedish_ci', 'comment' => '', 'charset' => 'latin1'),
'password' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'latin1_swedish_ci', 'comment' => '', 'charset' => 'latin1'),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'InnoDB')
);

/**
* Records
*
* @var array
*/
public $records = array(
array(
'id' => '4ec42132-9514-42ab-abff-24c3bd16a67e',
'username' => 'Lorem ipsum dolor sit amet',
'email' => 'Lorem ipsum dolor sit amet',
'password' => 'Lorem ipsum dolor sit amet'
),
);
/**
* Records
*
* @var array
*/
public $records = array(
array(
'id' => '4ec42132-9514-42ab-abff-24c3bd16a67e',
'username' => 'Lorem ipsum dolor sit amet',
'email' => 'test@test.com',
'password' => 'Lorem ipsum dolor sit amet',
'token_password' => ""
),
);
}
52 changes: 26 additions & 26 deletions Test/Fixture/UserminUserFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@
*/
class UserminUserFixture extends CakeTestFixture {

/**
* Fields
*
* @var array
*/
public $fields = array(
'id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'primary', 'collate' => 'latin1_swedish_ci', 'comment' => ' ', 'charset' => 'latin1'),
'username' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'latin1_swedish_ci', 'comment' => '', 'charset' => 'latin1'),
'email' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'latin1_swedish_ci', 'comment' => '', 'charset' => 'latin1'),
'password' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'latin1_swedish_ci', 'comment' => '', 'charset' => 'latin1'),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'InnoDB')
);
/**
* Fields
*
* @var array
*/
public $fields = array(
'id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'primary', 'collate' => 'latin1_swedish_ci', 'comment' => ' ', 'charset' => 'latin1'),
'username' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'latin1_swedish_ci', 'comment' => '', 'charset' => 'latin1'),
'email' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'latin1_swedish_ci', 'comment' => '', 'charset' => 'latin1'),
'password' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'latin1_swedish_ci', 'comment' => '', 'charset' => 'latin1'),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'InnoDB')
);

/**
* Records
*
* @var array
*/
public $records = array(
array(
'id' => '4ec41fd7-37bc-417f-8ee5-2454bd16a67e',
'username' => 'Lorem ipsum dolor sit amet',
'email' => 'Lorem ipsum dolor sit amet',
'password' => 'Lorem ipsum dolor sit amet'
),
);
/**
* Records
*
* @var array
*/
public $records = array(
array(
'id' => '4ec41fd7-37bc-417f-8ee5-2454bd16a67e',
'username' => 'test user',
'email' => 'test@test.com',
'password' => '23545234002'
),
);
}
21 changes: 21 additions & 0 deletions View/Umusers/reset_password.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
echo $this->Form->create( "Umuser", array("id"=>"reset_password", "name"=>"reset_password", "class"=>"form-horizontal") );
?>
<div class="control-group">
<label class="control-label" for="prependedInput"> <?php echo __("Password") ?>: </label>
<div class="controls">
<div class="input-prepend">
<span class="add-on">@</span>
<?php echo $this->Form->input("email", array("label"=>false, "class"=>"span2"));?>
</div>
</div>
</div>


<div class="form-actions">
<?php echo $this->Form->submit( __("Send") , array( "div"=>false, "class"=>"btn btn-primary" )) ?>
<button type="reset" class="btn">Cancel</button>
</div>
<?php
echo $this->Form->end();
?>