View file vkclone-0.0.1/protected/modules/users/controllers/DefaultController.php

File size: 2.81Kb
<?php
class DefaultController extends FrontEndController {
	public function filters() {
		return CMap::mergeArray(parent::filters(), array(
			'authenticationClosed - logout, fillName',
		));
	}

	/**
	 * Authentication/Registration for guests; Logout for users.
	 */
	public function accessRules() {
		return CMap::mergeArray(array(
			array('deny',
				'actions' => array('login', 'registration'),
				'users' => array('@'),
			),
			array('deny',
				'actions' => array('logout', 'fillName', 'afterRegistration'),
				'users' => array('?'),
			),
			array('allow'),
		), parent::accessRules());
	}

	/**
	 * Filter that supress request when authentication is closed.
	 */
	public function filterAuthenticationClosed($filterChain) {
		if (Settings::instance()->authenticationClosed)
			$this->render('authentication_closed');
		else
			$filterChain->run();

	}

	/**
	 * Displays the login page
	 */
	public function actionLogin() {
		$model = new User('login');

		if (isset($_POST[CHtml::modelName($model)])) {
			$model->attributes = $_POST[CHtml::modelName($model)];
			if ($model->validate() && $model->login()) {
				$this->redirect(Yii::app()->user->returnUrl);
			}
		}
		$this->render('login', array(
			'model' => $model,
		));
	}

	/**
	 * Displays the registration page
	 */
	public function actionRegistration() {
		if (Settings::instance()->registrationClosed) {
			$this->render('registration_closed', array('message' => Settings::instance()->registrationClosedMessage));
			Yii::app()->end();
		}

		$model = new User('registration');
		if (isset($_POST[CHtml::modelName($model)])) {
			$model->attributes = $_POST[CHtml::modelName($model)];
			if ($model->validate()) {
				if (($user = $model->registrate())) {
					$model->authenticate('password', array());
					$model->login();
					$this->redirect(Yii::app()->user->returnUrl);
				} else {
					throw new CHttpException(500, 'Registration error');
				}
			}

		}
		$this->render('registration', array('model' => $model));
	}


	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout() {
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}

	/**
	 * Renders form to fill first_name and last_name
	 */
	public function actionFillName() {
		$model = Yii::app()->user->record;
		$model->setScenario('fillName');

		if (isset($_POST[CHtml::modelName($model)])) {
			$model->attributes = $_POST[CHtml::modelName($model)];
			if ($model->validate()) {
				$model->save();
				Yii::app()->user->setAlert(Yii::t('Users', 'Your name sucessfully saved'), 'success');
				$this->redirect(array('afterRegistration'));
			}
		}
		$this->render('fillName', array(
			'model' => $model,
		));
	}

	/**
	 * Renders the page that shows after sucessful registration
	 */
	public function actionAfterRegistration() {
		$this->render('afterRegistration');
	}
}