Source for file create_agent.act.php

Documentation is available at create_agent.act.php

  1. <?php
  2.  
  3. /**
  4. * @package polyphony.agents
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: create_agent.act.php,v 1.17 2007/09/19 14:04:52 adamfranco Exp $
  10. */
  11.  
  12. require_once(POLYPHONY."/main/library/AbstractActions/MainWindowAction.class.php");
  13. require_once(HARMONI."GUIManager/Components/Blank.class.php");
  14.  
  15. /**
  16. * This action will allow for the creation of agents.
  17. *
  18. * @since 11/10/04
  19. *
  20. * @package polyphony.agents
  21. *
  22. * @copyright Copyright &copy; 2005, Middlebury College
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  24. *
  25. * @version $Id: create_agent.act.php,v 1.17 2007/09/19 14:04:52 adamfranco Exp $
  26. */
  27. class create_agentAction
  28. extends MainWindowAction
  29. {
  30. /**
  31. * Check Authorizations
  32. *
  33. * @return boolean
  34. * @access public
  35. * @since 4/26/05
  36. */
  37. function isAuthorizedToExecute () {
  38. // Check for authorization
  39. $authZManager = Services::getService("AuthZ");
  40. $idManager = Services::getService("IdManager");
  41. if ($authZManager->isUserAuthorized(
  42. $idManager->getId("edu.middlebury.authorization.add_children"),
  43. $idManager->getId("edu.middlebury.agents.all_agents")))
  44. {
  45. return TRUE;
  46. } else
  47. return FALSE;
  48. }
  49. /**
  50. * Return the heading text for this action, or an empty string.
  51. *
  52. * @return string
  53. * @access public
  54. * @since 4/26/05
  55. */
  56. function getHeadingText () {
  57. return dgettext("polyphony", "Create a new User");
  58. }
  59. /**
  60. * Build the content for this action
  61. *
  62. * @return void
  63. * @access public
  64. * @since 4/26/05
  65. */
  66. function buildContent () {
  67. $defaultTextDomain = textdomain("polyphony");
  68. $actionRows =$this->getActionRows();
  69. $pageRows = new Container(new YLayout(), OTHER, 1);
  70. $harmoni = Harmoni::instance();
  71. $harmoni->request->startNamespace("polyphony-agents");
  72. $harmoni->request->passthrough();
  73.  
  74. //'form_submitted' is a hidden field in the form, just a switch really
  75. if(RequestContext::value("form_submitted")){
  76. //basic form checking. If required fields aren't there, rebuild the form
  77. if(!RequestContext::value("username") || !RequestContext::value("password")){
  78. print "You must enter a username and password!<br />";
  79. print create_agentAction::createAgentForm();
  80. }else{
  81. $userName = RequestContext::value("username");
  82. $password = RequestContext::value("password");
  83. $displayName = RequestContext::value("display_name");
  84. $properties = array();
  85. //creates an array of properties
  86. foreach($harmoni->request->getKeys() as $key){
  87. $key_parts = explode("_",$key);
  88. if($key_parts[0]=="property"){
  89. $key_name = $key_parts[1];
  90. $properties[$key_name] = RequestContext::value($key);
  91. }
  92. }
  93. $agent = create_agentAction::makeNewAgent($userName, $password, $displayName, $properties);
  94. if($agent){
  95. print "User ".$agent->getDisplayName()." succesfully created.";
  96. $harmoni->history->goBack("polyphony/agents/create_agent");
  97. }else{
  98. print "Create agent failed.";
  99. create_agentAction::createAgentForm();
  100. }
  101. }
  102. }else{
  103. //if the form hasn't been submitted, print it out
  104. create_agentAction::createAgentForm();
  105. }
  106. // Layout
  107. $actionRows->add(new Block(ob_get_contents(), WIZARD_BLOCK), "100%", null, CENTER, TOP);
  108. ob_end_clean();
  109. $harmoni->request->endNamespace();
  110. textdomain($defaultTextDomain);
  111. }
  112.  
  113. /**
  114. * Print the form to create agents
  115. * The handler can handle any number of properties but they must be
  116. * named 'property_'.property_name
  117. * @return void
  118. * @static
  119. */
  120. function createAgentForm(){
  121. $harmoni = Harmoni::instance();
  122. print "<center><form action='".$harmoni->request->quickURL()."' method='post'>
  123. <table>";
  124. //switch($GLOBALS["AuthNMethod"]){
  125. // case "dbAuthType":
  126. print "<tr><td>
  127. *Username:
  128. </td><td>
  129. <input type='text' name='".RequestContext::name("username")."' />
  130. </td></tr>
  131. <tr><td>
  132. *Password:
  133. </td><td>
  134. <input type='password' name='".RequestContext::name("password")."' />
  135. </td></tr>";
  136. // break;
  137. //}
  138. print "<tr><td>
  139. *"._("Add to type: ")."
  140. </td><td>
  141. <select name='".RequestContext::name("authn_type")."'>";
  142. $authNManager = Services::getService("AuthN");
  143. $typesIterator =$authNManager->getAuthenticationTypes();
  144. while($typesIterator->hasNext()) {
  145. $tempType =$typesIterator->next();
  146. $authNMethods = Services::getService("AuthNMethods");
  147. $tempMethod =$authNMethods->getAuthNMethodForType($tempType);
  148. if (!$tempMethod->supportsTokenAddition()) continue;
  149. print "<option value='".HarmoniType::typeToString($tempType)."'>".HarmoniType::typeToString($tempType)."</option>";
  150. }
  151. print "</select>";
  152. print "</td></tr>";
  153. print " <tr><td>
  154. Display Name:
  155. </td><td>
  156. <input type='text' name='".RequestContext::name("display_name")."' />
  157. </td></tr>
  158. <!-- <tr><td>
  159. Department:
  160. </td><td>
  161. <input type='text' name='".RequestContext::name("property_department")."' />
  162. </td></tr> -->
  163. </table>
  164. <input type='submit' value='Create New User' />
  165. <input type='hidden' name='".RequestContext::name("form_submitted")."' value='true' />
  166. </form></center>";
  167. }
  168. /*
  169. * object Agent makeNewAgent(string $userName, string $passWord, string $displayName, mixed $propertiesArray)
  170. * makes a new agent, creates authentication mappings and so on
  171. */
  172. function makeNewAgent($userName, $password, $displayName, $propertiesArray){
  173. //authentication handling
  174. $authNMethodManager = Services::getService("AuthNMethodManager");
  175. $tokenMappingManager = Services::getService("AgentTokenMapping");
  176. //find the authn type. This is set in a hidden field in the form at the moment but could easily be changed to a drop down menu
  177. $authNType = HarmoniType::fromString(RequestContext::value('authn_type'));
  178. //for passing to the token handler
  179. $newTokensPassed["username"]=RequestContext::value("username");
  180. $newTokensPassed["password"]=RequestContext::value("password");
  181. //find what authentication method is associated with this type
  182. $authNMethod=$authNMethodManager->getAuthNMethodForType($authNType);
  183. //get tokens object for authentication type
  184. $tokens =$authNMethod->createTokensObject();
  185. //set the values of the tokens to the array we just created
  186. $tokens->initializeForTokens($newTokensPassed);
  187. //if a mapping already exists, there is alreadya a user with this name
  188. $mappingExists=$tokenMappingManager->getMappingForTokens($tokens, $authNType);
  189. if($mappingExists){
  190. print "This username is already in use, please choose another.";
  191. return false;
  192. }
  193. //the type for the user
  194. $userType = new HarmoniType("Agents", "edu.middlebury.harmoni", "User");
  195. //property manager is used for storing properties to the database
  196. $propertyManager = Services::getService("Property");
  197. //convert the array of properties we have to a properties object
  198. $propertyObject =$propertyManager->convertArrayToObject($propertiesArray, $userType);
  199. $agentManager = Services::getService("Agent");
  200. //create the agent entries and build an agent object
  201. $agent =$agentManager->createAgent($displayName, $userType, $propertyObject);
  202. //get the id and create a link between the agent and its authentication info
  203. $id =$agent->getId();
  204. $mapping =$tokenMappingManager->createMapping($id, $tokens, $authNType);
  205. //tell the specific AuthNMethod to add the tokens
  206. $authNMethod->addTokens($tokens);
  207. return $agent;
  208. }
  209. }
  210.  
  211. ?>

Documentation generated on Wed, 19 Sep 2007 10:22:27 -0400 by phpDocumentor 1.3.0RC3