Skip to content
Browse files

Adding a User model and Http auth adapter.

  • Loading branch information...
1 parent e8ebfc6 commit 6426bc04c9b8eb04e641c33525bdee4289d0b55b @gwoo committed
Showing with 70 additions and 8 deletions.
  1. +8 −8 extensions/adapter/data/source/http/GitHub.php
  2. +40 −0 extensions/adapter/security/auth/Http.php
  3. +22 −0 models/User.php
View
16 extensions/adapter/data/source/http/GitHub.php
@@ -14,6 +14,7 @@ class GitHub extends \lithium\data\source\Http {
protected $_strings = array(
'users' => '/users/{:user}/{:type}',
+ 'user' => '/user/{:user}/{:type}',
'issues' => '/issues',
'repos' => '/repos/{:user}/{:repo}/{:type}/{:id}',
'orgs' => '/orgs/{:org}/{:type}'
@@ -65,8 +66,6 @@ public function __construct(array $config = array()) {
* @return mixed
*/
public function read($query, array $options = array()) {
-
-
extract($query->export($this));
$path = $this->_path($source, $conditions);
foreach ($this->_params as $param) {
@@ -79,6 +78,9 @@ public function read($query, array $options = array()) {
if (empty($data)) {
return null;
}
+ if (empty($data[0])) {
+ return $this->item($query->model(), compact('data'), array('class' => 'entity'));
+ }
return $this->item($query->model(), $data, array('class' => 'set'));
}
@@ -116,9 +118,7 @@ public function cast($entity, array $data, array $options = array()) {
if (!is_array($val)) {
continue;
}
- $class = 'entity';
- $model = $entity->model();
- $data[$key] = $this->item($model, $val, compact('class'));
+ $data[$key] = $this->item($entity->model(), $val, array('class' => 'entity'));
}
return parent::cast($entity, $data, $options);
}
@@ -130,16 +130,16 @@ public function cast($entity, array $data, array $options = array()) {
* @param array $conditions
* @return string
*/
- protected function _path($source, array $conditions = array()) {
+ protected function _path($source, $conditions = array()) {
if (!isset($this->_strings[$source])) {
return null;
}
$string = $this->_strings[$source];
$conditions = array_map(function($value) {
return is_string($value) ? urlencode($value) : null;
- }, $conditions);
+ }, (array) $conditions);
$path = String::insert($string, $conditions, array('clean' => true));
- $path = rtrim(str_replace('//', '', $path), '/');
+ $path = rtrim(str_replace('//', '/', $path), '/');
return $path;
}
}
View
40 extensions/adapter/security/auth/Http.php
@@ -0,0 +1,40 @@
+<?php
+/**
+ * Lithium: the most rad php framework
+ *
+ * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ */
+
+namespace li3_github\extensions\adapter\security\auth;
+
+use li3_github\models\User;
+
+class Http extends \lithium\security\auth\adapter\Http {
+
+ /**
+ * Called by the `Auth` class to run an authentication check against the HTTP data using the
+ * credentials in a data container (a `Request` object), and returns an array of user
+ * information on success, or `false` on failure.
+ *
+ * @param object $request A env container which wraps the authentication credentials used
+ * by HTTP (usually a `Request` object). See the documentation for this
+ * class for further details.
+ * @param array $options Additional configuration options. Not currently implemented in this
+ * adapter.
+ * @return array Returns an array containing user information on success, or `false` on failure.
+ */
+ public function check($request, array $options = array()) {
+ $user = User::first();
+
+ if (isset($user->message)) {
+ $message = "To access {$this->_config['realm']}, "
+ . "use your GitHub username and password.";
+ $this->_writeHeader("WWW-Authenticate: Basic realm=\"{$message}\"");
+ return;
+ }
+ return compact('user');
+ }
+}
+
+?>
View
22 models/User.php
@@ -0,0 +1,22 @@
+<?php
+
+namespace li3_github\models;
+
+class User extends \lithium\data\Model {
+
+ protected $_meta = array('connection' => 'github', 'source' => 'user');
+}
+
+User::finder('repos', function($self, $params, $chain) {
+ $params['options']['conditions']['type'] = 'repos';
+ $data = $chain->next($self, $params, $chain);
+ return $data;
+});
+
+User::finder('orgs', function($self, $params, $chain) {
+ $params['options']['conditions']['type'] = 'orgs';
+ $data = $chain->next($self, $params, $chain);
+ return $data;
+});
+
+?>

0 comments on commit 6426bc0

Please sign in to comment.
Something went wrong with that request. Please try again.