[module] userGroups

Sorry for my noobish, but I can’t understand how to get the email address of the logged user.

Yii::app ()->user->id and Yii::app ()->user->name functions, but no Yii::app ()->user->mail or Yii::app ()->user->email…

Why that?

If i’d keep in session every user detail the session would easily become too big… Right now to get a user email you have to make a query

Anyhow i could add a function to automatically retrieve those data from the db.

Thanks for the suggestion ^^

If I try to access to the user data in this way, from another module:


$me = UserGroupsUser::model ()->findByPk (Yii::app ()->user->id);

yii say that:


include(UserGroupsGroup.php): failed to open stream: No such file or directory

and must be right, cause I’m not in the module… so, is the classic query the only way to get that data? or there’s a way to use UserGroupsUser model outside the userGroups module?

tia

Hi! Add path to userGroups’ models to main config file:




'import'=>array(

		'application.models.*',

		'application.components.*',

		...

		'application.modules.userGroups.models.*',

	),



All other models are already in the system "search path", but not models from modules.

I added a bug report regarding creating tables in the MySQL database. My database does not support Engine=InodDB so creating the tables fails when trying to install.

That’s not a bug.

Some tables are intended to be using an InnoDB engine for relations purposes.

If you are missing the engine on your mysql installation you should install it.

Thanks anyway for the support :D

HI nick,

i am a total newbie. I really like your module. Can you please tell me how to redirect to my index page instead of documentation page?

Thanks

Hi: I’ve just started playing with this extension and trying to add a Profile Extension.

I’m having an issue getting the Profile Extension to display, I get this error when trying to load the profile:


Undefined variable: dataProvider.

from \protected\views\user_extension\index.php(15)


14 <?php $this->widget('zii.widgets.CListView', array(

15     'dataProvider'=>$dataProvider,

16     'itemView'=>'_view',

17 )); ?>

I’ve basically just added a table with ug_id, firstname, etc added models and CRUD views as instructed and added the extension in the modules array.

There must be something else to do?

Hi! You cant’t directly call that view. Extension works as part of userGroups module, so you should point to application_address/userGroups to see data from user’s profile and all profile extensions loaded.

And don’t forget to fill extension table with user records (and proper ug_id field), if you have registered users already: extension table must “extend” every record from userGroups_user table.

Thanks veluroff: Yes, was trying to load /userGroups, and in the trace it looked like it was loading the Profile Extensions as desired. One such extension was what I named UserExtension.

I’m very new to yii (let alone extensions)… I added the following to user_extension\index.php


$dataProvider=new CActiveDataProvider('UserExtension');

which solved the dataProvider problem, but maybe not in the right way as I now have:


Property "UserExtension.relUserGroupsGroup" is not defined.

I’ve filled the user_extension table as you suggested (wasn’t previously), thanks.

Makes me wonder what happens when user records are deleted? :huh:

As I can’t see rest of your code, I suggest, you have troubles with relations. Check that first (see userGroups docs and this tutorial for help). Second: in your index view you should call


$dataProvider=new CActiveDataProvider($model);

because your previous call has to be in controller’s actionIndex:




$dataProvider=new CActiveDataProvider('UserExtension');

$this->render('application.views.user_extension.index',array(

   'dataProvider'=>$dataProvider,

));



ok… in the docs it says that it creates relations on the fly:

but I added a relation anyway, to no effect. OK, this is what I’ve done (stepping through the documentation):

  1. Create the database table and model

    table=user_extension; model=UserExtension (see code at #2)

  2. Add the profileViews method to the class [b]\protected\models\UserExtension.php

[/b]


<?php


/**

 * This is the model class for table "user_extension".

 *

 * The followings are the available columns in table 'user_extension':

 * @property string $ug_id

 * @property string $firstname

 * @property string $lastname

 *

 * The followings are the available model relations:

 * @property UsergroupsUser $ug

 */

class UserExtension extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @return UserExtension the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'user_extension';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('ug_id, firstname, lastname', 'required'),

			array('ug_id', 'length', 'max'=>20),

			array('firstname', 'length', 'max'=>50),

			array('lastname', 'length', 'max'=>100),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('ug_id, firstname, lastname', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'ug' => array(self::BELONGS_TO, 'UsergroupsUser', 'ug_id'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'ug_id' => 'Ug',

			'firstname' => 'Firstname',

			'lastname' => 'Lastname',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('ug_id',$this->ug_id,true);

		$criteria->compare('firstname',$this->firstname,true);

		$criteria->compare('lastname',$this->lastname,true);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

		/** 

	 * returns an array that contains the views name to be loaded 

	 * @return array 

	 */

	public function profileViews()

	{

		return array(

			UserGroupsUser::VIEW => 'index',

			UserGroupsUser::EDIT => 'update',

			UserGroupsUser::REGISTRATION => 'registration',

		); 

	}

}

  1. Creating UserGroupsUser::VIEW views

Reading between the lines and your last post I created and editted \protected\views\user_extension\index.php to:


<?php

$this->breadcrumbs=array(

	'User Extensions',

);


$this->menu=array(

	array('label'=>'Create UserExtension', 'url'=>array('create')),

	array('label'=>'Manage UserExtension', 'url'=>array('admin')),

);

?>


<h1>User Extensions</h1>


<?php

	$dataProvider=new CActiveDataProvider($model);

	 

	$this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

)); ?>



  1. Make userGroups aware of my Profile Extension

Editted \protected\config\main.php to:


	'modules'=>array(

		// uncomment the following to enable the Gii tool

		

		'gii'=>array(

			'class'=>'system.gii.GiiModule',

			'password'=>'gum',

		 	// If removed, Gii defaults to localhost only. Edit carefully to taste.

			'ipFilters'=>array('127.0.0.1','::1'),

		),

		

		'userGroups'=>array(

			'accessCode'=>'some_other_word',

			'profile'=>array('UserExtension'),

		),

	),



  1. From your last post editted \protected\controllers\UserExtensionController.php to:

	

<?php

class UserExtensionController extends Controller

{

   ...


   public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('UserExtension');

		$this->render('application.views.user_extension.index',array(

			'dataProvider'=>$dataProvider,

		));

	}


   ...



:unsure: So what have I left out?

Thanks for you help BTW…

G’day

I’m pretty new with Yii which I’m evaluating for a pretty ambitious web application.

The usergroups modules looks like it could save me a lot of grief on the user management side of things but I have a couple of questions…

Am I correct in guessing I can do away with the tables for users & groups that I’ve specified in my data model?

Would I be best to install usergroups & then create the rest of my data structure or the other way around?

Cheers & TIA,

Pedro :)

Hey Pedro: I’m new too (but welcome!)

Two nights ago I was installing userGroups into an existing app without much success.

Last night I started from scratch and it all worked fine.

Now I’m trying to extend my user Profiles :blink: (not with a lot of success) userGroups is pretty bare bones as far as the base fields is concerned, and I’d think that you’d want to extend it for just about any web app…

Scribbly, what kind of error you got now?

Show the full listing of error page.

Pedro, that’s true - just install userGroups and go ahead :)


CException


Property "UserExtension.relUserGroupsGroup" is not defined.


G:\xamp\htdocs\yii\db\ar\CActiveRecord.php(129)


117      */

118     public function __get($name)

119     {

120         if(isset($this->_attributes[$name]))

121             return $this->_attributes[$name];

122         else if(isset($this->getMetaData()->columns[$name]))

123             return null;

124         else if(isset($this->_related[$name]))

125             return $this->_related[$name];

126         else if(isset($this->getMetaData()->relations[$name]))

127             return $this->getRelated($name);

128         else

129             return parent::__get($name);

130     }

131 

132     /**

133      * PHP setter magic method.

134      * This method is overridden so that AR attributes can be accessed like roperties.

135      * @param string $name property name

136      * @param mixed $value property value

137      */

138     public function __set($name,$value)

139     {

140         if($this->setAttribute($name,$value)===false)

141         {


Stack Trace

#0 + G:\xamp\htdocs\yii\db\ar\CActiveRecord.php(129): Component->__get("relUserGroupsGroup")

#1 – G:\xamp\htdocs\test_ug\protected\modules\userGroups\views\user\_view.php(4): CActiveRecord->__get("relUserGroupsGroup")


1 <div class="view">

2      

3     <b><?php echo CHtml::encode($data->getAttributeLabel('group_id')); ?>:</b>

4     <?php echo CHtml::encode($data->relUserGroupsGroup->groupname); ?>

5     <br />

6 

7     <?php if (Yii::app()->user->id === $data->id || Yii::app()->user->pbac('userGroups.user.admin') || Yii::app()->user->pbac('userGroups.admin.admin')): ?>

8     <b><?php echo CHtml::encode($data->getAttributeLabel('email')); ?>:</b>

9     <?php echo CHtml::encode($data->email); ?>


#2 + G:\xamp\htdocs\yii\web\CBaseController.php(119): require("G:\xamp\htdocs\test_ug\protected\modules\userGroups\views\user\_...")

#3 + G:\xamp\htdocs\yii\web\CBaseController.php(88): CBaseController->renderInternal("G:\xamp\htdocs\test_ug\protected\modules\userGroups\views\user\_...", array("index" => 0, "data" => UserExtension, "widget" => CListView), true)

#4 + G:\xamp\htdocs\yii\web\CController.php(866): CBaseController->renderFile("G:\xamp\htdocs\test_ug\protected\modules\userGroups\views\user\_...", array("index" => 0, "data" => UserExtension, "widget" => CListView), true)

#5 + G:\xamp\htdocs\yii\zii\widgets\CListView.php(242): CController->renderPartial("_view", array("index" => 0, "data" => UserExtension, "widget" => CListView))

#6 + G:\xamp\htdocs\yii\zii\widgets\CBaseListView.php(158): CListView->renderItems()

#7  unknown(0): CBaseListView->renderSection(array("{items}", "items"))

#8 + G:\xamp\htdocs\yii\zii\widgets\CBaseListView.php(141): preg_replace_callback("/{(\w+)}/", array(CListView, "renderSection"), "{summary} {sorter} {items} {pager}")

#9 + G:\xamp\htdocs\yii\zii\widgets\CBaseListView.php(126): CBaseListView->renderContent()

#10 + G:\xamp\htdocs\yii\web\CBaseController.php(166): CBaseListView->run()

#11 – G:\xamp\htdocs\test_ug\protected\views\user_extension\index.php(20): CBaseController->widget("zii.widgets.CListView", array("dataProvider" => CActiveDataProvider, "itemView" => "_view"))


15     $dataProvider=new CActiveDataProvider($model);

16      

17     $this->widget('zii.widgets.CListView', array(

18     'dataProvider'=>$dataProvider,

19     'itemView'=>'_view',

20 )); ?>


#12 + G:\xamp\htdocs\yii\web\CBaseController.php(119): require("G:\xamp\htdocs\test_ug\protected\views\user_extension\index.php")

#13 + G:\xamp\htdocs\yii\web\CBaseController.php(88): CBaseController->renderInternal("G:\xamp\htdocs\test_ug\protected\views//user_extension/index.php", array("model" => UserExtension), true)

#14 + G:\xamp\htdocs\yii\web\CController.php(866): CBaseController->renderFile("G:\xamp\htdocs\test_ug\protected\views//user_extension/index.php", array("model" => UserExtension), true)

#15 – G:\xamp\htdocs\test_ug\protected\modules\userGroups\views\user\_view.php(28): CController->renderPartial("//user_extension/index", array("model" => UserExtension))


23 

24 

25 <?php

26 // render the profile extensions

27 foreach ($profiles as $p) {

28     $this->renderPartial('//'.str_replace(array('{','}'), NULL, $p['model']->tableName()).'/'.$p['view'], array('model' => $p['model']));

29 } 

30 ?>

31 

32 <?php #form for user approval ?>

33 <?php if ((Yii::app()->user->pbac('userGroups.user.admin') || Yii::app()->user->pbac('userGroups.admin.admin')) && (int)$data->status === UserGroupsUser::WAITING_APPROVAL) : ?>


#16 + G:\xamp\htdocs\yii\web\CBaseController.php(119): require("G:\xamp\htdocs\test_ug\protected\modules\userGroups\views\user\_...")

#17 + G:\xamp\htdocs\yii\web\CBaseController.php(88): CBaseController->renderInternal("G:\xamp\htdocs\test_ug\protected\modules\userGroups\views/user/_...", array("data" => UserGroupsUser, "profiles" => array(array("view" => "index", "model" => UserExtension))), true)

#18 + G:\xamp\htdocs\yii\web\CController.php(866): CBaseController->renderFile("G:\xamp\htdocs\test_ug\protected\modules\userGroups\views/user/_...", array("data" => UserGroupsUser, "profiles" => array(array("view" => "index", "model" => UserExtension))), true)

#19 + G:\xamp\htdocs\test_ug\protected\modules\userGroups\views\user\view.php(38): CController->renderPartial("/user/_view", array("data" => UserGroupsUser, "profiles" => array(array("view" => "index", "model" => UserExtension))))

#20 + G:\xamp\htdocs\yii\web\CBaseController.php(119): require("G:\xamp\htdocs\test_ug\protected\modules\userGroups\views\user\v...")

#21 + G:\xamp\htdocs\yii\web\CBaseController.php(88): CBaseController->renderInternal("G:\xamp\htdocs\test_ug\protected\modules\userGroups\views\user\v...", array("model" => UserGroupsUser, "profiles" => array(array("view" => "index", "model" => UserExtension))), true)

#22 + G:\xamp\htdocs\yii\web\CController.php(866): CBaseController->renderFile("G:\xamp\htdocs\test_ug\protected\modules\userGroups\views\user\v...", array("model" => UserGroupsUser, "profiles" => array(array("view" => "index", "model" => UserExtension))), true)

#23 + G:\xamp\htdocs\yii\web\CController.php(779): CController->renderPartial("view", array("model" => UserGroupsUser, "profiles" => array(array("view" => "index", "model" => UserExtension))), true)

#24 + G:\xamp\htdocs\test_ug\protected\modules\userGroups\controllers\UserController.php(167): CController->render("view", array("model" => UserGroupsUser, "profiles" => array(array("view" => "index", "model" => UserExtension))))

#25 + G:\xamp\htdocs\yii\web\actions\CInlineAction.php(50): UserController->actionView()

#26 + G:\xamp\htdocs\yii\web\CController.php(300): CInlineAction->runWithParams(array())

#27 + G:\xamp\htdocs\yii\web\filters\CFilterChain.php(134): CController->runAction(CInlineAction)

#28 + G:\xamp\htdocs\yii\web\filters\CFilter.php(41): CFilterChain->run()

#29 + G:\xamp\htdocs\test_ug\protected\components\Controller.php(43): CFilter->filter(CFilterChain)

#30 + G:\xamp\htdocs\yii\web\filters\CInlineFilter.php(59): Controller->filterUserGroupsAccessControl(CFilterChain)

#31 + G:\xamp\htdocs\yii\web\filters\CFilterChain.php(131): CInlineFilter->filter(CFilterChain)

#32 + G:\xamp\htdocs\yii\web\CController.php(283): CFilterChain->run()

#33 + G:\xamp\htdocs\yii\web\CController.php(257): CController->runActionWithFilters(CInlineAction, array("userGroupsAccessControl"))

#34 + G:\xamp\htdocs\yii\web\CWebApplication.php(277): CController->run("view")

#35 + G:\xamp\htdocs\yii\web\CController.php(749): CWebApplication->runController("/userGroups/user/view")

#36 + G:\xamp\htdocs\test_ug\protected\modules\userGroups\controllers\UGDefaultController.php(19): CController->forward("/userGroups/user/view")

#37 + G:\xamp\htdocs\yii\web\actions\CInlineAction.php(50): UGDefaultController->actionIndex()

#38 + G:\xamp\htdocs\yii\web\CController.php(300): CInlineAction->runWithParams(array())

#39 + G:\xamp\htdocs\yii\web\CController.php(278): CController->runAction(CInlineAction)

#40 + G:\xamp\htdocs\yii\web\CController.php(257): CController->runActionWithFilters(CInlineAction, array())

#41 + G:\xamp\htdocs\yii\web\CWebApplication.php(277): CController->run("")

#42 + G:\xamp\htdocs\yii\web\CWebApplication.php(136): CWebApplication->runController("userGroups")

#43 + G:\xamp\htdocs\yii\base\CApplication.php(158): CWebApplication->processRequest()

#44 + G:\xamp\htdocs\test_ug\index.php(13): CApplication->run()


2011-07-28 14:49:06 Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1 Yii Framework/1.1.8

 

I can’t see anywhere where I’m meant to add a relationship to Groups?

ie: my user_extension is related to the user not to the group…

Thanks guys :)

I’m still pretty much prototyping at the moment & don’t mind a few [but hopefully not many] false starts & re-trys.

@scribbly

In yii to make a model work properly you need to set a primary key inside the db table

Ug_id also need to be an index.

User extensions don’t necessarely need a controller.

I think i’ll release later tonight a complete example of how to implement a user extension, to avoid any misunderstanding.

Look at line #4 (in first stack trace):


<?php echo CHtml::encode($data->relUserGroupsGroup->groupname); ?>

So it happens… :D

By the way, it happens in userGroups code :)