HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
AdminController.php
Go to the documentation of this file.
1 <?php
2 
4 {
5  public $defaultAction = 'admin';
6  public $layout = '//layouts/column2';
7 
8  private $_model;
9 
13  public function filters()
14  {
15  return CMap::mergeArray(parent::filters(), array(
16  'accessControl', // perform access control for CRUD operations
17  ));
18  }
25  public function accessRules()
26  {
27  return array(
28  array('allow', // allow admin user to perform 'admin' and 'delete' actions
29  'actions' => array('admin','delete','create','update','view'),
30  'users' => UserModule::getAdmins(),
31  ),
32  array('deny', // deny all users
33  'users' => array('*'),
34  ),
35  );
36  }
40  public function actionAdmin()
41  {
42  $model = new User('search');
43  $model->unsetAttributes(); // clear any default values
44  if (isset($_GET['User'])) {
45  $model->attributes = $_GET['User'];
46  }
47 
48  $this->render('index', array(
49  'model' => $model,
50  ));
51  /*$dataProvider=new CActiveDataProvider('User', array(
52  'pagination'=>array(
53  'pageSize'=>Yii::app()->controller->module->user_page_size,
54  ),
55  ));
56 
57  $this->render('index',array(
58  'dataProvider'=>$dataProvider,
59  ));//*/
60  }
61 
65  public function actionView()
66  {
67  $model = $this->loadModel();
68  $this->render('view', array(
69  'model' => $model,
70  ));
71  }
72 
77  public function actionCreate()
78  {
79  $model = new User();
80  $profile = new Profile();
81  $this->performAjaxValidation(array($model, $profile));
82  if (isset($_POST['User'])) {
83  $model->attributes = $_POST['User'];
84  $model->activkey = Yii::app()->controller->module->encrypting(microtime().$model->password);
85  $profile->attributes = $_POST['Profile'];
86  $profile->user_id = 0;
87  if ($model->validate() && $profile->validate()) {
88  $model->password = Yii::app()->controller->module->encrypting($model->password);
89  if ($model->save()) {
90  $profile->user_id = $model->id;
91  $profile->save();
92  // Set default user assignments (role / account type)
93  $RegistrationForm = new RegistrationForm();
94  $RegistrationForm->setDefaultUserAssignments($model->id);
95  }
96  $this->redirect(array('view', 'id' => $model->id));
97  } else {
98  $profile->validate();
99  }
100  }
101 
102  $this->render('create', array(
103  'model' => $model,
104  'profile' => $profile,
105  ));
106  }
107 
112  public function actionUpdate()
113  {
114  $model = $this->loadModel();
115  $profile = $model->profile;
116  $this->performAjaxValidation(array($model, $profile));
117  if (isset($_POST['User'])) {
118  $model->attributes = $_POST['User'];
119  $profile->attributes = $_POST['Profile'];
120 
121  if ($model->validate() && $profile->validate()) {
122  $old_password = User::model()->notsafe()->findByPk($model->id);
123  if ($old_password->password != $model->password) {
124  $model->password = Yii::app()->controller->module->encrypting($model->password);
125  $model->activkey = Yii::app()->controller->module->encrypting(microtime().$model->password);
126  }
127  $model->save();
128  $profile->save();
129  $this->redirect(array('view', 'id' => $model->id));
130  } else {
131  $profile->validate();
132  }
133  }
134 
135  $this->render('update', array(
136  'model' => $model,
137  'profile' => $profile,
138  ));
139  }
140 
145  public function actionDelete()
146  {
147  if (Yii::app()->request->isPostRequest) {
148  // we only allow deletion via POST request
149  $model = $this->loadModel();
150  $profile = Profile::model()->findByPk($model->id);
151  $profile->delete();
152  $model->delete();
153  // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
154  if (!isset($_POST['ajax'])) {
155  $this->redirect(array('/user/admin'));
156  }
157  } else {
158  throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
159  }
160  }
161 
167  protected function performAjaxValidation($validate)
168  {
169  if (isset($_POST['ajax']) && $_POST['ajax'] === 'user-form') {
170  echo CActiveForm::validate($validate);
171  Yii::app()->end();
172  }
173  }
174 
179  public function loadModel()
180  {
181  if ($this->_model === null) {
182  if (isset($_GET['id'])) {
183  $this->_model = User::model()->notsafe()->findbyPk($_GET['id']);
184  }
185  if ($this->_model === null) {
186  throw new CHttpException(404, 'The requested page does not exist.');
187  }
188  }
189 
190  return $this->_model;
191  }
192 }