auth_object_linker.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Sloodle object authorization linker.
  4. * Allows authorised objects in SL to delegate authorisation to other objects,
  5. * or allows new objects in SL to initiate their own authorisation.
  6. * (Creates a new entry in the 'sloodle_active_object' DB table.)
  7. *
  8. * @package sloodleclassroom
  9. * @copyright Copyright (c) 2007-8 Sloodle (various contributors)
  10. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  11. *
  12. * @contributor Edmund Edgar
  13. * @contributor Peter R. Bloomfield
  14. *
  15. */
  16. // If fully authorising a new object ('delegating' trust),
  17. // then the following parameters are required:
  18. //
  19. // sloodlecontrollerid = the ID of the controller through which the current object may access Sloodle
  20. // sloodlepwd = the prim password or object-specific session key to authenticate access
  21. // sloodleobjuuid = the UUID of the object being authorised
  22. // sloodleobjname = the name of the object being authorised
  23. // sloodleobjpwd = a password for the new object
  24. // sloodlehttpinurl = an http-in url we can use to talk to the new object
  25. //
  26. // The following parameters are optional:
  27. //
  28. // sloodleobjtype = the type identifier for the object being authorised. Can be overridden later.
  29. //
  30. // With the above information, a new entry is made, indicating that the object is fully authorised.
  31. // The new object can ONLY be authorised against the controller the request is received on.
  32. // If successful, the status code returned is 1, and the data line will contain the authorisation ID of the object which has been authorised.
  33. // If an object needs the user to perform web-authorisation, then it can create an unauthorised entry.
  34. // To do this, the following parameters are required:
  35. //
  36. // sloodleobjuuid = the UUID of the object being authorised
  37. // sloodleobjname = the name of the object being authorised
  38. // sloodleobjpwd = a new password for the object (NOT including its UUID)
  39. //
  40. // The following parameter is optional:
  41. //
  42. // sloodleobjtype = the type identifier for the object. Can be overridden later.
  43. //
  44. // With this information, a new entry is made which is not linked to a particular user account.
  45. // As such, the entry is deemed 'unauthorised' and cannot be used until authorised.
  46. // If successful, status code 1 is returned, and the ID of the active object entry is returned on the data line.
  47. // The object should use this to build a URL to send the user to Sloodle for manual object authorisation.
  48. // Unauthorised entries will expire within 5 minutes and be deleted.
  49. /** Lets Sloodle know we are in a linker script. */
  50. define('SLOODLE_LINKER_SCRIPT', true);
  51. /** Grab the Sloodle/Moodle configuration. */
  52. require_once('../init.php');
  53. /** Include the Sloodle PHP API. */
  54. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  55. // Attempt to authenticate the request
  56. // (only require authentication if controller ID and/or password is set)
  57. $authrequired = (isset($_REQUEST['sloodlecontrollerid']) || isset($_REQUEST['sloodlepwd']));
  58. $sloodle = new SloodleSession();
  59. $request_auth = $sloodle->authenticate_request($authrequired);
  60. // Get the extra parameters
  61. $sloodleobjuuid = $sloodle->request->required_param('sloodleobjuuid');
  62. $sloodleobjname = $sloodle->request->required_param('sloodleobjname');
  63. $sloodleobjpwd = $sloodle->request->required_param('sloodleobjpwd');
  64. $sloodleobjtype = $sloodle->request->optional_param('sloodleobjtype', '');
  65. $sloodlecloneconfig = $sloodle->request->optional_param('sloodlecloneconfig', ''); // uuid of an object whose config we want to clone. combined with a layout id of 0. used for rezzing a mothership from a set
  66. $sloodlehttpinurl = $sloodle->request->optional_param('sloodlehttpinurl','');
  67. // When the set rezzes an item from a layout, it can pass this parameter saying what layout entry the object represented.
  68. // We'll use that to auto-configure the object based on the layout entry configurations.
  69. $sloodlelayoutentryid = $sloodle->request->optional_param('sloodlelayoutentryid',-1,PARAM_INT);
  70. // If the request was authenticated, then the object is being fully authorised.
  71. // Otherwise, it is simply a 'pending' authorisation.
  72. if ($request_auth) {
  73. // If the request is coming from an authorised object, then use that user as the authoriser for this one
  74. $sloodlepwd = $sloodle->request->required_param('sloodlepwd');
  75. $pwdparts = explode('|', $sloodlepwd, 2);
  76. if (count($pwdparts) >= 2 && strlen($pwdparts[0]) == 36) { // Do we have a UUID?
  77. $userid = $sloodle->course->controller->get_authorizing_user($pwdparts[0]);
  78. if ($userid) $sloodle->user->load_user($userid);
  79. }
  80. $httpinpassword = sloodle_random_prim_password();
  81. // Authorise the object on the controller
  82. $authid = $sloodle->course->controller->register_object($sloodleobjuuid, $sloodleobjname, $sloodle->user, $sloodleobjpwd, $httpinpassword, $sloodleobjtype);
  83. $alreadyconfigured = "0";
  84. if ($sloodlelayoutentryid > 0) {
  85. if ($sloodle->course->controller->configure_object_from_layout_entry($authid, $sloodlelayoutentryid)) {
  86. // This flag will tell the rezzer to tell the object that it's already configured
  87. // That way the object will know not to tell the user to configure it.
  88. $alreadyconfigured = "1";
  89. }
  90. } else if ( ($sloodlelayoutentryid == 0) && ($sloodlecloneconfig != '') ) { // use 0 to mean we want to configure based on the parent who authorized us, rather than on a layout. Doing this to make the mothership worked when rezzed by a Sloodle Set, but we may want to do the same kind of thing with Registration Booths etc.
  91. if ($result = $sloodle->course->controller->configure_object_from_parent($authid, $sloodlecloneconfig)) {
  92. // This flag will tell the rezzer to tell the object that it's already configured
  93. // That way the object will know not to tell the user to configure it.
  94. $alreadyconfigured = $result;
  95. $alreadyconfigured = "1";
  96. } else {
  97. $alreadyconfigured = "0";
  98. }
  99. }
  100. if ($authid) {
  101. $sloodle->response->set_status_code(1);
  102. $sloodle->response->set_status_descriptor('OK');
  103. $sloodle->response->add_data_line($authid);
  104. $sloodle->response->add_data_line($alreadyconfigured);
  105. } else {
  106. $sloodle->response->set_status_code(-201);
  107. $sloodle->response->set_status_descriptor('OBJECT_AUTH');
  108. $sloodle->response->add_data_line('Failed to register new active object.');
  109. }
  110. } else {
  111. // Create a new unauthorised entry
  112. $httpinpassword = sloodle_random_prim_password();
  113. $authid = $sloodle->course->controller->register_unauth_object($sloodleobjuuid, $sloodleobjname, $sloodleobjpwd, $sloodleobjtype, null, $sloodlehttpinurl, $httpinpassword);
  114. if ($authid != 0) {
  115. $sloodle->response->set_status_code(1);
  116. $sloodle->response->set_status_descriptor('OK');
  117. $sloodle->response->add_data_line($authid);
  118. $sloodle->response->add_data_line($alreadyconfigured="0");
  119. } else {
  120. $sloodle->response->set_status_code(-201);
  121. $sloodle->response->set_status_descriptor('OBJECT_AUTH');
  122. $sloodle->response->add_data_line('Failed to register new active object.');
  123. }
  124. }
  125. // Render the output
  126. sloodle_debug('<pre>');
  127. $sloodle->response->render_to_output();
  128. sloodle_debug('</pre>');
  129. ?>