Skip to content

Commit

Permalink
Added README file
Browse files Browse the repository at this point in the history
  • Loading branch information
stevepop committed Jun 17, 2011
1 parent 62fec3f commit f0273fa
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 84 deletions.
2 changes: 2 additions & 0 deletions protected/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is a Yii project I am following as I read the book, Agile Web Application Development with
Yii 1.1 ands PHP5
53 changes: 33 additions & 20 deletions protected/components/UserIdentity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,40 @@
*/
class UserIdentity extends CUserIdentity
{
/**
* Authenticates a user.
* The example implementation makes sure if the username and password
* are both 'demo'.
* In practical applications, this should be changed to authenticate
* against some persistent user identity storage (e.g. database).

private $_id;

/**
* Authenticates a user using the User data model.
* @return boolean whether authentication succeeds.
*/
public function authenticate()
{
$users=array(
// username => password
'demo'=>'demo',
'admin'=>'admin',
);
if(!isset($users[$this->username]))
public function authenticate()
{
$user=User::model()->findByAttributes(array('username'=>$this->username));
if($user===null)
{
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($users[$this->username]!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
$this->errorCode=self::ERROR_NONE;
return !$this->errorCode;
}
} else {
if($user->password!==$user->encrypt($this->password))
{
$this->errorCode=self::ERROR_PASSWORD_INVALID;
} else {
$this->_id = $user->id;
if(null===$user->last_login_time)
{
$lastLogin = time();
} else {
$lastLogin = strtotime($user->last_login_time);
}
$this->setState('lastLoginTime', $lastLogin);
$this->errorCode=self::ERROR_NONE;
}
}
return !$this->errorCode;
}

public function getId()
{
return $this->_id;
}
}
6 changes: 3 additions & 3 deletions protected/models/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* This is the model class for table "tbl_issue".
*/
class Issue extends CActiveRecord
class Issue extends TrackStarActiveRecord
{

const TYPE_BUG=0;
Expand Down Expand Up @@ -56,10 +56,10 @@ public function rules()
// will receive user inputs.
return array(
array('name', 'required'),
array('project_id, type_id, status_id, owner_id, requester_id, create_user_id, update_user_id', 'numerical', 'integerOnly'=>true),
//array('project_id, type_id, status_id, owner_id, requester_id, create_user_id, update_user_id', 'numerical', 'integerOnly'=>true),
array('name', 'length', 'max'=>256),
array('description', 'length', 'max'=>2000),
array('create_time, update_time', 'safe'),

// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name, description, project_id, type_id, status_id, owner_id, requester_id, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),
Expand Down
4 changes: 1 addition & 3 deletions protected/models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* This is the model class for table "tbl_project".
*/
class Project extends CActiveRecord
class Project extends TrackStarActiveRecord
{
/**
* The followings are the available columns in table 'tbl_project':
Expand Down Expand Up @@ -42,9 +42,7 @@ public function rules()
// will receive user inputs.
return array(
array('name, description', 'required'),
array('create_user_id, update_user_id', 'numerical', 'integerOnly'=>true),
array('name', 'length', 'max'=>128),
array('create_time, update_time', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name, description, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),
Expand Down
28 changes: 28 additions & 0 deletions protected/models/TrackStarActiveRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
abstract class TrackStarActiveRecord extends CActiveRecord
{

/*
* Prepares create_time, create_user_id, update_time and update_user_id attributes before performing validation.
*/

protected function beforeValidate()
{

//parent:: beforeValidate();
if($this->isNewRecord)
{
// set the create date, last updated date and the user doing the creating
$this->create_time=$this->update_time=new CDbExpression('NOW()');
$this->create_user_id=$this->update_user_id=Yii::app()->user->id;

} else {
//not a new record, so just set the last updated time and last updated user id
$this->update_time=new CDbExpression('NOW()');
$this->update_user_id=Yii::app()->user->id;
}
return parent::beforeValidate();
}

}
?>
27 changes: 23 additions & 4 deletions protected/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* This is the model class for table "tbl_user".
*/
class User extends CActiveRecord
class User extends TrackStarActiveRecord
{
/**
* The followings are the available columns in table 'tbl_user':
Expand All @@ -18,6 +18,8 @@ class User extends CActiveRecord
* @var integer $update_user_id
*/

public $password_repeat;

/**
* Returns the static model of the specified AR class.
* @return User the static model class
Expand All @@ -43,10 +45,13 @@ public function rules()
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('email', 'required'),
array('create_user_id, update_user_id', 'numerical', 'integerOnly'=>true),
array('email, username,password', 'required'),
array('email, username, password', 'length', 'max'=>256),
array('last_login_time, create_time, update_time', 'safe'),
array('email, username', 'unique'),
array('password', 'compare'),
array('password_repeat', 'safe'),


// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, email, username, password, last_login_time, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),
Expand Down Expand Up @@ -117,4 +122,18 @@ public function search()
'criteria'=>$criteria,
));
}

/**
* perform one-way encryption on the password before we store it in the database
*/
protected function afterValidate()
{
parent::afterValidate();
$this->password = $this->encrypt($this->password);
}

public function encrypt($value)
{
return md5($value);
}
}
24 changes: 0 additions & 24 deletions protected/views/project/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,6 @@
<?php echo $form->error($model,'description'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'create_time'); ?>
<?php echo $form->textField($model,'create_time'); ?>
<?php echo $form->error($model,'create_time'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'create_user_id'); ?>
<?php echo $form->textField($model,'create_user_id'); ?>
<?php echo $form->error($model,'create_user_id'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'update_time'); ?>
<?php echo $form->textField($model,'update_time'); ?>
<?php echo $form->error($model,'update_time'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'update_user_id'); ?>
<?php echo $form->textField($model,'update_user_id'); ?>
<?php echo $form->error($model,'update_user_id'); ?>
</div>

<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
Expand Down
35 changes: 5 additions & 30 deletions protected/views/user/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,11 @@
<?php echo $form->passwordField($model,'password',array('size'=>60,'maxlength'=>256)); ?>
<?php echo $form->error($model,'password'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'last_login_time'); ?>
<?php echo $form->textField($model,'last_login_time'); ?>
<?php echo $form->error($model,'last_login_time'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'create_time'); ?>
<?php echo $form->textField($model,'create_time'); ?>
<?php echo $form->error($model,'create_time'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'create_user_id'); ?>
<?php echo $form->textField($model,'create_user_id'); ?>
<?php echo $form->error($model,'create_user_id'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'update_time'); ?>
<?php echo $form->textField($model,'update_time'); ?>
<?php echo $form->error($model,'update_time'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'update_user_id'); ?>
<?php echo $form->textField($model,'update_user_id'); ?>
<?php echo $form->error($model,'update_user_id'); ?>
</div>
<div class="row">
<?php echo $form->label($model,'password_repeat'); ?>
<?php echo $form->passwordField($model,'password_repeat',array('size'=>60,'maxlength'=>256)); ?>
<?php echo $form->error($model,'password_repeat'); ?>
</div>

<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
Expand Down

0 comments on commit f0273fa

Please sign in to comment.