I can’t do anything in my site, because of that error.
This is the code for UsersController, maybe you see what’s happening:
<?php
declare(strict_types=1);
namespace App\Controller;
use Cake\Utility\Security;
use Cake\Mailer\Mailer;
use Cake\Routing\Router;
/**
* Users Controller
*
* @property \App\Model\Table\UsersTable $Users
* @method \App\Model\Entity\Userarray()|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = array())
*/
class UsersController extends AppController
{
public function initialize() : void
{
parent::initialize();
$countUsers = $this->Users->find()->count();
if(empty($countUsers)) $this->Auth->allow();
else $this->Auth->allow(['editPicture', 'forgotpassword', 'resetpassword']);
}
public function login() {
$this->loadModel('MyPermissions');
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Usuario o contraseña incorrecta.'));
}
$countUsers = $this->Users->find()->count();
$this->set('countUsers', $countUsers);
$countProfiles = $this->Users->Profiles->find()->count();
$this->set('countProfiles', $countProfiles);
$countPermissions = $this->MyPermissions->find()->count();
$this->set('countPermissions', $countPermissions);
}
public function logout() {
$this->Flash->success(__('Chau'));
$this->redirect($this->Auth->logout());
}
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
$this->paginate = [
'contain' => ['Profiles', 'Referrer', 'Groups.Nets'],
];
$users = $this->paginate($this->Users);
$this->set(compact('users'));
}
/**
* View method
*
* @param string|null $id User id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['Profiles', 'Referrer', 'Groups', 'Aros', 'EstadosxDias', 'EventosxUsers', 'Observations', 'Pacients', 'StatusGroups', 'StockxUsers', 'Turnos'],
]);
$this->set(compact('user'));
}
/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$user = $this->Users->newEmptyEntity();
if ($this->request->is('ajax')) {
$profile_id = $this->request->getData('profile_id');
$campo = ($profile_id==3) ? 'groups._ids' : 'group_id';
$this->set('campo', $campo);
return $this->response->withStringBody($profile_id);
}elseif ($this->request->is('post')) {
$image_file_name_url = $this->request->getData('image_file_name_url') ?? null;
$type = $image_file_name_url->getClientMediaType() ?? null;
if($type == "image/gif" || $type == "image/jpeg" || $type == "image/x-png" || empty($type)) {
$size = $image_file_name_url->getSize() ?? 0;
if($size < 20480) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if(!empty($image_file_name_url)){
$user->image_file_name_url = DS . 'upload' . DS . $this->modelClass . DS . $image_file_name_url->getClientFilename();
$user->image_file_name = WWW_ROOT . 'upload' . DS . $this->modelClass . DS . $image_file_name_url->getClientFilename();
$user->image_file_name_filename = $image_file_name_url->getClientFilename();
}
if ($this->Users->save($user)) {
if(!empty($image_file_name_url)) $image_file_name_url->moveTo(WWW_ROOT . 'upload' . DS . $this->modelClass . DS . $image_file_name_url->getClientFilename());
$this->Flash->success(__('The user has been saved.'));
$id = $this->Auth->user('id') ?? null;
$action = (!empty($id)) ? 'index' : 'login';
return $this->redirect(['action' => $action]);
}
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
}
$profiles = $this->Users->Profiles->find('list', ['limit' => 200])->all();
$groups = $this->Users->Groups->find('list', ['limit' => 200])->all();
$nets = $this->Users->Groups->Nets->find('list', ['limit' => 200])->all();
$referrer = $this->Users->find('list', ['limit' => 200])->all();
$this->set(compact('user', 'profiles', 'referrer', 'groups', 'nets'));
}
/**
* Edit method
*
* @param string|null $id User id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function edit($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['Groups'],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
$profiles = $this->Users->Profiles->find('list', ['limit' => 200])->all();
$referrer = $this->Users->Referrer->find('list', ['limit' => 200])->all();
$groups = $this->Users->Groups->find('list', ['limit' => 200])->all();
$this->set(compact('user', 'profiles', 'referrer', 'groups'));
}
/**
* Delete method
*
* @param string|null $id User id.
* @return \Cake\Http\Response|null|void Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$user = $this->Users->get($id);
if ($this->Users->delete($user)) {
$this->Flash->success(__('The user has been deleted.'));
} else {
$this->Flash->error(__('The user could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
public function forgotpassword()
{
if ($this->request->is('post')) {
$email = $this->request->getData('email');
$token = Security::hash(Security::randomBytes(25));
if ($email == NULL) {
$this->Flash->error(__('Por favor ingrese su dirección de correo'));
}
if ($user = $this->Users->find('all')->where(['email'=>$email])->first()) {
$user->token = $token;
if ($this->Users->save($user)){
$mailer = new Mailer('default');
$mailer->setTransport('default');
$mailer->setFrom(['mdeanquin@gmail.com' => 'Estado a tu lado'])
->setTo($email)
->setEmailFormat('html')
->setSubject('Solicitud de olvido de contraseña')
->deliver("Hola {$user->full_name}<br/>Por favor haga clic en el link debajo para resetear su contraseña<br/><br/>".Router::url(['action' => 'resetpassword', $token], true));
$this->Flash->success('Se ha enviado a su correo ('.$email.') el link para resetear su contraseña');
return $this->redirect(['action'=>'login']);
}
}
if ($total = $this->Users->find('all')->where(['email'=>$email])->count()==0) {
$this->Flash->error(__('Este e-mail no está registrado en el sistema'));
}
}
}
public function resetpassword($token)
{
if($user = $this->Users->find('all')->where(['token'=>$token])->first()) {
if($this->request->is('post')){
$user = $this->Users->patchEntity($user, $this->request->getData());
$user->token = null;
if ($this->Users->save($user)) {
$this->Flash->success('Se reseteó su contraseña exitosamente. Por favor ingrese al sistema usando su nueva contraseña');
return $this->redirect(['action'=>'login']);
}
$this->Flash->error(__('No se pudo resetear la contraseña. Por favor intente nuevamente.'));
}
}else{
$this->Flash->error(__('Este token ya ha sido utilizado.'));
return $this->redirect(['action'=>'login']);
}
}
public function changepassword($id = null)
{
$user = $this->Users->get($id);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__('Se ha modificado la contraseña.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('La contraseña no pudo ser modificada. Por favor, intente nuevamente.'));
}
$this->set(compact('user'));
}
public function getUsersMarkers() {
$this->viewBuilder()->setLayout('ajax');
$users = array();
if ($this->controllerUser['isAdmin'] || $this->controllerUser['isGeneralCoordinator'] || !$this->controllerUser['isAuth']){
$users = $this->User->find('all')->where(['User.profile_id IN' => [3,4,5,6,7]])->contain(['Profiles', 'Pacients', 'UsersGroups.Groups', 'UsersGroups.Nets']);
} else if ($this->controllerUser['isGroupCoordinator']){
$users = $this->User->find('all')->where([
'UsersGroups.group_id in ' => $this->controllerUser['group_id'],
'User.profile_id IN' => [3,4,5,6,7]])->contain(['Profiles', 'Pacients', 'UsersGroups.Groups', 'UsersGroups.Nets']);
} else if ($this->controllerUser['isNetCoordinator']){
$users = $this->User->find('all')->where([
'UsersGroups.group_id' => $this->controllerUser['group_id'],
'UsersGroups.net_id' => $this->controllerUser['net_id'],
'User.profile_id IN' => [3,4,5,6,7]])->contain(['Profiles', 'Pacients', 'UsersGroups.Groups', 'UsersGroups.Nets']);
} else if ($this->controllerUser['isNodeCoordinator']){
$users = $this->User->find('all')->where([
'UsersGroups.group_id' => $this->controllerUser['group_id'],
'UsersGroups.net_id' => $this->controllerUser['net_id'],
'UsersGroups.main_gate' => $this->controllerUser['main_gate'],
'User.profile_id IN' => [3,4,5,6,7]])->contain(['Profiles', 'Pacients', 'UsersGroups.Groups', 'UsersGroups.Nets']);
} else if ($this->controllerUser['isTacticOperator']){
$users = $this->User->find('all')->where([
'UsersGroups.group_id' => $this->controllerUser['group_id'],
'UsersGroups.net_id' => $this->controllerUser['net_id'],
'UsersGroups.main_gate' => $this->controllerUser['main_gate'],
'User.profile_id IN' => [3,4,5,6,7]])->contain(['Profiles', 'Pacients', 'UsersGroups.Groups', 'UsersGroups.Nets']);
} else if ($this->controllerUser['isSanitaryAgent'] ){
$users = $this->User->find('all')->where([
'UsersGroups.group_id' => $this->controllerUser['group_id'],
'UsersGroups.net_id' => $this->controllerUser['net_id'],
'UsersGroups.main_gate' => $this->controllerUser['main_gate'],
'User.profile_id IN' => [3,4,5,6,7]])->contain(['Profiles', 'Pacients', 'UsersGroups.Groups', 'UsersGroups.Nets']);
}
$jsonResponse = array();
foreach($users as $user){
$jsonRow = array();
$jsonRow['name'] = $user->full_name;
$jsonRow['profile_id'] = $user->Profiles->id;
$jsonRow['profile'] = $user->Profiles->name;
if(!empty($user->UsersGroups)){
foreach($user->UsersGroups as $usersGroup){
array_push($jsonRow['group'], $user->UsersGroups->Groups->name);
if($user->Profiles->id >= 4){
array_push($jsonRow['net'], $user->UsersGroups->Nets->name);
}
if($user->Profiles->id >= 5){
array_push($jsonRow['gate'], $user->UsersGroups->main_gate);
}
}
}
if($user->Profiles->id == 7){
$jsonRow['pacients'] = '';
foreach ($user->Pacients as $pacient){
$jsonRow['pacients'] .= '<br/>'. $pacient->name . ' '. $pacient->lastname;
}
}
$jsonRow['lat'] = $user->map_lat;
$jsonRow['long'] = $user->map_long;
$jsonRow['avatar'] = $user->image_file_name_url;
switch($user->profile_id){
case 4: $jsonRow['icon'] = 'cuf'; break;
case 5: $jsonRow['icon'] = 'rn'; break;
case 6: $jsonRow['icon'] = 'ot'; break;
case 7: $jsonRow['icon'] = 'as'; break;
default: $jsonRow['icon'] = 'unknown'; break;
}
array_push($jsonResponse, $jsonRow);
}
$this->set('model',$jsonResponse);
}
public function editPicture()
{
$this->autorender = false;
$users = $this->Users->find('all');
foreach($users as $user)
{
if(!empty($user->image_file_name_url)) {
$image = basename($user->image_file_name_url);
$user->image_file_name = WWW_ROOT . "upload" . DS . $this->modelClass . DS . $image;
$user->image_file_name_filename = $image;
$this->Users->save($user);
}
}
return $this->redirect(['action'=>'login']);
}
}
?>
To clarify: the UsersGroups is the intermediate table for a belongstomany association between Users and Groups.
I put
'debug' => filter_var(env('DEBUG', true), FILTER_VALIDATE_BOOLEAN),
in app.php and in app_local.php but it doesn’t show me any error, how can I solve that?