HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
AssignmentController.php
Go to the documentation of this file.
1 <?php
11 {
15  private $_authorizer;
16 
20  public function init()
21  {
22  $this->_authorizer = $this->module->getAuthorizer();
23  $this->layout = $this->module->layout;
24  $this->defaultAction = 'view';
25 
26  // Register the scripts
27  $this->module->registerScripts();
28  }
29 
33  public function filters()
34  {
35  return array('accessControl');
36  }
37 
44  public function accessRules()
45  {
46  return array(
47  array('allow', // Allow superusers to access Rights
48  'actions' => array(
49  'view',
50  'user',
51  'revoke',
52  'sites',
53  ),
54  'users' => $this->_authorizer->getSuperusers(),
55  ),
56  array('deny', // Deny all users
57  'users' => array('*'),
58  ),
59  );
60  }
61 
65  public function actionView()
66  {
67  // Create a data provider for listing the users
68  $dataProvider = new RAssignmentDataProvider(array(
69  'pagination' => array(
70  'pageSize' => 50,
71  ),
72  ));
73 
74  // Render the view
75  $this->render('view', array(
76  'dataProvider' => $dataProvider,
77  ));
78  }
79 
83  public function actionUser()
84  {
85  // Create the user model and attach the required behavior
86  $userClass = $this->module->userClass;
87  $model = CActiveRecord::model($userClass)->findByPk($_GET['id']);
88  $this->_authorizer->attachUserBehavior($model);
89 
90  $assignedItems = $this->_authorizer->getAuthItems(null, $model->getId());
91  $assignments = array_keys($assignedItems);
92 
93  // Make sure we have items to be selected
94  $assignSelectOptions = Rights::getAuthItemSelectOptions(null, $assignments);
95  if ($assignSelectOptions !== array()) {
96  $formModel = new AssignmentForm();
97 
98  // Form is submitted and data is valid, redirect the user
99  if (isset($_POST['AssignmentForm']) === true) {
100  $formModel->attributes = $_POST['AssignmentForm'];
101  if ($formModel->validate() === true) {
102  // Update and redirect
103  $this->_authorizer->authManager->assign($formModel->itemname, $model->getId());
104  $item = $this->_authorizer->authManager->getAuthItem($formModel->itemname);
105  $item = $this->_authorizer->attachAuthItemBehavior($item);
106 
107  Yii::app()->user->setFlash($this->module->flashSuccessKey,
108  Rights::t('core', 'Permission :name assigned.', array(':name' => $item->getNameText()))
109  );
110 
111  $this->redirect(array('assignment/user', 'id' => $model->getId()));
112  }
113  }
114  }
115  // No items available
116  else {
117  $formModel = null;
118  }
119 
120  // Create a data provider for listing the assignments
121  $dataProvider = new RAuthItemDataProvider('assignments', array(
122  'userId' => $model->getId(),
123  ));
124 
125  // Render the view
126  $this->render('user', array(
127  'model' => $model,
128  'dataProvider' => $dataProvider,
129  'formModel' => $formModel,
130  'assignSelectOptions' => $assignSelectOptions,
131  ));
132  }
133 
137  public function actionRevoke()
138  {
139  // We only allow deletion via POST request
140  if (Yii::app()->request->isPostRequest === true) {
141  $itemName = $this->getItemName();
142 
143  // Revoke the item from the user and load it
144  $this->_authorizer->authManager->revoke($itemName, $_GET['id']);
145  $item = $this->_authorizer->authManager->getAuthItem($itemName);
146  $item = $this->_authorizer->attachAuthItemBehavior($item);
147 
148  // Set flash message for revoking the item
149  Yii::app()->user->setFlash($this->module->flashSuccessKey,
150  Rights::t('core', 'Permission :name revoked.', array(':name' => $item->getNameText()))
151  );
152 
153  // if AJAX request, we should not redirect the browser
154  if (isset($_POST['ajax']) === false) {
155  $this->redirect(array('assignment/user', 'id' => $_GET['id']));
156  }
157  } else {
158  throw new CHttpException(400, Rights::t('core', 'Invalid request. Please do not repeat this request again.'));
159  }
160  }
161 
165  public function getItemName()
166  {
167  return isset($_GET['name']) === true ? urldecode($_GET['name']) : null;
168  }
169 }