Ubuntu How to add programs to the launcher (search).

In order to add a program (e.g opera) to the search launcher at the top left corner of Ubuntu add a file like opera.desktop to:

   ~/.local/share/applications/opera.desktop

Add the following contents to the above file:

   [Desktop Entry]
   Type = Application
   Name = My Opera
   Comment = Open opera from opera.desktop
   Exec = /usr/bin/opera
   Icon = /usr/share/opera/styles/images/Opera_256x256.png

Now when you type My Opera in the search launcher you will be able to find the applincation and open it.

apache virtual host redirect

If a resource has moved to another server, you may wish to have URLs continue to work for a time on the old server while people update their bookmarks.
Simply add (similar) to the following to your virtual host:

   
   <VirtualHost 199.1.1.1:80> 
	ServerName www.siteaddress.com
	ServerAlias siteaddress.com
	DocumentRoot /var/www/site
	RewriteEngine on
	RewriteRule ^/blog/(.+) http://blog.siteaddress.com/$1 [R,L]
   </VirtualHost>

Yii PHPUnit Testing PHP Fatal error: Class ‘CActiveRecord’ not found..Netbeans

When you create PHPUnit tests for your models in Yii using netbeans you may encounter this problem

PHP Fatal error:  Class 'CActiveRecord' not found..

The easiest solution is to include the following at the top of the model class, for example if the class is User then your code should look like


$yii='/opt/lampp/htdocs/yii/framework/yii.php';
require_once($yii);

class User extends CActiveRecord
{
// ......

}

Cheers 🙂

Yii Rights Extension – Error 403. There must be at least one superuser! – SOLVED

In my previous post I demonstrated how to install Yii Rights extension easily and quickly. We commented out the code for checking superuser given below.

Now to address this issue simply edit file /modules/rights/components/RAuthorizer.php and comment out the lines near line no 304 and 305.

    //if( $superusers===array() )
         //  throw new CHttpException(403, Rights::t('core', 'There must be at least one superuser!'));

However it will make any user to use the Rights interface, even the not logged in user can use this module and change permission which is not good for a production environment.

In order to enable user permission for using the Rights module I could not find any solution on searching the internet. Then I come up with a manual but very easy procedure. I entered a two entries in the database manually. .

1. There must be a record in the table AuthItem table with the name (e.g. admin ). If there is no such record add it manually.
2. Now add a record to Rights table with the itemname similar in step 1. (e.g. admin). The other two columns type and weight does not really matter here so put any integer value (e.g. 0).
3. Now uncomment the code above for if( $superusers===array() ).

Now only the user admin will be able to use Rights module and grant or revoke permissions because in Rights Extension now admin is the superuser.

Cheers 🙂

Using YiiSmartMenu with Rights Extension

Yii Smart can be used out of the box (require only a few steps) with Yii Rights Extension. The purpose of the Yii Smart menu is to show those menu items to the user for which the logged in user has access to. Yii smart menu can work with Rights Extension, RBAC etc. In order to install Yii Rights you can read in my previous post.

Follow the following steps to use YiiSmartMenu widget.

1. Download the widget class from here  http://www.yiiframework.com/extension/yiismartmenu/ and copy it to your components folder i.e yii_app/protected/components.

2. In order to change top menu open yii_app/protected/views/layouts/main.php and replace echo occurrence of  $this->widget('zii.widgets.CMenu', ... TO

$this->widget('application.components.YiiSmartMenu', array(
       'partItemSeparator'=>'.',
       'upperCaseFirstLetter'=>true,

The partItemSeparator concatenates ControllerID to ActionID e.g. Site and Index would be concatenated as Site.Index. Rights use authitem in this format and so ‘.’ is compatible with Rights.
Similarly upperCaseFirstLetter makes ControllerID and ActionID uppercase like in Site.Index, again its compatible with Rights Extension. ( You can verify this in AuthItem table of Rights extension that each operation is formed as ControllerID.ActionID).

if you want to check access to an individual action in your code use:

     Yii::app()->user->checkAccess("Site.Index", $_GET)';

The above code will return true if the current logged in user has access to site/index action.
If you want any menu item should be not be checked by YiiSmartMenu and hence should be visible always e.g. a Home link you can add visible property of the menu item like:

    'items'=>array(
         array('label'=>'Home', 'url'=>array('/site/index'), 'visible' => true),

Please note that any top menu item for which permissions is in the form of ControllerID.* in the Rights extension does not work, it may be a bug in this extension. In order to sort this out simply grant permissions like ControllerID.ActionID to the top menu item in the Rights extension.

🙂

Yii install Rights in easy steps – How to install Yii Rights

A few days ago I had to use Yii user permissions for a client website. I spent days looking here and there on google and trying several alternatives but no one worked for me easily. I tried several Yii extensions but I was not lucky to get any one working.
Finally I found Yii Rights extension but I could not any working instructions or a complete example. I decided to write this simple tutorial.

I used the following steps in order to use Yii Rights in my project.

1. Download the extension from its official site here. (http://www.yiiframework.com/extension/rights/). and extract the archive. Place the folder rights in protected/modules/ folder. If modules folder does not exist create it. The Yii application structure should look like the following:

    
    yii_project
    |
    |----protected
         |----models
         |----modules
              |----rights

2. Add the following lines to your application’s configuration. You would need to make the changes in three different places as shown below.

      'import'=>array(               // first place
          ......
          'application.modules.rights.*',
          'application.modules.rights.components.*', // Correct paths if necessary.
      ),
      .....
      'modules'=>array(             // second place
		.....
                'rights' => array(
                    'install' => FALSE,   // we will install tables manually
                ),
       ),
       ....
       'components'=>array(        // third place
                 'user'=>array(
                        'class' => 'RWebUser', 
	                 // enable cookie-based authentication
	                 'allowAutoLogin'=>true,
                  ),
                 'authManager' => array(
                         'class' => 'RDbAuthManager',
                  ),

3. You must have/create a table User with the columns id and username. Now copy the following SQL and run it.
in your database manually (may be one by one table). It will create four tables in your DB.

delimiter $$

CREATE TABLE `AuthAssignment` (
  `itemname` varchar(64) NOT NULL,
  `userid` varchar(64) NOT NULL,
  `bizrule` text,
  `data` text,
  PRIMARY KEY (`itemname`,`userid`),
  CONSTRAINT `AuthAssignment_ibfk_1` FOREIGN KEY (`itemname`) REFERENCES `AuthItem` (`name`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$

CREATE TABLE `AuthItem` (
  `name` varchar(64) NOT NULL,
  `type` int(11) NOT NULL,
  `description` text,
  `bizrule` text,
  `data` text,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$

CREATE TABLE `AuthItemChild` (
  `parent` varchar(64) NOT NULL,
  `child` varchar(64) NOT NULL,
  PRIMARY KEY (`parent`,`child`),
  KEY `child` (`child`),
  CONSTRAINT `AuthItemChild_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `AuthItem` (`name`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `AuthItemChild_ibfk_2` FOREIGN KEY (`child`) REFERENCES `AuthItem` (`name`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$

CREATE TABLE `Rights` (
  `itemname` varchar(64) NOT NULL,
  `type` int(11) NOT NULL,
  `weight` int(11) NOT NULL,
  PRIMARY KEY (`itemname`),
  CONSTRAINT `Rights_ibfk_1` FOREIGN KEY (`itemname`) REFERENCES `AuthItem` (`name`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$

4. Now you can run your Yii application with Yii Rights extension installed by typing the URL:
http://localhost/yiiapp/rights/ or http://localhost/yiiapp/index.php?r=rights/
where yiiapp is the name of your yii application. Here you may receive an error like:

 Error 403. There must be at least one superuser!

Now to address this issue simply edit file /modules/rights/components/RAuthorizer.php and comment out the lines near line no 304 and 305.

 
     //if( $superusers===array() )
         //  throw new CHttpException(403, Rights::t('core', 'There must be at least one superuser!'));

Now if you open the above link you will be on rights page. Now you can create roles, tasks and operations using the rights web interface.

5. The controllers must be extended from BController now instead of CController and the filters function inside each controller using Rights should look like the following.

 
        public function filters()
	{
		return array(
                    'rights'
		);
	}

If you want any action to be ignored by Rigths simply add a minus in from of it in the filters function for example:

 
        public function filters()
	{
		return array(
                    'rights -index'
		);
	}

Furthermore you can remove the accessRules function for all controllers using Rights as it is no more needed.