/lib/Modules/admin/controllers/AuthenticationController.php

https://github.com/caphrim007/nessquik · PHP · 318 lines · 252 code · 62 blank · 4 comment · 39 complexity · aae8743e249cffc1e11745b327b721b9 MD5 · raw file

  1. <?php
  2. /**
  3. * @author Tim Rupp
  4. */
  5. class Admin_AuthenticationController extends Zend_Controller_Action {
  6. public $session;
  7. const IDENT = __CLASS__;
  8. public function init() {
  9. parent::init();
  10. $config = Ini_Config::getInstance();
  11. $auth = Zend_Auth::getInstance();
  12. $request = $this->getRequest();
  13. $sessionUser = $auth->getIdentity();
  14. $sessionId = Account_Util::getId($sessionUser);
  15. $this->session = new Account($sessionId);
  16. if ($this->session->isFirstBoot()) {
  17. $this->_redirector = $this->_helper->getHelper('Redirector');
  18. $this->_redirector->gotoSimple('index', 'index', 'start');
  19. }
  20. if (!$this->session->acl->isAllowed('Capability', array('admin_operator', 'edit_auth'))) {
  21. $this->_redirector = $this->_helper->getHelper('Redirector');
  22. $this->_redirector->gotoSimple('permission-denied', 'error', 'default');
  23. }
  24. $this->view->assign(array(
  25. 'action' => $request->getActionName(),
  26. 'config' => $config,
  27. 'controller' => $request->getControllerName(),
  28. 'module' => $request->getModuleName(),
  29. 'session' => $this->session
  30. ));
  31. }
  32. public function indexAction() {
  33. $log = App_Log::getInstance(self::IDENT);
  34. try {
  35. if (!is_writable(_ABSPATH.'/etc/local/authentication.conf')) {
  36. if (!is_writable(_ABSPATH.'/etc/local/')) {
  37. throw new Zend_Controller_Action_Exception('The location configuration directory is not writable');
  38. }
  39. } else if (file_exists(_ABSPATH.'/etc/local/authentication.conf') && !is_writable(_ABSPATH.'/etc/local/authentication.conf')) {
  40. throw new Zend_Controller_Action_Exception('The local authentication config file exists but is not writable');
  41. } else if (!is_writable(_ABSPATH.'/etc/local/authentication.conf')) {
  42. throw new Zend_Controller_Action_Exception('The local authentication config file is not writable');
  43. }
  44. $isWritable = true;
  45. } catch (Exception $error) {
  46. $isWritable = false;
  47. $message = $error->getMessage();
  48. $log->err($message);
  49. }
  50. $this->view->assign(array(
  51. 'isWritable' => $isWritable,
  52. ));
  53. }
  54. public function searchAction() {
  55. $auth = Ini_Authentication::getInstance();
  56. $this->view->assign(array(
  57. 'auth' => $auth
  58. ));
  59. }
  60. public function editAction() {
  61. $isNew = false;
  62. $auth = Ini_Authentication::getInstance();
  63. $request = $this->getRequest();
  64. $request->setParamSources(array('_GET'));
  65. $id = $request->getParam('id');
  66. if ($id == '_new') {
  67. $uuid = UUID::generate(UUID::UUID_RANDOM, UUID::FMT_STRING);
  68. $isNew = true;
  69. } else {
  70. $uuid = $id;
  71. }
  72. if (empty($uuid)) {
  73. throw new Zend_Controller_Action_Exception('The UUID provided to the controller was empty');
  74. } else {
  75. if (isset($auth->auth->$uuid)) {
  76. $info = $auth->auth->$uuid;
  77. }
  78. if (empty($info)) {
  79. $info = new Zend_Config(array());
  80. }
  81. }
  82. $this->view->assign(array(
  83. 'id' => $uuid,
  84. 'info' => $info,
  85. 'isNew' => $isNew
  86. ));
  87. }
  88. public function orderAction() {
  89. $status = false;
  90. $message = null;
  91. $result = array();
  92. $request = $this->getRequest();
  93. $request->setParamSources(array('_POST'));
  94. $methods = $request->getParam('order');
  95. $log = App_Log::getInstance(self::IDENT);
  96. $auth = Ini_Authentication::getInstance();
  97. try {
  98. if (!is_writable(_ABSPATH.'/etc/local/authentication.conf')) {
  99. if (!is_writable(_ABSPATH.'/etc/local/')) {
  100. throw new Zend_Controller_Action_Exception('The location configuration directory is not writable');
  101. }
  102. } else if (file_exists(_ABSPATH.'/etc/local/authentication.conf') && !is_writable(_ABSPATH.'/etc/local/authentication.conf')) {
  103. throw new Zend_Controller_Action_Exception('The local authentication config file exists but is not writable');
  104. } else if (!is_writable(_ABSPATH.'/etc/local/authentication.conf')) {
  105. throw new Zend_Controller_Action_Exception('The local authentication config file is not writable');
  106. }
  107. foreach($methods as $key => $method) {
  108. $auth->auth->$method->priority = $key;
  109. $result['production']['auth'][$method] = $auth->auth->$method->toArray();
  110. }
  111. $config = new Zend_Config($result);
  112. $writer = new Zend_Config_Writer_Ini(array(
  113. 'config' => $config,
  114. 'filename' => _ABSPATH.'/etc/local/authentication.conf'
  115. ));
  116. $writer->write();
  117. $status = true;
  118. } catch (Exception $error) {
  119. $status = false;
  120. $message = $error->getMessage();
  121. $log->err($message);
  122. }
  123. $this->view->response = array(
  124. 'status' => $status,
  125. 'message' => $message
  126. );
  127. }
  128. public function deleteAction() {
  129. $status = false;
  130. $message = null;
  131. $priority = 0;
  132. $request = $this->getRequest();
  133. $request->setParamSources(array('_POST'));
  134. $id = $request->getParam('authenticationId');
  135. $log = App_Log::getInstance(self::IDENT);
  136. $auth = Ini_Authentication::getInstance()->toArray();
  137. try {
  138. if (!is_writable(_ABSPATH.'/etc/local/authentication.conf')) {
  139. if (!is_writable(_ABSPATH.'/etc/local/')) {
  140. throw new Zend_Controller_Action_Exception('The location configuration directory is not writable');
  141. }
  142. } else if (file_exists(_ABSPATH.'/etc/local/authentication.conf') && !is_writable(_ABSPATH.'/etc/local/authentication.conf')) {
  143. throw new Zend_Controller_Action_Exception('The local authentication config file exists but is not writable');
  144. } else if (!is_writable(_ABSPATH.'/etc/local/authentication.conf')) {
  145. throw new Zend_Controller_Action_Exception('The local authentication config file is not writable');
  146. }
  147. foreach($auth['auth'] as $key => $method) {
  148. if ($key == $id) {
  149. continue;
  150. } else {
  151. $auth['auth'][$key]['priority'] = $priority;
  152. $result['production']['auth'][$key] = $auth['auth'][$key];
  153. // For sanity's sake, reset the priorities
  154. $priority++;
  155. }
  156. }
  157. if (empty($result)) {
  158. $result = array();
  159. }
  160. $config = new Zend_Config($result);
  161. $writer = new Zend_Config_Writer_Ini(array(
  162. 'config' => $config,
  163. 'filename' => _ABSPATH.'/etc/local/authentication.conf'
  164. ));
  165. $writer->write();
  166. $status = true;
  167. } catch (Exception $error) {
  168. $status = false;
  169. $message = $error->getMessage();
  170. $log->err($message);
  171. }
  172. $this->view->response = array(
  173. 'status' => $status,
  174. 'message' => $message
  175. );
  176. }
  177. public function saveAction() {
  178. $status = false;
  179. $message = null;
  180. $log = App_Log::getInstance(self::IDENT);
  181. $auth = Ini_Authentication::getInstance()->toArray();
  182. $request = $this->getRequest();
  183. $request->setParamSources(array('_POST'));
  184. $params = $request->getParams();
  185. try {
  186. switch($params['auth-type']) {
  187. case 'Array':
  188. if (empty($params['username'])) {
  189. throw new Zend_Controller_Action_Exception('The username for the Array adapter cannot be empty');
  190. }
  191. $config = $this->_helper->CreateArrayAuth($params);
  192. break;
  193. case 'Cert':
  194. $config = $this->_helper->CreateCertAuth($params);
  195. break;
  196. case 'DbTable':
  197. $config = $this->_helper->CreateDbTableAuth($params);
  198. break;
  199. case 'Ldap':
  200. if (isset($params['bindRequiresDn']) && empty($params['username'])) {
  201. throw new Zend_Controller_Action_Exception('The username cannot be empty if binding requires a DN');
  202. }
  203. if (empty($params['baseDn'])) {
  204. throw new Zend_Controller_Action_Exception('The base Dn cannot be empty');
  205. }
  206. $config = $this->_helper->CreateLdapAuth($params);
  207. break;
  208. default:
  209. $config = null;
  210. break;
  211. }
  212. if (is_array($config)) {
  213. $oldAuth = new Zend_Config($auth, true);
  214. $newAuth = new Zend_Config($config);
  215. $oldAuth->merge($newAuth);
  216. $config = array(
  217. 'production' => $oldAuth->toArray()
  218. );
  219. $config = new Zend_Config($config);
  220. $writer = new Zend_Config_Writer_Ini(array(
  221. 'config' => $config,
  222. 'filename' => _ABSPATH.'/etc/local/authentication.conf'
  223. ));
  224. $writer->write();
  225. $status = true;
  226. }
  227. } catch (Exception $error) {
  228. $status = false;
  229. $message = $error->getMessage();
  230. $log->err($message);
  231. }
  232. $this->view->response = array(
  233. 'status' => $status,
  234. 'message' => $message
  235. );
  236. }
  237. public function testLdapAction() {
  238. $status = false;
  239. $message = null;
  240. $log = App_Log::getInstance(self::IDENT);
  241. $params = array('test' => array());
  242. try {
  243. $adapter = new Zend_Auth_Adapter_Ldap($params, $username, $password);
  244. $status = $adapter->authenticate();
  245. } catch (Exception $error) {
  246. $status = false;
  247. $message = $error->getMessage();
  248. $log->err($message);
  249. }
  250. $this->view->response = array(
  251. 'status' => $status,
  252. 'message' => $message
  253. );
  254. }
  255. }
  256. ?>