PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/library/My/Plugin/AclPlugin.php

https://github.com/mineirim/observer
PHP | 138 lines | 112 code | 7 blank | 19 comment | 24 complexity | a5c27351ca56cce3cf8e623fa54090d7 MD5 | raw file
  1. <?php
  2. class My_Plugin_AclPlugin extends Zend_Controller_Plugin_Abstract {
  3. /**
  4. * @var mixed
  5. */
  6. private $_auth;
  7. /**
  8. * @param $token
  9. */
  10. private function tokenValidate($request, $token) {
  11. $this->validateSystem($request);
  12. $token = base64_decode($token);
  13. if (strpos(substr($token, 0, strlen(base64_encode(date('Ymd')))), base64_encode(date('Ymd'))) === 0) {
  14. $email = base64_decode(substr($token, strlen(base64_encode(date('Ymd')))));
  15. $this->authentication($email);
  16. } else {
  17. throw new Exception('Token inválido');
  18. }
  19. }
  20. /**
  21. * @param $request
  22. * @return null
  23. */
  24. private function validateSystem($request) {
  25. $request->setParam('format', 'json');
  26. $sistemasModel = new Data_Model_DbTable_Sistemas();
  27. $systoken = $request->getHeader('systoken');
  28. if (!$systoken) {
  29. throw new Zend_Exception('O sistema não está cadastrado/autorizado no SISPLAN', 1);
  30. } else {
  31. $sistema = $sistemasModel->fetchRow(['chave=?' => $systoken]);
  32. if (count($sistema) === 0) {
  33. throw new Zend_Exception('O sistema não está cadastrado/autorizado no SISPLAN', 1);
  34. } else {
  35. \Zend_Registry::set('sistema', $sistema);
  36. }
  37. }
  38. }
  39. /**
  40. * @param $email
  41. */
  42. private function authentication($email, $cpf = '') {
  43. $db = Zend_Registry::get('db');
  44. $authadapter = new \Zend_Auth_Adapter_DbTable($db);
  45. // Assign the authentication informations to the adapter
  46. // try
  47. if ($cpf !== '') {
  48. $authadapter->setTableName('usuarios')
  49. ->setIdentityColumn('cpf')
  50. ->setCredentialColumn('cpf')
  51. ->setCredentialTreatment('? AND situacao_id=1');
  52. $authadapter->setIdentity($cpf)->setCredential($cpf);
  53. } else {
  54. $authadapter->setTableName('usuarios')
  55. ->setIdentityColumn('email')
  56. ->setCredentialColumn('email')
  57. ->setCredentialTreatment('? AND situacao_id=1');
  58. $authadapter->setIdentity($email)->setCredential($email);
  59. }
  60. $auth = \Zend_Auth::getInstance();
  61. $auth->clearIdentity();
  62. $auth->getStorage()->clear();
  63. $result = $authadapter->authenticate(); // $auth->authenticate ($authadapter);
  64. if ($result->isValid()) {
  65. $nome = getenv('Shib-inetOrgPerson-cn') ? getenv('Shib-inetOrgPerson-cn') : 'not shib';
  66. $auth->getStorage()->write($authadapter->getResultRowObject(null, ['senha', 'salt']));
  67. \Etc\Tools::auditLog(['url' => 'login', 'http_method' => 'POST',
  68. 'data_log' => '{"nome":"' . $nome . '", "cpf":"'.$cpf.'"}']);
  69. } else {
  70. if (getenv('Shib-brPerson-brPersonCPF') ) {
  71. $data = ['nome' => getenv('Shib-inetOrgPerson-cn'),
  72. 'email' => getenv('Shib-inetOrgPerson-mail'),
  73. 'usuario' => getenv('Shib-brPerson-brPersonCPF'),
  74. 'cpf' => getenv('Shib-brPerson-brPersonCPF'),
  75. 'situacao_id' => 3,
  76. ];
  77. $usuariosModel = new Data_Model_Usuarios;
  78. $usuariosModel->addUsuario($data);
  79. }
  80. error_log('nao autorizado: ' . $email . ' - cpf: ' . $cpf . PHP_EOL);
  81. throw new \Exception($result->getMessages()[0]);
  82. }
  83. }
  84. /**
  85. * @param Zend_Controller_Request_Http $request
  86. * @return null
  87. */
  88. public function preDispatch(Zend_Controller_Request_Abstract $request) {
  89. $auth_token = $request->getHeader('authtoken');
  90. $shib_cpf = getenv('Shib-brPerson-brPersonCPF');
  91. $shib_mail = getenv('Shib-inetOrgPerson-mail');
  92. $this->_auth = \Zend_Auth::getInstance();
  93. try {
  94. if (!$this->_auth->hasIdentity()) {
  95. if ($shib_cpf) {
  96. $this->authentication(null, $shib_cpf);
  97. }
  98. }
  99. } catch (Exception $e) {
  100. }
  101. $module = $request->getModuleName();
  102. if ($module == 'acesso' || $module == 'default') {
  103. $action = $request->getActionName();
  104. if ($action == 'get-token') {
  105. $this->validateSystem($request);
  106. }
  107. parent::preDispatch($request);
  108. } else {
  109. $controller = $action = '';
  110. if ($auth_token) {
  111. $this->tokenValidate($request, $auth_token);
  112. parent::preDispatch($request);
  113. return;
  114. }
  115. $this->_auth = Zend_Auth::getInstance();
  116. if (!$this->_auth->hasIdentity()) {
  117. if ($shib_cpf) {
  118. $this->authentication(null, $shib_cpf);
  119. parent::preDispatch($request);
  120. } else {
  121. $module = 'acesso';
  122. $controller = 'index';
  123. $action = 'index';
  124. $request->setModuleName($module);
  125. $request->setControllerName($controller);
  126. $request->setActionName($action);
  127. }
  128. }
  129. }
  130. }
  131. }