PageRenderTime 86ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/modules/srbac/SrbacModule.php

http://srbac.googlecode.com/
PHP | 333 lines | 234 code | 26 blank | 73 comment | 35 complexity | 4cf7713258dad2300deae41512002e72 MD5 | raw file
  1. <?php
  2. /**
  3. * SrbacModule class file.
  4. *
  5. * @author Spyros Soldatos <spyros@valor.gr>
  6. * @link http://code.google.com/p/srbac/
  7. */
  8. /**
  9. * SrbacModule is the module that loads the srbac module in the application
  10. *
  11. * @author Spyros Soldatos <spyros@valor.gr>
  12. * @package srbac
  13. * @since 1.0.0
  14. */
  15. class SrbacModule extends CWebModule {
  16. //Constants
  17. const ICON_PACKS = "noia,tango";
  18. const PRIVATE_ATTRIBUTES = "_icons,_cssPublished,_imagesPublished,defaultController,controllerMap,preload,behaviors";
  19. const TABLE_NAMES_ERROR = "Srbac is installed but the CDBAuthManger table names in the database are different from those
  20. in the CDBAuthManager configuration.<br />A common mistake is that names in database are in lowercase.<br />Srbac may not work correctly!!!";
  21. //Private attributes
  22. /* @var $_icons String The path to the icons */
  23. private $_icons;
  24. /* @var $_yiiSupportedVersion String The yii version tha srbac supports */
  25. private $_yiiSupportedVersion = "1.1.0";
  26. /* @var $_version Srbac version */
  27. private $_version = "1.3beta";
  28. /* @var $_cssPublished boolean If css file exists and is published */
  29. private $_cssPublished = false;
  30. /* @var $_imagesPublished boolean If images files exists and are published */
  31. private $_imagesPublished = false;
  32. // Srbac Attributes
  33. /* @var $debug If srbac is in debug mode */
  34. private $_debug = false;
  35. /* @var $pagesize int The number of items displayed in each page*/
  36. private $_pageSize = 15;
  37. /* @var $alwaysAllowed mixed The actions that are always allowed*/
  38. private $_alwaysAllowed = array();
  39. /* @var $userActions mixed Operations assigned to users by default*/
  40. private $_userActions = array();
  41. /* @var $listBoxNumberOfLines integer The number of lines in the assign tabview listboxes */
  42. private $_listBoxNumberOfLines = 10;
  43. /* @var $iconText boolean Display text next to the icons */
  44. private $_iconText = false;
  45. /* @var $_useHeader boolean Use header or not */
  46. private $_showHeader = false;
  47. /* @var $_useFooter boolean Use footer or not */
  48. private $_showFooter = false;
  49. /* @var $_cssUrl The url of the css file to register */
  50. private $_cssUrl;
  51. /* @deprecated $useAlwaysAllowedGui boolean */
  52. public $useAlwaysAllowedGui;
  53. /* @var $_message A warning/error message displayed in the top of each page */
  54. private $_message ="";
  55. /* @var $userid String The primary column of the users table*/
  56. public $userid = "userid";
  57. /* @var $username String The username column of the users table*/
  58. public $username = "username";
  59. /* @var $userclass String The name of the users Class*/
  60. public $userclass = "User";
  61. /* @var $superUser String The name of the superuser */
  62. public $superUser = "Authorizer";
  63. /* @var $css string The css to use */
  64. public $css = "srbac.css";
  65. /* @var $notAuthorizedView String The view to render when unathorized access*/
  66. public $notAuthorizedView = "srbac.views.authitem.unauthorized";
  67. /* @var $imagesPath string The path to srbac images*/
  68. public $imagesPath = "srbac.images";
  69. /* @var $imagesPack String The images theme to use*/
  70. public $imagesPack = "noia";
  71. /* @var $header String Srbac header*/
  72. public $header = "srbac.views.authitem.header";
  73. /* @var $footer String Srbac footer*/
  74. public $footer = "srbac.views.authitem.footer";
  75. /* @var $alwaysAllowedPath String */
  76. public $alwaysAllowedPath = "srbac.components";
  77. /* @var $delimeter The delimeter used in modules between moduleId and itemId */
  78. public $delimeter = "-";
  79. /**
  80. * this method is called when the module is being created you may place code
  81. * here to customize the module or the application
  82. */
  83. public function init() {
  84. // import the module-level models and components
  85. $this->setImport(array(
  86. 'srbac.models.*',
  87. 'srbac.components.Helper',
  88. 'srbac.components.SHtml',
  89. 'srbac.controllers.SBaseController'
  90. ));
  91. //Set layout to main
  92. if($this->layout =="") {
  93. $this->layout = "application.views.layouts.main";
  94. }
  95. //Publish css
  96. $this->_cssPublished = Helper::publishCss($this->css);
  97. //Publish images
  98. $this->setIconsPath(Helper::publishImages($this->imagesPath,$this->imagesPack));
  99. $this->_imagesPublished = $this->getIconsPath() == "" ? false : true;
  100. //Create the translation component
  101. $this->setComponents(
  102. array(
  103. 'tr'=>array(
  104. 'class'=>'CPhpMessageSource',
  105. 'basePath'=> dirname(__FILE__).DIRECTORY_SEPARATOR.'messages',
  106. 'onMissingTranslation'=>"Helper::markWords"
  107. ),
  108. )
  109. );
  110. }
  111. // SETTERS & GETTERS
  112. public function setCssUrl($cssUrl) {
  113. $this->_cssUrl = $cssUrl;
  114. }
  115. public function getCssUrl() {
  116. return $this->_cssUrl;
  117. }
  118. public function setDebug($debug) {
  119. if(is_bool($debug)) {
  120. $this->_debug = $debug;
  121. } else {
  122. throw new CException("Wrong value for srbac attribute debug in srbac configuration.
  123. '".$debug."' is not a boolean.");
  124. }
  125. }
  126. public function getDebug() {
  127. return $this->_debug;
  128. }
  129. public function setPageSize($pageSize) {
  130. if(is_numeric($pageSize)) {
  131. $this->_pageSize = (int) $pageSize;
  132. } else {
  133. throw new CException("Wrong value for srbac attribute pageSize in srbac configuration.
  134. '".$pageSize."' is not an integer.");
  135. }
  136. }
  137. public function getPageSize() {
  138. return $this->_pageSize;
  139. }
  140. public function setAlwaysAllowed($alwaysAllowed) {
  141. $this->_alwaysAllowed = $alwaysAllowed;
  142. }
  143. public function getAlwaysAllowed() {
  144. $paramAllowed = array();
  145. if(!is_file($this->getAlwaysAllowedFile())) {
  146. $handle = fopen($this->getAlwaysAllowedFile(), "wb");
  147. fwrite($handle, "<?php\n return array();\n?>");
  148. fclose($handle);
  149. }
  150. $guiAllowed = include($this->getAlwaysAllowedFile());
  151. if(!is_array($guiAllowed)){
  152. $guiAllowed = array();
  153. }
  154. if(is_array($this->_alwaysAllowed)) {
  155. $paramAllowed = $this->_alwaysAllowed;
  156. }else if(is_file(Yii::getPathOfAlias($this->_alwaysAllowed).".php")) {
  157. $paramAllowed = include(Yii::getPathOfAlias($this->_alwaysAllowed).".php");
  158. } else if(is_string($this->_alwaysAllowed)) {
  159. $paramAllowed = split(",", $this->_alwaysAllowed);
  160. }
  161. return array_merge($guiAllowed, $paramAllowed);
  162. }
  163. public function getAlwaysAllowedFile() {
  164. return Yii::getPathOfAlias($this->alwaysAllowedPath).DIRECTORY_SEPARATOR."allowed.php";
  165. }
  166. public function setUserActions($userActions) {
  167. if(is_array($userActions)) {
  168. $this->_userActions = $userActions;
  169. } else {
  170. $this->_userActions = explode(",",$userActions);
  171. }
  172. }
  173. public function getUserActions() {
  174. return $this->_userActions;
  175. }
  176. public function setListBoxNumberOfLines($size) {
  177. if(is_numeric($size)) {
  178. $this->_listBoxNumberOfLines = (int) $size;
  179. } else {
  180. throw new CException("Wrong value for srbac attribute listBoxNumberOfLines in srbac configuration.
  181. '".$size."' is not an integer.");
  182. }
  183. }
  184. public function getListBoxNumberOfLines() {
  185. return $this->_listBoxNumberOfLines;
  186. }
  187. public function setIconText($iconText) {
  188. if(is_bool($iconText)) {
  189. $this->_iconText = $iconText;
  190. } else {
  191. throw new CException("Wrong value for srbac attribute iconText in srbac configuration.
  192. '".$iconText."' is not a boolean.");
  193. }
  194. }
  195. public function getIconText() {
  196. return $this->_iconText;
  197. }
  198. public function setShowHeader($useHeader) {
  199. if(is_bool($useHeader)) {
  200. $this->_showHeader = $useHeader;
  201. } else {
  202. throw new CException("Wrong value for srbac attribute useHeader in srbac configuration.
  203. '".$useHeader."' is not a boolean.");
  204. }
  205. }
  206. public function getShowHeader() {
  207. return $this->_showHeader;
  208. }
  209. public function setShowFooter($useFooter) {
  210. if(is_bool($useFooter)) {
  211. $this->_showFooter = $useFooter;
  212. } else {
  213. throw new CException("Wrong value for srbac attribute footer in srbac configuration.
  214. '".$useFooter."' is not a boolean.");
  215. }
  216. }
  217. public function getShowFooter() {
  218. return $this->_showFooter;
  219. }
  220. /**
  221. * Checks if srbac is installed by checking if Auth items table exists.
  222. * @return boolean Whether srbac is installed or not
  223. */
  224. public function isInstalled() {
  225. try {
  226. $tables = Yii::app()->authManager->db->schema->tableNames;
  227. $itemTableName = Yii::app()->authManager->itemTable;
  228. $itemChildTableName = Yii::app()->authManager->itemChildTable ;
  229. $assignmentTableName = Yii::app()->authManager->assignmentTable ;
  230. $tablePrefix = AuthItem::model()->getDbConnection()->tablePrefix;
  231. if(!is_null($tablePrefix)) {
  232. $itemTableName = preg_replace('/{{(.*?)}}/',$tablePrefix.'\1',$itemTableName);
  233. $itemChildTableName = preg_replace('/{{(.*?)}}/',$tablePrefix.'\1',$itemChildTableName);
  234. $assignmentTableName = preg_replace('/{{(.*?)}}/',$tablePrefix.'\1',$assignmentTableName);
  235. }
  236. if(in_array($itemTableName, $tables) &&
  237. in_array($itemChildTableName, $tables) &&
  238. in_array($assignmentTableName, $tables)) {
  239. return true;
  240. }else {
  241. $tables = array_map('strtolower', $tables);
  242. if(in_array(strtolower($itemTableName), $tables) &&
  243. in_array(strtolower($itemChildTableName), $tables) &&
  244. in_array(strtolower($assignmentTableName), $tables)) {
  245. $this->_message = self::TABLE_NAMES_ERROR;
  246. return true;
  247. }
  248. }
  249. return false;
  250. } catch (CDbException $ex ) {
  251. return false;
  252. }
  253. }
  254. /**
  255. * Gets the user's class
  256. * @return userclass
  257. */
  258. public function getUserModel() {
  259. return new $this->userclass;
  260. }
  261. /**
  262. * this method is called before any module controller action is performed
  263. * you may place customized code here
  264. * @param CController $controller
  265. * @param CAction $action
  266. * @return boolean
  267. */
  268. public function beforeControllerAction($controller, $action) {
  269. if(parent::beforeControllerAction($controller, $action)) {
  270. return true;
  271. }
  272. else
  273. return false;
  274. }
  275. /**
  276. * Gets the path to the icon files
  277. * @return String The path to the icons
  278. */
  279. public function getIconsPath() {
  280. return $this->_icons;
  281. }
  282. public function setIconsPath($path) {
  283. $this->_icons = $path;
  284. }
  285. public function getSupportedYiiVersion() {
  286. return $this->_yiiSupportedVersion;
  287. }
  288. public function getVersion() {
  289. return $this->_version;
  290. }
  291. public function isCssPublished() {
  292. return $this->_cssPublished;
  293. }
  294. public function isImagesPublished() {
  295. return $this->_imagesPublished;
  296. }
  297. public function getAttributes() {
  298. return get_object_vars($this);
  299. }
  300. public function getMessage() {
  301. if($this->_message != ""){
  302. return Helper::translate("srbac",$this->_message);
  303. } else {
  304. return "";
  305. }
  306. }
  307. }