|
eZ Publish
[trunk]
|
00001 <?php 00002 /** 00003 * File containing the ezpRestClient class. 00004 * 00005 * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved. 00006 * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 00007 * @version //autogentag// 00008 * @package kernel 00009 */ 00010 00011 /** 00012 * Persistent object class representing a REST application. 00013 */ 00014 class ezpRestClient 00015 { 00016 public $id = null; 00017 00018 /** 00019 * Application name 00020 * @var string 00021 */ 00022 public $name = null; 00023 00024 /** 00025 * Application description 00026 * @var string 00027 */ 00028 public $description = null; 00029 00030 /** 00031 * Application client ID, as used over oAuth to authentify the application 00032 * @var string 00033 */ 00034 public $client_id = null; 00035 00036 /** 00037 * Application client secret, as used over oAuth to authentify the application 00038 * @var string 00039 */ 00040 public $client_secret = null; 00041 00042 /** 00043 * Application client endpoint URI. Used to validate the redirection URI requested by the authorize call. 00044 * @var string 00045 */ 00046 public $endpoint_uri = null; 00047 00048 /** 00049 * ID of the eZ Publish user who owns the application 00050 * @var int 00051 */ 00052 public $owner_id = null; 00053 00054 /** 00055 * Application creation date, as a unix timestamp 00056 * @var int 00057 */ 00058 public $created = null; 00059 00060 /** 00061 * Application update date, as a unix timestamp 00062 * @var int 00063 */ 00064 public $updated = null; 00065 00066 /** 00067 * Application version, used to pre-create a draft when first creation a new application. 00068 * @var int 00069 */ 00070 public $version = null; 00071 00072 public function getState() 00073 { 00074 $result = array(); 00075 $result['id'] = $this->id; 00076 $result['name'] = $this->name; 00077 $result['description'] = $this->description; 00078 $result['client_id'] = $this->client_id; 00079 $result['client_secret'] = $this->client_secret; 00080 $result['endpoint_uri'] = $this->endpoint_uri; 00081 $result['owner_id'] = $this->owner_id; 00082 $result['created'] = $this->created; 00083 $result['updated'] = $this->updated; 00084 $result['version'] = $this->version; 00085 return $result; 00086 } 00087 00088 public function setState( array $properties ) 00089 { 00090 foreach( $properties as $key => $value ) 00091 { 00092 $this->$key = $value; 00093 } 00094 } 00095 00096 /** 00097 * eZPersistentObject wrapper method 00098 * @param string $attributeName 00099 * @return mixed 00100 */ 00101 public function attribute( $attributeName ) 00102 { 00103 if ( property_exists( $this, $attributeName ) ) 00104 return $this->$attributeName; 00105 elseif ( $this->__isset( $attributeName ) ) 00106 return $this->__get( $attributeName ); 00107 else 00108 eZDebug::writeError( "Attribute '$attributeName' does not exist", __METHOD__ ); 00109 } 00110 00111 /** 00112 * eZPersistentObject wrapper method 00113 * @param string $attributeName 00114 * @return bool 00115 */ 00116 public function hasAttribute( $attributeName ) 00117 { 00118 return property_exists( $this, $attributeName ) or $this->__isset( $attributeName ); 00119 } 00120 00121 /** 00122 * eZPersistentObject wrapper method: 00123 * handles "function attributes" 00124 * @param string $propertyName 00125 * @return mixed 00126 */ 00127 public function __get( $propertyName ) 00128 { 00129 switch( $propertyName ) 00130 { 00131 case 'owner': 00132 { 00133 return $this->_owner(); 00134 } break; 00135 00136 default: 00137 throw new ezcBasePropertyNotFoundException( $propertyName ); 00138 } 00139 } 00140 00141 /** 00142 * Returns the eZUser who owns the object 00143 * @return eZUser 00144 */ 00145 protected function _owner() 00146 { 00147 static $owner = false; 00148 00149 if ( $owner === false ) 00150 { 00151 $owner = eZUser::fetch( $this->owner_id ); 00152 } 00153 00154 return $owner; 00155 } 00156 00157 public function __isset( $propertyName ) 00158 { 00159 return in_array( $propertyName, array( 'owner' ) ); 00160 } 00161 00162 /** 00163 * Validates an authorization request by an application using the ID, redirection URI and secret if provided. 00164 * 00165 * @var string $clientId 00166 * @var string $endPointUri 00167 * @var string $clientSecret 00168 * 00169 * @return bool True if the app is valid, false if it isn't 00170 * @todo Enhance the return variable, as several status would be required. Exceptions, or constants ? 00171 */ 00172 public static function authorizeApplication( $clientId, $endPointUri, $clientSecret = null ) 00173 { 00174 $client = self::fetchByClientId( $clientId ); 00175 00176 // no client found with this ID 00177 if ( $client === false ) 00178 return false; 00179 00180 if ( $clientSecret !== null && ( $clientSecret !== $client->client_secret ) ) 00181 return false; 00182 00183 if ( ( $client->endpoint_uri !== '' ) && ( $endPointUri !== $client->endpoint_uri ) ) 00184 return false; 00185 00186 return true; 00187 } 00188 00189 /** 00190 * Fetches a rest application using a client Id 00191 * @param string $clientId 00192 * @return ezpRestClient 00193 */ 00194 public static function fetchByClientId( $clientId ) 00195 { 00196 $session = ezcPersistentSessionInstance::get(); 00197 00198 $q = $session->createFindQuery( __CLASS__ ); 00199 $q->where( $q->expr->eq( 'client_id', $q->bindValue( $clientId ) ) ); 00200 $results = $session->find( $q, __CLASS__ ); 00201 if ( count( $results ) != 1 ) 00202 return false; 00203 else 00204 return array_shift( $results ); 00205 } 00206 00207 /** 00208 * Convenience method to validate a client secret. 00209 * 00210 * @param $secret 00211 * @return bool 00212 */ 00213 public function validateSecret( $secret ) 00214 { 00215 return $secret === $this->client_secret; 00216 } 00217 00218 /** 00219 * Checks if this application has been authorized by the current user 00220 * 00221 * @param mixed $scope The requested security scope 00222 * @param eZUser $user The user to check authorization for. Will check for current user if not given. 00223 * 00224 * @return bool 00225 * 00226 * @todo Handle non-authorization using 00227 */ 00228 public function isAuthorizedByUser( $scope, $user = null ) 00229 { 00230 if ( $user === null ) 00231 $user = eZUser::currentUser(); 00232 00233 if ( !$user->isLoggedIn() ) 00234 throw new Exception( "Anonymous user can not authorize an application" ); 00235 00236 $authorized = ezpRestAuthorizedClient::fetchForClientUser( $this, $user ); 00237 return ( $authorized instanceof ezpRestAuthorizedClient ); 00238 } 00239 00240 /** 00241 * Authorizes this application for a user 00242 * @param eZUser $user 00243 * @return void 00244 */ 00245 public function authorizeFor( $user = null ) 00246 { 00247 $authorization = new ezpRestAuthorizedClient(); 00248 $authorization->rest_client_id = $this->id; 00249 $authorization->user_id = $user->attribute( 'contentobject_id' ); 00250 00251 $session = ezcPersistentSessionInstance::get(); 00252 $session->save( $authorization ); 00253 } 00254 00255 /** 00256 * Validates an attempt (endpoint) redirect URI against the one configured for the client 00257 * 00258 * @param string $endPointUri 00259 * 00260 * @return bool true if the URI is valid, false otherwise 00261 */ 00262 public function isEndPointValid( $endPointUri ) 00263 { 00264 return ( $endPointUri === $this->endpoint_uri ); 00265 } 00266 00267 const STATUS_DRAFT = 1; 00268 const STATUS_PUBLISHED = 0; 00269 } 00270 ?>