Код: Выделить всё
<?php
class User extends CActiveRecord
{
const STATUS_NOACTIVE=0;
const STATUS_ACTIVE=1;
const STATUS_BANNED=-1;
//TODO: Delete for next version (backward compatibility)
const STATUS_BANED=-1;
/**
* The followings are the available columns in table 'users':
* @var integer $id
* @var string $username
* @var integer $id_group
* @var string $password
* @var string $email
* @var string $activkey
* @var integer $createtime
* @var integer $lastvisit
* @var integer $superuser
* @var integer $status
* @var timestamp $create_at
* @var timestamp $lastvisit_at
*/
/**
* Returns the static model of the specified AR class.
* @return CActiveRecord the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return Yii::app()->getModule('user')->tableUsers;
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.CConsoleApplication
return ((get_class(Yii::app())=='CConsoleApplication' || (get_class(Yii::app())!='CConsoleApplication' && Yii::app()->getModule('user')->isAdmin()))?array(
array('username', 'length', 'max'=>20, 'min' => 3,'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
array('password', 'length', 'max'=>128, 'min' => 4,'message' => UserModule::t("Incorrect password (minimal length 4 symbols).")),
array('email', 'email'),
array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
array('email', 'unique', 'message' => UserModule::t("This user's email address already exists.")),
array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => UserModule::t("Incorrect symbols (A-z0-9).")),
array('status', 'in', 'range'=>array(self::STATUS_NOACTIVE,self::STATUS_ACTIVE,self::STATUS_BANNED)),
array('superuser', 'in', 'range'=>array(0,1)),
array('create_at', 'default', 'value' => date('Y-m-d H:i:s'), 'setOnEmpty' => true, 'on' => 'insert'),
array('lastvisit_at', 'default', 'value' => '0000-00-00 00:00:00', 'setOnEmpty' => true, 'on' => 'insert'),
array('username, email, superuser, status', 'required'),
array('superuser, status', 'numerical', 'integerOnly'=>true),
array('id, username, password, email, activkey, create_at, lastvisit_at, superuser, status', 'safe', 'on'=>'search'),
):((Yii::app()->user->id==$this->id)?array(
array('username, email', 'required'),
array('username', 'length', 'max'=>20, 'min' => 3,'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
array('email', 'email'),
array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => UserModule::t("Incorrect symbols (A-z0-9).")),
array('email', 'unique', 'message' => UserModule::t("This user's email address already exists.")),
array('id_group', 'numerical'),
):array()));
}
/**
* @return array relational rules.
*/
public function relations()
{
Yii::import('application.modules.user.models.*');
return array(
'profile' => array(self::HAS_ONE, 'Profile', 'user_id'),
'group' => array(self::BELONGS_TO, 'Group', 'id'),
);
return $relations;
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => UserModule::t("Id"),
'username'=>UserModule::t("username"),
'password'=>UserModule::t("password"),
'verifyPassword'=>UserModule::t("Retype Password"),
'email'=>UserModule::t("E-mail"),
'verifyCode'=>UserModule::t("Verification Code"),
'activkey' => UserModule::t("activation key"),
'createtime' => UserModule::t("Registration date"),
'create_at' => UserModule::t("Registration date"),
'lastvisit_at' => UserModule::t("Last visit"),
'superuser' => UserModule::t("Superuser"),
'status' => UserModule::t("Status"),
'id_group' => UserModule::t("Group"),
);
}
public function scopes()
{
return array(
'active'=>array(
'condition'=>'status='.self::STATUS_ACTIVE,
),
'notactive'=>array(
'condition'=>'status='.self::STATUS_NOACTIVE,
),
'banned'=>array(
'condition'=>'status='.self::STATUS_BANNED,
),
'superuser'=>array(
'condition'=>'superuser=1',
),
'notsafe'=>array(
'select' => 'id, username, id_group, password, email, activkey, create_at, lastvisit_at, superuser, status',
),
);
}
public function defaultScope()
{
return CMap::mergeArray(Yii::app()->getModule('user')->defaultScope,array(
'alias'=>'user',
'select' => 'user.id, user.username, user.id_group, user.email, user.create_at, user.lastvisit_at, user.superuser, user.status',
));
}
public static function itemAlias($type,$code=NULL) {
$_items = array(
'UserStatus' => array(
self::STATUS_NOACTIVE => UserModule::t('Not active'),
self::STATUS_ACTIVE => UserModule::t('Active'),
self::STATUS_BANNED => UserModule::t('Banned'),
),
'AdminStatus' => array(
'0' => UserModule::t('No'),
'1' => UserModule::t('Yes'),
),
);
if (isset($code))
return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
else
return isset($_items[$type]) ? $_items[$type] : false;
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('username',$this->username,true);
$criteria->compare('id_group',$this->id_group);
$criteria->compare('password',$this->password);
$criteria->compare('email',$this->email,true);
$criteria->compare('activkey',$this->activkey);
$criteria->compare('create_at',$this->create_at);
$criteria->compare('lastvisit_at',$this->lastvisit_at);
$criteria->compare('superuser',$this->superuser);
$criteria->compare('status',$this->status);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>Yii::app()->getModule('user')->user_page_size,
),
));
}
public function getCreatetime() {
return strtotime($this->create_at);
}
public function setCreatetime($value) {
$this->create_at=date('Y-m-d H:i:s',$value);
}
public function getLastvisit() {
return strtotime($this->lastvisit_at);
}
public function setLastvisit($value) {
$this->lastvisit_at=date('Y-m-d H:i:s',$value);
}
}
Код: Выделить всё
<?php
class AdminController extends Controller
{
public $defaultAction = 'admin';
public $layout='/layouts/column1';
private $_model;
/**
* @return array action filters
*/
public function filters()
{
return CMap::mergeArray(parent::filters(),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 admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete','create','update','view'),
'users'=>UserModule::getAdmins(),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new User('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['User']))
$model->attributes=$_GET['User'];
$this->render('index',array(
'model'=>$model,
));
/*$dataProvider=new CActiveDataProvider('User', array(
'pagination'=>array(
'pageSize'=>Yii::app()->controller->module->user_page_size,
),
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));//*/
}
/**
* Displays a particular model.
*/
public function actionView()
{
$model = $this->loadModel();
$this->render('view',array(
'model'=>$model,
));
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new User;
$profile=new Profile;
$this->performAjaxValidation(array($model,$profile));
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
$model->activkey=Yii::app()->controller->module->encrypting(microtime().$model->password);
$profile->attributes=$_POST['Profile'];
$profile->user_id=0;
if($model->validate()&&$profile->validate()) {
$model->password=Yii::app()->controller->module->encrypting($model->password);
if($model->save()) {
$profile->user_id=$model->id;
$profile->save();
}
$this->redirect(array('view','id'=>$model->id));
} else $profile->validate();
}
$this->render('create',array(
'model'=>$model,
'profile'=>$profile,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
*/
public function actionUpdate()
{
$model=$this->loadModel();
$profile=$model->profile;
$this->performAjaxValidation(array($model,$profile));
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
$profile->attributes=$_POST['Profile'];
if($model->validate()&&$profile->validate()) {
$old_password = User::model()->notsafe()->findByPk($model->id);
if ($old_password->password!=$model->password) {
$model->password=Yii::app()->controller->module->encrypting($model->password);
$model->activkey=Yii::app()->controller->module->encrypting(microtime().$model->password);
}
$model->save();
$profile->save();
$this->redirect(array('view','id'=>$model->id));
} else $profile->validate();
}
$this->render('update',array(
'model'=>$model,
'profile'=>$profile,
));
}
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'index' page.
*/
public function actionDelete()
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
$model = $this->loadModel();
$profile = Profile::model()->findByPk($model->id);
$profile->delete();
$model->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_POST['ajax']))
$this->redirect(array('/user/admin'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
/**
* Performs the AJAX validation.
* @param CModel the model to be validated
*/
protected function performAjaxValidation($validate)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
{
echo CActiveForm::validate($validate);
Yii::app()->end();
}
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
*/
public function loadModel()
{
if($this->_model===null)
{
if(isset($_GET['id']))
$this->_model=User::model()->notsafe()->findbyPk($_GET['id']);
if($this->_model===null)
throw new CHttpException(404,'The requested page does not exist.');
}
return $this->_model;
}
}