Sunday, November 23, 2008

CakePHP Ajax Login using Auth Component.

Need to update a login controller/view to have AJAX login capability without too much changes to the view and controllers.

Controller

app/controllers/users_controller.php
class UsersController extends AppController {
 var $name = 'Users';
 var $helpers = array('Html', 'Form');
 var $components = array('Auth');

 function beforeFilter() {
   parent::beforeFilter();
   Auth->allow('logout');
 }


 function login(){
   if ($this->Auth->user()) {
     $this->User->id = $this->Auth->user('id');
     $this->User->saveField('last_login', date('Y-m-d H:i:s'));
 
     $this->Session->setFlash(sprintf("Welcome %s!", $this->Auth->user('username')));
     $this->redirect('/');
   }
 }

 function logout() {
   $this->Session->destroy();
   $this->redirect('/');
 }
}

View

views/users/login.ctp
  <div id="login-dialog"> 
  <?php echo $form->create('User', array('action'=>'login'))?>
  <?php flash('Auth.login'); ?>
  <?php echo $form->input('username');?>
  <?php echo $form->input('password', array('type' => 'password'));?>
  <?php echo $form->submit('Login');?>
  <?php echo $html->link('Forgot my password', array('action' => 'forgot_password'));?>
  <?php echo $form->end();?>
  <?php echo $html->link('Register', array('admin' => false, 'action' => 'register'));?>
  </div>


Above code are typical Users controller and view files.
What we need:

  1. Latest Jquery to make life easy. Save it to your app/webroot/js folder.

  2. Form plugin to make dealing with form easy. Save it to your app/webroot/js folder.

  3. JQuery Helper to make using JQuery with CakePHP easy.


Changes we need to implement:

  1. Add the RequestHandler component to our app_controller.php or to the UsersController.
        var $components = array('Auth', 'RequestHandler');


  2. Add the JQuery helper to our helpers.
        var $helpers = array('Html', 'Form', 'Jquery');

    Make sure to download the jquery helper from the link above and put it in your "app/views/helpers/" folder.

  3. Add the code below:

Controller changes:

 function login(){
   if ($this->Auth->user()) {
     $this->User->id = $this->Auth->user('id');
     $this->User->saveField('last_login', date('Y-m-d H:i:s'));
 
     $this->Session->setFlash(sprintf("Welcome %s!", $this->Auth->user('username')));
     $this->redirect('/');
   }

   if ($this->RequestHandler->isAjax()) {
      header('HTTP/1.0 401 Unauthorized');
      die();
   }
 }

View file changes

  <div id="login-dialog"> 
  <?php echo $form->create('User', array('action'=>'login'))?>
  <?php flash('Auth.login'); ?>
  <?php echo $form->input('username');?>
  <?php echo $form->input('password', array('type' => 'password'));?>
  <?php echo $form->submit('Login');?>
  <?php echo $html->link('Forgot my password', array('action' => 'forgot_password'));?>
  <?php echo $form->end();?>
  <?php echo $html->link('Register', array('admin' => false, 'action' => 'register'));?>
  </div>

<?php
 $loginURL = Router::url(array('controller' => 'users', 'action' => 'login'));
 $jquery->uses('jquery.form');
 $jquery->addScript(
 '  var options = {',
 '   url: "'.$loginURL.'",',
 '   success: function(data, statusText){',
 '     window.location = "/";',
 '    },',
 '   error: function(XMLHttpRequest, textStatus, errorThrown){',
 '     alert("Invalid Username and Password");',
 '    },',
 '  };',
 '  $("form#UserLoginForm").ajaxForm(options);'
 );
?>


There is no need to make changes to the default layout since the JQuery helper already includes the jquery library in the head of the layout.

The jquery library being loaded by the helper is hard coded so you need to make sure you got the correct jquery version in your /app/webroot/js folder.

The code also redirects to the root folder on successful authentication.


Need proxy? Lowest priced starting at $0.71/proxy. Insane!

Buy Instagram Followers

 TheFollowerShop - Buy Instagram Followers, Instagram Likes, Twitter Followers, Twitter Favorites

Sunday, November 02, 2008

RPM for Everybody

1. Listing content of an RPM file:

rpm -qpl package-version.rpm

2. Unpacking of an RPM archive:

rpm2cpio < myfile.rpm | cpio -i --make-directories

all contents of the RPM package will be extracted into the current directory creating relative path..

3. Installing an RPM package.

rpm -ivh package-version.rpm

4. Upgradinga an RPM package.

rpm -Uvh package-version.rpm

5. Listing installed packaged on your system:

rpm -qai

This will list all packages installed via the RPM system.

6. Uninstalling RPM packages:

rpm -e package-version.rpm

7. Building RPM from source:

@todo

Wednesday, February 20, 2008

Reusable Calendar System

The goal is to implement a calendar system that can be reused by web applications.

Feature and Capability List




  1. REST access (adding and retrieving events)

  2. Export to iCal, csv and text file

  3. Should handle (initially the following kinds of events)
    • Event with no timespan.

    • Event with timespan.

    • Repeating event.


  4. Provide a notification system to the application using the system.

Initially we will try to use google calendar API. This will provide us with proven platform and a working calendar system.



Google Calendar API


Google calendar already handle all the initial requirements all that is left is to understand how to use the Google Calendar so we can have our reusable and reliable calendar system.


Google calendar API already uses REST access. They also uses their own GData protocol. Learning Google data API protocol is a good addition since it is also used by "All" Google apps.



Overview of the Google Calendar API


GData uses either of two standard XML-based syndication formats: Atom or RSS. RSS has been in the internet for a longer time and Atom is more recent but provide more features.


Atoms provides Atom Publishing Protocol(APP), an HTTP-based protocol for publishing and editing web resources. It provides the create, edit, delete resource command using POST, PUT, or DELETE respectively. This is the same access as WebDAV and the newer protocol the CalDAV.


In RSS you cannot provide a query to server to get a response containing list of matching results. GData uses the standard extension mechanism of Atom to allow queries.


Read a more thorough overview of the GData protocol here

Sunday, November 18, 2007

Backup and Restore SVN Repository

Issue the following command:

svnadmin dump /var/repo/myRepo > /home/username/myRepoBackup

To restore the repository to a different machine or a different repository, create the new repository by issuing the following command:

svnadmin create /home/username/repo

To restore the data, enter this command:

svnadmin load /home/username/repo < /home/username/myRepoBackup

For more information see the free svn book about the topic here.

Friday, July 13, 2007

Monitoring the status of servers

I have been studying options on how to manage servers the easy way and came across this study:

Attacking Log analysis tools