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

/Library/Kumbia/Security/Security.php

http://kumbia-enterprise.googlecode.com/
PHP | 226 lines | 111 code | 15 blank | 100 comment | 25 complexity | d966750bd1c8cdf40fee9a9848d53389 MD5 | raw file
  1. <?php
  2. /**
  3. * Kumbia Enterprise Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the New BSD License that is bundled
  8. * with this package in the file docs/LICENSE.txt.
  9. *
  10. * If you did not receive a copy of the license and are unable to
  11. * obtain it through the world-wide-web, please send an email
  12. * to license@loudertechnology.com so we can send you a copy immediately.
  13. *
  14. * @category Kumbia
  15. * @package Security
  16. * @copyright Copyright (c) 2008-2009 Louder Technology COL. (http://www.loudertechnology.com)
  17. * @copyright Copyright (c) 2005-2009 Andres Felipe Gutierrez (gutierrezandresfelipe at gmail.com)
  18. * @license New BSD License
  19. * @version $Id: Security.php 136 2010-04-12 14:48:51Z gutierrezandresfelipe $
  20. */
  21. /**
  22. * Security
  23. *
  24. * Clase que contiene metodos utiles para manejar seguridad
  25. *
  26. * @category Kumbia
  27. * @package Security
  28. * @copyright Copyright (c) 2008-2009 Louder Technology COL. (http://www.loudertechnology.com)
  29. * @copyright Copyright (c) 2005-2009 Andres Felipe Gutierrez (gutierrezandresfelipe at gmail.com)
  30. * @license New BSD License
  31. * @access public
  32. */
  33. abstract class Security extends Object {
  34. /**
  35. * Objeto ACL que administra el acceso a la aplicaci?n
  36. *
  37. * @var Acl
  38. */
  39. static private $_securityAccessList;
  40. /**
  41. * Nombre del rol activo
  42. *
  43. * @var string
  44. */
  45. static private $_roleName;
  46. /**
  47. * Nombre del Rol por defecto
  48. *
  49. * @var string
  50. */
  51. static private $_defaultRoleName = 'Public';
  52. /**
  53. * Inicializa la lista de Acceso que controla la seguridad de una aplicacion
  54. *
  55. * @access public
  56. * @static
  57. */
  58. static public function initAccessManager(){
  59. $config = CoreConfig::readAppConfig();
  60. if(isset($config->application->securityAccessList)){
  61. $instanceName = Core::getInstanceName();
  62. self::$_securityAccessList = Acl::getAclFromDescriptor($config->application->securityAccessList);
  63. $activeApp = Router::getActiveApplication();
  64. if(isset($_SESSION['KSEC'][$instanceName][$activeApp]['roleName'])){
  65. self::$_roleName = $_SESSION['KSEC'][$instanceName][$activeApp]['roleName'];
  66. }
  67. }
  68. }
  69. /**
  70. * Devuelve el nombre del rol activo en la sesi?n
  71. *
  72. * @return string
  73. */
  74. static public function getActiveRole(){
  75. if(self::$_roleName!==null){
  76. return self::$_roleName;
  77. } else {
  78. $activeApp = Router::getActiveApplication();
  79. $instanceName = Core::getInstanceName();
  80. if(isset($_SESSION['KSEC'][$instanceName][$activeApp]['roleName'])){
  81. self::$_roleName = $_SESSION['KSEC'][$instanceName][$activeApp]['roleName'];
  82. return self::$_roleName = $_SESSION['KSEC'][$instanceName][$activeApp]['roleName'];
  83. } else {
  84. return 'Public';
  85. }
  86. }
  87. }
  88. /**
  89. * Establece el nombre del rol por defecto
  90. *
  91. * @param string $roleName
  92. */
  93. static public function setDefaultRole($roleName){
  94. self::$_defaultRoleName = $roleName;
  95. }
  96. /**
  97. * Devuelve el nombre del rol por defecto
  98. *
  99. * @return string
  100. * @static
  101. */
  102. static public function getDefaultRole(){
  103. return self::$_defaultRoleName;
  104. }
  105. /**
  106. * Establece el ? los roles del usuario activo en la aplicacion
  107. *
  108. * @param mixed $roleName
  109. * @static
  110. */
  111. static public function setActiveRole($roleName){
  112. self::$_roleName = $roleName;
  113. $instanceName = Core::getInstanceName();
  114. $_SESSION['KSEC'][$instanceName][$activeApp]['roleName'] = $roleName;
  115. }
  116. /**
  117. * Valida que se tenga acceso al recurso solicitado
  118. *
  119. * @param mixed $resource
  120. * @static
  121. */
  122. static public function checkResourceAccess($resource){
  123. if(self::$_securityAccessList!==null){
  124. if(self::$_securityAccessList->isAllowed(self::getActiveRole(), Router::getController(), Router::getAction())==false){
  125. Router::routeTo(array('action' => 'unauthorizedAccess'));
  126. }
  127. }
  128. }
  129. /**
  130. * Genera un INPUT tipo hidden con una llave unica utilizada
  131. * para comprobaciones de validez en transacciones AJAX
  132. *
  133. * @param boolean $kumbia
  134. * @return string
  135. */
  136. static public function generateRSAKey($kumbia){
  137. $h = date("G")>12 ? 1 : 0;
  138. $time = uniqid().mktime($h, 0, 0, date("m"), date("d"), date("Y"));
  139. $key = sha1($time);
  140. $_SESSION['rsa_key'] = $key;
  141. $xCode = "<input type='hidden' id='rsa32_key' value='$key' />\r\n";
  142. if($kumbia){
  143. formsPrint($xCode);
  144. } else {
  145. return $xCode;
  146. }
  147. return "";
  148. }
  149. /**
  150. * Crea un INPUT tipo hidden con una llave unica utilizada
  151. * para comprobaciones de validez en transacciones AJAX
  152. *
  153. * @param boolean $kumbia
  154. * @return string
  155. */
  156. static public function createSecureRSAKey($kumbia=true){
  157. $config = CoreConfig::getInstanceConfig();
  158. if($config->kumbia->secure_ajax){
  159. if($_SESSION['rsa_key']){
  160. if((time()%8)==0){
  161. return generateRSAKey($kumbia);
  162. } else {
  163. if($kumbia){
  164. formsPrint("<input type='hidden' id='rsa32_key' value=\"".$_SESSION['rsa_key']."\"/> \r\n");
  165. } else {
  166. echo "<input type='hidden' id='rsa32_key' value=\"".$_SESSION['rsa_key']."\"/> \r\n";
  167. }
  168. }
  169. } else {
  170. return generateRSAKey($kumbia);
  171. }
  172. }
  173. return null;
  174. }
  175. /**
  176. * Calcula un porcentaje de fortaleza de un password
  177. *
  178. * @param string $password
  179. * @return unknown
  180. */
  181. static public function passwordStrength($password){
  182. if($password==''){
  183. return 0;
  184. }
  185. $failTests = 0;
  186. $length = i18n::strlen($password);
  187. if($length<8){
  188. ++$failTests;
  189. }
  190. if(i18n::strtolower($password)==$password){
  191. ++$failTests;
  192. }
  193. if(i18n::strtoupper($password)==$password){
  194. ++$failTests;
  195. }
  196. if(preg_match('/[0-9]/', $password)==false){
  197. ++$failTests;
  198. }
  199. if(preg_match('/\W/', $password)==false){
  200. ++$failTests;
  201. }
  202. if(preg_match('/[bcdfghijklmnpqrstvwxyz][aeiou]/i', $password)){
  203. ++$failTests;
  204. }
  205. $eachchar = str_split($password);
  206. if(count(array_unique($eachchar))<5){
  207. ++$failTests;
  208. }
  209. return 1-($failTests/7);
  210. }
  211. }