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
Related
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']);
}
}
I m having trouble in updating the student profile form,after i gave the user_id to the student profile.Every time when i try to edit the form, on saving it provide new id to the existing user. I don't know where I'm getting wrong. Plz help.Thanks in Advance.
Here is my add and edit function in Student Profiles controller:
public function add() {
if ($this->request->is('post')) {
$this->StudentProfile->create();
$data = $this->request->data;
$data['StudentProfile']['user_id'] = $this->Auth->user('id');
// code implemented below
//$this->loadModel('User');
if ($this->StudentProfile->save($data)) {
//Update user here if Profile saved successfully
//$this->StudentProfile->id = $this->Auth->user('id');
$this->Session->setFlash(__('Your account profile has been created'));
$this->redirect(array('controller' => 'studentprofiles', 'action' => 'index'));
}else{
$this->Session->setFlash(__('Your Profile was saved, but an error has occurred while updating the Users table'));
//Email your self the user ID here or something ??
}
}
$h=array(
'fields' => array('Height.height'),
'recrusive' => 0
);
$this->loadModel('Height');
$height = $this->Height->find('list',$h);
$this->set(compact('height'));
}
public function edit($id = null) {
if (!$this->StudentProfile->exists($id)) {
throw new NotFoundException(__('Invalid student profile'));
}
if ($this->request->is(array('post', 'put'))) {
$this->request->data['StudentProfile']['user_id'] = $this->Auth- >user('id');
// code implemented below
// $this->loadModel('User');
if ($this->StudentProfile->save($this->request->data)) {
//$this->StudentProfile->id = $this->Auth->user('id');
$this->Session->setFlash(__('The student profile has been saved.'), 'default', array('class' => 'alert alert-success'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The student profile could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
}
} else {
$options = array('conditions' => array('StudentProfile.' . $this- >StudentProfile->primaryKey => $id));
$this->request->data = $this->StudentProfile->find('first', $options);
}
$h=array(
'fields' => array('Height.height'),
'recrusive' => 0
);
$this->loadModel('Height');
$height = $this->Height->find('list',$h);
$this->set(compact('height'));
}
Can't believe I'm replying to my own question . Well In case anyone have the same above problem,the solution is just add the following code after this-
$this->request->data['StudentProfile']['user_id'] = $this->Auth->user('id');
// add this code to avoiding giving new id to existing user on edit:
$this->request->data['StudentProfile']['id'] = $id;
A better way of saving edited information is to set the ID on the model before the save like so
$this->StudentProfile->id = $this->Auth->user('id');
If you initiate a save after setting the above then the row will be edited and not add a new row.
I am currently trying to update my user before sending an email which will have a confirm link with a resetkey for the user to reset the password.
The problem is that the user is not updating whilst the key is being generated.
the code can be seen hereunder
public function forgotpassword() {
if ($this->request->is('post')) {
$mail = $this->request->data['ContactDetail']['email'];
$data = $this->User->find('first', array('conditions' => array('ContactDetail.email' => $mail)));
if (!$data) {
$message = __('No Such E-mail address registerd with us ');
$this->Session->setFlash($message);
} else {
$data['User']['resetkey'] = Security::hash(mt_rand(),'md5',true);
debug($data);
if ($this->User->saveAssociated($data)) {
$this->Session->setFlash(__('The user has been saved.'));
}
else {
$this->Session->setFlash(__('The user could not be updated. Please, try again.'));
}
$key = $data['User']['resetkey'];
$id = $data['User']['id'];
$mail = $data['ContactDetail']['email'];
$email = new CakeEmail('default');
$email->to($mail);
$email->from("xxxx#yahoo.com");
$email->emailFormat('html');
$email->subject('Password reset instructions from');
$email->viewVars(array('key' => $key, 'id' => $id, 'rand' => mt_rand()));
$email->template('reset');
if ($email->send('reset')) {
$message = __('Please check your email for reset instructions.');
$this->Session->setFlash($message);
} else {
$message = __('Something went wrong with activation mail. Please try later.');
$this->Session->setFlash($message);
}
}
$this->redirect('/');
}
}
I checked the execution log of MySQL but all I am seeing are selects no updates or even inserts.
This is probably something very dumb but I can't understand why it doesn't say anything.
Why do you use saveAssociated method for update single field?
Something like this would be easier:
$this->User->updateAll(
array( 'User.resetkey' => $hash ),
array( 'User.id' => $userId )
);
try using
$this->User->saveMany($data, array('deep' => true));
When saving. Also
if (empty($data)) {...
Might be a safer way to go
This will work for you-
$this->User->id = $data['User']['id'];
if ($this->User->save($data)) {
$this->Session->setFlash(__('The user has been saved.'));
}else {
$this->Session->setFlash(__('The user could not be updated. Please, try again.'));
}
Try to debug like:
if ($this->User->saveAssociated($data)) {
$this->Session->setFlash(__('The user has been saved.'));
}
else {
$this->Session->setFlash(__('The user could not be updated. Please, try again.'));
}
$log=$this->User->getDataSource()->getLog(false, false);
echo "<pre>";print_r($log);exit;
I'm kind new to the cakephp and I want to know how to static the id where the id is on drop down or list. I have do hidden but its not enter the database. This is my coding:
This is in the controller
function add() {
if (!empty($this->data)) {
$this->Post->create();
if ($this->Post->save($this->data)) {
$this->Session->setFlash(__('The post has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
}
}
$users = $this->Post->User->find('list');
$this->set(compact('users'));
$this->set('userid',$this->Auth->user('id'));
}
this coding is in the ctp
<?php
echo $this->Form->input('user_id');
?>
The best way is to not send the user id to the browser and back at all, since that opens the possibility of form tinkering and security breaches/invalid results. Just inject the user id into the data before saving:
function add() {
if (!empty($this->data)) {
$this->Post->create();
// setting user id
$this->data['Post']['user_id'] = $this->Auth->user('id');
if ($this->Post->save($this->data)) {
$this->Session->setFlash(__('The post has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
}
}
$users = $this->Post->User->find('list');
$this->set(compact('users'));
}
I have a users controller, where if I add a user I want to redirect based on the usertype a user selects on making his account
the situation:
users table
id
name
usertype_id
The user add form has a select box for user type, I have two types of users: teachers and students (each another table, model, controller) if the user chooses teacher I want to redirect to /teachers/add/$id if the user chooses student I want to redirect to: /students/add/$id
this is what I have atm, but that doesn't work obviously
<?php
class UsersController extends AppController {
var $name = 'Users';
function add() {
if (!empty($this->data)) {
$this->User->create();
if ($this->User->save($this->data)) {
$id = $this->User->id;
if ($this->User->usertype_id=='1')
{
$this->redirect(array('students/add/'.$id));
} elseif ($this->User->usertype_id=='2') {
$this->redirect(array('teachers/add/'.$id));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
}
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
}
}
$usertypes = $this->User->Usertype->find('list');
$this->set(compact('usertypes'));
}
}
?>
I'm pretty sure the problem is the assumption that, because $this->User->id exists, $this->User->usertype_id must also exist, which it does not. I ran into that issue when I first started working with CakePHP, too. :)
If the user type was passed via the add form, you need to check the data array:
Change
if ($this->User->usertype_id=='1')
To
if ($this->data['User']['usertype_id'] == '1')
If that doesn't work (I can't remember if $this->data is emptied after a successful save) then you should store the value prior to the save, like so:
function add() {
if (!empty($this->data)) {
$usertype_id = $this->data['User']['usertype_id'];
$this->User->create();
if ($this->User->save($this->data)) {
$id = $this->User->id;
if ($usertype_id == '1') {
$this->redirect(array('students/add/'.$id));
} elseif ($usertype_id == '2') {
// the rest remains the same
Addendum
Rather than using the concatenation in your redirect, This looks cleaner to me:
$this->redirect(array('controller' => 'teachers', 'action' => 'add', $id));
But I guess that's just preference.
Addendum 2
I have some additional advice about cleaning up your controller and moving all of the logic to the model. This way you can re-use the code from other controllers in the future, and your current controller will be easier to read. I would change the entire method to look like this:
// this is in /controllers/users_controller.php
function add() {
if (!empty($this->data)) {
$saved_user = $this->User->save_user($this->data);
if ($saved_user['success']) {
$this->redirect(array(
'controller' => $saved_user['controller'],
'action' => 'add',
$this->User->id
));
}
}
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
$usertypes = $this->User->Usertype->find('list');
$this->set(compact('usertypes'));
}
// this is in your model, /models/user.php
function save_user($data) {
$this->create;
$usertype_id = $data['User']['usertype_id'];
return array(
'controller' => ($usertype_id == '2') ? 'teachers': 'students';
'success' => $this->save($data),
);
}