security component issue in cakephp - cakephp-2.0

In my application i am using security component and defined in the app controller like this
public $components = array('Security');
And i have created the form like this
<?php
echo $this->Form->create('Admin', array('id' => 'form',"url"=>array("plugin"=>"admins","controller"=>"admins","action"=>"save","admin"=>true)));
echo $this->Form->hidden("submit_type",array("id" => "id_submit_type","value" => ""));
echo $this->Form->input("username");
echo $this->Form->input("password");
echo $this->Form->button(__('Save'),array("id" => "save","class" => "form-button"));
echo $this->Form->button(__('Save & Cont'),array("id" => "savec","class" => "form-button"));
echo $this->Form->end();
?>
And in Javascript am updating the hidden field value like this
<script type="text/javascript">
$(document).ready(function()
{
$('#save, #savec').click(function() {
if($(this).is('#save'))
$('#id_submit_type').val("S");
else
$('#id_submit_type').val("C");
$('#form').submit();
});
});
</script>
After click on save button
CORE/Cake/Controller/Component/SecurityComponent.php line 234 → SecurityComponent->blackHole(AdminsController, string)
[internal function] → SecurityComponent->startup(AdminsController)
CORE/Cake/Utility/ObjectCollection.php line 131 → call_user_func_array(array, array)
[internal function] → ObjectCollection->trigger(CakeEvent)
CORE/Cake/Event/CakeEventManager.php line 247 → call_user_func(array, CakeEvent)
CORE/Cake/Controller/Controller.php line 670 → CakeEventManager->dispatch(CakeEvent)
CORE/Cake/Routing/Dispatcher.php line 183 → Controller->startupProcess()
CORE/Cake/Routing/Dispatcher.php line 161 → Dispatcher->_invoke(AdminsController, CakeRequest, CakeResponse)
APP/webroot/index.php line 97 → Dispatcher->dispatch(CakeRequest, CakeResponse)
If i am commenting to update the hidden fields in javascript
my functionality is working fine and saving the data into table
please tell me how to set the hidden variable values in javascript while we are using security component

Try like this, i don't know is it a correct solution or not.But it's working properly for me.
write the below code in your beforeFilter function
$this->Security->disabledFields = array('hiddenfield1', 'hiddenfield2'); // set the hidden fields like this
function beforeFilter() {
$this->Security->disabledFields = array('submit_type'); // for your code
}

Related

How to get hidden field value from view page form and set as cookie value and retrieving in the same view page in cakephp

Thanks in advance, I have a index.ctp page where i have a form which has a hidden field with value = "2". when submitting the form the value is passed to the controller and setting a cookie as
$cookieVal =$this->request->data['hideCookieVal'];
$this->cookie->write('hideNextCookieVal',$cookieVal, false, 3600);
and I am trying to pass in a variable to get that value in index.ctp so I used this
$this->set('nextCookie',$this->Cookie->read('hideNextCookieVal'));
In index.ctp page i am accessing this value in a variable name as $nextCookie.
I used this variable in a if condition to display the other section of the HTML page, if cookie is set but, it was throwing an error as
Error: Call to a member function write() on a non-object
File: D:\wamp\www\invl_exams\app\Controller\UsersController.php
Line: 135
My code as below:
UsersController.php
<?php
App::uses('CakeEmail', 'Network/Email');
class UsersController extends AppController
{
function index()
{
Line 134 $cookieVal = $this->request->data['hideCookieVal'];
Line 135 $this->cookie->write('hideNextCookieVal',$cookieVal, false, 3600);
Line 136 $this->set('nextCookie',$this->Cookie->read('hideNextCookieVal'));
}
}
index.ctp page
<form class="shopping-cart" name="cartTable" id="cartTable" method="post" action="<?php echo $this->webroot ?>users/index">
<input type="hidden" name="hideCookieVal" value="2">
<button type="submit" class="next pull-right" id="nextId">NEXT</button>
</form>
<?php
if($nextCookie == 2)
{
?>
<section>
<table>
<!-- Table content here -->
<table>
</section>
<?php
}
?>
You need to load the CookieComponent in your controller, And also, it's Cookie with the c letter in uppercase
<?php
App::uses('CakeEmail', 'Network/Email');
class UsersController extends AppController {
//Add this line
public $components = array('Cookie');
function index() {
Line 134 $cookieVal = $this->request->data['hideCookieVal'];
//Change cookie to Cookie
Line 135 $this->Cookie->write('hideNextCookieVal',$cookieVal, false, 3600);
Line 136 $this->set('nextCookie',$this->Cookie->read('hideNextCookieVal'));
}
}

Laravel 5.2 view set to public folder

I am trying to make my routes access to public folder as i am trying to use laravel as api and angular as frontend. I have changed the view file from config folder from
'paths' => [
realpath(base_path('resources/views')),
],
To
'paths' => [
realpath(base_path('public/views')),
],
in my public folder i have created a views folder and placed a file called test.html
Now
Route::get('/',function(){
return view('test.html');
});
it shows error
in FileViewFinder.php line 137
at FileViewFinder->findInPaths('test.html', array('C:\xampp\htdocs\customer_portal\public\views')) in FileViewFinder.php line 79
at FileViewFinder->find('test.html') in Factory.php line 165
at Factory->make('test.html', array(), array()) in helpers.php line 779
at view('test.html') in routes.php line 36
at RouteServiceProvider->{closure}()
Do you know what i am doing wrong?
The problem is that U use return view('test.html') on html file! Its wrong, view should be use for php blade templates. Instead return conent of your file (below code I copy-paste from my project FilesController):
use Response;
...
public function downloadFile(Request $request)
{
...
return Response::download(path_to_your_file, download_file_name);
}
and in routes.php file instead of Route::get('/',function(){... put:
Route::get('/', 'FilesController#downloadFile');

cakephp - change password method leaves blank the password field in database

i've created a method in my UsersController.php file , so that my users can change their password.
My method php code is:
public function changepass() {
$this->User->id = $this->Auth->user('id');
if($this->User->exists()) {
$new_pass = $this->request->data['User']['newpass'];
$repeat_pass = $this->request->data['User']['newrepeat'];
if($new_pass == $repeat_pass) {
$this->User->saveField('password',$new_pass);
$this->Session->setFlash(__('Updated successfully'));
$this->redirect(array('controller' => 'users','action' => 'dashboard'));
} else {
$this->Session->setFlash(__('Passwords did not match'));
$this->redirect(array('controller' => 'users','action' => 'changepass'));
}
}
}
and my changepass.ctp View file is:
<?php
echo $this->Form->create();
echo $this->Form->input('newpass',array('type'=>'text','label'=>array('text'=>'Enter new password')));
echo $this->Form->input('newrepeat',array('type'=>'text','label'=>array('text'=>'Confirm new password')));
?>
<button type="submit">Save</button>
<?php echo $this->Form->end(); ?>
When i try to browse under the users/changepass,it returns me 2 errors about an undefined index:
Notice (8): Undefined index: User [APP/Controller/UsersController.php, line 108]
Notice (8): Undefined index: User [APP/Controller/UsersController.php, line 109]
pointing at this part of my code:
$new_pass = $this->request->data['User']['newpass'];
$repeat_pass = $this->request->data['User']['newrepeat'];
and also it (immediately) changes the user password in my database to blank.
I cannot find out what's wrong in any way. I'd be very grateful if you could help me at this point.
Thank you in advance.
You forgot one vital part about form processing in the controller: Check on POST.
if ($this->request->is('post')) {
// only then try to access $this->request->data['User'] content!
}
In the case thats it not a post (get), you just display the form - especially without trying to save anything.
Tip: Take a look at baked code (using cake bake) and you will see how it's done properly.

custom config file error

I want to a create a config file for site configuration. I create config.php and put it into the Config folder. It's its content:
<?php
Configure::write('Title', 'My App');
Configure::write('Categories', array(
'Recipes' =>
array('url' => '/recipe', 'max' => 10),
'Foods' =>
array('url' => '/food', 'max' => 5)
));
And put this code at the end of bootstrap.php:
Configure::load('config');
But cake returns this error:
No variable $config found in ...........\app\Config\config.php.php
Error: An Internal Error Has Occurred.
Stack Trace
CORE\Cake\Core\Configure.php line 272 → PhpReader->read(string)
APP\Config\bootstrap.php line 110 → Configure::load(string)
CORE\Cake\Core\Configure.php line 92 → include(string)
CORE\Cake\bootstrap.php line 142 → Configure::bootstrap(boolean)
APP\webroot\index.php line 79 → include(string)
What's the problem??
Note: I'm using the latest version: 2.2.1 Stable
In your config file you should be using an array variable called $config instead of Configure::write if you want to use load() with the default PhpReader. See API
Ex.
<?php
$config = array(
'Title' => 'MyApp',
...
);
http://book.cakephp.org/2.0/en/development/configuration.html#built-in-configuration-readers

File upload error cakephp

When I choose an image and push upload, I get this error in my controller:
Notice (8): Undefined index: File [APP/controllers/garage_car_images_controller.php, line 22]
I've also noticed that the $form->create line shown below does NOT generate form code in the inspected html. This is VERY weird.
Any ideas how to fix this? Thanks!
My view code:
<div class="title_plate">
<h1 id="title_plate_header">add image</h1>
</div>
<div id="overlay_content">
<?php
echo $form->create('GarageCarImage', array('controller' => 'garage_car_images','action' => 'add', 'type' => 'file'));
echo $form->file('File');
echo $form->hidden('garage_car_id', array('value' => $this->params['pass'][0]));
echo "<br><br>";
echo "Make this image the default? " . $form->input('default', array('type' => 'select', 'label' => false, 'options' => array('0' => 'No', '1' => 'Yes')));
echo "<br>";
echo $form->submit('Upload');
echo $form->end();
?>
</div>
My controller code:
if (!empty($this->data) &&
is_uploaded_file($this->data['GarageCarImage']['File']['tmp_name'])) {
$fileData = fread(fopen($this->data['GarageCarImage']['File']['tmp_name'], "r"),
$this->data['GarageCarImage']['File']['size']);
$this->data['GarageCarImage']['type'] = $this->data['GarageCarImage']['File']['type'];
$this->data['GarageCarImage']['user_id'] = $this->Auth->user('id');
$this->GarageCarImage->save($this->data);
}
Are you sure it doesn't generate the form code? If it didn't, I don't think you would even be able to try and submit a form. I believe submitting the form occurs because you seem to issue a request for /garageCarImages/index -- to me, that says that the form is getting generated properly.
If indeed that is the case, you should just change the value of the first parameter to the name of the controller that you want the code to execute in. For me, typically, I use an Attachment controller to do this, so I would put:
echo $form->create( 'Attachment', array(...) );
Your situation will depend on where the code is located to process the upload. Most likely it's the ImageController (guessing here...), but I'm not sure without knowing more details of your system.

Resources