thyseus / yii-user-management

a user management module collection for the yii framework
185 stars 122 forks source link

Property "YumRole.role_id" is not defined exception - user role group and enableOnlineStatus related #107

Open chemicalcosh opened 11 years ago

chemicalcosh commented 11 years ago

I am having a problem where if I create a role group, assign a user to that group and then they login the exception "Property "YumRole.role_id" is not defined" is thrown after they are sent to the following simple controller:

class AdminController extends YumController
{
    // access rules
    public function accessRules() {
        return array(
            array('allow',
                'actions'=>array('index'),
                'expression' => 'Yii::app()->user->can("access_admin_site")',
            ),
            array('deny',  // deny all other users
                'users'=>array('*'),
            ),
        );
    }

     public function actionIndex() {
        $this->render('index');
     }
}

The users membership of the role is also removed in the mysql database table user_role when this exception is thrown(!)

In the method beforeAction() in YumController.php the following statement appears to be the cause of the error:

if(Yum::module()->enableOnlineStatus && !Yii::app()->user->isGuest)
      Yii::app()->user->data()->setLastAction();

If I set:

'user' => array(
 'enableOnlineStatus'=>false,
 ....
),

To stop it from firing then the problem does not occur anymore.

I believe it is something to do with this relation in YumRole.php but have not quite figured out why yet ;)

'activeusers'=>array(self::MANY_MANY, 'YumUser', Yum::module('role')->userRoleTable . '(role_id, user_id)', 'condition' => 'status = 3'),

Stack trace:

CException

Property "YumRole.role_id" is not defined.

/Users/cc/Sites/mywebapp/YiiRoot/framework/db/ar/CActiveRecord.php(143)

131      */
132     public function __get($name)
133     {
134         if(isset($this->_attributes[$name]))
135             return $this->_attributes[$name];
136         elseif(isset($this->getMetaData()->columns[$name]))
137             return null;
138         elseif(isset($this->_related[$name]))
139             return $this->_related[$name];
140         elseif(isset($this->getMetaData()->relations[$name]))
141             return $this->getRelated($name);
142         else
143             return parent::__get($name);
144     }
145 
146     /**
147      * PHP setter magic method.
148      * This method is overridden so that AR attributes can be accessed like properties.
149      * @param string $name property name
150      * @param mixed $value property value
151      */
152     public function __set($name,$value)
153     {
154         if($this->setAttribute($name,$value)===false)
155         {
Stack Trace
#0  
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/db/ar/CActiveRecord.php(143): CComponent->__get("role_id")
#1  
–  /Users/cc/Sites/mywebapp/WebRoot/protected/modules/user/components/CAdvancedArBehavior.php(165): CActiveRecord->__get("role_id")
160 
161         // An array of objects is given
162         foreach((array)$this->owner->$key as $foreignobject)
163         {
164             if(!is_numeric($foreignobject) && is_object($foreignobject))
165                 $foreignobject = $foreignobject->{$foreignobject->$relation['m2mForeignField']};
166             $this->execute(
167                     $this->makeManyManyInsertCommand($relation, $foreignobject));
168         }
169     }
170 
#2  
+  /Users/cc/Sites/mywebapp/WebRoot/protected/modules/user/components/CAdvancedArBehavior.php(107): CAdvancedArbehavior->writeRelation(array("key" => "roles", "foreignTable" => "YumRole", "m2mTable" => "user_role", "m2mThisField" => "user_id", ...))
#3  
–  /Users/cc/Sites/mywebapp/WebRoot/protected/modules/user/components/CAdvancedArBehavior.php(93): CAdvancedArbehavior->writeManyManyTables()
88     public function afterSave($event) 
89     {
90         if(!is_array($this->ignoreRelations))
91             throw new CException('ignoreRelations of CAdvancedArBehavior needs to be an array');
92 
93         $this->writeManyManyTables();
94         parent::afterSave($event);
95         return true;
96     }
97 
98     protected function writeManyManyTables() 
#4  
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/base/CComponent.php(558): CAdvancedArbehavior->afterSave(CEvent)
#5  
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/db/ar/CActiveRecord.php(838): CComponent->raiseEvent("onAfterSave", CEvent)
#6  
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/db/ar/CActiveRecord.php(918): CActiveRecord->onAfterSave(CEvent)
#7  
+  /Users/cc/Sites/mywebapp/WebRoot/protected/modules/user/models/YumUser.php(222): CActiveRecord->afterSave()
#8  
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/db/ar/CActiveRecord.php(1072): YumUser->afterSave()
#9  
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/db/ar/CActiveRecord.php(795): CActiveRecord->update(array("lastaction"))
#10 
–  /Users/cc/Sites/mywebapp/WebRoot/protected/modules/user/models/YumUser.php(88): CActiveRecord->save(false, array("lastaction"))
83     // last action when a user does something
84     public function setLastAction()
85     {
86     if (!Yii::app()->user->isGuest && !$this->isNewRecord) {
87             $this->lastaction = time();
88             return $this->save(false, array('lastaction'));
89         }
90     }
91 
92     public function getLogins()
93     {
#11 
–  /Users/cc/Sites/mywebapp/WebRoot/protected/modules/user/controllers/YumController.php(25): YumUser->setLastAction()
20         }
21     }
22 
23     public function beforeAction($action) {
24         if(Yum::module()->enableOnlineStatus && !Yii::app()->user->isGuest)
25             Yii::app()->user->data()->setLastAction();
26 
27         if(!isset(Yii::app()->cache))
28             throw new CHttpException(500, 'Please enable a caching component for yii-user-management to work.');
29 
30         return parent::beforeAction($action);
#12 
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/web/CController.php(306): YumController->beforeAction(CInlineAction)
#13 
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/web/filters/CFilterChain.php(133): CController->runAction(CInlineAction)
#14 
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/web/filters/CFilter.php(40): CFilterChain->run()
#15 
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/web/CController.php(1145): CFilter->filter(CFilterChain)
#16 
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/web/filters/CInlineFilter.php(58): CController->filterAccessControl(CFilterChain)
#17 
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/web/filters/CFilterChain.php(130): CInlineFilter->filter(CFilterChain)
#18 
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/web/CController.php(291): CFilterChain->run()
#19 
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/web/CController.php(265): CController->runActionWithFilters(CInlineAction, array("accessControl"))
#20 
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/web/CWebApplication.php(282): CController->run("index")
#21 
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/web/CWebApplication.php(141): CWebApplication->runController("admin/index")
#22 
+  /Users/cc/Sites/mywebapp/YiiRoot/framework/base/CApplication.php(169): CWebApplication->processRequest()
#23 
+  /Users/cc/Sites/mywebapp/WebRoot/index.php(13): CApplication->run()
thyseus commented 11 years ago

This looks like a bug in CAdvancedArBehavior for me. Need to investigate this. Thanks for reporting.

muet84 commented 11 years ago

Any update?? I am facing the same problem whie tried to creat user after assign Usermanagemnt role to any user.

Property "YumRole.role_id" is not defined.

Pl help.