[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

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

   1  <?php
   2  /**
   3   * @copyright    Copyright (C) 2005 - 2009 Open Source Matters, Inc. All rights reserved.
   4   * @license        GNU General Public License version 2 or later; see LICENSE.txt
   5   */
   6  
   7  // No direct access
   8  defined('_JEXEC') or die;
   9  
  10  /**
  11   * Joomla User plugin
  12   *
  13   * @package        Joomla.Plugin
  14   * @subpackage    User.joomla
  15   * @since        1.5
  16   */
  17  class plgUserJoomla extends JPlugin
  18  {
  19      /**
  20       * Remove all sessions for the user name
  21       *
  22       * Method is called after user data is deleted from the database
  23       *
  24       * @param    array        $user    Holds the user data
  25       * @param    boolean        $succes    True if user was succesfully stored in the database
  26       * @param    string        $msg    Message
  27       *
  28       * @return    boolean
  29       * @since    1.6
  30       */
  31  	public function onUserAfterDelete($user, $succes, $msg)
  32      {
  33          if (!$succes) {
  34              return false;
  35          }
  36  
  37          $db = JFactory::getDbo();
  38          $db->setQuery(
  39              'DELETE FROM '.$db->quoteName('#__session') .
  40              ' WHERE '.$db->quoteName('userid').' = '.(int) $user['id']
  41          );
  42          $db->Query();
  43  
  44          return true;
  45      }
  46  
  47      /**
  48       * Utility method to act on a user after it has been saved.
  49       *
  50       * This method sends a registration email to new users created in the backend.
  51       *
  52       * @param    array        $user        Holds the new user data.
  53       * @param    boolean        $isnew        True if a new user is stored.
  54       * @param    boolean        $success    True if user was succesfully stored in the database.
  55       * @param    string        $msg        Message.
  56       *
  57       * @return    void
  58       * @since    1.6
  59       */
  60  	public function onUserAfterSave($user, $isnew, $success, $msg)
  61      {
  62          // Initialise variables.
  63          $app    = JFactory::getApplication();
  64          $config    = JFactory::getConfig();
  65          $mail_to_user = $this->params->get('mail_to_user', 1);
  66  
  67          if ($isnew) {
  68              // TODO: Suck in the frontend registration emails here as well. Job for a rainy day.
  69  
  70              if ($app->isAdmin()) {
  71                  if ($mail_to_user) {
  72  
  73                      // Load user_joomla plugin language (not done automatically).
  74                      $lang = JFactory::getLanguage();
  75                      $lang->load('plg_user_joomla', JPATH_ADMINISTRATOR);
  76  
  77                      // Compute the mail subject.
  78                      $emailSubject = JText::sprintf(
  79                          'PLG_USER_JOOMLA_NEW_USER_EMAIL_SUBJECT',
  80                          $user['name'],
  81                          $config->get('sitename')
  82                      );
  83  
  84                      // Compute the mail body.
  85                      $emailBody = JText::sprintf(
  86                          'PLG_USER_JOOMLA_NEW_USER_EMAIL_BODY',
  87                          $user['name'],
  88                          $config->get('sitename'),
  89                          JUri::root(),
  90                          $user['username'],
  91                          $user['password_clear']
  92                      );
  93  
  94                      // Assemble the email data...the sexy way!
  95                      $mail = JFactory::getMailer()
  96                          ->setSender(
  97                              array(
  98                                  $config->get('mailfrom'),
  99                                  $config->get('fromname')
 100                              )
 101                          )
 102                          ->addRecipient($user['email'])
 103                          ->setSubject($emailSubject)
 104                          ->setBody($emailBody);
 105  
 106                      if (!$mail->Send()) {
 107                          // TODO: Probably should raise a plugin error but this event is not error checked.
 108                          JError::raiseWarning(500, JText::_('ERROR_SENDING_EMAIL'));
 109                      }
 110                  }
 111              }
 112          }
 113          else {
 114              // Existing user - nothing to do...yet.
 115          }
 116      }
 117  
 118      /**
 119       * This method should handle any login logic and report back to the subject
 120       *
 121       * @param    array    $user        Holds the user data
 122       * @param    array    $options    Array holding options (remember, autoregister, group)
 123       *
 124       * @return    boolean    True on success
 125       * @since    1.5
 126       */
 127  	public function onUserLogin($user, $options = array())
 128      {
 129          $instance = $this->_getUser($user, $options);
 130  
 131          // If _getUser returned an error, then pass it back.
 132          if ($instance instanceof Exception) {
 133              return false;
 134          }
 135  
 136          // If the user is blocked, redirect with an error
 137          if ($instance->get('block') == 1) {
 138              JError::raiseWarning('SOME_ERROR_CODE', JText::_('JERROR_NOLOGIN_BLOCKED'));
 139              return false;
 140          }
 141  
 142          // Authorise the user based on the group information
 143          if (!isset($options['group'])) {
 144              $options['group'] = 'USERS';
 145          }
 146  
 147          // Chek the user can login.
 148          $result    = $instance->authorise($options['action']);
 149          if (!$result) {
 150  
 151              JError::raiseWarning(401, JText::_('JERROR_LOGIN_DENIED'));
 152              return false;
 153          }
 154  
 155          // Mark the user as logged in
 156          $instance->set('guest', 0);
 157  
 158          // Register the needed session variables
 159          $session = JFactory::getSession();
 160          $session->set('user', $instance);
 161  
 162          $db = JFactory::getDBO();
 163  
 164          // Check to see the the session already exists.
 165          $app = JFactory::getApplication();
 166          $app->checkSession();
 167  
 168          // Update the user related fields for the Joomla sessions table.
 169          $db->setQuery(
 170              'UPDATE '.$db->quoteName('#__session') .
 171              ' SET '.$db->quoteName('guest').' = '.$db->quote($instance->get('guest')).',' .
 172              '    '.$db->quoteName('username').' = '.$db->quote($instance->get('username')).',' .
 173              '    '.$db->quoteName('userid').' = '.(int) $instance->get('id') .
 174              ' WHERE '.$db->quoteName('session_id').' = '.$db->quote($session->getId())
 175          );
 176          $db->query();
 177  
 178          // Hit the user last visit field
 179          $instance->setLastVisit();
 180  
 181          return true;
 182      }
 183  
 184      /**
 185       * This method should handle any logout logic and report back to the subject
 186       *
 187       * @param    array    $user        Holds the user data.
 188       * @param    array    $options    Array holding options (client, ...).
 189       *
 190       * @return    object    True on success
 191       * @since    1.5
 192       */
 193  	public function onUserLogout($user, $options = array())
 194      {
 195          $my         = JFactory::getUser();
 196          $session     = JFactory::getSession();
 197          $app         = JFactory::getApplication();
 198  
 199          // Make sure we're a valid user first
 200          if ($user['id'] == 0 && !$my->get('tmp_user')) {
 201              return true;
 202          }
 203  
 204          // Check to see if we're deleting the current session
 205          if ($my->get('id') == $user['id'] && $options['clientid'] == $app->getClientId()) {
 206              // Hit the user last visit field
 207              $my->setLastVisit();
 208  
 209              // Destroy the php session for this user
 210              $session->destroy();
 211          }
 212  
 213          // Force logout all users with that userid
 214          $db = JFactory::getDBO();
 215          $db->setQuery(
 216              'DELETE FROM '.$db->quoteName('#__session') .
 217              ' WHERE '.$db->quoteName('userid').' = '.(int) $user['id'] .
 218              ' AND '.$db->quoteName('client_id').' = '.(int) $options['clientid']
 219          );
 220          $db->query();
 221  
 222          return true;
 223      }
 224  
 225      /**
 226       * This method will return a user object
 227       *
 228       * If options['autoregister'] is true, if the user doesn't exist yet he will be created
 229       *
 230       * @param    array    $user        Holds the user data.
 231       * @param    array    $options    Array holding options (remember, autoregister, group).
 232       *
 233       * @return    object    A JUser object
 234       * @since    1.5
 235       */
 236  	protected function _getUser($user, $options = array())
 237      {
 238          $instance = JUser::getInstance();
 239          if ($id = intval(JUserHelper::getUserId($user['username'])))  {
 240              $instance->load($id);
 241              return $instance;
 242          }
 243  
 244          //TODO : move this out of the plugin
 245          jimport('joomla.application.component.helper');
 246          $config    = JComponentHelper::getParams('com_users');
 247          // Default to Registered.
 248          $defaultUserGroup = $config->get('new_usertype', 2);
 249  
 250          $acl = JFactory::getACL();
 251  
 252          $instance->set('id'            , 0);
 253          $instance->set('name'            , $user['fullname']);
 254          $instance->set('username'        , $user['username']);
 255          $instance->set('password_clear'    , $user['password_clear']);
 256          $instance->set('email'            , $user['email']);    // Result should contain an email (check)
 257          $instance->set('usertype'        , 'deprecated');
 258          $instance->set('groups'        , array($defaultUserGroup));
 259  
 260          //If autoregister is set let's register the user
 261          $autoregister = isset($options['autoregister']) ? $options['autoregister'] :  $this->params->get('autoregister', 1);
 262  
 263          if ($autoregister) {
 264              if (!$instance->save()) {
 265                  return JError::raiseWarning('SOME_ERROR_CODE', $instance->getError());
 266              }
 267          }
 268          else {
 269              // No existing user and autoregister off, this is a temporary user.
 270              $instance->set('tmp_user', true);
 271          }
 272  
 273          return $instance;
 274      }
 275  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1