/** * Method is called after user data is stored in the database * * @param array $user holds the new user data * @param boolean $isnew true if a new user is stored * @param boolean $success true if user was succesfully stored in the database * @param string $msg message * @return void */ public function onAfterStoreUser($user, $isnew, $succes, $msg) { $xprofile = \Hubzero\User\Profile::getInstance($user['id']); if (!is_object($xprofile)) { $params = Component::params('com_members'); $hubHomeDir = rtrim($params->get('homedir'), '/'); if (empty($hubHomeDir)) { // try to deduce a viable home directory based on sitename or live_site $sitename = strtolower(Config::get('sitename')); $sitename = preg_replace('/^http[s]{0,1}:\\/\\//', '', $sitename, 1); $sitename = trim($sitename, '/ '); $sitename_e = explode('.', $sitename, 2); if (isset($sitename_e[1])) { $sitename = $sitename_e[0]; } if (!preg_match("/^[a-zA-Z]+[\\-_0-9a-zA-Z\\.]+\$/i", $sitename)) { $sitename = ''; } if (empty($sitename)) { $sitename = strtolower(Request::base()); $sitename = preg_replace('/^http[s]{0,1}:\\/\\//', '', $sitename, 1); $sitename = trim($sitename, '/ '); $sitename_e = explode('.', $sitename, 2); if (isset($sitename_e[1])) { $sitename = $sitename_e[0]; } if (!preg_match("/^[a-zA-Z]+[\\-_0-9a-zA-Z\\.]+\$/i", $sitename)) { $sitename = ''; } } $hubHomeDir = DS . 'home'; if (!empty($sitename)) { $hubHomeDir .= DS . $sitename; } if (!empty($hubHomeDir)) { $db = App::get('db'); $component = new JTableExtension($this->database); $component->load($component->find(array('element' => 'com_members', 'type' => 'component'))); $params = new \Hubzero\Config\Registry($component->params); $params->set('homedir', $hubHomeDir); $component->params = $params->toString(); $component->store(); } } $xprofile = new \Hubzero\User\Profile(); $xprofile->set('gidNumber', $params->get('gidNumber', '100')); $xprofile->set('gid', $params->get('gid', 'users')); $xprofile->set('uidNumber', $user['id']); $xprofile->set('homeDirectory', $hubHomeDir . DS . $user['username']); $xprofile->set('loginShell', '/bin/bash'); $xprofile->set('ftpShell', '/usr/lib/sftp-server'); $xprofile->set('name', $user['name']); $xprofile->set('email', $user['email']); $xprofile->set('emailConfirmed', '3'); $xprofile->set('username', $user['username']); $xprofile->set('regIP', $_SERVER['REMOTE_ADDR']); $xprofile->set('emailConfirmed', -rand(1, pow(2, 31) - 1)); $xprofile->set('public', $params->get('privacy', 0)); if (isset($_SERVER['REMOTE_HOST'])) { $xprofile->set('regHost', $_SERVER['REMOTE_HOST']); } $xprofile->set('registerDate', Date::toSql()); $result = $xprofile->create(); if (!$result) { return new Exception('Unable to create \\Hubzero\\User\\Profile record', 500); } } else { $update = false; $params = Component::params('com_members'); if ($xprofile->get('username') != $user['username']) { $xprofile->set('username', $user['username']); $update = true; } if ($xprofile->get('name') != $user['name']) { $xprofile->set('name', $user['name']); $update = true; } if ($xprofile->get('email') != $user['email']) { $xprofile->set('email', $user['email']); $xprofile->set('emailConfirmed', 0); $update = true; } if ($xprofile->get('emailConfirmed') == '') { $xprofile->set('emailConfirmed', '3'); $update = true; } if ($xprofile->get('gid') == '') { $xprofile->set('gid', $params->get('gid', 'users')); $update = true; } if ($xprofile->get('gidNumber') == '') { $xprofile->set('gidNumber', $params->get('gidNumber', '100')); $update = true; } if ($xprofile->get('loginShell') == '') { $xprofile->set('loginShell', '/bin/bash'); $update = true; } if ($xprofile->get('ftpShell') == '') { $xprofile->set('ftpShell', '/usr/lib/sftp-server'); // This isn't right, but we're using an empty shell as an indicator that we should also update default privacy $xprofile->set('public', $params->get('privacy', 0)); $update = true; } if ($update) { $xprofile->update(); } } // Check if quota exists for the user $params = Component::params('com_members'); if ($params->get('manage_quotas', false)) { require_once PATH_CORE . DS . 'components' . DS . 'com_members' . DS . 'tables' . DS . 'users_quotas.php'; require_once PATH_CORE . DS . 'components' . DS . 'com_members' . DS . 'tables' . DS . 'quotas_classes.php'; $quota = new \Components\Members\Tables\UsersQuotas($this->database); $quota->load(array('user_id' => $user['id'])); if (!$quota->id) { $class = new \Components\Members\Tables\QuotasClasses($this->database); $class->load(array('alias' => 'default')); if ($class->id) { $quota->set('user_id', $user['id']); $quota->set('class_id', $class->id); $quota->set('soft_blocks', $class->soft_blocks); $quota->set('hard_blocks', $class->hard_blocks); $quota->set('soft_files', $class->soft_files); $quota->set('hard_files', $class->hard_files); $quota->store(); } } } }
/** * Utility method to act on a user after it has been saved. * * @param array $user Holds the new user data. * @param boolean $isnew True if a new user is stored. * @param boolean $success True if user was succesfully stored in the database. * @param string $msg Message. * @return void */ public function onUserAfterSave($user, $isnew, $success, $msg) { $userId = \Hubzero\Utility\Arr::getValue($user, 'id', 0, 'int'); if ($userId && $success) { try { $gids = JUserHelper::getUserGroups($userId); $db = App::get('db'); // // Quota class // require_once PATH_CORE . DS . 'components' . DS . 'com_members' . DS . 'tables' . DS . 'quotas_classes.php'; require_once PATH_CORE . DS . 'components' . DS . 'com_members' . DS . 'tables' . DS . 'users_quotas.php'; $row = new \Components\Members\Tables\UsersQuotas($db); // Check for an existing quota record $db->setQuery("SELECT * FROM `#__users_quotas` WHERE `user_id`=" . $userId); if ($quota = $db->loadObject()) { $row->bind($quota); } else { $row->user_id = $userId; } // If (no quota record OR a record and a quota class [e.g., not custom]) ... if (!$row->id || $row->id && $row->class_id) { $val = array('hard_files' => 0, 'soft_files' => 0, 'hard_blocks' => 0, 'soft_blocks' => 0); $db->setQuery("SELECT c.* FROM `#__users_quotas_classes` AS c LEFT JOIN `#__users_quotas_classes_groups` AS g ON g.`class_id`=c.`id` WHERE g.`group_id` IN (" . implode(',', $gids) . ")"); $cids = $db->loadObjectList(); if (count($cids) <= 0) { $db->setQuery("SELECT c.* FROM `#__users_quotas_classes` AS c WHERE c.`alias`=" . $db->quote('default')); $cids = $db->loadObjectList(); } // Loop through each usergroup and find the highest quota values foreach ($cids as $cls) { $cls->hard_blocks = intval($cls->hard_blocks); $cls->soft_blocks = intval($cls->soft_blocks); if ($cls->hard_blocks > $val['hard_blocks'] && $cls->soft_blocks > $val['soft_blocks']) { $row->class_id = $cls->id; } //$val['hard_files'] = ($val['hard_files'] > $cls->hard_files ?: $cls->hard_files); //$val['soft_files'] = ($val['soft_files'] > $cls->soft_files ?: $cls->soft_files); $val['hard_blocks'] = $val['hard_blocks'] > $cls->hard_blocks ? $val['hard_blocks'] : $cls->hard_blocks; $val['soft_blocks'] = $val['soft_blocks'] > $cls->soft_blocks ? $val['soft_blocks'] : $cls->soft_blocks; } $row->hard_files = $val['hard_files']; $row->soft_files = $val['soft_files']; $row->hard_blocks = $val['hard_blocks']; $row->soft_blocks = $val['soft_blocks']; if (!$row->check()) { throw new Exception($row->getError()); } if (!$row->store()) { throw new Exception($row->getError()); } } // // Session limits // require_once PATH_CORE . DS . 'components' . DS . 'com_tools' . DS . 'tables' . DS . 'sessionclass.php'; require_once PATH_CORE . DS . 'components' . DS . 'com_tools' . DS . 'tables' . DS . 'preferences.php'; $row = new \Components\Tools\Tables\Preferences($db); // Check for an existing quota record $db->setQuery("SELECT * FROM `#__users_tool_preferences` WHERE `user_id`=" . $userId); if ($quota = $db->loadObject()) { $row->bind($quota); } else { $row->user_id = $userId; } // If (no quota record OR a record and a quota class [e.g., not custom]) ... if (!$row->id || $row->id && $row->class_id) { $val = array('jobs' => 0); $db->setQuery("SELECT c.* FROM `#__tool_session_classes` AS c LEFT JOIN `#__tool_session_class_groups` AS g ON g.`class_id`=c.`id` WHERE g.`group_id` IN (" . implode(',', $gids) . ")"); $cids = $db->loadObjectList(); if (count($cids) <= 0) { $db->setQuery("SELECT c.* FROM `#__tool_session_classes` AS c WHERE c.`alias`=" . $db->quote('default')); $cids = $db->loadObjectList(); } // Loop through each usergroup and find the highest 'jobs allowed' value foreach ($cids as $cls) { } $cls->jobs = intval($cls->jobs); if ($cls->jobs > $val['jobs']) { $row->class_id = $cls->id; } $val['jobs'] = $val['jobs'] > $cls->jobs ? $val['jobs'] : $cls->jobs; $row->jobs = $val['jobs']; if (!$row->check()) { throw new Exception($row->getError()); } if (!$row->store()) { throw new Exception($row->getError()); } } } catch (Exception $e) { $this->_subject->setError($e->getMessage()); return false; } } return true; }