From db08fd1e0ee6b98d62eed5d991fd4849072c0bc8 Mon Sep 17 00:00:00 2001 From: charly palencia Date: Tue, 7 Feb 2012 19:59:37 -0500 Subject: [PATCH] [feature] add reset_password action --- .DS_Store | Bin 0 -> 6148 bytes Controller/UmusersController.php | 27 +++++++- Model/Umuser.php | 31 +++++++-- .../Case/Controller/UmusersControllerTest.php | 64 ++++++++++++++++++ Test/Case/Model/UmuserTest.php | 55 +++++++++++++++ Test/Fixture/UmuserFixture.php | 57 ++++++++-------- Test/Fixture/UserminUserFixture.php | 52 +++++++------- View/Umusers/reset_password.ctp | 21 ++++++ 8 files changed, 246 insertions(+), 61 deletions(-) create mode 100644 .DS_Store create mode 100644 Test/Case/Controller/UmusersControllerTest.php create mode 100644 Test/Case/Model/UmuserTest.php create mode 100644 View/Umusers/reset_password.ctp diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..933010e45941f331da8a317279eeff7f20ee4c7c GIT binary patch literal 6148 zcmeHKK~BRk5F9t9lyd2jV~$+;K`1Q}5?l)6h)^h?qNtJDqF(b19>oKA01w~?FuS%Y z?81cuLTFc7kK@d)opF?n0l2~A6e7VGYMwAK_b1x$gp z0`h&x>4HVT#-n~Z*ys^}*ygYr`|=|wF?qluVB?W@XwHWceW(jZ4Cli+o(EnLu<_`_ z;lkm=g_&JAp*WqL`E#Yi6&|fM1x$fhfy`g_rT&k;KmW%;c4Z2f0{=<@*B+h^2YjSZ yTQ?p~YHd!xri%%$@wg6QM@uoeQi}KJY8=m{Lo5O|9yvm@9|4uY8dKm`75D(0Xo-se literal 0 HcmV?d00001 diff --git a/Controller/UmusersController.php b/Controller/UmusersController.php index 009ee6b..25dfda9 100644 --- a/Controller/UmusersController.php +++ b/Controller/UmusersController.php @@ -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() { @@ -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')); + } + + } + } + + + } diff --git a/Model/Umuser.php b/Model/Umuser.php index 2c51a5c..4f0989b 100644 --- a/Model/Umuser.php +++ b/Model/Umuser.php @@ -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 diff --git a/Test/Case/Controller/UmusersControllerTest.php b/Test/Case/Controller/UmusersControllerTest.php new file mode 100644 index 0000000..33fa4bb --- /dev/null +++ b/Test/Case/Controller/UmusersControllerTest.php @@ -0,0 +1,64 @@ +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); + } + + +} + + +?> diff --git a/Test/Case/Model/UmuserTest.php b/Test/Case/Model/UmuserTest.php new file mode 100644 index 0000000..0d72920 --- /dev/null +++ b/Test/Case/Model/UmuserTest.php @@ -0,0 +1,55 @@ +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 ); + } + } + + +} + + +?> diff --git a/Test/Fixture/UmuserFixture.php b/Test/Fixture/UmuserFixture.php index 171c361..ee8ea8d 100644 --- a/Test/Fixture/UmuserFixture.php +++ b/Test/Fixture/UmuserFixture.php @@ -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' => "" + ), + ); } diff --git a/Test/Fixture/UserminUserFixture.php b/Test/Fixture/UserminUserFixture.php index 3b3dd39..335a996 100644 --- a/Test/Fixture/UserminUserFixture.php +++ b/Test/Fixture/UserminUserFixture.php @@ -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' + ), + ); } diff --git a/View/Umusers/reset_password.ctp b/View/Umusers/reset_password.ctp new file mode 100644 index 0000000..57f6e4b --- /dev/null +++ b/View/Umusers/reset_password.ctp @@ -0,0 +1,21 @@ +Form->create( "Umuser", array("id"=>"reset_password", "name"=>"reset_password", "class"=>"form-horizontal") ); +?> +
+ +
+
+ @ + Form->input("email", array("label"=>false, "class"=>"span2"));?> +
+
+
+ + +
+ Form->submit( __("Send") , array( "div"=>false, "class"=>"btn btn-primary" )) ?> + +
+Form->end(); +?>