Added DB Access + Error page
This commit is contained in:
1
demos/phonebook/protected/.htaccess
Normal file
1
demos/phonebook/protected/.htaccess
Normal file
@@ -0,0 +1 @@
|
||||
deny from all
|
||||
27
demos/phonebook/protected/components/UserIdentity.php
Normal file
27
demos/phonebook/protected/components/UserIdentity.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* UserIdentity represents the data needed to identity a user.
|
||||
* It contains the authentication method that checks if the provided
|
||||
* data can identity the user.
|
||||
*/
|
||||
class UserIdentity extends CUserIdentity
|
||||
{
|
||||
/**
|
||||
* Validates the username and password.
|
||||
* This method should check the validity of the provided username
|
||||
* and password in some way. In case of any authentication failure,
|
||||
* set errorCode and errorMessage with appropriate values and return false.
|
||||
* @param string username
|
||||
* @param string password
|
||||
* @return boolean whether the username and password are valid
|
||||
*/
|
||||
public function authenticate()
|
||||
{
|
||||
if($this->username==='demo' && $this->password==='demo')
|
||||
$this->errorCode=self::ERROR_NONE;
|
||||
else
|
||||
$this->errorCode=self::ERROR_PASSWORD_INVALID;
|
||||
return !$this->errorCode;
|
||||
}
|
||||
}
|
||||
21
demos/phonebook/protected/config/main.php
Normal file
21
demos/phonebook/protected/config/main.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
// This is the main Web application configuration. Any writable
|
||||
// application properties can be configured here.
|
||||
return array(
|
||||
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
|
||||
'name'=>'Yii Framework: Phone Book Demo',
|
||||
|
||||
// autoloading model and component classes
|
||||
'import'=>array(
|
||||
'application.models.*',
|
||||
'application.components.*',
|
||||
),
|
||||
|
||||
// application components
|
||||
'components'=>array(
|
||||
'db'=>array(
|
||||
'connectionString'=>'sqlite:protected/data/phonebook.db',
|
||||
),
|
||||
),
|
||||
);
|
||||
143
demos/phonebook/protected/controllers/SiteController.php
Normal file
143
demos/phonebook/protected/controllers/SiteController.php
Normal file
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
class SiteController extends CController implements IWebServiceProvider
|
||||
{
|
||||
/**
|
||||
* Declares the 'phonebook' Web service action.
|
||||
*/
|
||||
public function actions()
|
||||
{
|
||||
return array(
|
||||
'phonebook'=>array(
|
||||
'class'=>'CWebServiceAction',
|
||||
'classMap'=>array(
|
||||
'Contact',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the default action that displays the phonebook Flex client.
|
||||
*/
|
||||
public function actionIndex()
|
||||
{
|
||||
$this->render('index');
|
||||
}
|
||||
|
||||
/**
|
||||
* This action serves as a SOAP client to test the phonebook Web service.
|
||||
*/
|
||||
public function actionTest()
|
||||
{
|
||||
$wsdlUrl=Yii::app()->request->hostInfo.$this->createUrl('phonebook');
|
||||
$client=new SoapClient($wsdlUrl);
|
||||
echo "<pre>";
|
||||
echo "login...\n";
|
||||
$client->login('demo','demo');
|
||||
echo "fetching all contacts\n";
|
||||
print_r($client->getContacts());
|
||||
echo "\ninserting a new contact...";
|
||||
$contact=new Contact;
|
||||
$contact->name='Tester Name';
|
||||
$contact->phone='123-123-1234';
|
||||
$client->saveContact($contact);
|
||||
echo "done\n\n";
|
||||
echo "fetching all contacts\n";
|
||||
print_r($client->getContacts());
|
||||
echo "</pre>";
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is required by IWebServiceProvider.
|
||||
* It makes sure the user is logged in before making changes to data.
|
||||
* @param CWebService the currently requested Web service.
|
||||
* @return boolean whether the remote method should be executed.
|
||||
*/
|
||||
public function beforeWebMethod($service)
|
||||
{
|
||||
$safeMethods=array(
|
||||
'login',
|
||||
'getContacts',
|
||||
);
|
||||
$pattern='/^('.implode('|',$safeMethods).')$/i';
|
||||
if(!Yii::app()->user->isGuest || preg_match($pattern,$service->methodName))
|
||||
return true;
|
||||
else
|
||||
throw new CException('Login required.');
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is required by IWebServiceProvider.
|
||||
* @param CWebService the currently requested Web service.
|
||||
*/
|
||||
public function afterWebMethod($service)
|
||||
{
|
||||
}
|
||||
|
||||
/*** The following methods are Web service APIs ***/
|
||||
|
||||
/**
|
||||
* @param string username
|
||||
* @param string password
|
||||
* @return boolean whether login is valid
|
||||
* @soap
|
||||
*/
|
||||
public function login($username,$password)
|
||||
{
|
||||
$identity=new UserIdentity($username,$password);
|
||||
if($identity->authenticate())
|
||||
Yii::app()->user->login($identity);
|
||||
return $identity->isAuthenticated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all contact records.
|
||||
* @return Contact[] the contact records
|
||||
* @soap
|
||||
*/
|
||||
public function getContacts()
|
||||
{
|
||||
return Contact::model()->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates or inserts a contact.
|
||||
* If the ID is null, an insertion will be performed;
|
||||
* Otherwise it updates the existing one.
|
||||
* @param Contact contact model
|
||||
* @return boolean whether saving is successful
|
||||
* @soap
|
||||
*/
|
||||
public function saveContact($contact)
|
||||
{
|
||||
if($contact->id > 0) // update
|
||||
{
|
||||
$contact->isNewRecord=false;
|
||||
if(($oldContact=Contact::model()->findByPk($contact->id))!==null)
|
||||
{
|
||||
$oldContact->attributes=$contact->attributes;
|
||||
return $oldContact->save();
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
else // insert
|
||||
{
|
||||
$contact->isNewRecord=true;
|
||||
$contact->id=null;
|
||||
return $contact->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the specified contact record.
|
||||
* @param integer ID of the contact to be deleted
|
||||
* @return integer number of records deleted
|
||||
* @soap
|
||||
*/
|
||||
public function deleteContact($id)
|
||||
{
|
||||
return Contact::model()->deleteByPk($id);
|
||||
}
|
||||
}
|
||||
BIN
demos/phonebook/protected/data/phonebook.db
Normal file
BIN
demos/phonebook/protected/data/phonebook.db
Normal file
Binary file not shown.
6
demos/phonebook/protected/data/schema.sql
Normal file
6
demos/phonebook/protected/data/schema.sql
Normal file
@@ -0,0 +1,6 @@
|
||||
CREATE TABLE Contact
|
||||
(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
name VARCHAR(128) NOT NULL,
|
||||
phone VARCHAR(64) NOT NULL
|
||||
);
|
||||
25
demos/phonebook/protected/models/Contact.php
Normal file
25
demos/phonebook/protected/models/Contact.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
class Contact extends CActiveRecord
|
||||
{
|
||||
/**
|
||||
* @var integer ID of this record
|
||||
* @soap
|
||||
*/
|
||||
public $id;
|
||||
/**
|
||||
* @var string name
|
||||
* @soap
|
||||
*/
|
||||
public $name;
|
||||
/**
|
||||
* @var string phone number
|
||||
* @soap
|
||||
*/
|
||||
public $phone;
|
||||
|
||||
public static function model($className=__CLASS__)
|
||||
{
|
||||
return parent::model($className);
|
||||
}
|
||||
}
|
||||
43
demos/phonebook/protected/views/site/index.php
Normal file
43
demos/phonebook/protected/views/site/index.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="language" content="en" />
|
||||
<title><?php echo $this->pageTitle; ?></title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Phone Book Demo</h1>
|
||||
<p>
|
||||
This demo shows how to use Yii to implement a Web service used by an Adobe Flex 3.0 client.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
In order to see this demo, the PHP SOAP extension must be enabled and your browser should have
|
||||
installed Adobe Flash Player version 9 or above.
|
||||
</p>
|
||||
|
||||
<div>
|
||||
<?php echo CHtml::link('View WSDL for this Web service',array('phonebook')); ?>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<?php if(extension_loaded('soap')): ?>
|
||||
<?php $this->widget('CFlexWidget',array(
|
||||
'baseUrl'=>Yii::app()->baseUrl.'/flex/bin',
|
||||
'name'=>'phonebook',
|
||||
'width'=>'800',
|
||||
'height'=>'300',
|
||||
'align'=>'left',
|
||||
'flashVars'=>array(
|
||||
'wsdl'=>$this->createUrl('phonebook'),
|
||||
))); ?>
|
||||
<?php else: ?>
|
||||
Sorry, the PHP SOAP extension is not enabled on your Web server.
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user