Permalink
Browse files

adding users with auth

  • Loading branch information...
1 parent 1c767c4 commit 7bc07e938d449efef8be37ac5b72b2aa93a753b9 @gwoo gwoo committed Feb 17, 2010
View
@@ -1 +1,2 @@
-.DS_Store
+.DS_Store
+resources/tmp/*
View
@@ -54,10 +54,28 @@
* it intercepts any writes where the `'expires'` key is set in the options array. When creating a
* new application, it is suggested that you change the value of `'key'` below.
*/
-// use \lithium\storage\Session;
-//
-// Session::config(array(
-// 'default' => array('adapter' => 'Php')
-// ));
+use \lithium\storage\Session;
+
+Session::config(array(
+ 'default' => array('adapter' => 'Php')
+));
+
+use \lithium\security\Auth;
+
+Auth::config(array(
+ 'user' => array(
+ 'adapter' => 'lithium\security\auth\adapter\Form',
+ 'model' => 'app\models\User',
+ 'query' => 'login',
+ 'fields' => array('username', 'password')
+ )
+));
+
+// use \lithium\action\Dispatcher;
+//
+// Dispatcher::applyFilter('_call', function($self, $params, $chain) {
+// $result = Auth::check('user', $params['request']);
+// return $chain->next($self, $params, $chain);
+// });
?>
@@ -0,0 +1,66 @@
+<?php
+
+namespace app\controllers;
+
+use \app\models\User;
+use \lithium\security\Auth;
+use \lithium\storage\Session;
+
+class UsersController extends \lithium\action\Controller {
+
+ public function index() {
+ $users = User::all();
+ return compact('users');
+ }
+
+ public function login() {
+ if (!empty($this->request->data)) {
+ $user = Auth::check('user', $this->request);
+ return compact('user');
+ }
+ }
+
+ public function logout() {
+ Session::delete('user');
+ $this->redirect(array('action' => 'login'));
+ }
+
+ public function view($id = null) {
+ $user = User::find($id);
+ return compact('user');
+ }
+
+ public function register() {
+ if (!empty($this->request->data)) {
+ $user = User::create($this->request->data);
+ if ($user->save()) {
+ $this->redirect(array(
+ 'controller' => 'users', 'action' => 'view',
+ 'args' => array($user->id)
+ ));
+ }
+ }
+ if (empty($user)) {
+ $user = User::create();
+ }
+ return compact('user');
+ }
+
+ public function edit($id = null) {
+ $user = User::find($id);
+ if (empty($user)) {
+ $this->redirect(array('controller' => 'users', 'action' => 'index'));
+ }
+ if (!empty($this->request->data)) {
+ if ($user->save($this->request->data)) {
+ $this->redirect(array(
+ 'controller' => 'users', 'action' => 'view',
+ 'args' => array($user->id)
+ ));
+ }
+ }
+ return compact('user');
+ }
+}
+
+?>
View
@@ -19,6 +19,7 @@ class Post extends \lithium\data\Model {
public static function __init($options = array()) {
parent::__init($options);
static::applyFilter('save', function ($self, $params, $chain) {
+ $params['record']->type = 'post';
if (empty($params['record']->created)) {
$params['record']->created = date('Y-m-d H:i:s');
}
View
@@ -11,16 +11,33 @@ class SphereView extends \lithium\data\Model {
'id' => '_design/all',
'language' => 'javascript',
'views' => array(
- 'latest' => array(
+ 'posts' => array(
'map' => 'function(doc) {
- emit(doc.created, doc);
+ if (doc.type == "post") {
+ emit(doc.created, doc);
+ }
}'
),
- 'popular' => array(
+ 'users' => array(
'map' => 'function(doc) {
- emit(doc.points, doc);
+ if (doc.type == "user") {
+ emit(doc.created, doc);
+ }
}'
),
+ ),
+ ),
+ 'user' => array(
+ 'id' => '_design/user',
+ 'language' => 'javascript',
+ 'views' => array(
+ 'by_username' => array(
+ 'map' => 'function(doc) {
+ if(doc.type == "user") {
+ emit(doc.username, doc);
+ }
+ }'
+ )
)
)
);
View
@@ -0,0 +1,49 @@
+<?php
+
+namespace app\models;
+
+use \lithium\util\String;
+
+class User extends \lithium\data\Model {
+
+ public $validates = array();
+
+ protected $_meta = array('source' => 'lithosphere');
+
+ protected $_schema = array(
+ 'username' => array('type' => 'string', 'length' => 250),
+ 'password' => array('type' => 'string', 'length' => 250),
+ 'email' => array('type' => 'string', 'length' => 250),
+ 'created' => array('type' => 'date'),
+ );
+
+ public static function __init($options = array()) {
+ parent::__init($options);
+ static::applyFilter('save', function ($self, $params, $chain) {
+ $params['record']->type = 'user';
+ if (empty($params['record']->created)) {
+ $params['record']->created = date('Y-m-d H:i:s');
+ $params['record']->password = String::hash($params['record']->password);
+ }
+ return $chain->next($self, $params, $chain);
+ });
+ }
+
+ public static function login($data) {
+ if (empty($data['conditions']['username']) || empty($data['conditions']['password'])) {
+ return false;
+ }
+ $result = static::first(array(
+ 'conditions' => array(
+ 'design' => 'user', 'view' => 'by_username',
+ 'key' => json_encode($data['conditions']['username'])
+ )
+ ));
+ if ($data['conditions']['password'] === $result->password) {
+ return $result;
+ }
+ return false;
+ }
+}
+
+?>
@@ -20,6 +20,16 @@
<div id="header">
<h1>Lithosphere</h1>
<h2><?=$this->html->link('power of community', '/');?></h2>
+ <div style="float:right;color: green">
+ <?php
+ if ($user = \lithium\storage\Session::read('user')) {
+ echo $user['username'] . ' > ';
+ echo $this->html->link('logout', array(
+ 'controller' => 'users', 'action' => 'logout'
+ ));
+ }
+ ?>
+ </div>
</div>
<div id="content">
<?=$this->content;?>
View
@@ -0,0 +1,14 @@
+<?php
+ if (!empty($user)) {
+ echo "Nice, {$user['username']} you logged in.";
+ }
+?>
+
+<h3><?=$this->title('Login')?></h3>
+<?php
+echo $this->form->create();
+echo $this->form->field('username');
+echo $this->form->field('password', array('type' => 'password'));
+echo $this->form->submit('login');
+echo $this->form->end();
+?>
@@ -0,0 +1,9 @@
+<h3><?=$this->title('Register')?></h3>
+<?php
+echo $this->form->create();
+echo $this->form->field('username');
+echo $this->form->field('password', array('type' => 'password'));
+echo $this->form->field('email');
+echo $this->form->submit('save');
+echo $this->form->end();
+?>
@@ -0,0 +1,2 @@
+<h3><?=$user->username;?></h3>
+<?=$user->email;?>

0 comments on commit 7bc07e9

Please sign in to comment.