I am updating a project from CakePHP 3.5 to 4.0.
In one of my models, I am doing this:
public function afterSave(Event $event, EntityInterface $entity)
{
Cache::clear(false, 'dbResults');
}
This works in the "old" version of my project with CakePHP 3.5, but fails with CakePHP 4.0 with the error
The "" cache configuration does not exist.#0 /var/www/repo/public/vendor/cakephp/cakephp/src/Cache/Cache.php(230): Cake\Cache\Cache::_buildEngine('')
I don't understand why the code tries to search for a configuration with empty string "".
Here is the relevant part of my app.php config file:
'Cache' => [
'default' => [
'className' => 'File',
'prefix' => 'headless_',
'path' => CACHE,
'url' => env('CACHE_DEFAULT_URL', null),
],
'_cake_core_' => [
'className' => 'File',
'prefix' => 'headless_cake_core_',
'path' => CACHE . 'persistent/',
'serialize' => true,
'duration' => '+2 minutes',
'url' => env('CACHE_CAKECORE_URL', null),
],
'_cake_model_' => [
'className' => 'File',
'prefix' => 'headless_cake_model_',
'path' => CACHE . 'models/',
'serialize' => true,
'duration' => '+2 minutes',
'url' => env('CACHE_CAKEMODEL_URL', null),
],
'permissions' => [
'className' => 'File',
'prefix' => 'headless_',
'path' => CACHE . 'permissions/',
'url' => env('CACHE_PERMISSIONS_URL', null),
],
'dbResults' => [
'className' => 'File',
'prefix' => 'headless_',
'path' => CACHE . 'dbResults/',
'url' => env('CACHE_PERMISSIONS_URL', null),
'duration' => '+1440 minutes',
],
'pages' => [
'className' => 'File',
'prefix' => 'page_',
'path' => CACHE . 'pages/',
'serialize' => true,
'duration' => '+320 minutes',
],
'page_contents' => [
'className' => 'File',
'prefix' => 'pc_',
'path' => CACHE . 'page_contents/',
'serialize' => true,
'duration' => '+320 minutes',
],
],
In addition to that, in my bootstrap.php file I am doing
Cache::setConfig(Configure::consume('Cache'));
I don't understand why I get this error when everything seems to be properly configured.
Please update the following line
Cache::clear('dbResults');
In CakePHP 4, Cache::clear() accepts only one parameter as opposed to CakePHP 3. They removed the $check parameter. Unfortunately migration guide does not seem to be updated about this.
Reference:
https://book.cakephp.org/4/en/core-libraries/caching.html#clearing-cached-data
Related
I am implementing LDAP authentication in laravel app. When I run this code I am getting an error.
I don't know whether it is correct or not. I am totally new to LDAP and I don't know how it works by seeing the documentation I have done till now.
Please help me out for setting the correct configuration settings for laravel-ldap.
Adldap \ Auth \ BindException (49)
Invalid credentials
protected function attemptLogin(Request $request)
{
$ldap = new Adldap;
$data = Adldap::users()->get();
dd($data);
}
the credential which I got from client
User Name: username
Password: password
IP:172.16.xx.xx
Port: 389
Attributes: CN=user.admin,CN=Users,DC=UATADSRV,DC=COM
my .env file
LDAP_HOSTS=172.16.xx.xx
LDAP_BASE_DN=CN=user.admin,CN=Users,DC=UATADSRV,DC=COM
LDAP_USER_ATTRIBUTE=samaccountname
LDAP_CONNECTION=default
LDAP_USERNAME=username
LDAP_PASSWORD=password
ldap.php
return [
'logging' => env('LDAP_LOGGING', false),
'connections' => [
'default' => [
'auto_connect' => env('LDAP_AUTO_CONNECT', true),
'connection' => Adldap\Connections\Ldap::class,
'settings' => [
'schema' => Adldap\Schemas\ActiveDirectory::class,
'account_prefix' => env('LDAP_ACCOUNT_PREFIX', ''),
'account_suffix' => env('LDAP_ACCOUNT_SUFFIX', ''),
'hosts' => explode(' ', env('LDAP_HOSTS', 'corp-dc1.corp.acme.org corp-dc2.corp.acme.org')),
'port' => env('LDAP_PORT', 389),
'timeout' => env('LDAP_TIMEOUT', 5),
'base_dn' => env('LDAP_BASE_DN', 'dc=corp,dc=acme,dc=org'),
'username' => env('LDAP_USERNAME'),
'password' => env('LDAP_PASSWORD'),
'follow_referrals' => false,
'use_ssl' => env('LDAP_USE_SSL', false),
'use_tls' => env('LDAP_USE_TLS', false),
],
],
],
];
ldap_auth.php
return [
'connection' => env('LDAP_CONNECTION', 'default'),
'provider' => Adldap\Laravel\Auth\DatabaseUserProvider::class,
'model' => App\User::class,
'rules' => [
Adldap\Laravel\Validation\Rules\DenyTrashed::class,
],
'scopes' => [
],
'identifiers' => [
'rules' => [
Adldap\Laravel\Validation\Rules\DenyTrashed::class,
],
'scopes' => [
],
'identifiers' => [
'ldap' => [
// 'locate_users_by' => 'userprincipalname',
'locate_users_by' => 'samaccountname',
'bind_users_by' => 'distinguishedname',
],
'database' => [
'guid_column' => 'objectguid',
'username_column' => 'username',
],
'windows' => [
'locate_users_by' => 'samaccountname',
'server_key' => 'AUTH_USER',
],
],
'passwords' => [
'sync' => env('LDAP_PASSWORD_SYNC', false),
'column' => 'password',
],
'login_fallback' => env('LDAP_LOGIN_FALLBACK', false),
'sync_attributes' => [
'email' => 'userprincipalname',
'username' => 'samaccountname',
'name' => 'cn',
],
'logging' => [
'enabled' => env('LDAP_LOGGING', true),
'events' => [
\Adldap\Laravel\Events\Importing::class => \Adldap\Laravel\Listeners\LogImport::class,
\Adldap\Laravel\Events\Synchronized::class => \Adldap\Laravel\Listeners\LogSynchronized::class,
\Adldap\Laravel\Events\Synchronizing::class => \Adldap\Laravel\Listeners\LogSynchronizing::class,
\Adldap\Laravel\Events\Authenticated::class => \Adldap\Laravel\Listeners\LogAuthenticated::class,
\Adldap\Laravel\Events\Authenticating::class => \Adldap\Laravel\Listeners\LogAuthentication::class,
\Adldap\Laravel\Events\AuthenticationFailed::class => \Adldap\Laravel\Listeners\LogAuthenticationFailure::class,
\Adldap\Laravel\Events\AuthenticationRejected::class => \Adldap\Laravel\Listeners\LogAuthenticationRejection::class,
\Adldap\Laravel\Events\AuthenticationSuccessful::class => \Adldap\Laravel\Listeners\LogAuthenticationSuccess::class,
\Adldap\Laravel\Events\DiscoveredWithCredentials::class => \Adldap\Laravel\Listeners\LogDiscovery::class,
\Adldap\Laravel\Events\AuthenticatedWithWindows::class => \Adldap\Laravel\Listeners\LogWindowsAuth::class,
\Adldap\Laravel\Events\AuthenticatedModelTrashed::class => \Adldap\Laravel\Listeners\LogTrashedModel::class,
],
],
];
Thank you
The LDAP server is saying that your provided credentials are wrong.
Are you sure, that your username and password is 100% correct? A space or any kind of invalid character?
CN=user.admin,CN=Users,DC=UATADSRV,DC=COM`
Also, is it working with ldap_bind() function?
I want to protect my REST API by using an oauth2 authentication. I'm using bshaffer/oauth2-server-php in combination with zend 3.
I've the following config:
// autoload/oauth2.global.php
return [
'zf-oauth2' => [
'db' => [
'dsn' => sprintf(
'mysql:dbname=%s;host=%s',
false !== getenv('DB_NAME') ? getenv('DB_NAME') : '',
false !== getenv('DB_HOST') ? getenv('DB_HOST') : ''
),
'username' => false !== getenv('DB_USER') ? getenv('DB_USER') : '',
'password' => false !== getenv('DB_PASS') ? getenv('DB_PASS') : '',
],
'storage' => MyApp\OAuth2Module\Adapter\PdoAdapter::class,
'enforce_state' => true,
'allow_implicit' => true,
'access_lifetime' => 3600,
'api_problem_error_response' => false,
'options' => [
'use_jwt_access_tokens' => false,
'store_encrypted_token_string' => true,
'use_openid_connect' => false,
'id_lifetime' => 3600,
'www_realm' => 'Service',
'token_param_name' => 'access_token',
'token_bearer_header_name' => 'Bearer',
'require_exact_redirect_uri' => true,
'allow_public_clients' => true,
'allow_credentials_in_request_body' => true,
'always_issue_new_refresh_token' => false,
'refresh_token_lifetime' => 1209600,
],
],
];
And my auth route looks like this:
// autoload/router.global.php
return [
'router' => [
'routes' => [
'api' => [
'type' => Literal::class,
'options' => [
'route' => '/api',
],
'may_terminate' => false,
'child_routes' => [
'rest' => [
'type' => Literal::class,
'options' => [
'route' => '/rest',
],
'may_terminate' => false,
'child_routes' => [
'oauth' => [
'type' => Literal::class,
'options' => [
'route' => '/oauth',
'defaults' => [
'controller' => 'ZF\OAuth2\Controller\Auth',
'action' => 'token',
],
],
],
],
],
],
],
],
],
];
Everything works fine so far. I can post my client credentials to the oauth endpoint and get an access token.
But how can I protect the other endpoints? F.e. I make a GET request to /api/rest/myapp/GetList. The list of my entities should only be retrieved if the user also sends the authorization bearer with the request but I can't find a solution for this. Is it possible to set a parameter (something like "require_token") in the route config to "activate" this behavior? Or what is the correct way to protect my REST API?
There's no build-in system to make this. You will create a listener which's listens MvcEvent::Event_ROUTE and place it after router then check if there's a routematch. If there's one, check if it's protected route. If it's apply authentication logic.
This is how I defined route for my api. It is prefixed with /api/v1. But now few new modules are added in api v2 and all v1 apis are remains same and available in v2. How can i modify this routes that will serve all routes belongs to /api/v1 and when /api/v1 is called and it should serve both /api/v2 and /api/v1 when /api/v2 is called?
module.config.php
'product' => array(
'type' => 'Zend\Router\Http\Segment',
'options' => array(
'route' => '/api/v1/categories[/:id]',
'defaults' => array(
'controller' => CategoryController::class,
),
),
),
'products' => array(
'type' => 'Zend\Router\Http\Segment',
'options' => array(
'route' => '/api/v1/products[/:id]',
'defaults' => array(
'controller' => ProductsController::class,
),
),
),
// ... at lots of v1 apis
//these are introduced in v2
'trends' => array(
'type' => 'Zend\Router\Http\Segment',
'options' => array(
'route' => '/api/v2/trends[/:id]',
'defaults' => array(
'controller' => TrendsController::class,
),
),
),
You can move those common v1 and v2 to a single parent route and v2-only ones to another. Below is sample (not tested) code that should help you understand the idea.
return [
// in Config.router.routes
'api' => [
'child_routes' => [
'v1' => [
'child_routes' => [
// your API 1-and-2 routes
'product' => [/* … */],
'products' => [/* … */]
],
'may_terminate' => false,
'options' => [
'constraints' => ['version' => 'v1|v2'],
'route' => '/:version'
],
'type' => Segment::class
],
'v2' => [
'child_routes' => [
// your API 2 routes
'trends' => [/* … */]
],
'may_terminate' => false,
'options' => ['route' => '/v2'],
'type' => Literal::class
]
],
'may_terminate' => false,
'options' => ['route' => '/api'],
'type' => Literal::class
]
];
If you prefer to not use child routes, you can simply add a route parameter/constraint instead of /v1:
return [
'product' => [
'options' => [
'constraints' => [
'id' => '…',
'version' => 'v1|v2'
],
'defaults' => ['controller' => CategoryController::class],
'route' => '/api/:version/categories[/:id]'
],
'type' => Segment::class
]
];
I know this is late, but I just found this question.
Whilst #gsc's answer is somewhat ok, this is not the correct answer.
This is the correct answer, and this is how I use it:
'api' => [
/** Our main route is /api **/
'may_terminate' => true,
'options' => ['route' => '/api'],
'type' => Literal::class,
'child_routes' => [
/** Since our main route is /api, this will become /api/v1/YOUR_ACTIONS **/
'v1' => [
'type' => Segment::class,
'options' => [
'route' => '/v1[/:action]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => Controller\ApiV1Controller::class,
'action' => 'index',
],
],
],
/** Since our main route is /api, this will become /api/v2/YOUR_ACTIONS **/
'v2' => [
'type' => Segment::class,
'options' => [
'route' => '/v2[/:action]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => Controller\ApiV2Controller::class,
'action' => 'index',
],
],
],
/** Add as many "versions" as you want, all with different controllers. **/
],
],
This allows you to use different "versions" of your controller and is shorter, better understandable and complies to standards.
Enjoy!
Error > while login or signup using yii2+ mongodb gives an error.
MongoDB connection with yii2 is done. please help me to solve this problem.
main-local.php
return [
'components' => [
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://sameer:27017/demo',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => true,
],
],
];
main.php
return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => false,
],
],
];
Check your vendor\yiisoft\extension.php
I had the same error after I had installed one package which add uncorrect config array to the file vendor\yiisoft\extension.php
'tigrov/yii2-country' =>
array (
'name' => 'tigrov/yii2-country',
'version' => '9999999-dev',
'alias' =>
array (
'#tigrov/country' => $vendorDir . '/tigrov/yii2-country/src',
),
'bootstrap' => 'tigrov\\country\\Bootstrap',
),
I've changed the previous one to the next one (just wrapped bootstrap in array)
'tigrov/yii2-country' =>
array (
'name' => 'tigrov/yii2-country',
'version' => '9999999-dev',
'alias' =>
array (
'#tigrov/country' => $vendorDir . '/tigrov/yii2-country/src',
),
array(
'bootstrap' => 'tigrov\\country\\Bootstrap',
)
),
I am working on a project which is auction site and it is actually
clone of www.swoop.com.
This clone is developed on CakePHP platform and I have a issues there.
I purchase that script from http://www.moneymakersnetwork.info/products/Swoopo-Clone-PHP-Script.html and there is a demo: http://demo.bidscout.net (username: demo, password: password1).
In that cms there is FCKEditor integrated and it is not working. It is
gives me that following error message:
The requested URL /js/fckeditor/editor/fckeditor.html was not found on
this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
And if I add new category it is come to a drop down list box and those
links are broken.
Ok, my host is like this: http://demosite.org/mysite/.
App and webroot folders are located in http://demosite.org/mysite/app and http://demosite.org/mysite/app/webroot.
I have a cofig.php file and it is look like this (http://demosite.org/mysite/app/config):
<?php
$config = array(
'Database' => array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'myuser',
'password' => 'mypassword',
'database' => 'mydb',
'prefix' => ''
),
'App' => array(
'encoding' => 'UTF-8',
'baseUrl' => '',
'base' => '',
'dir' => 'app',
'webroot' => 'webroot',
'name' => 'mysite',
'url' => 'http://demo.org/mysite/',
'timezone' => 'Asia/Jakarta',
'language' => 'en',
'email' => 'test#demo.com',
'theme' => 'ddbids',
'currency' => 'USD',
'pageLimit' => 100,
'bidHistoryLimit' => 10,
'remember_me' => '+30 days',
'auctionUpdateFrequency' => 1,
'timeSyncFrequency' => 9,
'gateway' => true,
'demoMode' => false,
'noAutobids' => false,
'cronTime' => 1,
'Image' => array(
'thumb_width' => 100,
'thumb_height' => 100,
'max_width' => 340,
'max_height' => 230
),
'Dob' => array(
'year_min' => date('Y') - 100,
'year_max' => date('Y') - 17
)
),
'Paypal' => array(
'url' => 'https://www.paypal.com/cgi-bin/webscr',
'email' => '',
'lc' => 'GB'
),
'Email' => array(
'delivery' => 'smtp',
'sendAs' => 'both',
'host' => 'localhost',
'port' => 25,
'timeout' => 60,
'username' => '',
'password' => ''
),
'Cache' => array(
//'disable' => true,
'check' => false,
'time' => '' // relative time such as +1 day, +2 months, +3 minutes
),
'debug' => 1
);
?>
I am very new to the CakePHP. Please help me.
Your problem is that (probably) the links are hard coded and expect your site to be at:
http://demosite.org/ instead of http://demosite.org/mysite/
this way your /js/fckeditor/editor/fckeditor.html can be found.
At least that's how it sounds to me this problem.
Just try this.
put the whole FCK folder in js FOLDER of in APP ie., APP/webroot/js/Editor