linea21-modules
[ class tree: linea21-modules ] [ index: linea21-modules ] [ all elements ]

Source for file class.auth.php

Documentation is available at class.auth.php

  1. <?php
  2. /**
  3.  * @package linea21.modules
  4.  * @subpackage auth
  5.  * @author Simon Georget <simon@linea21.com>
  6.  * @version $id SVN
  7.  * @access public
  8.  * @license http://opensource.org/licenses/gpl-3.0.html
  9.  *  Plugin Management
  10.  */
  11.  
  12. class auth {
  13.  
  14.  
  15.     protected $dispatcher = null;
  16.  
  17.     public function __construct({
  18.         $this->dispatcher = $GLOBALS['dispatcher'];
  19.     }
  20.  
  21.     public function __call($method$arguments{
  22.         $event $this->dispatcher->notifyUntil(new sfEvent($this'auth.extensible_function'array(
  23.                 'method'    => $method,
  24.                 'arguments' => $arguments
  25.         )));
  26.         if (!$event->isProcessed())
  27.         {
  28.             throw new Exception(sprintf('Call to undefined method %s::%s.'get_class($this)$method));
  29.         }
  30.  
  31.         return $event->getReturnValue();
  32.     }
  33.  
  34.     /**
  35.      * AuthenthificationProcess()
  36.      * Authentification d'un utilisateur
  37.      *
  38.      * @param  $login 
  39.      * @param  $pass 
  40.      * @param  $type ='PUBLIC' || 'ADMIN'
  41.      * @return boolean (true) ou message d'erreur
  42.      */
  43.     public function logIn($login$pass$type)
  44.     {
  45.  
  46.         // Notify the beginning of the current method
  47.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.logIn'array('login' => $login'password' => $pass'type' => $type)));
  48.  
  49.         if($type == 'PUBLIC'$src LOG_PUBLIC_ACCESS;
  50.         if($type == 'ADMIN'$src LOG_ADMIN_ACCESS;
  51.         logfile($srcarray($type$login$_SERVER['HTTP_USER_AGENT']$_SERVER['HTTP_REFERER']$_SERVER['REQUEST_METHOD']$_SERVER['REQUEST_URI']$_SERVER['QUERY_STRING']$_SERVER['HTTP_COOKIE']i2c_realip()));
  52.  
  53.         $crypt_pass crypt($passSALT_CRYPT);
  54.         list($login$crypt_pass$GLOBALS['sql_object']->DBEscape(array($login$crypt_pass));
  55.  
  56.         $data $GLOBALS['sql_object']->DBSelect(SQL_Get_UserInfo4Auth($login$crypt_pass));
  57.  
  58.         if ($data != && count($data== 1{
  59.  
  60.             // préparation du tableau de droits
  61.             $right['dashboard'$data[0]['rights_dashboard'];
  62.             $right['workshop'$data[0]['rights_workshop'];
  63.             $right['project'$data[0]['rights_project'];
  64.             $right['publication'$data[0]['rights_publication'];
  65.             $right['news'$data[0]['rights_news'];
  66.             $right['yellowpages'$data[0]['rights_yellowpages'];
  67.             $right['theme'$data[0]['rights_theme'];
  68.             $right['scale'$data[0]['rights_scale'];
  69.             $right['level'$data[0]['rights_level'];
  70.             $right['category_user'$data[0]['rights_category_user'];
  71.  
  72.             $_SESSION['authenticated'true;
  73.             $_SESSION['userid'base64_encode($data[0]['user_id']);
  74.             $_SESSION['userwhois'base64_encode($crypt_pass);
  75.             $_SESSION['userlogin'$login;
  76.             $_SESSION['lastcon'$data[0]['user_last_con'];
  77.  
  78.             if(!empty($data[0]['profile_firstname'])) {
  79.                 $_SESSION['userfirstname'$data[0]['profile_firstname'];
  80.             }
  81.             if(!empty($data[0]['profile_lastname'])) {
  82.                 $_SESSION['userlastname'$data[0]['profile_lastname'];
  83.             }
  84.             $_SESSION['userright'$right;
  85.  
  86.             $workgroups $this->retrieveUserWorkgroups(array('login' => $login'pass' => $crypt_pass));
  87.             $table['workshop'base64_encode($workgroups);
  88.             $table['lifetime'time(3600 720;
  89.  
  90.             $this->destroyUserCookie();
  91.             
  92.             // we update the last connexion field
  93.             $GLOBALS['sql_object']->DBQuery('UPDATE l21_user SET user_last_con = NOW() where user_id=' $data[0]['user_id'';');
  94.  
  95.             // Filter data event + return value
  96.             $r $this->dispatcher->filter(new sfEvent(__FUNCTION__'auth.extend_session'array('data' => $table))$table);
  97.             $table $r->getReturnValue();
  98.  
  99.             $this->setUserCookie($table);
  100.  
  101.  
  102.             if ($type == 'ADMIN'{
  103.                 // si est simple utilisateur
  104.                 if (!array_search ('A' $right&& !array_search ('O' $right)) {
  105.                     return _t('divers','errorauth');
  106.                     // est utilisateur avec droits
  107.                 }
  108.             }
  109.             return true;
  110.         else {
  111.             $error_msg _t('divers','errorauth');
  112.             return $error_msg;
  113.         }
  114.     }
  115.  
  116.     /**
  117.      * isSimpleUser()
  118.      *
  119.      * @return boolean 
  120.      */
  121.     public function isSimpleUser({
  122.         if(!isset($_SESSION['userright'])) return false;
  123.  
  124.         if(!in_array('A'$_SESSION['userright']&& !in_array('O'$_SESSION['userright'])) {
  125.             return true;
  126.         else {
  127.             return false;
  128.         }
  129.     }
  130.  
  131.     /**
  132.      * isSuperAdmin()
  133.      *
  134.      * @return boolean 
  135.      */
  136.     public function isSuperAdmin({
  137.         return $this->hasRight('theme');
  138.     }
  139.  
  140.     /**
  141.      * logOut()
  142.      *
  143.      * @return boolean 
  144.      */
  145.     public function logOut({
  146.  
  147.         $_SESSION array();
  148.         session_unset();
  149.         session_destroy();
  150.         $this->destroyUserCookie();
  151.     }
  152.     /**
  153.      * hasRight()
  154.      *
  155.      * @param string $item 
  156.      * @return bool 
  157.      */
  158.     public function hasRight($item)
  159.     {
  160.         // Notify the beginning of the current method
  161.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.has_right'array('element' => $item)));
  162.  
  163.         switch ($item{
  164.             case 'category_user':
  165.                 if ($this->GetSessionElement('right_category_user'!= 'A'return false;
  166.                 else return true;
  167.             case 'theme':
  168.                 if ($this->GetSessionElement('right_theme'!= 'A'return false;
  169.                 else return true;
  170.             case 'level':
  171.                 if ($this->GetSessionElement('right_level'!= 'A'return false;
  172.                 else return true;
  173.             case 'scale':
  174.                 if ($this->GetSessionElement('right_scale'!= 'A'return false;
  175.                 else return true;
  176.             case 'yellowpages':
  177.                 if ($this->GetSessionElement('right_yellowpages'!= 'A'return false;
  178.                 else return true;
  179.             case 'news':
  180.                 if ($this->GetSessionElement('right_news'!= 'U'return true;
  181.                 else return false;
  182.             case 'project':
  183.                 if ($this->GetSessionElement('right_project'!= 'A'return false;
  184.                 else return true;
  185.             case 'publication':
  186.                 if ($this->GetSessionElement('right_publication'!= 'U'return true;
  187.                 else return false;
  188.             case 'workshop':
  189.                 if ($this->GetSessionElement('right_workshop'!= 'U'return true;
  190.                 else return false;
  191.             case 'workshoprep':
  192.                 if ($this->GetSessionElement('right_workshop'!= 'U'return true;
  193.                 else return false;
  194.             case 'dashboard':
  195.                 if ($this->GetSessionElement('right_dashboard'!= 'U'return true;
  196.                 else return false;
  197.             default:
  198.                 return false;
  199.         }
  200.     }
  201.  
  202.     /**
  203.      * GetSessionElement()
  204.      *
  205.      * @param string $item 
  206.      * @return $result 
  207.      */
  208.  
  209.     public function GetSessionElement($item)    {
  210.         // Notify the beginning of the current method
  211.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.get_session_element'array('element' => $item)));
  212.  
  213.         switch ($item{
  214.             case 'id':
  215.                 return base64_decode($_SESSION['userid']);
  216.                 break;
  217.             case 'whois':
  218.                 return base64_decode($_SESSION['userwhois']);
  219.                 break;
  220.             case 'login':
  221.                 return $_SESSION['userlogin'];
  222.                 break;
  223.             case 'firstname':
  224.                 if(isset($_SESSION['userfirstname']))
  225.                     return $_SESSION['userfirstname'];
  226.                 break;
  227.             case 'lastname':
  228.                 if(isset($_SESSION['userlastname']))
  229.                     return $_SESSION['userlastname'];
  230.                 break;
  231.             case 'right_theme':
  232.                 return $_SESSION['userright']['theme'];
  233.                 break;
  234.             case 'right_dashboard':
  235.                 return $_SESSION['userright']['dashboard'];
  236.                 break;
  237.             case 'right_workshop':
  238.                 return $_SESSION['userright']['workshop'];
  239.                 break;
  240.             case 'right_project':
  241.                 return $_SESSION['userright']['project'];
  242.                 break;
  243.             case 'right_publication':
  244.                 return $_SESSION['userright']['publication'];
  245.                 break;
  246.             case 'right_news':
  247.                 return $_SESSION['userright']['news'];
  248.                 break;
  249.             case 'right_yellowpages':
  250.                 return $_SESSION['userright']['yellowpages'];
  251.                 break;
  252.             case 'right_scale':
  253.                 return $_SESSION['userright']['scale'];
  254.                 break;
  255.             case 'right_level':
  256.                 return $_SESSION['userright']['level'];
  257.                 break;
  258.             case 'right_category_user':
  259.                 return $_SESSION['userright']['category_user'];
  260.                 break;
  261.             default:
  262.                 return false;
  263.                 break;
  264.         }
  265.     }
  266.  
  267.     /**
  268.      * isWorkgroupUser()
  269.      * check if the current user belongs to a workgroup
  270.      * used in PUBLIC app
  271.      * @param  $workshop_id 
  272.      * @return boolean 
  273.      */
  274.     public function isWorkgroupUser($workshop_id$sql_object)
  275.     {
  276.         // check if user has cookie, if not we exit
  277.         if(!$this->isAuthenticated()) return false;
  278.  
  279.         // if is SuperAdmin we allow the access
  280.         if($this->isSuperAdmin()) return true;
  281.  
  282.         // Notify the beginning of the current method
  283.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.is_workgroup_user'array('id' => $workshop_id)));
  284.  
  285.         include_once('../admin/'.SQL.'.inc.php');
  286.  
  287.         // 1 -we check that login and password stored in sessions are corrects
  288.         $q SQL_Get_UserInfo4Auth($this->GetSessionElement('login')$this->GetSessionElement('whois'));
  289.         $data $sql_object->DBSelect($q);
  290.  
  291.         if ($data != && count($data== && $data[0]['user_id'== $this->GetSessionElement('id')) {
  292.             // 2 - if ok,  we check that the user belongs to the given group
  293.             $r $sql_object->DBSelect(SQL_Get_isWorkgroupUser($data[0]['user_id']));
  294.  
  295.             for ($i 0$i count($r)$i++{
  296.                 if ($workshop_id == $r[$i]['jwu_workshop_id']{
  297.                     return true;
  298.                 }
  299.  
  300.             }
  301.         }
  302.         return false;
  303.     }
  304.  
  305.     /**
  306.      * isWorkgroupOrganiser()
  307.      * check if a given user is workgroup organiser
  308.      * used in ADMIN app
  309.      * @param  $id_user 
  310.      * @param  $sql_object 
  311.      * @param  $id_workshop 
  312.      * @return boolean 
  313.      */
  314.     public function isWorkgroupOrganiser($id_user$sql_object$id_workshop)
  315.     {
  316.         // Notify the beginning of the current method
  317.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.is_workgroup_organiser'array('id' => $id_user'workgroup_id' => $id_workshop)));
  318.  
  319.         $data $sql_object->DBSelect(SQL_Get_isWorkgroupOrganiser($id_user));
  320.  
  321.         for ($i 0$i count($data)$i++{
  322.             if ($id_workshop == $data[$i]['jwu_workshop_id'])
  323.                 return true;
  324.         }
  325.         return false;
  326.     }
  327.  
  328.     public function isAuthenticated({
  329.         if(isset($_SESSION['authenticated'])) return true;
  330.         else return false;
  331.     }
  332.  
  333.     /**
  334.      * identifyWithLogin()
  335.      * Authentification d'un utilisateur
  336.      * par son login seulement
  337.      * (oubli du mot de passe)
  338.      * @param  $login 
  339.      * @return array 
  340.      */
  341.     public function identifyWithLogin($login{
  342.  
  343.         // Notify the beginning of the current method
  344.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.identify_login'array('login' => $login)));
  345.  
  346.         $data $GLOBALS['sql_object']->DBSelect(SQL_get_UserInfo($login));
  347.         if ($data != && count($data== 1{
  348.             return $data[0];
  349.         else {
  350.             return false;
  351.         }
  352.     }
  353.  
  354.  
  355.     /**
  356.      * retrieveUserWorkgroups()
  357.      * Retrieve Workgroups for a given user
  358.      * @param  $a array (optional)
  359.      * @return string 
  360.      */
  361.     public function retrieveUserWorkgroups($a array()) {
  362.  
  363.         // user already logged-in
  364.         if(isset($_SESSION['authenticated'])) {
  365.             $login $_SESSION['userlogin'];
  366.             $crypt_pass base64_decode($_SESSION['userwhois']);
  367.         }
  368.         else
  369.         {
  370.             if(isset($a['login'])) {
  371.                 $login $a['login'];
  372.                 $crypt_pass $a['pass'];
  373.             else {
  374.                 return false;
  375.             }
  376.         }
  377.  
  378.         // Notify the beginning of the current method
  379.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.retrieve_user_workgroups'array('login' => $login'password' => $crypt_pass)));
  380.  
  381.         $data_w $GLOBALS['sql_object']->DBSelect(SQL_Get_UserWorkshop($login$crypt_pass));
  382.         $workgroups '';
  383.         for ($i 0$i count($data_w)$i++{
  384.             $workgroups .= "/" $data_w[$i]['jwu_workshop_id'];
  385.         }
  386.         return $workgroups;
  387.  
  388.     }
  389.  
  390.     /**
  391.      * updateSessionPassword()
  392.      * Mise à jour du mot de passe en session (cas de changement)
  393.      * @param  $newpass 
  394.      * @return boolean 
  395.      */
  396.     public function updateSessionPassword($newpass{
  397.  
  398.         // Notify the beginning of the current method;
  399.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.update_cookie_password'));
  400.  
  401.         $_SESSION['userwhois']base64_encode($newpass);
  402.  
  403.         return true;
  404.     }
  405.  
  406.     /**
  407.      * updateCookieWorkshop()
  408.      * Update workgroups list in cookie
  409.      * used in public/ part
  410.      *
  411.      * @return void 
  412.      */
  413.     function updateCookieWorkshop()
  414.     {
  415.         // Notify the beginning of the current method
  416.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.update_cookie_workshop'));
  417.  
  418.         $str base64_encode($this->retrieveUserWorkgroups());
  419.         setcookie('linea21[workshop]'$str$_COOKIE['linea21']['lifetime']'/');
  420.     }
  421.  
  422.     /**
  423.      * setUserCookie()
  424.      * set les infos contenu en tableau PHP en cookie
  425.      *
  426.      * @param  $array 
  427.      * @return void 
  428.      */
  429.     public function setUserCookie($array)
  430.     {
  431.         // Notify the beginning of the current method
  432.         // Filter data event + return value
  433.         $r $this->dispatcher->filter(new sfEvent(__FUNCTION__'auth.set_user_cookie'array('data' => $array))$array);
  434.         $array $r->getReturnValue();
  435.  
  436.         $cookie_expires $array['lifetime'];
  437.         while (list($key$value@each($array)) {
  438.             setcookie('linea21[' $key ']'$value$cookie_expires'/');
  439.         }
  440.     }
  441.  
  442.     /**
  443.      * destroyUserCookie()
  444.      * Détruit les infos contenues en cookie
  445.      *
  446.      * @return void 
  447.      */
  448.     public function destroyUserCookie()
  449.     {
  450.         // Notify the beginning of the current method
  451.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.destroy_user_cookie'));
  452.  
  453.         while (list($key$val@each($_COOKIE['linea21'])) {
  454.             setcookie('linea21[' $key ']'''(time(3600)'/');
  455.         }
  456.     }
  457.  
  458.     /**
  459.      * setCookieLastPage()
  460.      * used for the admin/ part
  461.      */
  462.     public function setCookieLastPage({
  463.  
  464.         // Notify the beginning of the current method
  465.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.set_cookie_lastpage'));
  466.  
  467.         if(isset($_SESSION['authenticated'])) {
  468.             setcookie('linea21_lastpage''index.php?'.$_SERVER['QUERY_STRING'](time(3600 720)'/');
  469.             setcookie('linea21_lastuser'$_SESSION['userlogin'](time(3600 720)'/');
  470.             setcookie('linea21_lastactivity'time()(time(3600 720)'/');
  471.         }
  472.         return true;
  473.     }
  474.     /**
  475.      * isActive()
  476.      * check if last activity is less than SESS_INACTIVITY_MAXTIME
  477.      * If more, destroy the session
  478.      */
  479.     public function isActive({
  480.         
  481.         if (!$this->isAuthenticated()) return true;
  482.         
  483.         if (isset($_SESSION['LAST_ACTIVITY']&& (time($_SESSION['LAST_ACTIVITY'SESS_INACTIVITY_MAXTIME)) {
  484.             // last request was more than SESS_INACTIVITY_MAXTIME (in seconds) ago
  485.             session_unset();     // unset $_SESSION variable for the run-time
  486.             session_destroy();   // destroy session data in storage
  487.             $_SESSION array();
  488.         }
  489.         $_SESSION['LAST_ACTIVITY'time()// update last activity time stamp
  490.     }
  491.  
  492. }
  493.  
  494. ?>

Documentation generated on Mon, 08 Apr 2013 18:12:11 +0200 by phpDocumentor 1.4.1