HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
UsersSitesRights.php
Go to the documentation of this file.
1 <?php
3 {
9  private $__key = 'Site';
10 
16  public static function model($className = __CLASS__)
17  {
18  return parent::model($className);
19  }
20 
26  public function tableName()
27  {
28  return 'users_sites_rights';
29  }
30 
38  public function search($params)
39  {
40  $sites = array();
41  $json = $this->commandFind($params['userId']);
42  $dataProvider = CJSON::decode($json);
43  foreach ($dataProvider["itemsList"][0]["itemObject"] as $item) {
44  $item['urls'] = YFormatter::arrayUrlsToStringUrls($item['urls']);
45  $sites[] = $item;
46  }
47  foreach ($sites as $site) {
48  $ids['sites'][] = $site['id'];
49  $ids['users'][] = $site['userId'];
50  }
51  if (empty($sites)) {
52  return $sites;
53  }
54  $usersSitesRights = $this->findAllByAttributes(array(
55  'Site_Id' => $ids['sites'], 'User_Id' => $ids['users'], )
56  );
57  foreach ($sites as &$site) {
58  $site['Rights'] = array();
59  foreach ($usersSitesRights as $userSiteRight) {
60  if ($site['id'] == $userSiteRight->Site_Id &&
61  $site['userId'] == $userSiteRight->User_Id) {
62  $site['Rights'] = $userSiteRight->Rights;
63  }
64  }
65  if (!empty($params['rights']) && !array_intersect($params['rights'], $site['Rights'])) {
66  $site = array();
67  }
68  }
69 
70  return array_filter($sites);
71  }
72 
78  public function createRequest($params)
79  {
80  $pattern = Dictionary::switchCh($params['pattern']);
81  $criterions = $this->__createRequestCriterions($params);
82  $json = json_encode($criterions);
83  Logger::log("Sending request -> \n".$json, true);
84  $path = Yii::app()->getBasePath().'/json_temp/';
85  $file = fopen($path.$params['userId'].'_request.json', 'w');
86  fwrite($file, $json);
87  }
88 
96  public function commandFind($userId)
97  {
98  $api = Yii::app()->params['api'];
99  $path = Yii::app()->getBasePath().'/shell/';
100  $pathJson = Yii::app()->getBasePath().'/json_temp/';
101  $cmd = "sh ".$path."site_find_by_criterions.sh $api $pathJson $userId";
102  $json = shell_exec($cmd);
103  $file = fopen($pathJson.$userId.'_response.json', 'w');
104  fwrite($file, $json);
105  fclose($file);
106 
107  return $this->getResponse($userId);
108  }
109 
117  public function getResponse($userId)
118  {
119  $path = Yii::app()->getBasePath().'/json_temp/';
120 
121  return file_get_contents($path.$userId.'_response.json');
122  }
123 
132  public function setRightsAllRecords($records, $rights = array())
133  {
134  if (empty($records)) {
135  return false;
136  }
137  foreach ($records as $record) {
138  $params = $this->buildRightsSingleRecordParams(
139  $record, $rights
140  );
141  $this->setRightsSingleRecord($params);
142  }
143 
144  return true;
145  }
146 
155  public function setRightsSingleRecord($params, $autoSet = false)
156  {
157  $result = false;
158  if (empty($params['siteId']) ||
159  empty($params['userId']) ||
160  (!isset($params['rights']) &&
161  empty($autoSet))) {
162  return $result;
163  }
164  if (empty($params['conditions'])) {
165  $params['conditions'] = 'User_Id = :userId AND Site_Id = :siteId';
166  }
167  if (empty($params['params'])) {
168  $params['params'] = array(
169  ':siteId' => $params['siteId'],
170  ':userId' => $params['userId'],
171  );
172  }
173  if (!empty($autoSet)) {
174  $params['rights'] = array_keys($this->getRightsList());
175  }
176  if (!empty($params['rights'])) {
177  $rightsBitMask = $this->createBitMask(
178  $params['rights'], $this->statesData
179  );
180  $isExists = $this->findAll($params['conditions'], $params['params']);
181  if (!empty($isExists)) {
182  if (!empty($autoSet)) {
183  $result = true;
184  } else {
185  $result = $this->updateAll(
186  array('Rights' => $rightsBitMask),
187  $params['conditions'],
188  $params['params']
189  );
190  }
191  } else {
192  // TODO: Yii - why need 'new' every time for save?
193  $model = new UsersSitesRights();
194  $model->Site_Id = $params['siteId'];
195  $model->User_Id = $params['userId'];
196  $model->Rights = $rightsBitMask;
197  $result = $model->save();
198  }
199  } else {
200  $result = $this->deleteAll(
201  $params['conditions'], $params['params']
202  );
203  }
204 
205  return $result;
206  }
207 
215  public function encode($data)
216  {
217  return base64_encode(serialize($data));
218  }
219 
227  public function decode($data)
228  {
229  return unserialize(base64_decode($data));
230  }
231 
239  public function buildFindParams($params)
240  {
241  if (!empty($params['rights'])) {
242  if (strpos($params['rights'], ',') !== false) {
243  $params['rights'] = explode(',', $params['rights']);
244  } else {
245  $params['rights'] = (array) $params['rights'];
246  }
247  foreach ($params['rights'] as &$right) {
248  $right .= 'Site';
249  }
250  }
251  if (!empty($params['currentPage']) && $params['currentPage'] >= 2) {
252  $currentPage = $params['currentPage'] * $params['pageSize'];
253  $params['limit'] = array(
254  ($currentPage - $params['pageSize']),
255  ($currentPage + 1),
256  );
257  }
258  if (empty($params['limit'])) {
259  $params['limit'] = array(0, ($params['pageSize'] + 1));
260  }
261 
262  return $params;
263  }
264 
273  public function buildRightsSingleRecordParams($record, $rights = array())
274  {
275  $siteId = $this->_getValueFromRecord($record, array(
276  'id', 'siteId', 'Id', 'Site_Id',
277  ));
278  $userId = $this->_getValueFromRecord($record, array(
279  'userId', 'User_Id',
280  ));
281  $rights = (!empty($rights[$siteId]) ? $rights[$siteId] : array());
282 
283  return array(
284  'siteId' => $siteId,
285  'userId' => $userId,
286  'rights' => $rights,
287  );
288  }
289 
295  protected function _getRightsKey()
296  {
297  return $this->__key;
298  }
299 
307  private function __createRequestCriterions($params)
308  {
309  extract($params);
310  $criterions = array(
311  'url' => "http://%$pattern%",
312  'criterions' => array(
313  "ORDER BY" => "CDate ASC",
314  ),
315  );
316  if (!empty($searchUserId)) {
317  $criterions['criterions']['WHERE']['AND'][] = "`User_Id`=$searchUserId";
318  }
319  if (!empty($siteId)) {
320  $criterions['criterions']['WHERE']['AND'][] = "`Site_Id`=\"$siteId\"";
321  }
322  if (!empty($limit)) {
323  $criterions['criterions']['LIMIT'] = implode(', ', $limit);
324  }
325  if (!empty($criterions['criterions']['WHERE']['AND'])) {
326  $criterions['criterions']['WHERE'] = implode(' AND ', $criterions['criterions']['WHERE']['AND']);
327  }
328 
329  return $criterions;
330  }
331 }