2012-10-21

Yii Tips: Add user role to accessRules() in controller


By default there is no such thing as 'role'.

To add role to accessRules() in controller you could use expressions... smth like this: 'expression' => function(){return Yii::app()->user->getState("role") == 'admin';}

My accessRules():
public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions                                                                
                'actions'=>array('index','view'),
                'users'=>array('*'),      
            ),              
            array('allow', // allow authenticated user to perform 'create' and 'update' actions                                                      
                'actions'=>array('create','update'),
                'users'=>array('@'),      
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions                                                              
                'actions'=>array('admin','delete'),
                //Check if user role is admin                                                  
                'expression' => function(){return Yii::app()->user->getState("role") == 'admin';},
            ),              
            array('deny',  // deny all users  
                'users'=>array('*'),      
            ),              
        );


BTW, you need to set state for user's role in userIdentity component in authenticate.. like this $this->setState('role', $user->role);
My authenticate() in userIdentity.php:
public function authenticate()
    {
        $user = User::model()->findByAttributes(array('email'=>$this->username));

        if ($user===null) { // No user found!
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        } else if ($user->password !== md5($this->password) ) { // Invalid password!
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        } else { // Okay!    
            $this->errorCode=self::ERROR_NONE;
            $this->setState('email', $user->email);
            $this->setState('username', $user->username);
            // Store the role in a session: 
            $this->setState('role', $user->role);
            $this->_id = $user->id;      
        }
        return !$this->errorCode;    
    }

2012-10-20

My Vim Cheat Sheet



COMMAND mode:
:ci( - replace text between brackets
:ci" - replace text between quotes
ctrl+g - see where you are
g ctrl+g - advanced where you are
`. - jum to location of last modification
dtx - deletes upto and including the next x
dftx - deletes downto the previous x
:%s/TEXT//ng - count the number of matches
:e - reload current file (similar to :edit)
:set ft=html - change filetype (for example, to indent html inside php file)
Splited views (also helpfull with NERDTree plugin):
ctrl+ww - toogle between splited views 
ctrl+wv - split vertical
ctrl+wh - split horizontal

INSERT mode:
ctrl+t - insert indent to current line
ctrl+d - delet indent to current line
ctr+y - insert chars frome above line
ctrl+e - insert chars frome below line

ctrl+x xtrl+o - omni autocomplete(ctrl+n, ctrl+p to move next/previos line in omni autocomplete)


VISUAL mode:
ctrl+v - enter VISUAL BLOCK selection mode
Adding smth to multipple lines: select VISUAL BLOCK, press "I" (capital i) type smth you want to add (it appears only in first line which were selected), hit "ESC"... tadam chars you've entered added to every line which been previously selected. BTW: instead of "I" you could type "c" - it will cut selected blocks and qfter that you could type anything you want (also helpfull to uncomment block of code)


...to be continued

Other stuff:
:cd %:p:h '  - will change the working directory to the dir for current editing file
J - will join that line and the next line together (shift+j)

2012-10-19

Mac OS terminal/iterm tips (autocomplete, ignore case etc.)


add this to your ~/.inputrc:



#Pressing alt+left/right arrow force cursor to jump 1 word left/right
"\e[1;9D": backward-word
"\e[1;9C": forward-word
#this will print all ambiguous variant if you presse TAB
set show-all-if-ambiguous ON
#ignore case in files/folders names in terminal
set completion-ignore-case on
#tab cycles through completion
TAB: menu-complete
#shift+TAB to show all ambiguous variant
"\e[Z": complete 

2012-10-15

Install rockmongo in MAMP in mac os X


You should have mongodb installed and running on standart port. mamp instaled.
Turn off MAMP (if it's running)
Download sources from https://github.com/mongodb/mongo-php-driver to /Users/your_username/Temp/
Just do like written here in Installation section: https://github.com/mongodb/mongo-php-driver
phpize
./configure
make
sudo make 

after that you will have mongo.so and mongo.la somewhere 
in /Users/your_username/Temp/mongodb-mongo-php-driver-622828c/modules/

Then copy mongo.so and mongo.la to your MAMP (path should be smth like this):
/Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/no-debug-non-zts-20090626/

Edit your php.ini, it will be somwhere here: 
/Applications/MAMP/bin/php/php5.3.6/conf). Add this line to it's extension section:
extension=mongo.so

Turn on MAMP
Check phpinfo at http://localhost/MAMP/ (search for 'mongo')
Go to rockmongo http://localhost/rockmongo
Thanks to: 
http://www.joyceleong.com/log/mongodb-with-mamp-on-os-x/
https://github.com/mongodb/mongo-php-driver

2012-10-14

Writing An Hadoop MapReduce Program In Python

Great article: Writing An Hadoop MapReduce Program In Python


My bash history (replace "const" with your username):
  543  cd /usr/local/Cellar/hadoop/1.0.3/libexec
  544  hadoop dfs -copyFromLocal /Users/const/Dev/PrjGit/mrpy/tmp/ /user/const/mrpy
  545  hadoop dfs -la
  546  hadoop dfs -ls
  547  hadoop dfs -ls /user/const/mrpy/
  550  jar /usr/local/Cellar/hadoop/1.0.3/libexec/contrib/streaming/hadoop-*streaming*.jar -file /Users/const/Dev/PrjGit/mrpy/mapper.py -mapper /Users/const/Dev/PrjGit/mrpy/mapper.py -file /Users/const/Dev/PrjGit/mrpy/reducer.py -reducer /Users/const/Dev/PrjGit/mrpy/reducer.py -input /user/const/mrpy/* -output /user/const/mrpy-output
  551  hadoop dfs -ls /user/const/mrpy-output
  552  hadoop dfs -cat /user/const/mrpy-output/part-00000

2012-10-09

Automator script: open chosen/selected folder in iTerm terminal

Here is the AppleScript:
on run {input, parameters}
   tell application "iTerm"
   activate
      repeat with someFilename in input
         make new terminal
         tell the first terminal
            activate current session
            launch session "Default Session"
            tell the last session
               set thePath to (quoted form of POSIX path of ¬
                  (someFilename as string))
               set cdCommand to "if [ -d " & thePath & " ]; then cd " ¬ 
                  & thePath & "; else cd `dirname " & thePath & ¬ 
                  "`; fi; clear"
               write text cdCommand
            end tell
         end tell
      end repeat
   end tell
   return input
end run

2012-10-06

Iterm2 how to setup word jumpig using alt+left alt+right arrow


In Preferences->Profiles->Keys:

find "option ←" and edit its "action" like this: select "send escape sequence" and in the "escape sequence box" enter "b"

find "option  →" and edit its "action" like this: select "send escape sequence" and in the "escape sequence box" enter "f"

Add this to your ~/.inputrc:
#Pressing alt+left/right arrow force cursor to jump 1 word left/right
"\e[1;9D": backward-word
"\e[1;9C": forward-word