[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/plugins/user/ -> joomla.php (source)

   1  <?php
   2  /**
   3  * @version        $Id: joomla.php 14401 2010-01-26 14:10:00Z louis $
   4  * @package        Joomla
   5  * @subpackage    JFramework
   6  * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7  * @license        GNU/GPL, see LICENSE.php
   8  * Joomla! is free software. This version may have been modified pursuant
   9  * to the GNU General Public License, and as distributed it includes or
  10  * is derivative of works licensed under the GNU General Public License or
  11  * other free or open source software licenses.
  12  * See COPYRIGHT.php for copyright notices and details.
  13  */
  14  
  15  // Check to ensure this file is included in Joomla!
  16  defined('_JEXEC') or die( 'Restricted access' );
  17  
  18  jimport('joomla.plugin.plugin');
  19  
  20  /**
  21   * Joomla User plugin
  22   *
  23   * @package        Joomla
  24   * @subpackage    JFramework
  25   * @since         1.5
  26   */
  27  class plgUserJoomla extends JPlugin
  28  {
  29      /**
  30       * Constructor
  31       *
  32       * For php4 compatability we must not use the __constructor as a constructor for plugins
  33       * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
  34       * This causes problems with cross-referencing necessary for the observer design pattern.
  35       *
  36       * @param     object $subject The object to observe
  37       * @param     array  $config  An array that holds the plugin configuration
  38       * @since 1.5
  39       */
  40  	function plgUserJoomla(& $subject, $config) {
  41          parent::__construct($subject, $config);
  42      }
  43  
  44      /**
  45       * Remove all sessions for the user name
  46       *
  47       * Method is called after user data is deleted from the database
  48       *
  49       * @param     array          holds the user data
  50       * @param    boolean        true if user was succesfully stored in the database
  51       * @param    string        message
  52       */
  53  	function onAfterDeleteUser($user, $succes, $msg)
  54      {
  55          if(!$succes) {
  56              return false;
  57          }
  58  
  59          $db =& JFactory::getDBO();
  60          $db->setQuery('DELETE FROM #__session WHERE userid = '.$db->Quote($user['id']));
  61          $db->Query();
  62  
  63          return true;
  64      }
  65  
  66      /**
  67       * This method should handle any login logic and report back to the subject
  68       *
  69       * @access    public
  70       * @param   array   holds the user data
  71       * @param     array   array holding options (remember, autoregister, group)
  72       * @return    boolean    True on success
  73       * @since    1.5
  74       */
  75  	function onLoginUser($user, $options = array())
  76      {
  77          jimport('joomla.user.helper');
  78  
  79          $instance =& $this->_getUser($user, $options);
  80  
  81          // if _getUser returned an error, then pass it back.
  82          if (JError::isError( $instance )) {
  83              return $instance;
  84          }
  85  
  86          // If the user is blocked, redirect with an error
  87          if ($instance->get('block') == 1) {
  88              return JError::raiseWarning('SOME_ERROR_CODE', JText::_('E_NOLOGIN_BLOCKED'));
  89          }
  90  
  91          // Get an ACL object
  92          $acl =& JFactory::getACL();
  93  
  94          // Get the user group from the ACL
  95          if ($instance->get('tmp_user') == 1) {
  96              $grp = new JObject;
  97              // This should be configurable at some point
  98              $grp->set('name', 'Registered');
  99          } else {
 100              $grp = $acl->getAroGroup($instance->get('id'));
 101          }
 102  
 103          //Authorise the user based on the group information
 104          if(!isset($options['group'])) {
 105              $options['group'] = 'USERS';
 106          }
 107  
 108          if(!$acl->is_group_child_of( $grp->name, $options['group'])) {
 109              return JError::raiseWarning('SOME_ERROR_CODE', JText::_('E_NOLOGIN_ACCESS'));
 110          }
 111  
 112          //Mark the user as logged in
 113          $instance->set( 'guest', 0);
 114          $instance->set('aid', 1);
 115  
 116          // Fudge Authors, Editors, Publishers and Super Administrators into the special access group
 117          if ($acl->is_group_child_of($grp->name, 'Registered')      ||
 118              $acl->is_group_child_of($grp->name, 'Public Backend'))    {
 119              $instance->set('aid', 2);
 120          }
 121  
 122          //Set the usertype based on the ACL group name
 123          $instance->set('usertype', $grp->name);
 124  
 125          // Register the needed session variables
 126          $session =& JFactory::getSession();
 127          $session->set('user', $instance);
 128  
 129          // Get the session object
 130          $table = & JTable::getInstance('session');
 131          $table->load( $session->getId() );
 132  
 133          $table->guest         = $instance->get('guest');
 134          $table->username     = $instance->get('username');
 135          $table->userid         = intval($instance->get('id'));
 136          $table->usertype     = $instance->get('usertype');
 137          $table->gid         = intval($instance->get('gid'));
 138  
 139          $table->update();
 140  
 141          // Hit the user last visit field
 142          $instance->setLastVisit();
 143  
 144          return true;
 145      }
 146  
 147      /**
 148       * This method should handle any logout logic and report back to the subject
 149       *
 150       * @access public
 151       * @param  array    holds the user data
 152       * @param     array   array holding options (client, ...)
 153       * @return object   True on success
 154       * @since 1.5
 155       */
 156  	function onLogoutUser($user, $options = array())
 157      {
 158          $my =& JFactory::getUser();
 159          //Make sure we're a valid user first
 160          if($user['id'] == 0 && !$my->get('tmp_user')) return true;
 161  
 162          //Check to see if we're deleting the current session
 163          if($my->get('id') == $user['id'])
 164          {
 165              // Hit the user last visit field
 166              $my->setLastVisit();
 167  
 168              // Destroy the php session for this user
 169              $session =& JFactory::getSession();
 170              $session->destroy();
 171          } else {
 172              // Force logout all users with that userid
 173              $table = & JTable::getInstance('session');
 174              $table->destroy($user['id'], $options['clientid']);
 175          }
 176          return true;
 177      }
 178  
 179      /**
 180       * This method will return a user object
 181       *
 182       * If options['autoregister'] is true, if the user doesn't exist yet he will be created
 183       *
 184       * @access    public
 185       * @param   array   holds the user data
 186       * @param     array   array holding options (remember, autoregister, group)
 187       * @return    object    A JUser object
 188       * @since    1.5
 189       */
 190      function &_getUser($user, $options = array())
 191      {
 192          $instance = new JUser();
 193          if($id = intval(JUserHelper::getUserId($user['username'])))  {
 194              $instance->load($id);
 195              return $instance;
 196          }
 197  
 198          //TODO : move this out of the plugin
 199          jimport('joomla.application.component.helper');
 200          $config   = &JComponentHelper::getParams( 'com_users' );
 201          $usertype = $config->get( 'new_usertype', 'Registered' );
 202  
 203          $acl =& JFactory::getACL();
 204  
 205          $instance->set( 'id'            , 0 );
 206          $instance->set( 'name'            , $user['fullname'] );
 207          $instance->set( 'username'        , $user['username'] );
 208          $instance->set( 'password_clear'    , $user['password_clear'] );
 209          $instance->set( 'email'            , $user['email'] );    // Result should contain an email (check)
 210          $instance->set( 'gid'            , $acl->get_group_id( '', $usertype));
 211          $instance->set( 'usertype'        , $usertype );
 212  
 213          //If autoregister is set let's register the user
 214          $autoregister = isset($options['autoregister']) ? $options['autoregister'] :  $this->params->get('autoregister', 1);
 215  
 216          if($autoregister)
 217          {
 218              if(!$instance->save()) {
 219                  return JError::raiseWarning('SOME_ERROR_CODE', $instance->getError());
 220              }
 221          } else {
 222              // No existing user and autoregister off, this is a temporary user.
 223              $instance->set( 'tmp_user', true );
 224          }
 225  
 226          return $instance;
 227      }
 228  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1