PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/rights/services/RightsService.class.php

http://pagizer-cms.googlecode.com/
PHP | 616 lines | 421 code | 90 blank | 105 comment | 73 complexity | 673297820b31d9f55950a1c1e074e7a5 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. class m_rights_services_RightsService
  3. {
  4. /**
  5. * @var m_rights_services_RightsService
  6. */
  7. private static $instance;
  8. /**
  9. * Constructor
  10. */
  11. private function __construct()
  12. {
  13. // Code if needed...
  14. }
  15. /**
  16. * @return m_rights_services_RightsService
  17. */
  18. public static function getInstance()
  19. {
  20. if (self::$instance === null)
  21. {
  22. self::$instance = new self();
  23. }
  24. return self::$instance;
  25. }
  26. /************************* GENERIC *****************************/
  27. /**
  28. * Get a module path by is his name
  29. *
  30. * @param string $moduleName
  31. * @return array
  32. */
  33. public function getModulesPathByName($moduleName)
  34. {
  35. $fileResolver = f_file_Resolver::getInstance();
  36. return $fileResolver->getModulePath($moduleName);
  37. }
  38. /**
  39. * Get all modules name
  40. *
  41. * @return array
  42. */
  43. public function getAllModulesName()
  44. {
  45. $fileResolver = f_file_Resolver::getInstance();
  46. $modulesDir = $fileResolver->fromDirectory(MODULES_DIR)->setMask("*")->setRecursive(false)->find();
  47. $webappDir = $fileResolver->fromDirectory(WEBAPP_MODULES_DIR)->setMask("*")->setRecursive(false)->find();
  48. $tmp = array();
  49. if(is_array($modulesDir) && count($modulesDir) > 0)
  50. {
  51. foreach($modulesDir as $mdir)
  52. {
  53. $mtmp = explode(DS,$mdir);
  54. $tmp[] = end($mtmp);
  55. }
  56. }
  57. if(is_array($webappDir) && count($webappDir) > 0)
  58. {
  59. foreach($webappDir as $wdir)
  60. {
  61. $wtmp = explode(DS,$wdir);
  62. $tmp[] = end($wtmp);
  63. }
  64. }
  65. return array_unique($tmp);
  66. }
  67. /**
  68. * Get all actions files path from a module path
  69. *
  70. * @param string $path
  71. * @return array
  72. */
  73. public function getActionsFilesPathFromModulePath($path)
  74. {
  75. $fileResolver = f_file_Resolver::getInstance();
  76. return $fileResolver->fromDirectory($path.DS."actions")->setMask("*Action.class.php")->setRecursive(true)->find();
  77. }
  78. /**
  79. * Get all actions names from a module path
  80. *
  81. * @param string $path
  82. * @return array
  83. */
  84. public function getActionsNamesFromModulePath($path)
  85. {
  86. $actionsFiles = $this->getActionsFilesPathFromModulePath($path);
  87. $res = array();
  88. foreach($actionsFiles as $actionFile)
  89. {
  90. $tmp = explode(DS,$actionFile);
  91. $name = explode("Action",$tmp[count($tmp)-1]);
  92. if($tmp[count($tmp)-2] == "block")
  93. {
  94. $res[] = $tmp[count($tmp)-2]."_".$name[0];
  95. }
  96. else
  97. {
  98. $res[] = $name[0];
  99. }
  100. }
  101. return $res;
  102. }
  103. /************************** ROLES ******************************/
  104. public function getAllRights()
  105. {
  106. $modules = $this->getAllModulesName();
  107. $allRights = array();
  108. $rightInfo = array();
  109. foreach($modules as $module)
  110. {
  111. $rights = $this->readRightsFromModule($module);
  112. if(count($rights) > 0)
  113. {
  114. foreach($rights as $right)
  115. {
  116. $allRights[] = $module."_".$right['name'];
  117. }
  118. }
  119. }
  120. return $allRights;
  121. }
  122. /**
  123. * Save $group's $rules in Database
  124. *
  125. * @param m_users_documents_Group $group
  126. * @param mixed $rules
  127. */
  128. public function saveRightsForGroup($group, $rules)
  129. {
  130. $groupRights = $group->getRights();
  131. if(!is_array($rules))
  132. {
  133. $rules = split("[,; ]", $rules);
  134. }
  135. if(empty($groupRights) == true)
  136. {
  137. $doc = m_rights_documents_Rights::createNew()->setAuthor("pagizer")->setRules(serialize($rules))->save();
  138. $group->addRightsById($doc->getUniqueId())->save();
  139. }
  140. else
  141. {
  142. $groupRights = $groupRights[0];
  143. $groupRights->setRules(serialize($rules))->save();
  144. }
  145. }
  146. /**
  147. * Read the Rights file of $moduleName and return all rules
  148. *
  149. * @param string $moduleName
  150. * @return array
  151. */
  152. public function readRightsFromModule($moduleName)
  153. {
  154. $file = f_file_Resolver::getModuleFilePath($moduleName, "config".DIRECTORY_SEPARATOR."rights.xml");
  155. $rights = array();
  156. if(!is_null($file))
  157. {
  158. $xmlFileObject = f_xml_Xpath::getInstance($file);
  159. $properties = $xmlFileObject->fromXpath("rule")->getElements();
  160. /* @var $var f_xml_XmlElement */
  161. foreach($properties as $var)
  162. {
  163. $rights[] = $var->getAttributes();
  164. }
  165. }
  166. return $rights;
  167. }
  168. /**
  169. * Read the Rigths file from module name and return the title name
  170. *
  171. * @param $moduleName
  172. * @return string
  173. */
  174. public function readTitleRightsFromModule($moduleName)
  175. {
  176. $file = f_file_Resolver::getModuleFilePath($moduleName, "config".DIRECTORY_SEPARATOR."rights.xml");
  177. if(!is_null($file))
  178. {
  179. $xmlFileObject = f_xml_Xpath::getInstance($file);
  180. $title = $xmlFileObject->fromXpath("rights")->getAttributeValue("name");
  181. }
  182. return $title;
  183. }
  184. /**
  185. * Return an array of $moduleName's rules that $group can access
  186. *
  187. * @param string $moduleName
  188. * @param m_users_documents_Group $group
  189. * @return array
  190. */
  191. public function readRightsFromModuleForGroup($moduleName,$group)
  192. {
  193. $rights = $this->readRightsFromModule($moduleName);
  194. $groupRights = array();
  195. $groupRights = $this->getRightsFromGroup($group);
  196. $resRights = array();
  197. foreach($rights as $rule)
  198. {
  199. $val = false;
  200. if(in_array($moduleName."_".$rule['name'],$groupRights))
  201. {
  202. $val = true;
  203. }
  204. $resRights[$rule['name']] = $val;
  205. }
  206. return $resRights;
  207. }
  208. /**
  209. * Return an array of group's rules
  210. *
  211. * @param $group
  212. * @return array
  213. */
  214. public function getRightsFromGroup($group)
  215. {
  216. $tabRights = $group->getRights();
  217. $groupRights = array();
  218. if(empty($tabRights) == false)
  219. {
  220. $tabRights = $tabRights[0];
  221. $groupRights = unserialize($tabRights->getRules());
  222. }
  223. return $groupRights;
  224. }
  225. /**
  226. * Check if the Rights file of the given $moduleName exist
  227. *
  228. * @return boolean
  229. */
  230. public function checkRightsFileExistForModule($moduleName)
  231. {
  232. $file = f_file_Resolver::getModuleFilePath($moduleName, "config".DS."rights.xml");
  233. return !is_null($file);
  234. }
  235. /************************** RIGHTS ******************************/
  236. public function getRightsFromDocument($groupId, $module, $document)
  237. {
  238. $groups = f_relation_Manager::getInstance()->getParentsIdsByDocType($groupId, "modules_users/group");
  239. $groups[]['parent_id'] = $groupId;
  240. $validResult = array();
  241. $nbGroup = count($groups);
  242. foreach($groups as $i => $group)
  243. {
  244. $result = $this->getRightFromAcl($group['parent_id'], $module, $document);
  245. if(empty($result) && empty($validResult))
  246. {
  247. if($i < $nbGroup-1)
  248. {
  249. $validResult = $this->getRightsFromDocument($group['parent_id'], $module, $document);
  250. foreach($validResult as $i => $value)
  251. {
  252. $validResult[$i] = $value == '1' && $i != 'front' ? '2' : $value;
  253. }
  254. continue;
  255. }
  256. elseif($nbGroup == 1)
  257. {
  258. $validResult = array('1', '1', '1', '1', '1');
  259. }
  260. $keys = array('front', 'read', 'edit', 'add', 'delete');
  261. $result = array_combine($keys, $validResult);
  262. $users = $this->getUserByRightsFromDocument($document, $module, 'front', '3');
  263. if(!empty($users))
  264. {
  265. $result['front'] = '0';
  266. }
  267. return $result;
  268. }
  269. elseif(!empty($result))
  270. {
  271. foreach($result as $key => $value)
  272. {
  273. if(isset($validResult[$key]) && ($validResult[$key] == '2' || $validResult[$key] == '4'))
  274. {
  275. continue;
  276. }
  277. if($value > '0')
  278. {
  279. if($key == 'front')
  280. {
  281. $users = $this->getUserByRightsFromDocument($document, $module, 'front', '3');
  282. if(!empty($users))
  283. {
  284. if($group['parent_id'] != $groupId && in_array($group['parent_id'], $users))
  285. {
  286. $validResult[$key] = '4';
  287. }
  288. else
  289. {
  290. $validResult[$key] = in_array($groupId, $users) ? '3' : '0';
  291. }
  292. }
  293. else
  294. {
  295. $validResult[$key] = '1';
  296. }
  297. }
  298. else
  299. {
  300. $validResult[$key] = $group['parent_id'] == $groupId ? '1' : '2';
  301. }
  302. }
  303. else
  304. {
  305. $validResult[$key] = $group['parent_id'] == $groupId && count($groups) > 1 ? $validResult[$key] : '0';
  306. if($key == 'front' && $groupId == $group['parent_id'] && $result[$key] != '1')
  307. {
  308. $users = $this->getUserByRightsFromDocument($document, $module, 'front', '3');
  309. if(empty($users))
  310. {
  311. $this->saveRightsForDocument($groupId, $module, $document, 'front', array('front' => '1'));
  312. $validResult[$key] = '1';
  313. }
  314. }
  315. }
  316. }
  317. }
  318. }
  319. return $validResult;
  320. }
  321. public function getUserByRightsFromDocument($document, $module, $rule, $value = '1')
  322. {
  323. $dbProvider = f_database_Provider::getInstance();
  324. $sql = 'SELECT `user_id` FROM `f_acl` WHERE `document_uid` = :documentId AND `document_lang` = :documentLang AND `module` = :module AND `' . $rule . '` = '.$value;
  325. $dbProvider->setQuery($sql)->execute(array('documentId' => $document->getUniqueId(), 'documentLang' => $document->getLang(), 'module' => $module));
  326. return $dbProvider->getQueryResults(PDO::FETCH_COLUMN);
  327. }
  328. public $jsTab;
  329. public function saveRightsForDocument($groupId, $module, $document, $rule, $rights = null)
  330. {
  331. $docRights = $this->getRightFromAcl($groupId, $module, $document);
  332. if(empty($docRights))
  333. {
  334. $sql = 'INSERT INTO `f_acl` (`document_uid`,`document_lang`,`module`,`user_id` , `front`, `read`,`edit`,`add`,`delete`) VALUES (:documentId, :documentLang, :module, :group, :front, :read, :edit, :add, :delete)';
  335. $docRights = $this->getRightsFromDocument($groupId, $module, $document);
  336. }
  337. else
  338. {
  339. $sql = 'UPDATE `f_acl` SET `front` = :front, `read` = :read,`edit` = :edit,`add` = :add,`delete` = :delete WHERE `document_uid` = :documentId AND `document_lang` = :documentLang AND `module` = :module AND `user_id` = :group';
  340. }
  341. $rights = $this->changeRights($docRights, $rule, $rights);
  342. $infos = array('documentId' => $document->getUniqueId(), 'documentLang' => $document->getLang(), 'module' => $module, 'group' => $groupId);
  343. $args = array_merge($infos, $rights);
  344. $dbProvider = f_database_Provider::getInstance();
  345. $dbProvider->setQuery($sql)->execute($args);
  346. $documents = m_backoffice_services_ListService::getInstance()->getModuleListedDocuments($module);
  347. $results = f_relation_Manager::getInstance()->getChildsByDocType($document->getUniqueId(), $documents, $document->getLang());
  348. foreach($results as $result)
  349. {
  350. if($rule != 'front')
  351. {
  352. unset($rights['front']);
  353. }
  354. $this->saveRightsForDocument($groupId, $module, $result, $rule, $rights);
  355. }
  356. //to keep the hierarchy of with the blocks rights.
  357. if($document->getDocumentModel() == "modules_website/page")
  358. {
  359. $blocks = $document->getAllBlocks();
  360. $infos['module'] = 'page';
  361. foreach($blocks as $block)
  362. {
  363. $outRights = $document->setBlockRight($groupId, $block['blockId'], $rule, $rights);
  364. $infos['blockId'] = $block['blockId'];
  365. $infos['rights'] = $outRights;
  366. $this->jsTab[] = $infos;
  367. unset($infos['blockId']);
  368. }
  369. $infos['module'] = $module;
  370. }
  371. $infos['rights'] = $this->getRightsFromDocument($groupId, $module, $document);
  372. $this->jsTab[] = $infos;
  373. }
  374. /**
  375. * Get rights from the table ACL
  376. * @param <type> $groupId
  377. * @param <type> $module
  378. * @param <type> $document
  379. * @return <type> array(5)
  380. */
  381. private function getRightFromAcl($groupId, $module, $document)
  382. {
  383. $dbProvider = f_database_Provider::getInstance();
  384. $sql = 'SELECT `front`, `read`, `edit`, `add`, `delete` FROM `f_acl` WHERE `document_uid` = :documentId AND `document_lang` = :documentLang AND `module` = :module AND `user_id` = :group LIMIT 1';
  385. $dbProvider
  386. ->setQuery($sql)
  387. ->execute(array('documentId' => $document->getUniqueId(), 'documentLang' => $document->getLang(), 'module' => $module, 'group' => $groupId));
  388. $result = $dbProvider->getQueryResults(PDO::FETCH_ASSOC);
  389. return !empty($result) ? $result[0] : array();
  390. }
  391. /**
  392. * Change the rights. true -> false : false -> true.
  393. * @param <type> $docRights -> actual rights
  394. * @param <type> $rule -> rule to change
  395. * @return <type> array() with key 'front', 'read', 'edit', 'add', 'delete'
  396. */
  397. public function changeRights($docRights, $rule, $rights = null)
  398. {
  399. if(is_null($rights) && !is_null($rule))
  400. {
  401. $keys = array_keys($docRights);
  402. if($rule == 'front')
  403. {
  404. $rights = array_values($docRights);
  405. $rights[0] = ($docRights['front'] < '2') ? '3' : '0';
  406. }
  407. else
  408. {
  409. switch($rule)
  410. {
  411. case "read":
  412. $rights = ($docRights[$rule] == '0') ? array('1', '0', '0', '0') : array('0', '0', '0', '0');
  413. break;
  414. case "edit":
  415. $rights = ($docRights[$rule] == '0') ? array('1', '1', '0', '0') : array('1', '0', '0', '0');
  416. break;
  417. case "add":
  418. $rights = ($docRights[$rule] == '0') ? array('1', '1', '1', '0') : array('1', '1', '0', '0');
  419. break;
  420. case "delete":
  421. $rights = ($docRights[$rule] == '0') ? array('1', '1', '1', '1') : array('1', '1', '1', '0');
  422. break;
  423. }
  424. array_unshift($rights, $docRights['front']);
  425. }
  426. $rights = array_combine($keys, $rights);
  427. }
  428. else
  429. {
  430. if(count($rights) != 5)
  431. {
  432. foreach($docRights as $key => $right)
  433. {
  434. $rights[$key] = isset($rights[$key]) ? $rights[$key] : $docRights[$key];
  435. }
  436. }
  437. }
  438. return $rights;
  439. }
  440. /********************* GET PERMISSIONS **************************/
  441. /**
  442. * Get all user's rights
  443. *
  444. * @param m_users_documents_Users $user
  445. * @return array
  446. */
  447. public function getUserRights($user)
  448. {
  449. if(is_null($user))
  450. {
  451. throw new Exception("Parameter given is null!");
  452. }
  453. if(empty($user) == true)
  454. {
  455. throw new Exception("Parameter given is empty!");
  456. }
  457. $res = array();
  458. $rights = array();
  459. $res = $this->getInheritRightsForGroup($user);
  460. $rights = $this->getRightsFromGroup($user);
  461. if(!empty($rights))
  462. {
  463. foreach($rights as $right)
  464. {
  465. if(!in_array($right,$res))
  466. {
  467. $res[] = $right;
  468. }
  469. }
  470. }
  471. return $res;
  472. }
  473. /**
  474. * Get all rights which are inherit from an sub group
  475. *
  476. * @param m_users_documents_Group $group
  477. * @return array
  478. */
  479. public function getInheritRightsForGroup($group)
  480. {
  481. if(is_null($group))
  482. {
  483. throw new Exception("Parameter given is null!");
  484. }
  485. if(empty($group))
  486. {
  487. throw new Exception("Parameter given is empty!");
  488. }
  489. $inheritRights = array();
  490. $parents = f_relation_Manager::getInstance()->getParentsByDocType($group->getUniqueId(), "modules_users/group");
  491. foreach($parents as $parent)
  492. {
  493. $rights = $this->getRightsFromGroup($parent);
  494. foreach($rights as $right)
  495. {
  496. if(!in_array($right,$inheritRights))
  497. {
  498. $inheritRights[] = $right;
  499. }
  500. }
  501. }
  502. return $inheritRights;
  503. }
  504. /**
  505. * Show if a group is inherit from the su group
  506. * @param $group
  507. * @return bool
  508. */
  509. public function isInheritSuGroup($group)
  510. {
  511. $superGroup = f_document_Provider::getInstance()->loadDocuments("modules_users/group")->retrieveFirstDocument();
  512. $parents = f_relation_Manager::getInstance()->getParentsByDocType($group->getUniqueId(), "modules_users/group");
  513. if($superGroup->getUniqueId() == $group->getUniqueId())
  514. {
  515. return true;
  516. }
  517. foreach($parents as $parent)
  518. {
  519. if($parent->getUniqueId() == $superGroup->getUniqueId())
  520. {
  521. return true;
  522. }
  523. }
  524. return false;
  525. }
  526. }