Tuesday, August 24, 2010

Yii - Authentication from mysql database using a md5 password

Before proceeding make sure the database has been connected to Yii application. (see previous post)
For this example, the passwords are stored in the database as md5 hash.
You may be required to change the password encoding scheme.

Edit the authenticate method in ../my_app/protected/components/UserIdentity.php to look like this -

public function authenticate()
    {
        //$users=array(
            // username => password
            //'demo'=>'demo',
            //'admin'=>'admin',
        //);
       
        $user = myUsersTable::model()->findByAttributes( array( 'my_userid_column_name' => $this->username));
        if ($user===null) { // No user was found!
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        }
        // $user->Password refers to the "password" column name from the database
        else if($user->Password !== md5("my_salt1".$this->password))
        {   
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        }
        else { // User/pass match
            $this->errorCode=self::ERROR_NONE;
        }       
        return !$this->errorCode;
    }

Enter the username/password pair on the login page and you should be good to go.

Friday, August 6, 2010

A UNIX survival guide for students


1. Basic UNIX commands
    > ls
        List the contents in current directory.   
    > ls -alF
        List the contents in long format in the current directory.
       
    > cd directoryname
        Change current directory to another directory.
       
    > mkdir directoryname
        Create a directory.
       
    > cp file1.txt file2.txt
        Make a copy of file1.txt.
       
    > mv /home/harpreet/file1.txt /tmp/file2.txt
        Move the file to different location or can be used for renaming a file.
       
    > rm filename.txt
        Delete a file.
       
    > rm -R directoryname
        Delete the contents of a directory.
       
    > head -20 filename.txt
        Get 20 lines from the top of the file.
       
    > tail -30 filename.txt
        Get 30 lines from the bottom of the file.
   
2. vi filename.txt
    Edit a file.
    a. Esc dd
        Deletes the current line
    b. Esc u
        Undo the last action
    c. Esc :0       (zero)
        Move cursor to first line of file
    d. Esc $
        Move to end of current line
    e. Esc Shift+g
        Move to end of file
    f. Esc yy
        Copy the current line
        Esc y3y
        Copy the next three lines
    g. Esc p
        Paste the copied line(s)
    h. Esc :35
        Moves the cursor to line number 35
    i. Esc /wordtofind
        Find the word within the file. Press 'n' to move to the next occurrence.
       
3. > grep 'wordtofind' filename.txt
        Finds the line containing a string from the file
       grep -i 'wordtofind' filename.txt
        Case-insensitive find.

4. > find /tmp/directoryname -name test.txt
        Find a file with name 'test.txt' in the given path.

5. > find . -type f -exec grep -i Row2 {} \; -print
        Find the word 'row2' from all the files in current path.
        Also, shows the line containing an occurrence of the word.
   
6. > scp harpreet@hostname.edu:/tmp/file.txt .
        Copy a file located on another server to the current directory on the present server.
   
    > scp file.txt harpreet@hostname.edu:.
        Copy a file from current server to another server into your home directory.
   
    > scp file.txt harpreet@hostname.edu:/home/directory/file.txt
        Copy a file from one machine to another.   
   
7. > tar cvzf new_tar_file.tar.gz file1.txt file2.txt
        Create a tar file new_tar_file.tar.gz containing file1.txt and file2.txt
   
    > tar xvzf abc.tar.gz
        Untar the contents of a file abc.tar.gz to the current directory
   
8. awk - Let us create a file 'test.txt' with these contents
       Row1-column1 Row1-column2 Row1-column3 Row1-column4
   Row2-column1 Row2-column2-HARPREET Row2-column3 Row2-column4
   Row3-column1 Row3-column2 Row3-column3 Row3-column4-SINGH
       
    Observe the output for various commands
    >  awk '{print $2}' test.txt
      Row1-column2
    Row2-column2-HARPREET
    Row3-column2
        The above command prints the second column
       
    String matching using awk :
    >  awk '{if ( $4 ~ /SINGH/ ) { print $2 $3}}' test.txt
    Row3-column2Row3-column3
        The above command matches the string 'SINGH' and prints column 2 and 3.   

Corrections/improvements/questions are welcome :)