BjyAuthorize: Parent Role id "user" does not exist - zend-framework2

I'm having a Parent Role id "user" does not exist error
What I'm trying to get BjyAuthorize to work. In my config, I have
'bjyauthorize' => array(
// Using the authentication identity provider, which basically reads the roles from the auth service's identity
'identity_provider' => 'BjyAuthorize\Provider\Identity\AuthenticationIdentityProvider',
'resource_providers' => [
'BjyAuthorizeProviderResourceConfig' => [
'user' => [],
],
],
'role_providers' => array(
// using an object repository (entity repository) to load all roles into our ACL
'BjyAuthorize\Provider\Role\ObjectRepositoryProvider' => array(
'object_manager' => 'doctrine.entity_manager.orm_default',
'role_entity_class' => 'SorgcaUser\Entity\Role',
),
'BjyAuthorizeProviderResourceConfig' => array(
'allow' => array(
array('user', 'admin', 'manage'),
),
),
),
),
In my controller I would simply call $this->isAllowed('admin', 'manage');
Now, calling isAllowed function will throw the error saying my Parent Role id does not exist
But, in my user_role table, I have
I'm not really sure how to begin debugging. Please advice.

Related

Zend Form Validation set error messages

I have setup a form along with filters and validators which seem to work correctly.
However, I can not seem to get custom error messages to work. So far I have tried.
$inputFilter->add(array(
'name' => 'message',
'required' => TRUE,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'messages' => array(
NotEmpty::IS_EMPTY => "You must specify your message",
),
),
),
));
All I get is the standard validation error message 'Value is required and can't be empty'
Please can someone point me in the right direction, many thanks.
You need to put your messages stack into options top key in validator configuration like below:
Wrong:
'validators' => array(
array(
'name' => 'NotEmpty',
'messages' => array(
NotEmpty::IS_EMPTY => "You must specify your message",
),
),
),
Correct:
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
NotEmpty::IS_EMPTY => "You must specify your message",
),
),
),
),

Match multiple domains with zf2 router

I need to match 2 different domains with my ZF2:
www.gamempire.it is the base
www.rankempire.it have to match to only a specific controller
So I tried adding this to my router configuration, but with no success (it match to my default controller):
'rankempire' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'www.rankempire.it',
'defaults' => array(
'controller' => 'rank',
'action' => 'index',
),
),
),
How to solve the problem?
Thanks
Solved putting the hostname route configuration to the end of the router

How to make ZfcUser use an abstract db_adapter

I'm using Zend Framework 2 and I've installed the module ZfcUser.
Everything was working correctly until I've decided to add a second db adapter in my config/autoload/global.php
<?php
return array(
'db' => array(
'adapters' => array(
'cleardb' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=secret;host=secret.com',
'driver_options' => array(
...
),
),
'other' => array(...)
)
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Db\Adapter\AdapterAbstract'
=> 'Zend\Db\Adapter\AdapterAbstractServiceFactory',
),
),
);
Now, ZfcUser can't find my default db adapter.
In the zfcuser.global.php I've updated this line:
'zend_db_adapter' => 'cleardb'
This is the error I get when I try to login:
An alias "zfcuser_zend_db_adapter" was requested but no service could be found.
Your help is really appreciated.

Zend Framework 2: How to properly replace Figlet with reCaptcha on zfcUser

I am trying to replace Figlet with reCaptcha on a zfcUser registration form. Partial instruction on how to accomplish this can be found on https://github.com/ZF-Commons/ZfcUser#changing-registration-captcha-element but no complete instruction exists.
Checking the README.md file has a two-step instruction on how to accomplish this but still the CAPTCHA uses Figlet when rendered on the form.
Has anyone successfully implemented this? I really need a hand on this one.
Thanks in advance.
EDIT: Here is a proven working solution I developed:
1. Add to composer.json
// Add the lines below under the "require" element:
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": ">2.2.0rc1",
"zendframework/zendservice-recaptcha": "2.*"
}
2. Goto to your project's ZF2 installation directory and execute this command:
php composer.phar update
3. Replace or Create config/autoload/database.global.php with:
<?php
$config = array(
'dbdriver' => 'pdo',
'dbhost' => 'localhost',
'dbport' => '3306',
'dbname' => 'CHANGEME',
'dbuser' => 'CHANGEME',
'dbpass' => 'CHANGEME',
);
return array(
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
'db' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname='.$config['dbname'].';host='.$config['dbhost'],
'username' => $config['dbuser'],
'password' => $config['dbpass'],
),
);
4: Execute this on your mySQL server:
CREATE TABLE `user`
(
`user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(255) DEFAULT NULL UNIQUE,
`email` VARCHAR(255) DEFAULT NULL UNIQUE,
`display_name` VARCHAR(50) DEFAULT NULL,
`password` VARCHAR(128) NOT NULL,
`state` SMALLINT UNSIGNED
) ENGINE=InnoDB CHARSET="utf8";
5. Create/Replace config/autoload/recaptcha.global.php with:
<?php
define('RECAPTCHA_PRIVATE_KEY','CHANGEME');
define('RECAPTCHA_PUBLIC_KEY','CHANGEME');
return array(
'zfcuser' => array(
'form_captcha_options' => array(
'class' => 'Zend\Captcha\ReCaptcha',
'options' => array(
'privkey' => RECAPTCHA_PRIVATE_KEY,
'pubkey' => RECAPTCHA_PUBLIC_KEY,
),
),
),
'di'=> array(
'instance'=>array(
'alias'=>array(
'recaptcha_element' => 'Zend\Form\Element\Captcha',
),
'ZfcUser\Form\Register' => array(
'parameters' => array(
'captcha_element'=>'recaptcha_element',
),
),
),
),
);
6. Create/Replace config/autoload/zfcuser.global.php with:
<?php
$settings = array(
'enable_registration' => true,
'enable_username' => true,
'auth_adapters' => array( 100 => 'ZfcUser\Authentication\Adapter\Db' ),
'enable_display_name' => false,
'auth_identity_fields' => array( 'email' ),
'use_registration_form_captcha' => true,
'user_login_widget_view_template' => 'zfc-user/user/login.phtml',
);
return array(
'zfcuser' => $settings,
'service_manager' => array(
'aliases' => array(
'zfcuser_zend_db_adapter' => (isset($settings['zend_db_adapter'])) ? $settings['zend_db_adapter']: 'Zend\Db\Adapter\Adapter',
),
),
);
7. Navigate to http://yourdomain.com/user
8. Enjoy! :)
This is how I did it, it might not be the best or correct way but it worked for me:
Add the recaptcha service to your composer.json file:
"require": {
"Zendframework/zendservice-recaptcha": "2.*"
}
Run composer to get the service. Then you need to specify the ReCaptcha config.
I created a separate config file to store the ReCaptcha keys:
//zfcuser.local.php
return array(
'zfcuser' => array(
'form_captcha_options' => array(
'options' => array(
'privkey' => RECAPTCHA_PRIVATE_KEY,
'pubkey' => RECAPTCHA_PUBLIC_KEY,
),
),
),
);
Then ZfcUser captcha config looks like so, telling it to use the ReCaptcha service:
//zfcuser.global.php
'form_captcha_options' => array(
'class' => 'Zend\Captcha\ReCaptcha',
'options' => array(
'wordLen' => 6,
'expiration' => 300,
'timeout' => 300,
),
),
Edit:
You don't need the recaptcha.global.php. You can call the config file whatever you like aslong as it ends with .global.php or .local.php. You usually name things .local.php when you don't want them in version control.
In this case I named the file zfcuser.local.php because all it does is store the ReCaptcha keys and I didn't want them in version control.
All the config files get merged in to one array when the application is started. So basically, ignore the ZfcUser documentation. Or maybe someone else can explain how to get it working that way.
The third block of code is the zfcuser.global.php.

404 HTTP errors not being rendered in JSON using Zend framework 2

I'm creating a simple restful api using zend framework2 and I've references Rob Allen's notes on the subject as well as this excellent tutorial by http://hounddog.github.com/blog/getting-started-with-rest-and-zend-framework-2/
below is my module_config.php. You'll see that I have routes and the JSON view strategy configured. My understanding is that when you set up the JSON strategy in this fashion it accommodates all modules. The issue is that when an invalid route is entered the 404 response is sent back in html even though the Accept header is requesting Application/json.
I've been struggling with this for a 2 days now any advice or help would be appreciated
This curl call to the api generates the expected 404 error.
curl -i -H "Accept: application/json" http://myapi-dev.local/xxx/1
Module_config.php
return array(
'controllers' => array(
'invokables' => array(
'Dips\Controller\Roles' => 'Dips\Controller\RolesController', //maps controller alias to a physical controller
),
),
'router' => array(
'routes' => array(
'dips' => array(
'type' => 'segment',
'options' => array(
'route' => '/dips/roles/:apikey/:uname/:appname',
'constraints' => array(
'apikey' => '[a-zA-Z0-9]+',
'uname' => '[a-zA-Z]+',
'appname' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Dips/Controller/Roles',
),
),
),
),
),
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy',
),
),
);
You would need to implement an additional rendering strategy and put it in the stack above the default 404 rendering strategy. Alternatively, you could extend the existing ViewJsonStrategy to include error handling.
The default 404 Strategy is Zend/Mvc/View/Http/RouteNotFoundStrategy. If you look at it's detectNotFoundError method, you can see when it gets triggered.

Resources