/** * Register the Registration ID (token) obtained from Google Cloud Messaging for a user * @param Chamilo\UserBundle\Entity\User $user The user * @param string $registrationId The token registration id from Google Cloud Messaging * @return int The id after insert or the number of affected rows after update. Otherwhise return false */ public static function setGcmRegistrationId(Chamilo\UserBundle\Entity\User $user, $registrationId) { $registrationId = Security::remove_XSS($registrationId); $extraFieldValue = new ExtraFieldValue('user'); return $extraFieldValue->save(['variable' => self::EXTRA_FIELD_GCM_REGISTRATION, 'value' => $registrationId, 'item_id' => $user->getId()]); }
/** * Creates a new user for the platform * @author Hugues Peeters <*****@*****.**>, * @author Roan Embrechts <*****@*****.**> * @param string Firstname * @param string Lastname * @param int Status (1 for course tutor, 5 for student, 6 for anonymous) * @param string e-mail address * @param string Login * @param string Password * @param string Any official code (optional) * @param string User language (optional) * @param string Phone number (optional) * @param string Picture URI (optional) * @param string Authentication source (optional, defaults to 'platform', dependind on constant) * @param string Account expiration date (optional, defaults to '0000-00-00 00:00:00') * @param int Whether the account is enabled or disabled by default * @param int The department of HR in which the user is registered (optional, defaults to 0) * @param array Extra fields * @param string Encrypt method used if password is given encrypted. Set to an empty string by default * @return mixed new user id - if the new user creation succeeds, false otherwise * @desc The function tries to retrieve $_user['user_id'] from the global space. If it exists, $_user['user_id'] is the creator id. If a problem arises, it stores the error message in global $api_failureList * @assert ('Sam','Gamegie',5,'*****@*****.**','jo','jo') > 1 * @assert ('Pippin','Took',null,null,'jo','jo') === false */ public static function create_user($firstName, $lastName, $status, $email, $loginName, $password, $official_code = '', $language = '', $phone = '', $picture_uri = '', $auth_source = PLATFORM_AUTH_SOURCE, $expiration_date = '0000-00-00 00:00:00', $active = 1, $hr_dept_id = 0, $extra = null, $encrypt_method = '', $send_mail = false) { global $_configuration; $original_password = $password; $access_url_id = 1; if (api_get_multiple_access_url()) { $access_url_id = api_get_current_access_url_id(); } if (is_array($_configuration[$access_url_id]) && isset($_configuration[$access_url_id]['hosting_limit_users']) && $_configuration[$access_url_id]['hosting_limit_users'] > 0) { $num = self::get_number_of_users(); if ($num >= $_configuration[$access_url_id]['hosting_limit_users']) { return api_set_failure('portal users limit reached'); } } if ($status === 1 && is_array($_configuration[$access_url_id]) && isset($_configuration[$access_url_id]['hosting_limit_teachers']) && $_configuration[$access_url_id]['hosting_limit_teachers'] > 0) { $num = self::get_number_of_users(1); if ($num >= $_configuration[$access_url_id]['hosting_limit_teachers']) { return api_set_failure('portal teachers limit reached'); } } $firstName = Security::remove_XSS($firstName); $lastName = Security::remove_XSS($lastName); $loginName = Security::remove_XSS($loginName); $phone = Security::remove_XSS($phone); // database table definition $table_user = Database::get_main_table(TABLE_MAIN_USER); //Checking the user language $languages = api_get_languages(); if (!in_array($language, $languages)) { $language = Container::getTranslator()->getLocale(); } $creator_id = api_get_user_id(); // First check wether the login already exists if (!self::is_username_available($loginName)) { return api_set_failure('login-pass already taken'); } if (empty($encrypt_method)) { $password = api_get_encrypted_password($password); } else { if ($_configuration['password_encryption'] === $encrypt_method) { if ($encrypt_method == 'md5' && !preg_match('/^[A-Fa-f0-9]{32}$/', $password)) { return api_set_failure('encrypt_method invalid'); } else { if ($encrypt_method == 'sha1' && !preg_match('/^[A-Fa-f0-9]{40}$/', $password)) { return api_set_failure('encrypt_method invalid'); } } } else { return api_set_failure('encrypt_method invalid'); } } //@todo replace this date with the api_get_utc_date function big problem with users that are already registered $current_date = api_get_utc_datetime(); $em = Database::getManager(); $expirationDate = new \DateTime($expiration_date); $user = new \Chamilo\UserBundle\Entity\User(); $user->setLastname($lastName)->setFirstname($firstName)->setUsername($loginName)->setPassword($password)->setEmail($email)->setOfficialCode($official_code)->setPictureUri($picture_uri)->setCreatorId($creator_id)->setAuthSource($auth_source)->setPhone($phone)->setLanguage($language)->setExpirationDate($expirationDate)->setHrDeptId($hr_dept_id)->setActive($active); /*$sql = "INSERT INTO $table_user ". "SET lastname = '".Database::escape_string(trim($lastName))."',". "firstname = '".Database::escape_string(trim($firstName))."',". "username = '******',". "status = '".Database::escape_string($status)."',". "password = '******',". "email = '".Database::escape_string($email)."',". "official_code = '".Database::escape_string($official_code)."',". "picture_uri = '".Database::escape_string($picture_uri)."',". "creator_id = '".Database::escape_string($creator_id)."',". "auth_source = '".Database::escape_string($auth_source)."',". "phone = '".Database::escape_string($phone)."',". "language = '".Database::escape_string($language)."',". "registration_date = '".$current_date."',". "expiration_date = '".Database::escape_string($expiration_date)."',". "hr_dept_id = '".Database::escape_string($hr_dept_id)."',". "active = '".Database::escape_string($active)."'"; $result = Database::query($sql);*/ $em->persist($user); $em->flush(); if ($user) { $userId = $user->getId(); if (api_get_multiple_access_url()) { UrlManager::add_user_to_url($userId, api_get_current_access_url_id()); } else { //we are adding by default the access_url_user table with access_url_id = 1 UrlManager::add_user_to_url($userId, 1); } $group = $em->getRepository('ChamiloUserBundle:Group')->find($status); $user->addGroup($group); //$user->addRole($roleName); $em->persist($user); $em->flush(); if (!empty($email) && $send_mail) { $recipient_name = api_get_person_name($firstName, $lastName, null, PERSON_NAME_EMAIL_ADDRESS); $emailsubject = '[' . api_get_setting('platform.site_name') . '] ' . get_lang('YourReg') . ' ' . api_get_setting('platform.site_name'); $sender_name = api_get_person_name(api_get_setting('platform.administrator_name'), api_get_setting('platform.administrator_surname'), null, PERSON_NAME_EMAIL_ADDRESS); $email_admin = api_get_setting('platform.administrator_email'); $params = array('complete_user_name' => api_get_person_name($firstName, $lastName), 'login_name' => $loginName, 'password' => stripslashes($original_password)); $message = \Swift_Message::newInstance()->setSubject($emailsubject)->setFrom(array($email_admin => $sender_name))->setTo(array($email => $recipient_name))->setBody(Container::getTemplate()->render('ChamiloCoreBundle:Mailer:User/new_user.html.twig', $params), 'text/html')->addPart(Container::getTemplate()->render('ChamiloCoreBundle:Mailer:User/new_user.text.twig', $params), 'text/plain')->setEncoder(Swift_Encoding::get8BitEncoding()); $type = $message->getHeaders()->get('Content-Type'); $type->setValue('text/html'); $type->setParameter('charset', 'utf-8'); Container::getMailer()->send($message); /* MANAGE EVENT WITH MAIL */ /*if (EventsMail::check_if_using_class('user_registration')) { $values["about_user"] = $return; $values["password"] = $original_password; $values["send_to"] = array($return); $values["prior_lang"] = null; EventsDispatcher::events('user_registration', $values); } else { @api_mail_html($recipient_name, $email, $emailsubject, $emailbody, $sender_name, $email_admin); }*/ /* ENDS MANAGE EVENT WITH MAIL */ } // Add event to system log $user_id_manager = api_get_user_id(); $user_info = api_get_user_info($userId); Event::addEvent(LOG_USER_CREATE, LOG_USER_ID, $userId, api_get_utc_datetime(), $user_id_manager); Event::addEvent(LOG_USER_CREATE, LOG_USER_OBJECT, $user_info, api_get_utc_datetime(), $user_id_manager); } else { return api_set_failure('error inserting in Database'); } if (is_array($extra) && count($extra) > 0) { $res = true; foreach ($extra as $fname => $fvalue) { $res = $res && self::update_extra_field_value($userId, $fname, $fvalue); } } self::update_extra_field_value($userId, 'already_logged_in', 'false'); return $userId; }