How Add Admin User To The User Table


I did the Yii blog tutorial, but in the tutorial I don’t find any informations or examples about, how I can add the admin to the user table. I didthis description., and I found sql command in the data directory, but that only add the demo user to the database.

And my main problem is that, I have no idea, how I can add the admin user to the table with hashed password.

I found this only:

INSERT INTO tbl_user (username, password, email) VALUES ('demo','$2a$10$JTJf6/XqC94rrOtzuF397OHa4mbmZrVTBOQCmYD9U.obZRUut4BoC','');

I read this auth topic too, but I don’t find any examples :(. I think I should generate the User model and crud with the gii, but I would like to do this without these :).

I only just want to add the admin user to my table, with hashed password code.

Can somebody help me? Or give me an example, or description, what is the easiest way to add the user? This is a very small webapp, so I don’t need RBAC, just add the admin with hashed code.


What exactly you want to do?


INSERT INTO tbl_user (username, password, email) VALUES ('demo','$2a$10$JTJf6/XqC94rrOtzuF397OHa4mbmZrVTBOQCmYD9U.obZRUut4BoC','');

works either with sql query builder or using save method of User model

"I only just want to add the admin user to my table, with hashed password code."

your above sql query doesn’t work?

Do you want to set permissions without RBAC ?

I only just want to insert the admin to the table.

But I can’t use the simple SQL query, because the Yii have to generate the hashed password and then insert into database.

I tried generate the CRUD, create the model, etc, but doesn’t work, and I don’t find any example :(

Yes, I want set permission without RBAC. RBAC for me is difficult, and the Yii too :).

  1. The document you mentioned

uses the crypt php method, so you could generate the hash before save to the database like that

INSERT INTO tbl_user (username, password, email) VALUES ('demo',crypt('your password'),'');

if you have another hash generator please post the code

  1. crud generates only the basic things and you can extends or modify all generated code, it is useful and save you from a lot of time :)

  2. if you want to use a very very simple permission system

after of the line $this->_id=$record->id; in authenticate method of UserIdentity you could use something like that

if $record->username == 'admin'

  Yii::app()->session['user_role'] = "admin";


  Yii::app()->session['user_role'] = "user";

and check in controller/action what you want

 if (Yii::app()->session['user_role'] != "admin") 

    throw new CHttpException(403, 'You are not authorized to per-form this action');

but remember, for more complex permission system you should learn the RBAC :)