I have two tables in database and two controllers in cakephp, UsersController and DatasController. I wanna save authenticated user`s id in datas table.
Here is my code ...
class DatasController extends AppController{
public function adddam(){
$uid=$this->Auth->user('id');
$datas = $this->Datas->newEntity();
if ($this->request->is('post')) {
$datas = $this->Datas->newEntity();
$this->request->data['user_id'] =$uid;
$datas = $this->Datas->patchEntity($datas, $this->request->data);
$this->Datas->save($datas);
}
}
}
But it does not work correctly.Are there somethings else that I forget?
I gather that you're using CakePhp3 then try this structure to Add 'Datas'
class DatasController extends AppController {
public function adddam() {
$uid = $this->Auth->User()['id'];
$datas = $this->Datas->newEntity();
if ($this->request->is('post')) {
$datas = $this->Datas->patchEntity($datas, $this->request->data);
$datas['user_id'] = $uid;
if($this->Datas->save($datas))
{
$this->Flash->success(__('The datas has been saved.'));
return $this->redirect(['action' => 'adddam']); //Redirect when you want
} else {
$this->Flash->error(__('The datas could not be saved. Please, try again.'));
}
}
$this->set(compact('datas'));
$this->set('_serialize', ['datas']);
}
}
Related
Hello everyone i am using cakephp 2.x, as i am new to here, i need to encrypt my password before it stores to database
User.ctp : I am posting like this to post
<?php
echo $this->Form->input('password',array('type'=>'password','label'=>false,'div'=>false,'class'=>'form-control','id'=>'password'));
?>
Controller:
public function setting()
{
$this->layout='setting_template';
if($this->Session->read('username')==""){
$this->redirect(array('action' => 'user_login'));
}
elseif ($this->Session->read('username') == "admin" )
{
if($this->request->is('post'))
{
$this->data['password'] = encrypt($this->data ['password']);
if ($this->Login->save($this->request->data)) {
$this->Session->setFlash('The user has been saved');
$this->redirect(array('action' => 'setting'));
} else {
$this->Session->setFlash('The user could not be saved. Please, try again.');
}
}
$opp=$this->Login->find('all');
$this->set('login',$opp);
}
else{
echo "<script type='text/javascript'> alert('Permission Denied'); </script>";
$this->redirect(array('action' => 'index'));
}
}
Login controller:
public function login()
{
$this->layout='login_template';
if($this->data)
{
$this->Session->write('id',$this->data['Login']['id'] );
$results = $this->Login->find('first',array('conditions' => array('Login.password' => $this->data['Login']['password'],'Login.username' => $this->data['Login']['username'])));
$this->Session->write('name',$results['Login']['name']);
if ($results['Login']['id'])
{
$this->Session->write($this->data['Login']['username'].','. $this->data['Login']['password']);
$this->Session->write('username',$this->data['Login']['username']);
$this->redirect(array('action'=>'index'));
}
else
{
$this->Session->setFlash("error");
}
}
How can i encrypt the password file and also how can use the Model
As you are using CakePhp go with framework's best practices.
When creating new user records you can hash a password in the
beforeSave callback of your model using appropriate password hasher
class:
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
class User extends AppModel {
public function beforeSave($options = array()) {
if (!empty($this->data[$this->alias]['password'])) {
$passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256'));
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
return true;
}
}
You don’t need to hash passwords before calling $this->Auth->login(). The various authentication objects will hash passwords individually.
If you are using different model than User for authentication you need to define that in AppController. In your Case you need to do something like this in AppController:
$this->Auth->authenticate = array(
'Form' => array('userModel' => 'Login')
);
If you wish to hash your password, try this:
$hashedPassword = AuthComponent::password('original_password');
See Here :Cakephp Password Hashing.
Class FramingPartnersController extends CroogoAppController {
public $components = array('Paginator', 'Session');
public $uses = array('FramingPartner');
public function index() {
$test = 'This is Framing Partners Index';
$this->set(compact('test'));
}
public function profile() {
if(!empty($this->request->data)){
if ($this->request->is('post')) {
$this->FramingPartner->create();
if ($this->FramingPartner->save($this->request->data)) {
$this->Session->setFlash(__d('croogo', 'The profile has been saved'), 'flash', array('class' => 'success'));
$this->redirect(array('action' => 'frames'));
} else {
$this->Session->setFlash(__d('croogo', 'The profile could not be saved. Please, try again.'), 'default', array('class' => 'error'));
}
}
}
}
public function frames() {
$test = 'This is Framing Partners frames';
$this->set(compact('test'));
}
}
I've two different tables each for profile and frames and want to send data to their respective table from the above mentioned actions.
In short, data from profile should go to profile table and data from frame should go to frame table.
public function frames(){
if(!empty($this->request->data)){
if ($this->request->is('post')) {
$this->loadModel('Frame');
$this->Frame->create();
if ($this->Frame->save($this->request->data)) {
$this->Session->setFlash(__d('croogo', 'The profile has been saved'), 'flash', array('class' => 'success'));
/* $this->redirect(array('action' => 'frames'));*/
} else {
$this->Session->setFlash(__d('croogo', 'The profile could not be saved. Please, try again.'), 'default', array('class' => 'error'));
}
}
}
}
In this way, I'm loading a different model from a different controller, i.e. loading Frame Model from FramingPartnersController.
Explicitly, I'm loading Frame Model which will automatically load Frame table.
I'm trying to create a log system for users add and notificate the admins when a new user has been added, asking him permissions to let the new user login.
Anyone knows if there is a plugin for it available? Or how can I save this data in two tables.
I've tried this
public function add() {
$user = $this->Users->newEntity();
$log = $this->Logs->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
$user->criado_por = $this->Auth->user('nome');
$user->modificado_por = $this->Auth->user('nome');
$user->userstatus = 'waiting for permitions';
$log->date = date::now();
$log->newuser = $user->name;
$log->whocreate = $this->Auth->User('name');
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
Here's a notification plugin for CakePHP: Notifier
i have trouble in creating method in controller of cakephp to update the my existing row in a table, can anyone suggest me appropriate model method to update the row in table
<?php
class UsersController extends AppController
{
public function update($id)
{
if(isset($_REQUEST['update']))
{
// method of model to update the row
}
else
$this->set('user',$this->User->find('first',array('conditions'=>array('id'=>$id))));
}
}
?>
http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-save-array-data-null-boolean-validate-true-array-fieldlist-array
$this->User->id = $id;
$this->User->save($this->request->data);
Try the code.......
<?php
class UsersController extends AppController {
public function update($id = null) {
if ($id) {
if ($this->request->is('post')) {
$this->User->id = $id;
$this->User->save($this->request->data);
} else {
$this->set('user', $this->User->find('first', array('conditions' => array('id' => $id))));
}
}
}
}
?>
#burzum after reading the tutorial which link provide by you i found the solution to the my problem, in model updateAll() method available in model by using this i have update row of the table.
public function update($id)
{
if(isset($_REQUEST['update']))
{
$this->User->id=$id;
if($this->User->updateAll(array('User.fname'=>"'".$_REQUEST['fname']."'",'User.lname'=>"'".$_REQUEST['lname']."'",'User.email'=>"'".$_REQUEST['email']."'"),array('id'=>$id)))
echo '<script>alert("update successfully")</script>';
else
echo '<script>alert("failes to update ")</script>';
}
else
$this->set('user',$this->User->find('first',array('conditions'=>array('id'=>$id))));
}
I have following code to upload a file in cakephp
class UsersController extends AppController {
var $name = 'Users';
function index() {
$this->set('users', $this->User->find('all'));
}
function add() {
if (!empty($this->data)) {
if ($this->User->save($this->data)) {
$this->Session->setFlash('Your user data has been saved.');
$this->redirect(array('action' => 'index'));
}
$this->User->create();
if ($this->uploads() && $this->User->save($this->data)) {
$this->Session->setFlash(_('Upload saved', true));
} else {
$this->Session->setFlash(_('Upload not saved.Please try again', true));
}
}
}
function uploads() {
$uploads_dir = '/uploads';
$users = $this->request->data['Upload']['file'];
if ($users['error'] === UPLOAD_ERR_OK) {
if (move_uploaded_file($users['User']['file'], $uploads_dir)) {
$this->User->saveAll($this->data);
return true;
}
}
return false;
}
function edit($id = null) {
$this->User->id = $id;
if (empty($this->data)) {
$this->data = $this->User->read();
} else {
if ($this->User->save($this->data)) {
$this->Session->setFlash('Your user details has been updated.');
$this->redirect(array('action' => 'index'));
}
}
}
function delete($id) {
$this->User->delete($id);
$this->Session->setFlash('This user has been deleted.');
$this->redirect(array('action' => 'index'));
}
}
When I'm trying to upload, the file is uplaoding but I need the file uploaded to be viewed when the hyperlink given for each .jpeg to be displayed but I'm getting the error as
" The requested address '/Users/app/webroot/index.php/uploads' was not found on this server."
Also please help me how to store the uploaded image in another folder
PS:: code should b purely in CakePHP
Please help,
Thanks in advance
Make sure the folder used for uploading files have sufficient permissions and you can use Cakephp Media view to download files. For your reference,check this.
Downloading files using media view in CakePHP
Use image name or image id as an argument in function download and change path to
'path' => APP . 'uploads' . DS
"uploads" folder should be inside webroot directory.