Hellow good day. i have done what you asked but it still won’t work. here’s my full code
namespace App\Controller;
use chillerlan\QRCode\QRCode;
/**
* Riders Controller
*
* @method \App\Model\Entity\Rider[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class RidersController extends AppController
{
// in src/Controller/UsersController.php
public function beforeFilter(\Cake\Event\EventInterface $event)
{
parent::beforeFilter($event);
$this->Authentication->allowUnauthenticated(['login']);
}
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
//use for skipping the authorization making anyone view the contents
$this->Authorization->skipAuthorization();
$riders = $this->paginate($this->Riders);
$this->set(compact('riders'));
}
/**
* View method
*
* @param string|null $id Rider id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
try {
$rider = $this->Riders->get($id, [
'contain' => [],
]);
$this->Authorization->authorize($rider); // Authorization needed to perform action
} catch (\Throwable $th) {
$this->Flash->error('Sorry only Admin is allowed to view riders');
return $this->redirect(['action' => 'index']);
}
$this->set(compact('rider'));
}
/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$rider = $this->Riders->newEmptyEntity();
//this "try/catch" code will prevent users for accidentally doing something that its not authorized.
//but you must put false in the policy
try {
$this->Authorization->authorize($rider); //Authorization needed to perform action
if ($this->request->is('post')) {
$rider = $this->Riders->patchEntity($rider, $this->request->getData());
if ($this->Riders->save($rider)) {
$this->Flash->success(__('The rider has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The rider could not be saved. Please, try again.'));
}
} catch (\Throwable $th) {
$this->Flash->error('Sorry only Admin is allowed to add riders');
return $this->redirect(['action' => 'index']);
}
$this->set(compact('rider'));
}
/**
* Edit method
*
* @param string|null $id Rider 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)
{
$rider = $this->Riders->get($id, [
'contain' => [],
]);
//this "try/catch" code will prevent users for accidentally doing something that its not authorized.
//but you must put false in the policy
try {
$this->Authorization->authorize($rider); //Authorization needed to perform action
if ($this->request->is(['patch', 'post', 'put'])) {
$rider = $this->Riders->patchEntity($rider, $this->request->getData());
if ($this->Riders->save($rider)) {
$this->Flash->success(__('Rider Changes has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The changes could not be saved. Please, try again.'));
}
} catch (\Throwable $th) {
$this->Flash->error('Sorry only Admin is allowed to edit riders');
return $this->redirect(['action' => 'index']);
}
$this->set(compact('rider'));
}
/**
* Delete method
*
* @param string|null $id Rider 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']);
$rider = $this->Riders->get($id);
//this "try/catch" code will prevent users for accidentally doing something that its not authorized.
//but you must put false in the policy
try {
$this->Authorization->authorize($rider); //Authorization needed to perform action
if ($this->Riders->delete($rider)) {
$this->Flash->success(__('The rider has been deleted.'));
} else {
$this->Flash->error(__('The rider could not be deleted. Please, try again.'));
}
} catch (\Throwable $th) {
$this->Flash->error('Sorry only Admin is allowed to delete riders or Current Rider is active on Queue. Please Check!');
return $this->redirect(['action' => 'index']);
}
return $this->redirect(['action' => 'index']);
}
// Logout Function
public function logout()
{
//use for skipping the authorization making anyone view the contents
$this->Authorization->skipAuthorization();
$this->Authentication->logout();
return $this->redirect(['controller' => 'Homepages', 'action' => 'homepage']);
}
public function generateQRCode($id = null)
{
$rider = $this->Riders->get($id, [
'contain' => [],
]);
// Check if the current user can generate a QR code for this rider
$this->Authorization->authorize($rider, 'cangenerateQRCode');
$riderData = [
'id' => $rider->id,
'First Name' => $rider->first_name,
'Last Name' => $rider->last_name,
];
// Convert the rider data to a JSON string
$riderData = json_encode($riderData);
// Generate the QR code
$imgSrc = (new QRCode)->render($riderData);
debug($imgSrc);
$this->set(compact('imgSrc'));
}
}
in the policy
namespace App\Policy;
use App\Model\Entity\Rider;
use Authorization\IdentityInterface;
/**
* Rider policy
*/
class RiderPolicy
{
/**
* Check if $user can add Rider
*
* @param \Authorization\IdentityInterface $user The user.
* @param \App\Model\Entity\Rider $rider
* @return bool
*/
public function canAdd(IdentityInterface $user, Rider $rider)
{
if ($user->role === 'Admin' || $user->role === 'admin') {
return true;
} else {
return false;
}
}
/**
* Check if $user can edit Rider
*
* @param \Authorization\IdentityInterface $user The user.
* @param \App\Model\Entity\Rider $rider
* @return bool
*/
public function canEdit(IdentityInterface $user, Rider $rider)
{
if ($user->role === 'Admin' || $user->role === 'admin') {
return true;
} else {
return false;
}
}
/**
* Check if $user can delete Rider
*
* @param \Authorization\IdentityInterface $user The user.
* @param \App\Model\Entity\Rider $rider
* @return bool
*/
public function canDelete(IdentityInterface $user, Rider $rider)
{
if ($user->role === 'Admin' || $user->role === 'admin') {
return true;
} else {
return false;
}
}
/**
* Check if $user can view Rider
*
* @param \Authorization\IdentityInterface $user The user.
* @param \App\Model\Entity\Rider $rider
* @return bool
*/
public function canView(IdentityInterface $user, Rider $rider)
{
if ($user->role === 'Admin' || $user->role === 'admin') {
return true;
} else {
return false;
}
}
public function cangenerateQRCode(IdentityInterface $user, Rider $rider)
{
if ($user->role === 'Admin' || $user->role === 'admin') {
return true;
} else {
return false;
}
}
}
and in the view
<div class="column-responsive column-80">
<div class="riders view content">
<table>
<tr>
<img src="<?/the code/= $imgSrc ?>" alt="QR Code" />
<th><?= __('First Name') ?></th>
<td><?= h($rider->first_name) ?></td>
</tr>
<tr>
<th><?= __('Last Name') ?></th>
<td><?= h($rider->last_name) ?></td>
</tr>
<tr>
<th><?= __('Address') ?></th>
<td><?= h($rider->address) ?></td>
</tr>
<tr>
<th><?= __('Id') ?></th>
<td><?= $this->Number->format($rider->id) ?></td>
</tr>
<tr>
<th><?= __('Contact') ?></th>
<td><?= h($rider->contact) ?></td>
</tr>
<tr>
<th><?= __('Brand') ?></th>
<td><?= h($rider->brand) ?></td>
</tr>
<tr>
<th><?= __('Model') ?></th>
<td><?= h($rider->model) ?></td>
</tr>
<tr>
<th><?= __('Plate #') ?></th>
<td><?= h($rider->plate) ?></td>
</tr>
</table>
</div>
</div>
</div>
also I’ve been having trouble for the cake php here in my screen. it’s not like the first that i can have those console from cake here is the image.
it became so small i could hardly read anything inside the box.
Im sorry for being troublesome. I just don’t know how to get through this problem myself.