SiteController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. class SiteController extends CController implements IWebServiceProvider
  3. {
  4. /**
  5. * Declares the 'phonebook' Web service action.
  6. */
  7. public function actions()
  8. {
  9. return array(
  10. 'phonebook'=>array(
  11. 'class'=>'CWebServiceAction',
  12. 'classMap'=>array(
  13. 'Contact',
  14. ),
  15. ),
  16. );
  17. }
  18. /**
  19. * This is the default action that displays the phonebook Flex client.
  20. */
  21. public function actionIndex()
  22. {
  23. $this->render('index');
  24. }
  25. /**
  26. * This action serves as a SOAP client to test the phonebook Web service.
  27. */
  28. public function actionTest()
  29. {
  30. $wsdlUrl=Yii::app()->request->hostInfo.$this->createUrl('phonebook');
  31. $client=new SoapClient($wsdlUrl);
  32. echo "<pre>";
  33. echo "login...\n";
  34. $client->login('demo','demo');
  35. echo "fetching all contacts\n";
  36. print_r($client->getContacts());
  37. echo "\ninserting a new contact...";
  38. $contact=new Contact;
  39. $contact->name='Tester Name';
  40. $contact->phone='123-123-1234';
  41. $client->saveContact($contact);
  42. echo "done\n\n";
  43. echo "fetching all contacts\n";
  44. print_r($client->getContacts());
  45. echo "</pre>";
  46. }
  47. /**
  48. * This method is required by IWebServiceProvider.
  49. * It makes sure the user is logged in before making changes to data.
  50. * @param CWebService the currently requested Web service.
  51. * @return boolean whether the remote method should be executed.
  52. */
  53. public function beforeWebMethod($service)
  54. {
  55. $safeMethods=array(
  56. 'login',
  57. 'getContacts',
  58. );
  59. $pattern='/^('.implode('|',$safeMethods).')$/i';
  60. if(!Yii::app()->user->isGuest || preg_match($pattern,$service->methodName))
  61. return true;
  62. else
  63. throw new CException('Login required.');
  64. }
  65. /**
  66. * This method is required by IWebServiceProvider.
  67. * @param CWebService the currently requested Web service.
  68. */
  69. public function afterWebMethod($service)
  70. {
  71. }
  72. /*** The following methods are Web service APIs ***/
  73. /**
  74. * @param string username
  75. * @param string password
  76. * @return boolean whether login is valid
  77. * @soap
  78. */
  79. public function login($username,$password)
  80. {
  81. $identity=new UserIdentity($username,$password);
  82. if($identity->authenticate())
  83. Yii::app()->user->login($identity);
  84. return $identity->isAuthenticated;
  85. }
  86. /**
  87. * Returns all contact records.
  88. * @return Contact[] the contact records
  89. * @soap
  90. */
  91. public function getContacts()
  92. {
  93. return Contact::model()->findAll();
  94. }
  95. /**
  96. * Updates or inserts a contact.
  97. * If the ID is null, an insertion will be performed;
  98. * Otherwise it updates the existing one.
  99. * @param Contact contact model
  100. * @return boolean whether saving is successful
  101. * @soap
  102. */
  103. public function saveContact($contact)
  104. {
  105. if($contact->id > 0) // update
  106. {
  107. $contact->isNewRecord=false;
  108. if(($oldContact=Contact::model()->findByPk($contact->id))!==null)
  109. {
  110. $oldContact->attributes=$contact->attributes;
  111. return $oldContact->save();
  112. }
  113. else
  114. return false;
  115. }
  116. else // insert
  117. {
  118. $contact->isNewRecord=true;
  119. $contact->id=null;
  120. return $contact->save();
  121. }
  122. }
  123. /**
  124. * Deletes the specified contact record.
  125. * @param integer ID of the contact to be deleted
  126. * @return integer number of records deleted
  127. * @soap
  128. */
  129. public function deleteContact($id)
  130. {
  131. return Contact::model()->deleteByPk($id);
  132. }
  133. }