1
0

Added LogIn (pw in db)

This commit is contained in:
2014-05-28 10:38:11 +02:00
parent d74a13f661
commit fa36f26f2e
9 changed files with 2851 additions and 143 deletions

View File

@@ -17,10 +17,14 @@ class UserIdentity extends CUserIdentity
*/
public function authenticate()
{
$connection = Yii::app()->db;
$command=$connection->createCommand("SELECT SValue FROM othervalues WHERE Name = 'AdminPassword'");
$pw = $command->queryScalar();
$users=array(
// username => password
'demo'=>'demo',
'admin'=>'admin', //TODO Change me
'admin' => $pw,
);
if(!isset($users[$this->username]))
$this->errorCode=self::ERROR_USERNAME_INVALID;

View File

@@ -27,20 +27,20 @@ class ProgrammeController extends Controller
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','create','update','admin','delete'),
'actions'=>array('index','view'),
'users'=>array('*'),
),
// array('allow', // allow authenticated user to perform 'create' and 'update' actions
// 'actions'=>array('create','update'),
// 'users'=>array('@'),
// ),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','admin','delete'),
'users'=>array('@'),
),
// array('allow', // allow admin user to perform 'admin' and 'delete' actions
// 'actions'=>array('admin','delete'),
// 'actions'=>array(),
// 'users'=>array('admin'),
// ),
// array('deny', // deny all users
// 'users'=>array('*'),
// ),
array('deny', // deny everythign else to all users
'users'=>array('*'),
),
);
}
@@ -106,6 +106,7 @@ class ProgrammeController extends Controller
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'admin' page.
* @param integer $id the ID of the model to be deleted
* @throws CHttpException on invalid request
*/
public function actionDelete($id)
{

View File

@@ -57,8 +57,41 @@ class SiteController extends Controller
$data['model'] = new SendMailForm();
}
$this->render('about', $data);
}
/**
* Displays the login page
*/
public function actionLogin()
{
$model=new LoginForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo TbActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
$this->redirect(Yii::app()->user->returnUrl);
}
// display the login form
$this->render('login', array('model'=>$model));
}
/**
* Logs out the current user and redirect to homepage.
*/
public function actionLogout()
{
Yii::app()->user->logout();
$this->redirect(Yii::app()->homeUrl);
}
}

View File

@@ -0,0 +1,77 @@
<?php
/**
* LoginForm class.
* LoginForm is the data structure for keeping
* user login form data. It is used by the 'login' action of 'SiteController'.
*/
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;
private $_identity;
/**
* Declares the validation rules.
* The rules state that username and password are required,
* and password needs to be authenticated.
*/
public function rules()
{
return array(
// username and password are required
array('username, password', 'required'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'rememberMe'=>'Remember me next time',
);
}
/**
* Authenticates the password.
* This is the 'authenticate' validator as declared in rules().
*/
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->username,$this->password);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect username or password.');
}
}
/**
* Logs in the user using the given username and password in the model.
* @return boolean whether login is successful
*/
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else
return false;
}
}

View File

@@ -47,6 +47,7 @@
['label' => 'Programme', 'url' => '/programme/', 'active' => ($this->selectedNav === 'prog')],
['label' => '', 'items' => ProgrammeHelper::GetProgDropDownList(), 'htmlOptions' => ['class' => 'dropdown-append']],
['label' => 'About', 'url' => '/about', 'active' => ($this->selectedNav === 'about')],
['label' => '[[Log "'.Yii::app()->user->name.'" out]]', 'url' => '/logout', 'visible' => !Yii::app()->user->isGuest]
],
],
TbHtml::navbarSearchForm('search', '',

View File

@@ -0,0 +1,55 @@
<?php
/* @var $this SiteController */
/* @var $model LoginForm */
/* @var $form CActiveForm */
$this->pageTitle = Yii::app()->name . ' - Login';
$this->breadcrumbs = array(
'Login',
);
?>
<div class="container well">
<h1>Login</h1>
<p>Please fill out the following form with your login credentials:</p>
<div class="form">
<?php $form = $this->beginWidget('TbActiveForm', array(
'id' => 'login-form',
'enableClientValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div>
<?php echo $form->labelEx($model, 'username'); ?>
<?php echo $form->textField($model, 'username'); ?>
<?php echo $form->error($model, 'username'); ?>
</div>
<div>
<?php echo $form->labelEx($model, 'password'); ?>
<?php echo $form->passwordField($model, 'password'); ?>
<?php echo $form->error($model, 'password'); ?>
</div>
<div class="rememberMe">
<?php echo $form->checkBox($model, 'rememberMe'); ?>
<?php echo $form->label($model, 'rememberMe'); ?>
<?php echo $form->error($model, 'rememberMe'); ?>
</div>
<div class="buttons">
<?php echo TbHtml::submitButton('Login'); ?>
</div>
<?php $this->endWidget(); ?>
</div>
<!-- form -->
</div><!-- Container -->