Cakephp - Custom authentification (basicauth) in a plugin - cakephp

I am currently developing an Restfull API for my website.
I decided to develop it as a plugin.
I am using a custom class that extends BasicAuthentification. It allows me to check the client-app credential, in order to limit the API use to only approved developers.
This file works perfectly when added in the CakePHP CORE : Cake/Controller/Component/Auth/DeviceAuthentification.php
Since I am developing a Plugin, I would like everything to be inside the same directory.
Therefore in my plugin directory called MyApi, I added my custom class in the following path :
MyApi/Controller/Auth/DeviceAuthentification.php
In order to load it, in my Plugin's controller MyApiAppController, I added the following code :
public $components = array(
'Auth' => array(
'authenticate' => 'Device', // I also tried MyApi.Device
'sessionKey' => false
)
);
It does not load, the error is the following :
Authentication adapter "Device" was not found.
Anybody has an idea ?

Well, after having a look in the core file AuthComponent, it seems that you need to have the following path :
MyApi/Controller/Component/Auth/DeviceAuthentification.php
instead of
MyApi/Controller/Auth/DeviceAuthentification.php
Therefore, whenever you are working in the Plugin directory, you need to add the directory Component

Related

cakephp3 load plugin for special prefix

I have Cakephp 3 application and i want to load this plugin and the event listener below it just for admin prefix. ( not the entire project ) .
how can i achive this?
this is my
config/bootstrap.php
where i load this plugin . (I load these at end of the file )
...
Plugin::load('Josegonzalez/Upload');
\Cake\Event\EventManager::instance()->on(new \App\Controller\Event\AppEventListener());
i want these to line only run on admin prefix
You can't conditionally run code based upon routing inside boostrap.php as the routing configuration for you app hasn't been loaded yet.
You'll have to do it old school style and use $_SERVER['REQUEST_URI'].
$url = $_SERVER['REQUEST_URI'];
$admin = '/admin/';
if(substr($url, 0, strlen($admin)) === $admin) {
Plugin::load('Josegonzalez/Upload');
\Cake\Event\EventManager::instance()->on(new \App\Controller\Event\AppEventListener());
}
Updated
Your plugin should have it's own bootstrap file. Which contains
Upload/config/bootstrap.php:
\Cake\Event\EventManager::instance()->on(new \App\Controller\Event\AppEventListener());
The app's bootstrap then tells CakePHP to include the bootstrap when loading the plugin.
App/config/bootstrap.php:
Plugin::load('Josegonzalez/Upload', ['bootstrap' => true]);
You shouldn't conditionally load your plugin.
I think you are searching for authentication on requests to your plugin. You want to limit use of your plugin only to authorized administrators. That is not a plugin issue. It is an authentication issue and you should be using the Authentication component in your plugin to verify the request is valid, and that the current user is an administrator.
Quoting cakephp plugin docs https://book.cakephp.org/3.0/en/plugins.html#plugin-routes
You can also load plugin routes in your application’s routes list. Doing this provides you more control on how plugin routes are loaded and allows you to wrap plugin routes in additional scopes or prefixes.
So you can do
Router::scope('/', function ($routes) {
// Connect other routes.
$routes->scope('/admin', function ($routes) {
$routes->loadPlugin('Josegonzalez/Upload');
});
});
For your event listener, load it inside plugin bootstrap configuration
// inside plugin bootstra.php
\Cake\Event\EventManager::instance()->on(new
\App\Controller\Event\AppEventListener());

Integrating Pusher with Laravel and Ionic 1( AngularJS) in chat app

I'm trying to set up a chat application with the following stack (Laravel, AngularJS, Ionic, Pusher)
I'm using AngularJS v1.6.x and Laravel 5.3 (which has already Pusher integrated with it per default)
So I followed the docs to create a pusher account, and set up an Event in laravel with broadcasting and everything, and in the front-end (Ionic side) I have to add Pusher, which made me a bit confused since in pretty much all the tutorials they mentioned using Laravel Echo or Redis/Socket.IO
So my question how can I make this architecture work from the AngularJS side, do I only need to include the Pusher javascript file and declare the channel and work or is there something else I can do.
Cheers.
Yes. if using Pusher you don't need include in to project other tools. Such as Socket.io, redis or any other comet servers.
I figured it out! for anyone out there who has problems setting up Pusher with Laravel, here are the steps to follow:
Add Pusher to laravel using composer (composer require
pusher/pusher-php-server)
Inside your .env file add:
PUSHER_APP_ID=YOUR_APP_ID PUSHER_KEY=YOUR_APP_KEY
PUSHER_SECRET=YOUR_APP_SECRET
Set the broadcast driver to pusher:
BROADCAST_DRIVER=pusher
Inside config/Broadcasting.php
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => 'eu',
'encrypted' => true
],
],
create an event that gonna be broadcastable

Multiple prefix in cakephp 3.x

IN cakephp 2.x we can configure multiple prefixes in core.php file. like
Configure::write('Routing.prefixes', array('admin','blogger'));
But in cake php 3.X the structure of directory has been changed. There is no core.php file, So how we can configure multiple prefix in cakephp 3.x
Route prefixes get defined in the config/routes.php file in CakePHP 3 using Router::prefix(). So in your case you'd want something like this:-
Router::prefix('admin', function ($routes) {
$routes->fallbacks('DashedRoute');
});
Router::prefix('blogger', function ($routes) {
$routes->fallbacks('DashedRoute');
});
One other change you need to take into account from CakePHP 2.x to 3.x is that prefixes are mapped to sub-namespaces in your application’s Controller namespace. So for example, if you have a Pages model non-prefixed actions would go in PagesController and any actions prefixed admin would reside in Admin/PagesController.
Checkout the documentation on routes for more details.

rails angular js subdomain

I am new to angular js and I want request the server which is already implemented with subdomain for api part . I am facing issues when I want request the server api part from angular js . I tried like the following
1st case:
#service = $resource('/api.lvh.me/properties/:property_id/rates/:id',{})
2nd case:
#service = $resource('/api.lvh.me/properties/:property_id/rates/:id.json',{})
3rd case:
#service = $resource('http://api.lvh.me:3000/properties/:property_id/rates/:id.json',{})
no one is working for me , but when I tried the following its working fine
#service = $resource('/properties/:property_id/rates/:id.json',{})
In this case its hitting the normal controllers(not the api part/controllers) and I had to manage json type requests and rendering data accordingly , but I dont recommend this as I have already built the api part with sub domain constraint in rails , I want to go with this .
my routes defined like the following
namespace :api, defaults: {format: 'json'} , :path => '' , :constraints => {:subdomain => 'api'} do
....
end
Can anyone please help how to request the api part on the server .
Thanks ,
finally I fixed it by using rack-cors gem , I configured middileware as they suggested in application.rb file and changed my routing way like the following
$resource('http://api.lvh.me:3000/properties/:property_id/rates/:id',{})
Hope it helps for someone.

CSS and JavaScript Files Not Linking in CakePHP Website

I'm running my test sites off my local host using MAMP on PHP v5.2.4. Ive made quite a few attempts to take backups of two websites configured in CakePHP v1.2, along with transferring the databases and putting in the correct credentials for the database. How the websites connect to the database is through the database.php file in the app/config folder, putting in the credentials below:
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysqli',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'nameofdatabase',
'prefix' => '',
);
}
I had the database configurations put in correctly after inputting the username and password.
On the homepage, it looks like the following:
But, when I click on one of the links, it looks like the normal page, reading in the CSS and JavaScript files out of the webroot folder.
I tried following #EoinMurphy's answer and writing in the RewriteBase / code on all of the .htaccess files with no luck. Even though I have the wrong version of CakePHP, I still used these instructions on configuring the website because it had clearer instructions then this set of instructions that confused me.
I don't understand why it seems the CSS and JavaScript is being read on all of the internal pages, but not the homepage of the website? The only way I can figure out how to link the CSS and JavaScript files is using the <link src="" ... /> on the file to output the index page content.
The only other earlier issue was the output of the links like (http://...) after the links, but it was simply the following in the CSS that make it output:
a:link:after, a:visited:after {
content:" (" attr(href) ")"; /*the content CSS made it spill out the links*/
font-size:90%;
}
This is relating to my latest findings with moving CakePHP websites from a live environment to a development environment.
When CakePHP is put into a sub-folder of the webroot, the Apache config, RewriteBase must be used.
So if you've installed WAMP, enabled the MOD_REWRITE extension, and moved the 'live' website into C:\wamp\www\live
Open the .htaccess files in \live.htaccess, \live\app.htaccess, \live\app\webroot.htaccess
and add the line 'RewriteBase \live'.
As seen here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
Just to recap a few things to remember:
Enable mod_rewrite
RewriteBase if in a subfolder
Ensure database base settings are CORRECT.

Resources