Create Project fails to assign default user

Hello,

When I create a project, I am able to click on the create issue link even through there are no users assigned to the project.

Have I missed something in the book?

Is the create project supposed to assign the current user as owner and requester?

If I create a project, I can attempt to create an issue and the dropdowns for owner and requester are blank.

Did I miss a step in the book?

I guess nobody is having this issue.

Hi, I’m having the same issue. Seems like others are also having the same problem here:

related topic

When a project is first created the owner is not assigned, nor is the tbl_project_user_assignment populated From the looks of it, seems like the book left that task up to the reader to complete. I think this problem may be the root cause of another issue that occurs when you try to add an issue to a project that has no owner.

If you create a new project, then add an issue, Trackstar will allow you to create the project and add the issue, but the issue owner and requester drop down fields do not populate in views/issue/_form.

if you check the Project Model you will see a relation named users which refrerences the tbl_project_user_assignment table.

The Project::model()->users relation is used by the Project.getUserOptions() function to return an array of related users in a list control which is used to populate the dropdown on the create Issue _form.

Ok. So how to fix all this?

First, lets make sure that nobody except someone with the correct RBAC privileges is even allowed to see the menu item to create a project. This can be accomplished by adding the ‘visible’ parameter to the menu url as shown here:




...

$this->menu=array(

	array('label'=>'Create Project',

           	'url'=>array('create'),

           	'visible'=>Yii::app()->user->checkAccess('createProject',array(),false),

        	),

...



Note that you should already have a ‘createProject’ operation set up via RBAC in your ‘authItems’ table.

Of course a savvy web user can still try to hack the page url and guess the route, so we also need make a change to the actionCreate() method in ProjectController:





	public function actionCreate()

	{

		//Only authorized RBAC permission can create new project

            	if(!Yii::app()->user->checkAccess('createProject'))

		{

			throw new CHttpException(403,"You are not authorized to perform this action.");

		}

            	

            	$model=new Project;

...



Now we are ready to make the key change that will ensure that a user gets assigned to the project upon creation of a new project. This code also goes in the actionCreate method of ProjectController as murph has correctly anticipated. Here is how I coded the entire method for reference.





	/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */

	public function actionCreate()

	{

		//Only authorized RBAC permission can create new projects

            	if(!Yii::app()->user->checkAccess('createProject'))

		{

			throw new CHttpException(403,"You are not authorized to perform this action.");

		}

            	

            	$model=new Project;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Project]))

		{

			$model->attributes=$_POST['Project'];

			if($model->save())

                    	{

                         	//automatically assign the current user as the owner of newly created project

                            	$model->associateUserToProject(Yii::app()->user);


                        	//render the view

                        	$this->redirect(array('view','id'=>$model->id));

                    	}

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}






Now when you create a new Project, the user_id and project_id are stored in tbl_project_user_assignment and you should be able to see the drop down box populate with the owner and requester. Of course, since you havent yet added any other users to the project, the drop down box should only display one choice,that is the user who just created the project.

What you might also notice is that when you attempt to add a new issue to the project you just created , the status_id, type_id, owner_id and requester_id fields may not populate in the database. If these values show up as NULL In tbl_issue in mySQL. this can occur if you do not have a rule set in the Issue model to mark these fields as ‘safe’ for persistent storage.

you may need to update the rules() method in Issue.php so that the actionUpdate and actionCreate methods will actually save the choices in the database as opposed to chucking them out. Here is how I did it.




	public function rules()

	{

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

		// will receive user inputs.

		return array(

			array('name', 'required'),

			array('name', 'length', 'max'=>256),

			array('description', 'length', 'max'=>2000),

                    	//following rule is used by actionUpdate and actionCreate

                    	array('type_id, status_id, owner_id, requester_id','safe'),

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

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

			array('id, name, description, pcase_id, type_id, status_id, owner_id, requester_id, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),

		);

	}




I am by no means an expert, but this solution seems to work.

Thank you for sharing,

This is greatly appreciated.