PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/src/plugins/cloud/web/cloud-user.php

https://github.com/qyjohn/openqrm
PHP | 428 lines | 365 code | 34 blank | 29 comment | 25 complexity | 898237ac1168f2f9148ba497e4cddee1 MD5 | raw file
  1. <SCRIPT LANGUAGE="JavaScript">
  2. <!-- Original: ataxx@visto.com -->
  3. function getRandomNum(lbound, ubound) {
  4. return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
  5. }
  6. function getRandomChar(number, lower, upper, other, extra) {
  7. var numberChars = "0123456789";
  8. var lowerChars = "abcdefghijklmnopqrstuvwxyz";
  9. var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  10. var otherChars = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";
  11. var charSet = extra;
  12. if (number == true)
  13. charSet += numberChars;
  14. if (lower == true)
  15. charSet += lowerChars;
  16. if (upper == true)
  17. charSet += upperChars;
  18. if (other == true)
  19. charSet += otherChars;
  20. return charSet.charAt(getRandomNum(0, charSet.length));
  21. }
  22. function getPassword(length, extraChars, firstNumber, firstLower, firstUpper, firstOther, latterNumber, latterLower, latterUpper, latterOther) {
  23. var rc = "";
  24. if (length > 0)
  25. rc = rc + getRandomChar(firstNumber, firstLower, firstUpper, firstOther, extraChars);
  26. for (var idx = 1; idx < length; ++idx) {
  27. rc = rc + getRandomChar(latterNumber, latterLower, latterUpper, latterOther, extraChars);
  28. }
  29. return rc;
  30. }
  31. function statusMsg(msg) {
  32. window.status=msg;
  33. return true;
  34. }
  35. </script>
  36. <link rel="stylesheet" type="text/css" href="../../css/htmlobject.css" />
  37. <?php
  38. /*
  39. This file is part of openQRM.
  40. openQRM is free software: you can redistribute it and/or modify
  41. it under the terms of the GNU General Public License version 2
  42. as published by the Free Software Foundation.
  43. openQRM is distributed in the hope that it will be useful,
  44. but WITHOUT ANY WARRANTY; without even the implied warranty of
  45. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  46. GNU General Public License for more details.
  47. You should have received a copy of the GNU General Public License
  48. along with openQRM. If not, see <http://www.gnu.org/licenses/>.
  49. Copyright 2009, Matthias Rechenburg <matt@openqrm.com>
  50. */
  51. // error_reporting(E_ALL);
  52. $thisfile = basename($_SERVER['PHP_SELF']);
  53. $RootDir = $_SERVER["DOCUMENT_ROOT"].'/openqrm/base/';
  54. $BaseDir = $_SERVER["DOCUMENT_ROOT"].'/openqrm/';
  55. $CloudDir = $_SERVER["DOCUMENT_ROOT"].'/cloud-portal/';
  56. require_once "$RootDir/include/user.inc.php";
  57. require_once "$RootDir/class/image.class.php";
  58. require_once "$RootDir/class/resource.class.php";
  59. require_once "$RootDir/class/virtualization.class.php";
  60. require_once "$RootDir/class/appliance.class.php";
  61. require_once "$RootDir/class/deployment.class.php";
  62. require_once "$RootDir/class/openqrm_server.class.php";
  63. require_once "$RootDir/include/htmlobject.inc.php";
  64. // special clouduser class
  65. require_once "$RootDir/plugins/cloud/class/clouduser.class.php";
  66. require_once "$RootDir/plugins/cloud/class/cloudusergroup.class.php";
  67. require_once "$RootDir/plugins/cloud/class/clouduserslimits.class.php";
  68. require_once "$RootDir/plugins/cloud/class/cloudconfig.class.php";
  69. global $OPENQRM_SERVER_BASE_DIR;
  70. $refresh_delay=5;
  71. $openqrm_server = new openqrm_server();
  72. $OPENQRM_SERVER_IP_ADDRESS=$openqrm_server->get_ip_address();
  73. global $OPENQRM_SERVER_IP_ADDRESS;
  74. global $OPENQRM_WEB_PROTOCOL;
  75. // if ldap is enabled do not allow access the the openQRM cloud user administration
  76. $central_user_management = false;
  77. if (file_exists("$RootDir/plugins/ldap/.running")) {
  78. $central_user_management = true;
  79. }
  80. // check if we got some actions to do
  81. if(htmlobject_request('action') != '') {
  82. switch (htmlobject_request('action')) {
  83. case 'delete':
  84. if(isset($_REQUEST['identifier'])) {
  85. foreach($_REQUEST['identifier'] as $id) {
  86. $cl_user = new clouduser();
  87. $cl_user->get_instance_by_id($id);
  88. // remove user from htpasswd
  89. $username = $cl_user->name;
  90. $openqrm_server_command="htpasswd -D $CloudDir/user/.htpasswd $username";
  91. $output = shell_exec($openqrm_server_command);
  92. // remove permissions and limits
  93. $cloud_user_limit = new clouduserlimits();
  94. $cloud_user_limit->remove_by_cu_id($id);
  95. // remove from db
  96. $cl_user->remove($id);
  97. }
  98. }
  99. break;
  100. case 'enable':
  101. if(isset($_REQUEST['identifier'])) {
  102. foreach($_REQUEST['identifier'] as $id) {
  103. $cl_user = new clouduser();
  104. $cl_user->get_instance_by_id($id);
  105. $cl_user->activate_user_status($id, 1);
  106. }
  107. }
  108. break;
  109. case 'disable':
  110. if(isset($_REQUEST['identifier'])) {
  111. foreach($_REQUEST['identifier'] as $id) {
  112. $cl_user = new clouduser();
  113. $cl_user->get_instance_by_id($id);
  114. $cl_user->activate_user_status($id, 0);
  115. }
  116. }
  117. break;
  118. case 'update':
  119. if(isset($_REQUEST['identifier'])) {
  120. foreach($_REQUEST['identifier'] as $id) {
  121. $up_ccunits = $_REQUEST['cu_ccunits'];
  122. $cl_user = new clouduser();
  123. $cl_user->get_instance_by_id($id);
  124. $cl_user->set_users_ccunits($id, $up_ccunits[$id]);
  125. }
  126. }
  127. break;
  128. case 'limit':
  129. // gather user_limits parameter in array
  130. foreach ($_REQUEST as $key => $value) {
  131. if (strncmp($key, "cl_", 3) == 0) {
  132. $user_limits_fields[$key] = $value;
  133. }
  134. }
  135. $cloud_user_id = $_REQUEST['cl_cu_id'];
  136. $cloud_user_limit = new clouduserlimits();
  137. $cloud_user_limit->get_instance_by_cu_id($cloud_user_id);
  138. $cl_id = $cloud_user_limit->id;
  139. $cloud_user_limit->update($cl_id, $user_limits_fields);
  140. // echo "Updated limits for Cloud user $cloud_user_id<br>";
  141. break;
  142. }
  143. }
  144. function cloud_user_manager() {
  145. global $OPENQRM_USER;
  146. global $OPENQRM_SERVER_IP_ADDRESS;
  147. global $OPENQRM_WEB_PROTOCOL;
  148. global $thisfile;
  149. global $central_user_management;
  150. $table = new htmlobject_db_table('cu_id', 'DESC');
  151. $cc_conf = new cloudconfig();
  152. // get external name
  153. $external_portal_name = $cc_conf->get_value(3); // 3 is the external name
  154. if (!strlen($external_portal_name)) {
  155. $external_portal_name = "$OPENQRM_WEB_PROTOCOL://$OPENQRM_SERVER_IP_ADDRESS/cloud-portal";
  156. }
  157. $arHead = array();
  158. $arHead['cu_id'] = array();
  159. $arHead['cu_id']['title'] ='ID';
  160. $arHead['cu_name'] = array();
  161. $arHead['cu_name']['title'] ='Name';
  162. $arHead['cu_forename'] = array();
  163. $arHead['cu_forename']['title'] ='Fore name';
  164. $arHead['cu_lastname'] = array();
  165. $arHead['cu_lastname']['title'] ='Last name';
  166. $arHead['cu_cg_id'] = array();
  167. $arHead['cu_cg_id']['title'] ='Group';
  168. $arHead['cu_email'] = array();
  169. $arHead['cu_email']['title'] ='Email';
  170. $arHead['cu_ccunits'] = array();
  171. $arHead['cu_ccunits']['title'] ='CC-Units';
  172. $arHead['cu_status'] = array();
  173. $arHead['cu_status']['title'] ='Status';
  174. $arBody = array();
  175. // db select
  176. $cl_user_count = 0;
  177. $cl_user = new clouduser();
  178. $user_array = $cl_user->display_overview($table->offset, $table->limit, $table->sort, $table->order);
  179. foreach ($user_array as $index => $cu) {
  180. $cu_status = $cu["cu_status"];
  181. if ($cu_status == 1) {
  182. $status_icon = "<img src=\"/cloud-portal/img/active.png\">";
  183. } else {
  184. $status_icon = "<img src=\"/cloud-portal/img/inactive.png\">";
  185. }
  186. // set the ccunits input
  187. $ccunits = $cu["cu_ccunits"];
  188. if (!strlen($ccunits)) {
  189. $ccunits = 0;
  190. }
  191. $cu_id = $cu["cu_id"];
  192. $ccunits_input = "<input type=\"text\" name=\"cu_ccunits[$cu_id]\" value=\"$ccunits\" size=\"5\ maxsize=\"10\">";
  193. // user login link
  194. $tclu = new clouduser();
  195. $tclu->get_instance_by_id($cu_id);
  196. $user_auth_str = "://".$tclu->name.":".$tclu->password."@";
  197. $external_portal_user_auth = str_replace("://", $user_auth_str, $external_portal_name);
  198. $user_login_link = "<a href=\"".$external_portal_user_auth."/user/mycloud.php\" title=\"Login\" target=\"_BLANK\" onmouseover=\"return statusMsg('')\">".$tclu->name."</a>";
  199. // group
  200. $cloudusergroup = new cloudusergroup();
  201. $cloudusergroup->get_instance_by_id($cu["cu_cg_id"]);
  202. $cg_name = $cloudusergroup->name;
  203. $arBody[] = array(
  204. 'cu_id' => $cu["cu_id"],
  205. 'cu_name' => $user_login_link,
  206. 'cu_forename' => $cu["cu_forename"],
  207. 'cu_lastname' => $cu["cu_lastname"],
  208. 'cu_cg_id' => $cg_name,
  209. 'cu_email' => $cu["cu_email"],
  210. 'cu_ccunits' => $ccunits_input,
  211. 'cu_status' => $status_icon,
  212. );
  213. $cl_user_count++;
  214. }
  215. $table->id = 'Tabelle';
  216. $table->css = 'htmlobject_table';
  217. $table->border = 1;
  218. $table->cellspacing = 0;
  219. $table->cellpadding = 3;
  220. $table->form_action = $thisfile;
  221. $table->identifier_type = "checkbox";
  222. $table->head = $arHead;
  223. $table->body = $arBody;
  224. if ($OPENQRM_USER->role == "administrator") {
  225. if (!$central_user_management) {
  226. $table->bottom = array('update', 'enable', 'disable', 'limits', 'delete');
  227. } else {
  228. $table->bottom = array('update', 'enable', 'disable', 'limits');
  229. }
  230. $table->identifier = 'cu_id';
  231. }
  232. $table->max = $cl_user->get_count();
  233. if (!$central_user_management) {
  234. $create_user_link = '<a href='.$thisfile.'?action=create>Create new Cloud User</a>';
  235. } else {
  236. $create_user_link = '';
  237. }
  238. //------------------------------------------------------------ set template
  239. $t = new Template_PHPLIB();
  240. $t->debug = false;
  241. $t->setFile('tplfile', './tpl/' . 'cloud-user-manager-tpl.php');
  242. $t->setVar(array(
  243. 'thisfile' => $thisfile,
  244. 'create_user_link' => $create_user_link,
  245. 'external_portal_name' => $external_portal_name,
  246. 'cloud_user_table' => $table->get_string(),
  247. ));
  248. $disp = $t->parse('out', 'tplfile');
  249. return $disp;
  250. }
  251. function cloud_create_user() {
  252. global $OPENQRM_USER;
  253. global $OPENQRM_SERVER_IP_ADDRESS;
  254. global $OPENQRM_WEB_PROTOCOL;
  255. global $thisfile;
  256. $cc_conf = new cloudconfig();
  257. // get external name
  258. $external_portal_name = $cc_conf->get_value(3); // 3 is the external name
  259. if (!strlen($external_portal_name)) {
  260. $external_portal_name = "$OPENQRM_WEB_PROTOCOL://$OPENQRM_SERVER_IP_ADDRESS/cloud-portal";
  261. }
  262. $cu_name = htmlobject_input('cu_name', array("value" => '', "label" => 'User name'), 'text', 20);
  263. // root password input plus generate password button
  264. $generate_pass = "Password&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name=\"cu_password\" type=\"password\" id=\"cu_password\" value=\"\" size=\"10\" maxlength=\"10\">";
  265. $generate_pass .= "<input type=\"button\" name=\"gen\" value=\"generate\" onclick=\"this.form.cu_password.value=getPassword(10, false, true, true, true, false, true, true, true, false);\"><br>";
  266. // the user group select
  267. $cloudusergroup = new cloudusergroup();
  268. $cloudusergroup_list = array();
  269. $cloudusergroup_list_select = array();
  270. $cloudusergroup_list = $cloudusergroup->get_list();
  271. foreach ($cloudusergroup_list as $id => $cg) {
  272. $cloudusergroup_list_select[] = array("value" => $cg['value'], "label" => $cg['label']);
  273. }
  274. $cu_forename = htmlobject_input('cu_forename', array("value" => '', "label" => 'Fore name'), 'text', 50);
  275. $cu_lastname = htmlobject_input('cu_lastname', array("value" => '', "label" => 'Last name'), 'text', 50);
  276. $cu_email = htmlobject_input('cu_email', array("value" => '', "label" => 'Email'), 'text', 50);
  277. $cu_street = htmlobject_input('cu_street', array("value" => '', "label" => 'Street+number'), 'text', 100);
  278. $cu_city = htmlobject_input('cu_city', array("value" => '', "label" => 'City'), 'text', 100);
  279. $cu_country = htmlobject_input('cu_country', array("value" => '', "label" => 'Country'), 'text', 100);
  280. $cu_phone = htmlobject_input('cu_phone', array("value" => '', "label" => 'Phone'), 'text', 100);
  281. //------------------------------------------------------------ set template
  282. $t = new Template_PHPLIB();
  283. $t->debug = false;
  284. $t->setFile('tplfile', './tpl/' . 'cloud-user-create-tpl.php');
  285. $t->setVar(array(
  286. 'cu_name' => $cu_name,
  287. 'generate_pass' => $generate_pass,
  288. 'cu_cg' => htmlobject_select('cu_cg_id', $cloudusergroup_list_select, 'Group'),
  289. 'cu_forename' => $cu_forename,
  290. 'cu_lastname' => $cu_lastname,
  291. 'cu_email' => $cu_email,
  292. 'cu_street' => $cu_street,
  293. 'cu_city' => $cu_city,
  294. 'cu_country' => $cu_country,
  295. 'cu_phone' => $cu_phone,
  296. 'thisfile' => 'cloud-action.php',
  297. 'external_portal_name' => $external_portal_name,
  298. ));
  299. $disp = $t->parse('out', 'tplfile');
  300. return $disp;
  301. }
  302. function cloud_set_user_limits($cloud_user_id) {
  303. global $OPENQRM_USER;
  304. global $thisfile;
  305. $cloud_user = new clouduser();
  306. $cloud_user->get_instance_by_id($cloud_user_id);
  307. $cloud_user_limit = new clouduserlimits();
  308. $cloud_user_limit->get_instance_by_cu_id($cloud_user_id);
  309. $resource_limit = $cloud_user_limit->resource_limit;
  310. $memory_limit = $cloud_user_limit->memory_limit;
  311. $disk_limit = $cloud_user_limit->disk_limit;
  312. $cpu_limit = $cloud_user_limit->cpu_limit;
  313. $network_limit = $cloud_user_limit->network_limit;
  314. $cl_resource_limit = htmlobject_input('cl_resource_limit', array("value" => $resource_limit, "label" => 'Max Resource'), 'text', 20);
  315. $cl_memory_limit = htmlobject_input('cl_memory_limit', array("value" => $memory_limit, "label" => 'Max Memory'), 'text', 20);
  316. $cl_disk_limit = htmlobject_input('cl_disk_limit', array("value" => $disk_limit, "label" => 'Max Disk Space'), 'text', 20);
  317. $cl_cpu_limit = htmlobject_input('cl_cpu_limit', array("value" => $cpu_limit, "label" => 'Max CPU'), 'text', 20);
  318. $cl_network_limit = htmlobject_input('cl_network_limit', array("value" => $network_limit, "label" => 'Max NIC'), 'text', 20);
  319. //------------------------------------------------------------ set template
  320. $t = new Template_PHPLIB();
  321. $t->debug = false;
  322. $t->setFile('tplfile', './tpl/' . 'cloud-user-set-limit-tpl.php');
  323. $t->setVar(array(
  324. 'cloud_user_id' => $cloud_user_id,
  325. 'cu_name' => $cloud_user->name,
  326. 'cl_resource_limit' => $cl_resource_limit,
  327. 'cl_memory_limit' => $cl_memory_limit,
  328. 'cl_disk_limit' => $cl_disk_limit,
  329. 'cl_cpu_limit' => $cl_cpu_limit,
  330. 'cl_network_limit' => $cl_network_limit,
  331. 'thisfile' => $thisfile,
  332. ));
  333. $disp = $t->parse('out', 'tplfile');
  334. return $disp;
  335. }
  336. $output = array();
  337. if(htmlobject_request('action') != '') {
  338. switch (htmlobject_request('action')) {
  339. case 'create':
  340. if (!$central_user_management) {
  341. $output[] = array('label' => 'Create Cloud User', 'value' => cloud_create_user());
  342. }
  343. break;
  344. case 'limits':
  345. if(isset($_REQUEST['identifier'])) {
  346. foreach($_REQUEST['identifier'] as $id) {
  347. $output[] = array('label' => 'Cloud User Limits', 'value' => cloud_set_user_limits($id));
  348. }
  349. }
  350. $output[] = array('label' => 'Cloud User Manager', 'value' => cloud_user_manager());
  351. break;
  352. default:
  353. $output[] = array('label' => 'Cloud User Manager', 'value' => cloud_user_manager());
  354. break;
  355. }
  356. } else {
  357. $output[] = array('label' => 'Cloud User Manager', 'value' => cloud_user_manager());
  358. }
  359. echo htmlobject_tabmenu($output);
  360. ?>