AuthManager is null

Hi folks,

I’ve been trying to write some RBAC using the CPhpAuthManager default. I added a very simple protected/data/auth.php file:




<?php 

$authManager = Yii::app()->authManager;

$authManager->createOperation('createThingy', 'create a thingy');

...



However, when I call Yii::app()->user->checkAccess(…), I get the following fatal error:




Fatal error: Call to a member function createOperation() on a non-object in C:\xampp\htdocs\myproject\protected\data\auth.php on line 3.



which means that Yii:app()->authManager returns a null value (I double-checked this via a var_dump call and it was indeed null). Now, I’ve tried this both with and without declaring an AuthManager component in the main config. I thought that by default Yii uses the CPhpAuthManager, so the call should work regardless.

Anyone encountered this before or know what I’m missing?

Thanks!

Just some more info, I’m running PHP 5.2.9, Yii 1.1.5.

If I call


Yii::app()->getAuthManager();

directly then I get:




 Fatal error: Maximum function nesting level of '100' reached, aborting! in C:\xampp\htdocs\yii_framework\base\CModule.php on line 165

Call Stack

#	Time	Memory	Function	Location

1	0.0009	69720	{main}( )	..\index-test.php:0

2	0.0441	1686032	CApplication->run( )	..\index-test.php:17

3	0.0496	1831616	CWebApplication->processRequest( )	..\CApplication.php:135

4	0.0534	2023320	CWebApplication->runController( )	..\CWebApplication.php:121

5	0.0618	2480824	CController->run( )	..\CWebApplication.php:324

6	0.0627	2516456	CController->runActionWithFilters( )	..\CController.php:257

7	0.0645	2596616	CFilterChain->run( )	..\CController.php:283

8	0.0649	2605808	CInlineFilter->filter( )	..\CFilterChain.php:130

9	0.0649	2605808	CController->filterAccessControl( )	..\CInlineFilter.php:59

10	0.0664	2687096	CFilter->filter( )	..\CController.php:1084

11	0.0734	3015176	CFilterChain->run( )	..\CFilter.php:41

12	0.0734	3015880	CController->runAction( )	..\CFilterChain.php:133

13	0.0734	3015880	CInlineAction->run( )	..\CController.php:300

14	0.0735	3017568	EventController->actionCreate( )	..\CInlineAction.php:57

15	0.1306	4857744	CActiveRecord->save( )	..\EventController.php:101

16	0.1324	4865704	CActiveRecord->insert( )	..\CActiveRecord.php:764

17	0.1324	4867104	Event->beforeSave( )	..\CActiveRecord.php:994

18	0.1324	4867496	ActiveRecord->beforeSave( )	..\Event.php:311

19	0.1335	4880608	CWebUser->checkAccess( )	..\ActiveRecord.php:42

20	0.1335	4881048	CWebApplication->getAuthManager( )	..\CWebUser.php:735

21	0.1335	4881048	CModule->getComponent( )	..\CWebApplication.php:165

22	0.1365	5067568	CPhpAuthManager->init( )	..\CModule.php:372

23	0.1366	5068320	CPhpAuthManager->load( )	..\CPhpAuthManager.php:52

24	0.1366	5069696	CPhpAuthManager->loadFromFile( )	..\CPhpAuthManager.php:412

25	0.1374	5094744	require( 'C:\xampp\htdocs\poncla-yii\protected\data\auth.php' )	..\CPhpAuthManager.php:485

26	0.1379	5101824	require_once( 'C:\xampp\htdocs\poncla-yii\protected\data\auth\event.php' )	..\auth.php:5

27	0.1380	5102208	CModule->__get( )	..\CModule.php:0

28	0.1380	5102520	CModule->getComponent( )	..\CModule.php:86

29	0.1384	5124936	CPhpAuthManager->init( )	..\CModule.php:372

30	0.1384	5125184	CPhpAuthManager->load( )	..\CPhpAuthManager.php:52

31	0.1385	5126712	CPhpAuthManager->loadFromFile( )	..\CPhpAuthManager.php:412

32	0.1390	5149224	require( 'C:\xampp\htdocs\poncla-yii\protected\data\auth.php' )	..\CPhpAuthManager.php:485

33	0.1392	5150864	CWebApplication->getAuthManager( )	..\auth.php:8

34	0.1392	5150864	CModule->getComponent( )	..\CWebApplication.php:165

35	0.1396	5162784	CPhpAuthManager->init( )	..\CModule.php:372

36	0.1396	5163032	CPhpAuthManager->load( )	..\CPhpAuthManager.php:52

37	0.1396	5164560	CPhpAuthManager->loadFromFile( )	..\CPhpAuthManager.php:412

38	0.1401	5187072	require( 'C:\xampp\htdocs\poncla-yii\protected\data\auth.php' )	..\CPhpAuthManager.php:485

...



You are getting a recursive call to a file and this will eventually cause the system to fall over. The auth manager in the application is using your file for authorisation information. You however are calling the auth manager from inside this file so when you include it into the auth manager for the application, it calls back to itself and so-on.

Create an empty auth file and use the functions on the CPhpAuthManager to read and write to this file, you will make sure the format of the code is correct and it will not recurse.

Ohh, I see. So my script should be separate from the auth file and I should run it once to generate the contents of the auth.php which is then used by the auth manager.

Thanks!