Sie sind auf Seite 1von 5

To summarize whats been covered here

To change the entire look for the entire site, edit layouts/main.php, but be sure to use the echo
$content line where appropriate.
To change the default layout for every Controller, edit this line in components/Controller.php:

public $layout='//layouts/column1';
To change the default layout for every View in an individual Controller, add this line to that Controllers
definition:

class SiteController extends Controller {


public $layout = 'column2';
To change the layout used for a single action, add this line to the corresponding action:

// protected/controllers/SiteController.php
public function actionIndex() {
$this->layout = 'home';
And remember that column1.php and column2.php just hijack the page-specific content before it gets
passed on to the main.php layout file.

In addition, for any serious Web applications, we recommend using the following strategy to
enhance the security of cookie-based login.

When a user successfully logs in by filling out a login form, we generate and store a
random key in both the cookie state and in persistent storage on server side (e.g.
database).

Upon a subsequent request, when the user authentication is being done via the cookie
information, we compare the two copies of this random key and ensure a match before
logging in the user.

If the user logs in via the login form again, the key needs to be re-generated.

By using the above strategy, we eliminate the possibility that a user may re-use an old state
cookie which may contain outdated state information.
To implement the above strategy, we need to override the following two methods:

CUserIdentity::authenticate(): this is where the real authentication is performed. If the


user is authenticated, we should re-generate a new random key, and store it in the
database as well as in the identity states viaCBaseUserIdentity::setState.

CWebUser::beforeLogin(): this is called when a user is being logged in. We should


check if the key obtained from the state cookie is the same as the one from the database.

//code for user registration form


public function rules() {
return array(
array('username, password, confirm_password, email, first_name, last_name,
date_born, sex', 'required'),
array('username', 'unique'),
array('email', 'unique'),
array('username, email', 'length', 'max' => 45),
array('email', 'email', 'message' => 'Isso da no parece um email hein...'),
array('password', 'length', 'min' => 6, 'max' => 45),
array('confirm_password', 'compare', 'compareAttribute' => 'password',
'message' => 'T igual no hein'),
array('first_name, last_name', 'length', 'max' => 45),
array('sex', 'length', 'min' => 1, 'max' => 1),
array('username, email, first_name, last_name, description', 'safe', 'on' =>
'search'),
);
}

<div id="background">
<div class="register-form">
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'register-form',
'action' => '/users/register',
'enableAjaxValidation' => true,
'enableClientValidation' => true,
'clientOptions' => array(
'validateOnChange' => true,
'validateOnSubmit' => true,
),
));

?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model, 'username'); ?>
<?php echo $form->textField($model, 'username', array('size' => 20,
'maxlength' => 45, 'placeholder' => 'Vai ser seu link!')); ?>
<div class="clear"></div>
<?php echo $form->error($model, 'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'password'); ?>
<?php echo $form->passwordField($model, 'password', array('size' => 20,
'maxlength' => 45, 'placeholder' => 'Cuidado pra ningum saber')); ?>
<div class="clear"></div>
<?php echo $form->error($model, 'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'confirm_password'); ?>
<?php echo $form->passwordField($model, 'confirm_password', array('size' =>
20, 'maxlength' => 45, 'placeholder' => 'Igual a de cima')); ?>
<div class="clear"></div>
<?php echo $form->error($model, 'confirm_password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'email'); ?>
<?php echo $form->textField($model, 'email', array('size' => 20, 'maxlength'
=> 45, 'placeholder' => 'something@somethingelse.com')); ?>
<div class="clear"></div>
<?php echo $form->error($model, 'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'first_name'); ?>
<?php echo $form->textField($model, 'first_name', array('size' => 20,
'maxlength' => 45, 'placeholder' => 'Qual seu nome?')); ?>
<div class="clear"></div>
<?php echo $form->error($model, 'first_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'last_name'); ?>
<?php echo $form->textField($model, 'last_name', array('size' => 20,
'maxlength' => 45, 'placeholder' => 'E sobrenome?')); ?>
<div class="clear"></div>
<?php echo $form->error($model, 'last_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'date_born'); ?>
<?php

$this->widget('CMaskedTextField', array(
'model' => $model,
'attribute' => 'date_born',
'mask' => '99/99/9999',
'htmlOptions' => array('size' => 20, 'placeholder' => 'dd/mm/aaaa')
));
?>
<div class="clear"></div>
<?php echo $form->error($model, 'date_born'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'sex'); ?>
<?php echo $form->dropDownList($model, 'sex', array('m' => 'Masculino', 'f' =>
'Feminino'), array('size' => 1, 'maxlength' => 1)); ?>
</div>
<div class="row-buttons">
<?php echo CHtml::submitButton('Registrar'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
</div>

<?php
class UsersController extends Controller {
/**
* @return array action filters
*/
public function filters() {
return array(
'accessControl', // perform access control for CRUD operations
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules() {
return array(
array('allow', // allow all users to perform 'create' action
'actions' => array('register'),
'users' => array('*'),
),
array('allow', // allow authenticated user to perform 'selfdelete' and
'update' actions
'actions' => array('selfdelete', 'update'),

'users' => array('@'),


),
array('allow', // allow admins users to perform 'delete'
'actions' => array('delete'),
'users' => Yii::app()->params['admins'],
),
array('deny', // deny all users
'users' => array('*'),
),
);
}
/**
* Creates and saves a new User
*/
public function actionRegister() {
$model = new User;
$this->performAjaxValidation($model);
if (isset($_POST['User'])) {
$model->attributes = $_POST['User'];
if ($model->validate())
if ($model->save()) {
$this->redirect(array('/site/index'));
}
}
}
/**
* Performs the AJAX validation.
* @param CModel the model to be validated
*/
protected function performAjaxValidation($model) {
if (isset($_POST['ajax']) && $_POST['ajax'] === 'create-user-form') {
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}

Using theme
'theme'=>'wpcraft',
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
//tell the application to use your WebUser class instead of the default CWebUser
'class'=>'WebUser',
),

Das könnte Ihnen auch gefallen