08 April 2012

Integrate Vtiger with CAS

Few months ago, I had a project, where I had to integrate Vtiger with CAS i.e Centralized Authentication Service For more information on CAS, Please refer this link CAS
After long time research, I successfully managed to make it work with Vtiger.

Use following simple steps to integrate it

Download Php - PEAR classClick here
Download CAS module from - Here
Edit file: /vtiger530/modules/Users/Authenticate.php

Replace lines 28-37

global $mod_strings, $default_charset;
$focus = new Users();
// Add in defensive code here.
$focus->column_fields["user_name"] = to_html($_REQUEST['user_name']);
$user_password = vtlib_purify($_REQUEST['user_password']);
$focus->load_user($user_password);

With following Code(change bold items):

global $mod_strings, $default_charset;
require_once('modules/CAS.php');
phpCAS::setDebug();
phpCAS::client(CAS_VERSION_2_0,'CAS_SERVER_IP_HERE',CAS_SERVER_PORT_HERE,'CAS_SERVER_WEBFOLDER_HERE',FALSE);
phpCAS::setNoCasServerValidation();
phpCAS::forceAuthentication();
$focus = new Users();
$focus->column_fields["user_name"] = phpCAS::getUser();
$user_password = "no_use";
$focus->load_user_cas($user_password);

Edit file: /vtiger530/modules/Users/Users.php
Add this function into class User
function load_user_cas($user_password) {
 $usr_name = $this->column_fields["user_name"];
 if(isset($_SESSION['loginattempts'])){
  $_SESSION['loginattempts'] += 1;
 } else{
  $_SESSION['loginattempts'] = 1;
 }
if($_SESSION['loginattempts'] > 5){
   $this->log->warn("security: " . $usr_name . " has attempted to login ".$_SESSION['loginattempts'] . " times.");
   }
   $this->log->debug("starting user load for $usr_name");
   $validation = 0;
   unset($_SESSION['validation']);
   if( !isset($this->column_fields["user_name"]) || $this->column_fields["user_name"] == "" || !isset($user_password) || $user_password == "") return null;
   if($this->validation_check('aw5jbhvkzs9pbwfnzxmvc3vnyxjzywxlc19tzc5nawy=','1a44d4ab8f2d6e15e0ff6ac1c2c87e6f', '866bba5ae0a15180e8613d33b0acc6bd') == -1)$validation = -1;
  if($this->validation_check('aw5jbhvkzs9pbwfnzxmvcg93zxjlzf9iev9zdwdhcmnybs5nawy=' ,'3d49c9768de467925daabf242fe93cce') == -1)
                $validation = -1;
   if($this->authorization_check('aw5kzxgucghw' ,'peegahjlzj0nahr0cdovl3d3dy5zdwdhcmnybs5jb20nihrhcmdldd0nx2jsyw5rjz48aw1nigjvcmrlcj0nmccgc3jjpsdpbmnsdwrll2ltywdlcy9wb3dlcmvkx2j5x3n1z2fyy3jtlmdpzicgywx0psdqb3dlcmvkiej5ifn1z2fyq1jnjz48l2e+', 1) == -1)
                $validation = -1;
   $encrypted_password = $this->encrypt_password($user_password);
   $authcheck = true;

   if(!$authcheck){
                $this->log->warn("user authentication for $usr_name failed");
                return null;
   }
   $query = "select * from $this->table_name where user_name='$usr_name'";

   $result = $this->db->requiresingleresult($query, false);

   $row = $this->db->fetchbyassoc($result);
   echo $this->id = $row['id'];
   $user_hash = strtolower(md5($user_password));

   // if there is no user_hash is not present or is out of date, then create a new one.
   if(!isset($row['user_hash']) || $row['user_hash'] != $user_hash){
                $query = "update $this->table_name set user_hash='$user_hash' where id='{$row['id']}'";
                $this->db->query($query, true, "error setting new hash for {$row['user_name']}: ");
   }
   $this->loadpreferencesfromdb($row['user_preferences']);
   if ($row['status'] != "inactive")
                $this->authenticated = true;
   unset($_SESSION['loginattempts']);
   return $this;
  }

Till this step, You will be able to able to authenticate your self in CAS, Now we need to validate ticket received from CAS server and prepare session of CAS in order to authenticate user for accessing application
For this add following line of code , inside index.php in root folder

If(isset($_GET[‘ticket’]) && $_GET[‘ticket’] != ‘’ && !isset($_SESSION[‘phpCAS’])){
  includes(‘modules/Users/Authenticate.php’);
}


Now we will be able to validate our ticket from CAS server and prepare sessions in order to authenticate in Vtiger.

Changes for Logout from CAS from Vitger
Edit :- Logout.php

Added Following Line of Code to enable logout from CAS session in vtiger

require_once('modules/CAS.php');
phpCAS::client(CAS_VERSION_2_0,'CAS Server',8443,'cas-web',FALSE);
phpCAS::logoutWithRedirectService('Your Site Name');


I hope these steps will help you in your Projects
If still have any doubts , feel free to put your doubts here

06 November 2011

How to install apache and php on Linux Red Hat 64 bit

In order to enable server listen web requests, we need to install apache on server
Following steps have been performed on linux red hat 64 bit operating system 
1:- First login as root on server through Putty
 
2:- Perform yum installation of apache (Yum is an automatic updater and  package installer/remover for rpm systems) 
   Type following commands
 # yum install httpd httpd-devel 
 
After installation test your installation by executing following command
 
# service httpd start
With this, your apache has been successfully started.
 
3:- Configure APACHE
 For this, we have to edit httpd.conf file located at /etc/httpd/conf/http.conf
 
Type following command
 # sudo nano httpd.conf
 
Now edit following entry
Servername IP ADDRESS:80
 
Now restart your server, type following command
 # service httpd restart
 Your apache installation is complete now
 
2:- Install PHP
 
1:- Type following command
 
# yum install php
With this your PHP have been installed on server
 
2:- Create a file named index.php where by default apache will point to
 
Type following commands
 
# cd /var/www/html
# sudo nano index.php
 
Now type following code inside it 
<?php phpinfo(); ?> 
and Save it 
 
3:- Restart apache
 
# service httpd restart
 
Final Step is to test installation of php and apache on server
 
Open web browser and type 
https://your ip address
 
It should open all the information about php extensions etc
 
Note:- 
 If after successful installation of apache,it does not listen to web request simply type following commands
 # iptables -I INPUT -p tcp --dport 80 -j ACCEPT
# /etc/init.d/iptables save 

How to create and Instance on Amazon?

Login to amazon web services, Go to http://aws.amazon.com/
Now Go to Amazon EC2 and Launch Instance


 

Select Instance of your choice.
(In this case, we selected below mention machine).
Red Hat Enterprise Linux 6.1 64-bit (AMI Id: ami-31d41658)
Red Hat Enterprise Linux 6.1, EBS-boot, 64-bit architecture.
Root Device Size: 6 GB

Select Instance Type

Default Options

Give name key (not necessary)
(This will be the name of your Amazon EC2 instance).



Create new key pair and download it to your local machine.

A .pem file will be downloaded to your machine. Keep it at safe place. It is the key which allow you to enter into Linux box.
Should be kept very safe.
Now create a new security group and add a new rule for SSH (port 22) connection. Adding this rule will allow you to connect to Linux instance through PuTTY or WinSCP (or any client which can establish SSH connection) from your local machine, which will establish ssh connection.


 Review and Launch Instance


Within a minute, you will be able to see that instance is running.
Important to note:-
  • If instance is not in use, stop the instance. You will not lose the data.
  • To remove the EC2 instance permanently, terminate it from “Instance Actions” menu.
  • Once you terminate the instance, it is gone forever.
  • Stopping an instance detach Elastic IP  associated with it.
  • Security key is your gate pass to enter into Linux box. Keep it very safe.
  • To enhance security, always associate a keypassphrase with PuTTY key.