CDbAuthManager internal error

I get this error, the error is in the Yii internals. I do not know how to resolve the error.

Fatal error: Call to a member function createCommand() on a non-object in /Applications/MAMP/htdocs/projects/DevelopmentProject/yii/framework/web/auth/CDbAuthManager.php on line 265

Also various other things are not working, like the checkAccess method …

Code I used to create DB records …




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


			// Create tasks / operations and assign operations to tasks

			$task = $auth->createTask("userManagement", "Manages the users");

			$auth->createOperation("editSubUser");

			$auth->createOperation("deleteSubUser");

			$task->addChild("editSubUser");

			$task->addChild("deleteSubUser");


			// Add tasks to roles

			$role = $auth->createRole("accountHolder");

			$role->addChild("userManagement");

			$role->addChild("deleteSubUser");


			$auth->save();



Code I used to check the access




if(Yii::app()->user->checkAccess("accountHolder")) echo("Hello"); exit;



Code I used for assignment …




Yii::app()->authManager->assign("accountHolder", $user->id);



The assignment is in the database with correct userid.

The other items are in the database as well.

Fixed this by using …

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

Instead of …

$a = new CDbAuthManager;

Still do not know whats wrong with other part of system i.e. checkAccess.

Here is some test code I wrote …




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

echo($a->isAssigned("accountHolder", Yii::app()->user->id));

if(Yii::app()->user->checkAccess("accountHolder")) echo("Hello"); exit;



It echos a 1 which is correct, but it does not echo the Hello which is strange.

Here is some more test code I wrote …




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

echo("<pre>"); print_r($a->getOperations(Yii::app()->user->id)); echo("</pre>");

if(Yii::app()->user->checkAccess("accountHolder")) echo("Hello"); exit;



It returns an empty array and it does not echo the hello.

So it assumes no operations are assigned to the accountholder however here is the code I used to create the hierachy …




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


			// Create tasks / operations and assign operations to tasks

			$task = $auth->createTask("userManagement", "Manages the users");

			$auth->createOperation("editSubUser");

			$auth->createOperation("deleteSubUser");

			$task->addChild("editSubUser");

			$task->addChild("deleteSubUser");


			// Add tasks to roles

			$role = $auth->createRole("accountHolder");

			$role->addChild("userManagement");

			$role->addChild("deleteSubUser");



So I cannot see why not.

I think there are many steps:

1:check your authManger config

in main.php




'authManager' => array(

    'class' => 'CDbAuthManager',

    'connectionID' => 'db',

),



2:setup your mysql.conf as output query log.

general_log = 1

general_log_file = "J:/mysql/data/mysql.log"

4:add tables:

[code]

CREATE TABLE authitem (

    `name` varchar(64) NOT NULL,


    `type` int(11) NOT NULL,


    `description` text,


    `bizrule` text,


    `data` text,


    PRIMARY KEY (`name`)


  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE authitemchild (

         `parent` v

I think there are many steps:

1:check your authManger config

in main.php




'authManager' => array(

    'class' => 'CDbAuthManager',

    'connectionID' => 'db',

),



2:setup your mysql.conf as output query log.

general_log = 1

general_log_file = "J:/mysql/data/mysql.log"

if you addChild() or assign()

you can look this log ,maybe you could find some erros. :lol:

4:add tables:




CREATE TABLE `authitem` (

        `name` varchar(64) NOT NULL,

        `type` int(11) NOT NULL,

        `description` text,

        `bizrule` text,

        `data` text,

        PRIMARY KEY (`name`)

      ) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `authitemchild` (

             `parent` varchar(64) NOT NULL,

             `child` varchar(64) NOT NULL,

             PRIMARY KEY (`parent`,`child`),

             KEY `child` (`child`),

             CONSTRAINT `authitemchild_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `authitem` (`name`) ON DELETE CASCADE ON UPDATE CASCADE,

             CONSTRAINT `authitemchild_ibfk_2` FOREIGN KEY (`child`) REFERENCES `authitem` (`name`) ON DELETE CASCADE ON UPDATE CASCADE

           ) ENGINE=InnoDB DEFAULT CHARSET=utf8;




CREATE TABLE `authassignment` (

              `itemname` varchar(64) NOT NULL,

              `userid` varchar(64) NOT NULL,

              `bizrule` text,

              `data` text,

              PRIMARY KEY (`itemname`,`userid`),

              CONSTRAINT `authassignment_ibfk_1` FOREIGN KEY (`itemname`) REFERENCES `authitem` (`name`) ON DELETE CASCADE ON UPDATE CASCADE

            ) ENGINE=InnoDB DEFAULT CHARSET=utf8;



3:create role,task and operation




//create

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

$roleAdmin = $auth->createRole('admin');

$roleGuest = $auth->createRole('guest');

$taskUserManager = $auth->createTask('userManage', 'manage user group');

$operationReadUser = $auth->createOperation('readUser', 'read user');

$operationDeleteUser = $atuh->createOperation('deleteUser', 'delete user');


//add child

$roleGuest->addChild('readUser');


$taskUserManager->addChild('guest');

$taskUserManager->addChild('deleteUser');


$roleAdmin->addChild('userManager');

$roleAdmin->addChild('guest');

$roleAdmin->addChild('deleteUser');


//assign

$user = User::model()->findByPk('1'); //query a user which id = 1

if (NULL!=$user){

$auth->assign('admin', $user->id);//指定用户权限

}


//check

if (Yii::app()->authManager->checkAccess('userManager', $user->id)){

echo "hello";

}



good luck!

vist my blog!

Hi,

I have done all the stuff you listed, I will also include a DB now as well …




-- phpMyAdmin SQL Dump

-- version 3.2.5

-- http://www.phpmyadmin.net

--

-- Host: localhost

-- Generation Time: Apr 01, 2011 at 11:04 PM

-- Server version: 5.1.44

-- PHP Version: 5.3.2


SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


--

-- Database: `developmentapp`

--


-- --------------------------------------------------------


--

-- Table structure for table `AuthAssignment`

--


CREATE TABLE `AuthAssignment` (

  `itemname` varchar(64) NOT NULL,

  `userid` varchar(64) NOT NULL,

  `bizrule` text,

  `data` text,

  PRIMARY KEY (`itemname`,`userid`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;


--

-- Dumping data for table `AuthAssignment`

--


INSERT INTO `AuthAssignment` VALUES('accountHolder', '1', NULL, 'N;');

INSERT INTO `AuthAssignment` VALUES('standard', '3', NULL, 'N;');

INSERT INTO `AuthAssignment` VALUES('accountHolder', '4', NULL, 'N;');

INSERT INTO `AuthAssignment` VALUES('accountHolder', '5', NULL, 'N;');

INSERT INTO `AuthAssignment` VALUES('standard', '6', NULL, 'N;');

INSERT INTO `AuthAssignment` VALUES('standard', '7', NULL, 'N;');


-- --------------------------------------------------------


--

-- Table structure for table `AuthItem`

--


CREATE TABLE `AuthItem` (

  `name` varchar(64) NOT NULL,

  `type` int(11) NOT NULL,

  `description` text,

  `bizrule` text,

  `data` text,

  PRIMARY KEY (`name`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;


--

-- Dumping data for table `AuthItem`

--


INSERT INTO `AuthItem` VALUES('accountHolder', 2, '', NULL, 'N;');

INSERT INTO `AuthItem` VALUES('deleteSubUser', 0, '', NULL, 'N;');

INSERT INTO `AuthItem` VALUES('editSubUser', 0, '', NULL, 'N;');

INSERT INTO `AuthItem` VALUES('userManagement', 1, 'Manages the users', NULL, 'N;');


-- --------------------------------------------------------


--

-- Table structure for table `AuthItemChild`

--


CREATE TABLE `AuthItemChild` (

  `parent` varchar(64) NOT NULL,

  `child` varchar(64) NOT NULL,

  PRIMARY KEY (`parent`,`child`),

  KEY `child` (`child`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;


--

-- Dumping data for table `AuthItemChild`

--


INSERT INTO `AuthItemChild` VALUES('accountHolder', 'userManagement');

INSERT INTO `AuthItemChild` VALUES('userManagement', 'deleteSubUser');

INSERT INTO `AuthItemChild` VALUES('userManagement', 'editSubUser');


-- --------------------------------------------------------


--

-- Table structure for table `Company`

--


CREATE TABLE `Company` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(50) NOT NULL,

  `contact_number` varchar(20) NOT NULL,

  `email` varchar(50) NOT NULL,

  `type` tinyint(2) NOT NULL,

  `activated` tinyint(1) NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;


--

-- Dumping data for table `Company`

--


INSERT INTO `Company` VALUES(1, 'Mutable Labs', '01154745647', 'web@mutablelabs.com', 1, 1);

INSERT INTO `Company` VALUES(2, 'Pivotal', '01137463746', 'web@pivotal.com', 1, 1);

INSERT INTO `Company` VALUES(3, 'Parsons', '01158473647', 'web@parsons.com', 1, 1);


-- --------------------------------------------------------


--

-- Table structure for table `User`

--


CREATE TABLE `User` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `fk_id_with_company_id` int(11) NOT NULL,

  `email` varchar(50) NOT NULL,

  `first_name` varchar(20) NOT NULL,

  `last_name` varchar(20) NOT NULL,

  `password` varchar(100) NOT NULL,

  `activated` tinyint(1) NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;


--

-- Dumping data for table `User`

--


INSERT INTO `User` VALUES(1, 1, 'web@mutablelabs.com', 'James', 'Barnsley', '098f6bcd4621d373cade4e832627b4f6', 1);

INSERT INTO `User` VALUES(3, 1, 'eric@hotmail.com', 'Ericson', 'Smith', '098f6bcd4621d373cade4e832627b4f6', 1);

INSERT INTO `User` VALUES(4, 2, 'web@pivotal.com', 'John', 'Smith', '098f6bcd4621d373cade4e832627b4f6', 1);

INSERT INTO `User` VALUES(5, 3, 'web@parsons.com', 'Helen', 'Richards', '098f6bcd4621d373cade4e832627b4f6', 1);

INSERT INTO `User` VALUES(6, 1, 'test@hotmail.com', 'testa', 'test', 'd027eb0ee23c9fcaa2b9ce4f221c5a77', 1);

INSERT INTO `User` VALUES(7, 1, 'test1@hotmail.com', 'aaa', 'test', 'd027eb0ee23c9fcaa2b9ce4f221c5a77', 1);




My config file is correct.

And the code remains the same as the code in my latest post on this thread.

your table’s engine is myisam, not innodb, so foreign key not working!

Ok, yes I was going to ask.

How does Yii know which table the "userid" column in AuthAssignments relates to?