Yii: Adding Users and User Management to Your Site
Read more
Yii: Adding Users and User Management to Your Site
Packt
540 min read
2013-02-21 00:00:00
0 Likes
0 Comments
(For more resources related to this topic, see here.)
Mission Checklist
This project assumes that you have a web development environment prepared. The files for this project include a Yii project directory with a database schema. To prepare for the project, carry out the following steps replacing the username lomeara with your own username:
Copy the project files into your working directory.
If you have a link for a previous project, remove it from the webroot directory.
rm /opt/lampp/htdocs/cddb
Create a link in the webroot directory to the copied directory.
cd /opt/lampp/htdocs sudo ln -s ~/projects/ch3 cbdb
Import the project into NetBeans (remember to set the project URL to http://localhost/cbdb) and configure for Yii development with PHPUnit.
Create a database named cbdb and load the database schema (~/projects/ch3/ protected/data/schema.sql) into it.
If you are not using the XAMPP stack or if your access to MySQL is password protected, you should review and update the Yii configuration file (in NetBeans it is ch3/Source Files/protected/config/main.php).
Adding a User Object with CRUD
As a foundation for our user management system, we will add a User table to the database and then use Gii to build a quick functional interface.
Engage Thrusters
Let's set the first building block by adding a User table containing the following information:
A username
Password hash
Reference to a person entry for first name and last name
In NetBeans, open a SQL Command window for the cbdb database and run the following command:
CREATE TABLE 'user' ( 'id' int(10) unsigned NOT NULL AUTO_INCREMENT, 'username' varchar(20) NOT NULL, 'pwd_hash' char(34) NOT NULL, 'person_id' int(10) unsigned NOT NULL, PRIMARY KEY ('id'), UNIQUE KEY 'username' ('username'), CONSTRAINT 'userperson_ibfk_2' FOREIGN KEY ('person_id') REFERENCES 'person' ('id') ON DELETE CASCADE ) ENGINE=InnoDB;
Open a web browser to the Gii URL http://localhost/cbdb/index.php/gii(the password configured in the sample code is yiibook) and use Gii to generate a model from the user table.
Then, use Gii to generate CRUD from the user model.
Back in NetBeans, add a link to the user index in your site's logged in menu (ch3 | Source Files | protected | views | layouts | main.php). It should look like this:
Right-click on the project name, run the site, and log in with the default username and password (admin/admin). You will see a menu that includes a link named Users.
If you click on the Users link in the menu and then click on Create User, you will see a pretty awful-looking user-creation screen. We are going to fix that. First, we will update the user form to include fields for first name, last name, password, and repeat password. Edit ch3 | Source Files | protected | views | user | _form.php and add those fields.
Start by changing all instances of $model to $user. Then, add a call to errorSummary on the person data under the errorSummary call on user.
These changes are going to completely break the User create/update form for the time being.
We want to capture the password data and ultimately make a hash out of it to store securely in the database. To collect the form inputs, we will add password fields to the User model that do not correspond to values in the database. Edit the User model ch3 | Source Files | protected | models | User.php and add two public variables to the class:
class User extends CActiveRecord { public $password; public $password_repeat;
In the same User model file, modify the attribute labels function to include labels for the new password fields.
Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at $19.99/month. Cancel anytime
In the same User model file, update the rules function with the following rules:
Require username
Limit length of username and password
Compare password with password repeat
Accept only safe values for username and password
We will come back to this and improve it, but for now, it should look like the following:
public function rules() { // NOTE: you should only define rules for those attributes //that will receive user inputs. return array( array('username', 'required'), array('username', 'length', 'max'=>20), array('password', 'length', 'max'=>32), array('password', 'compare'), array('password_repeat', 'safe'), ); }
In order to store the user's first and last name, we must change the Create action in the User controller ch3 | Source Files | protected | controllers | UserController. php to create a Person object in addition to a User object.
Change the variable name $model to $user, and add an instance of the Person model.
public function actionCreate() { $user=new User; $person=new Person; // Uncomment the following line if AJAX validation is //needed // $this->performAjaxValidation($user); if(isset($_POST['User'])) { $user->attributes=$_POST['User']; if($user->save()) $this->redirect(array('view','id'=>$user->id)); } $this->render('create',array( 'user'=>$user, 'person'=>$person, )); }
Don't reload the create user page yet. First, update the last line of the User Create view ch3 | Source Files | protected | views | user | create.php to send a User object and a Person object.
Make a change to the attributeLabels function in the Person model (ch3 | Source Files | protected | models | Person.php) to display clearer labels for first name and last name.
public function attributeLabels() { return array( 'id' => 'ID', 'fname' => 'First Name', 'lname' => 'Last Name', ); }
The resulting user form should look like this:
Looks pretty good, but if you try to submit the form, you will receive an error. To fix this, we will change the User Create action in the User controller ch3 | Source Files | protected | controllers | UserController.php to check and save both User and Person data.
Great! Now you can create users, but if you try to edit a user entry, you see another error. This fix will require a couple of more changes.
First, in the user controller ch3 | Source Files | protected | controllers | UserController.php, change the loadModel function to load the user model with its related person information:
One more piece of user management housekeeping; try deleting a user. Look in the database for the user and the person info. Oops. Didn't clean up after itself, did it? Update the User controller ch3 | Source Files | protected | controllers | UserController.php once again. Change the call to delete in the User delete action:
$this->loadModel($id)->person->delete();
Objective Complete - Mini Debriefing
We have added a new object, User, to our site, and associated it with the Person object to capture the user's first and last name. Gii helped us get the basic structure of our user management function in place, and then we altered the model, view, and controller to bring the pieces together.