/extensions/OpenStackManager/special/SpecialNovaKey.php

https://github.com/ChuguluGames/mediawiki-svn · PHP · 272 lines · 217 code · 30 blank · 25 comment · 30 complexity · 02ce16a572b211331b26e13b6d7c915e MD5 · raw file

  1. <?php
  2. class SpecialNovaKey extends SpecialNova {
  3. var $userNova;
  4. /**
  5. * @var OpenStackNovaUser
  6. */
  7. var $userLDAP;
  8. function __construct() {
  9. parent::__construct( 'NovaKey' );
  10. }
  11. function execute( $par ) {
  12. global $wgRequest, $wgUser;
  13. if ( ! $wgUser->isLoggedIn() ) {
  14. $this->notLoggedIn();
  15. return true;
  16. }
  17. $this->userLDAP = new OpenStackNovaUser();
  18. if ( ! $this->userLDAP->exists() ) {
  19. $this->noCredentials();
  20. return true;
  21. }
  22. $action = $wgRequest->getVal( 'action' );
  23. if ( $action == "import" ) {
  24. $this->importKey();
  25. } elseif ( $action == "delete" ) {
  26. $this->deleteKey();
  27. } else {
  28. $this->listKeys();
  29. }
  30. }
  31. /**
  32. * @return bool
  33. */
  34. function importKey() {
  35. global $wgRequest, $wgOut;
  36. global $wgOpenStackManagerNovaKeypairStorage;
  37. $project = '';
  38. if ( $wgOpenStackManagerNovaKeypairStorage == 'nova' ) {
  39. $project = $wgRequest->getVal( 'project' );
  40. if ( $project && ! $this->userLDAP->inProject( $project ) ) {
  41. $this->notInProject();
  42. return true;
  43. }
  44. $userCredentials = $this->userLDAP->getCredentials( $project );
  45. $this->userNova = new OpenStackNovaController( $userCredentials );
  46. }
  47. $this->setHeaders();
  48. $wgOut->setPagetitle( wfMsg( 'openstackmanager-importkey' ) );
  49. $keyInfo = array();
  50. if ( $wgOpenStackManagerNovaKeypairStorage == 'nova' ) {
  51. $keyInfo['keyname'] = array(
  52. 'type' => 'text',
  53. 'label-message' => 'openstackmanager-novakeyname',
  54. 'default' => '',
  55. 'section' => 'key/info',
  56. 'name' => 'keyname',
  57. );
  58. }
  59. $keyInfo['key'] = array(
  60. 'type' => 'textarea',
  61. 'section' => 'key/info',
  62. 'default' => '',
  63. 'label-message' => 'openstackmanager-novapublickey',
  64. 'name' => 'key',
  65. );
  66. $keyInfo['action'] = array(
  67. 'type' => 'hidden',
  68. 'default' => 'import',
  69. 'name' => 'action',
  70. );
  71. $keyInfo['project'] = array(
  72. 'type' => 'hidden',
  73. 'default' => htmlentities( $project ),
  74. 'name' => 'project',
  75. );
  76. $keyForm = new SpecialNovaKeyForm( $keyInfo, 'openstackmanager-novakey' );
  77. $keyForm->setTitle( SpecialPage::getTitleFor( 'NovaKey' ) );
  78. $keyForm->setSubmitID( 'novakey-form-createkeysubmit' );
  79. $keyForm->setSubmitCallback( array( $this, 'tryImportSubmit' ) );
  80. $keyForm->show();
  81. }
  82. /**
  83. * @return bool
  84. */
  85. function deleteKey() {
  86. global $wgOut, $wgRequest;
  87. global $wgOpenStackManagerNovaKeypairStorage;
  88. $this->setHeaders();
  89. $wgOut->setPagetitle( wfMsg( 'openstackmanager-deletekey' ) );
  90. $keyInfo = array();
  91. $hash = '';
  92. $keypairs = array();
  93. if ( $wgOpenStackManagerNovaKeypairStorage == 'nova' ) {
  94. $keyname = $wgRequest->getVal( 'keyname' );
  95. $project = $wgRequest->getVal( 'project' );
  96. if ( $project && ! $this->userLDAP->inProject( $project ) ) {
  97. $this->notInProject();
  98. return true;
  99. }
  100. $keyInfo['keyname'] = array(
  101. 'type' => 'hidden',
  102. 'default' => $project,
  103. 'name' => 'keyname',
  104. );
  105. $keyInfo['project'] = array(
  106. 'type' => 'hidden',
  107. 'default' => $keyname,
  108. 'name' => 'project',
  109. );
  110. } elseif ( $wgOpenStackManagerNovaKeypairStorage == 'ldap' ) {
  111. $hash = $wgRequest->getVal( 'hash' );
  112. $keypairs = $this->userLDAP->getKeypairs();
  113. if ( ! $wgRequest->wasPosted() ) {
  114. $wgOut->addHTML( Html::element( 'pre', array(), $keypairs[$hash] ) );
  115. $wgOut->addWikiMsg( 'openstackmanager-deletekeyconfirm' );
  116. }
  117. $keyInfo['hash'] = array(
  118. 'type' => 'hidden',
  119. 'default' => $hash,
  120. 'name' => 'hash',
  121. );
  122. }
  123. $keyInfo['key'] = array(
  124. 'type' => 'hidden',
  125. 'default' => $keypairs[$hash],
  126. 'name' => 'key',
  127. );
  128. $keyInfo['action'] = array(
  129. 'type' => 'hidden',
  130. 'default' => 'delete',
  131. 'name' => 'action',
  132. );
  133. $keyForm = new SpecialNovaKeyForm( $keyInfo, 'openstackmanager-novakey' );
  134. $keyForm->setTitle( SpecialPage::getTitleFor( 'NovaKey' ) );
  135. $keyForm->setSubmitID( 'novakey-form-deletekeysubmit' );
  136. $keyForm->setSubmitCallback( array( $this, 'tryDeleteSubmit' ) );
  137. $keyForm->show();
  138. return true;
  139. }
  140. /**
  141. * @return void
  142. */
  143. function listKeys() {
  144. global $wgOut;
  145. global $wgOpenStackManagerNovaKeypairStorage;
  146. $this->setHeaders();
  147. $wgOut->setPagetitle( wfMsg( 'openstackmanager-keylist' ) );
  148. $out = '';
  149. $sk = $wgOut->getSkin();
  150. if ( $wgOpenStackManagerNovaKeypairStorage == 'nova' ) {
  151. $out .= $sk->link( $this->getTitle(), wfMsgHtml( 'openstackmanager-importkey' ), array(), array( 'action' => 'import' ) );
  152. $projects = $this->userLDAP->getProjects();
  153. foreach ( $projects as $project ) {
  154. $userCredentials = $this->userLDAP->getCredentials( $project );
  155. $this->userNova = new OpenStackNovaController( $userCredentials );
  156. $keypairs = $this->userNova->getKeypairs();
  157. if ( ! $keypairs ) {
  158. continue;
  159. }
  160. $out .= Html::element( 'h2', array(), $project );
  161. $projectOut = Html::element( 'th', array(), wfMsg( 'openstackmanager-name' ) );
  162. $projectOut .= Html::element( 'th', array(), wfMsg( 'openstackmanager-fingerprint' ) );
  163. foreach ( $keypairs as $keypair ) {
  164. $keyOut = Html::element( 'td', array(), $keypair->getKeyName() );
  165. $keyOut .= Html::element( 'td', array(), $keypair->getKeyFingerprint() );
  166. $projectOut .= Html::rawElement( 'tr', array(), $keyOut );
  167. }
  168. $out .= Html::rawElement( 'table', array( 'id' => 'novakeylist', 'class' => 'wikitable sortable collapsible' ), $projectOut );
  169. }
  170. } elseif ( $wgOpenStackManagerNovaKeypairStorage == 'ldap' ) {
  171. $out .= $sk->link( $this->getTitle(), wfMsgHtml( 'openstackmanager-importkey' ), array(), array( 'action' => 'import' ) );
  172. $keypairs = $this->userLDAP->getKeypairs();
  173. $keysOut = '';
  174. $keysOut .= Html::element( 'th', array(), wfMsg( 'openstackmanager-keys' ) );
  175. $keysOut .= Html::element( 'th', array(), wfMsg( 'openstackmanager-actions' ) );
  176. foreach ( $keypairs as $hash => $key ) {
  177. $keyOut = Html::element( 'td', array(), $key );
  178. $msg = wfMsgHtml( 'openstackmanager-delete' );
  179. $link = $sk->link( $this->getTitle(), $msg, array(), array( 'action' => 'delete', 'hash' => $hash ) );
  180. $action = Html::rawElement( 'li', array(), $link );
  181. $action = Html::rawElement( 'ul', array(), $action );
  182. $keyOut .= Html::rawElement( 'td', array(), $action );
  183. $keysOut .= Html::rawElement( 'tr', array(), $keyOut );
  184. }
  185. $out .= Html::rawElement( 'table', array( 'id' => 'novakeylist', 'class' => 'wikitable' ), $keysOut );
  186. } else {
  187. $wgOut->addWikiMsg( 'openstackmanager-invalidkeypair' );
  188. }
  189. $wgOut->addHTML( $out );
  190. }
  191. /**
  192. * @param $formData
  193. * @param string $entryPoint
  194. * @return bool
  195. */
  196. function tryImportSubmit( $formData, $entryPoint = 'internal' ) {
  197. global $wgOut;
  198. global $wgOpenStackManagerNovaKeypairStorage;
  199. if ( $wgOpenStackManagerNovaKeypairStorage == 'ldap' ) {
  200. $success = $this->userLDAP->importKeypair( $formData['key'] );
  201. if ( ! $success ) {
  202. $wgOut->addWikiMsg( 'openstackmanager-keypairimportfailed' );
  203. return true;
  204. }
  205. $wgOut->addWikiMsg( 'openstackmanager-keypairimported' );
  206. } elseif ( $wgOpenStackManagerNovaKeypairStorage == 'nova' ) {
  207. # wgOpenStackManagerNovaKeypairStorage == 'nova'
  208. # OpenStack's EC2 API doesn't yet support importing keys, use
  209. # of this option isn't currently recommended
  210. $keypair = $this->userNova->importKeypair( $formData['keyname'], $formData['key'] );
  211. $wgOut->addWikiMsg( 'openstackmanager-keypairimportedfingerprint', $keypair->getKeyName(), $keypair->getKeyFingerprint() );
  212. } else {
  213. $wgOut->addWikiMsg( 'openstackmanager-invalidkeypair' );
  214. }
  215. $out = '<br />';
  216. $sk = $wgOut->getSkin();
  217. $out .= $sk->link( $this->getTitle(), wfMsgHtml( 'openstackmanager-backkeylist' ) );
  218. $wgOut->addHTML( $out );
  219. return true;
  220. }
  221. /**
  222. * @param $formData
  223. * @param string $entryPoint
  224. * @return bool
  225. */
  226. function tryDeleteSubmit( $formData, $entryPoint = 'internal' ) {
  227. global $wgOut;
  228. $success = $this->userLDAP->deleteKeypair( $formData['key'] );
  229. if ( $success ) {
  230. $wgOut->addWikiMsg( 'openstackmanager-deletedkey' );
  231. } else {
  232. $wgOut->addWikiMsg( 'openstackmanager-deletedkeyfailed' );
  233. }
  234. $out = '<br />';
  235. $sk = $wgOut->getSkin();
  236. $out .= $sk->link( $this->getTitle(), wfMsgHtml( 'openstackmanager-backkeylist' ) );
  237. $wgOut->addHTML( $out );
  238. return true;
  239. }
  240. }
  241. class SpecialNovaKeyForm extends HTMLForm {
  242. }