From 869e1ccb1ae2a346f60554e7ab8e5cd5f9aff4db Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Thu, 18 Sep 2014 15:45:10 +0200 Subject: [PATCH] Adding md5, none password support. --- app/config/config.yml | 10 +-- main/admin/user_edit.php | 68 +++++++------------ main/inc/lib/usermanager.lib.php | 48 ++++++++++--- .../CoreBundle/Resources/config/services.yml | 2 +- src/Chamilo/UserBundle/Security/Encoder.php | 23 ++++++- 5 files changed, 90 insertions(+), 61 deletions(-) diff --git a/app/config/config.yml b/app/config/config.yml index cdafa4071c..845d5605a8 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -199,11 +199,11 @@ doctrine_migrations: table_name: chamilo_migration_versions name: Chamilo Migrations -fos_advanced_encoder: - encoders: - FOS\UserBundle\Model\UserInterface: sha512 - legacy_encoder: - id: chamilo_core.encoder +#fos_advanced_encoder: +# encoders: +# FOS\UserBundle\Model\UserInterface: sha512 +# legacy_encoder: +# id: chamilo_core.encoder sonata_admin: title: Chamilo diff --git a/main/admin/user_edit.php b/main/admin/user_edit.php index b9c70c33a6..7036ebda54 100644 --- a/main/admin/user_edit.php +++ b/main/admin/user_edit.php @@ -204,43 +204,13 @@ $form->addElement( $status, array( 'id' => 'status_select', - 'onchange' => 'javascript: display_drh_list();', - 'class' => 'chzn-select' + 'class' => 'chzn-select', + 'multiple' => 'multiple' ) ); $display = isset($user_data['status']) && ($user_data['status'] == STUDENT || (isset($_POST['status']) && $_POST['status'] == STUDENT)) ? 'block' : 'none'; -/* -$form->addElement('html', '
'); -$drh_select = $form->addElement('select', 'hr_dept_id', get_lang('Drh'), array(), 'id="drh_select"'); -$drh_list = UserManager :: get_user_list(array('status' => DRH), api_sort_by_first_name() ? array('firstname', 'lastname') : array('lastname', 'firstname')); - -if (count($drh_list) == 0) { - $drh_select->addOption('- '.get_lang('ThereIsNotStillAResponsible', '').' -', 0); -} else { - $drh_select->addOption('- '.get_lang('SelectAResponsible').' -', 0); -} - -foreach($drh_list as $drh) { - $drh_select->addOption(api_get_person_name($drh['firstname'], $drh['lastname']), $drh['user_id']); -} -$form->addElement('html', '
'); -*/ - -//Language -/*if (api_is_platform_admin()) { - $group = array(); - $group[] =$form->createElement('radio', 'platform_admin', null, get_lang('Yes'), 1); - $group[] =$form->createElement('radio', 'platform_admin', null, get_lang('No'), 0); - - $user_data['status'] == 1 ? $display = 'block':$display = 'none'; - - $form->addElement('html', '
'); - $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), null, false); - $form->addElement('html', '
'); -}*/ - //Language $form->addElement('select_language', 'language', get_lang('Language')); @@ -252,8 +222,17 @@ $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), ' ', false); // Registration Date $creatorInfo = api_get_user_info($user_data['creator_id']); -$date = sprintf(get_lang('CreatedByXYOnZ'), 'user_information.php?user_id='.$user_data['creator_id'], $creatorInfo['username'], $user_data['registration_date']); -$form->addElement('html', '
'.$date.'
'); +$date = sprintf( + get_lang('CreatedByXYOnZ'), + 'user_information.php?user_id='.$user_data['creator_id'], + $creatorInfo['username'], + $user_data['registration_date'] +); +$form->addElement( + 'label', + get_lang('RegistrationDate'), + $date +); // Expiration Date if (!$user_data['platform_admin']) { @@ -309,15 +288,17 @@ if ($expiration_date == '0000-00-00 00:00:00') { } $user = Database::getManager()->getRepository('ChamiloUserBundle:User')->find($user_data['user_id']); -/*$roles = $user->getGroups(); +$roles = $user->getGroups(); -$role = array(); +$roleToArray = array(); if (!empty($roles)) { - $role = current($roles); - $role = $role->getId(); + foreach($roles as $role) { + $roleId = $role->getId(); + $roleToArray[] = $roleId; + } } -$user_data['status'] = $role;*/ +$user_data['status'] = $roleToArray; $form->setDefaults($user_data); $error_drh = false; @@ -352,7 +333,7 @@ if ($form->validate()) { $email = $user['email']; $phone = $user['phone']; $username = $user['username']; - $status = intval($user['status']); + $status = $user['status']; $send_mail = intval($user['send_mail']); $reset_password = intval($user['reset_password']); $hr_dept_id = isset($user['hr_dept_id']) ? intval($user['hr_dept_id']) : null; @@ -366,7 +347,7 @@ if ($form->validate()) { $active = isset($user['active']) ? intval($user['active']) : 0; - if (api_get_setting('login_is_email') == 'true') { + if (api_get_setting('profile.login_is_email') == 'true') { $username = $email; } @@ -393,9 +374,10 @@ if ($form->validate()) { $reset_password ); - if (api_get_setting('openid_authentication') == 'true' && !empty($user['openid'])) { + /*if (api_get_setting('openid_authentication') == 'true' && !empty + ($user['openid'])) { $up = UserManager::update_openid($user_id, $user['openid']); - } + }*/ // Using the extra field value obj $extraFieldValues = new ExtraFieldValue('user'); diff --git a/main/inc/lib/usermanager.lib.php b/main/inc/lib/usermanager.lib.php index 04f88e5720..e6d2163465 100644 --- a/main/inc/lib/usermanager.lib.php +++ b/main/inc/lib/usermanager.lib.php @@ -777,13 +777,18 @@ class UserManager $auth_source = $auth_source; } - if ($user_id != strval(intval($user_id))) return false; - if ($user_id === false) return false; + if ($user_id != strval(intval($user_id))) { + return false; + } - //Checking the user language - $languages = api_get_languages(); - if (!in_array($language, $languages['folder'])) { - $language = api_get_setting('platformLanguage'); + if ($user_id === false) { + return false; + } + + // Checking the user language. + $languages = api_get_platform_isocodes(); + if (!in_array($language, $languages)) { + $language = Container::getTranslator()->getLocale(); } if (!is_null($password)) { @@ -802,6 +807,8 @@ class UserManager } } + + $em = Database::getManager(); /** @var Chamilo\UserBundle\Entity\User $user */ @@ -818,10 +825,13 @@ class UserManager if ($user_info['active'] != $active) { self::change_active_state($user_id, $active); } - // Adding user - $user->setLastname($lastname) + + // Updating user + + $user + ->setLastname($lastname) ->setFirstname($firstname) - ->setPassword($password) + //->setPassword($password) ->setUsername($username) ->setAuthSource($auth_source) ->setLanguage($language) @@ -834,10 +844,26 @@ class UserManager ->setHrDeptId($hr_dept_id) ; - $group = $em->getRepository('ChamiloUserBundle:Group')->find($status); - $user->addGroup($group); + if (!empty($original_password)) { + $user->setPlainPassword($original_password); + } + + if (is_array($status)) { + foreach ($status as $groupId) { + $group = $em->getRepository('ChamiloUserBundle:Group')->find($groupId); + $user->addGroup($group); + } + } else { + $group = $em->getRepository('ChamiloUserBundle:Group')->find( + $status + ); + $user->addGroup($group); + } + Container::getUserManager()->updateUser($user, true); + if (!empty($email) && $send_email) { + //Container::getMailer()->send() $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( diff --git a/src/Chamilo/CoreBundle/Resources/config/services.yml b/src/Chamilo/CoreBundle/Resources/config/services.yml index c042acb0cd..6e6ec3bb23 100644 --- a/src/Chamilo/CoreBundle/Resources/config/services.yml +++ b/src/Chamilo/CoreBundle/Resources/config/services.yml @@ -125,9 +125,9 @@ services: chamilo_core.encoder: class: Chamilo\UserBundle\Security\Encoder + arguments: ['%password_encryption%'] # Settings - chamilo_core.settings_schema.admin: class: Chamilo\CoreBundle\Settings\AdminSettingsSchema tags: diff --git a/src/Chamilo/UserBundle/Security/Encoder.php b/src/Chamilo/UserBundle/Security/Encoder.php index de50212179..860abca566 100644 --- a/src/Chamilo/UserBundle/Security/Encoder.php +++ b/src/Chamilo/UserBundle/Security/Encoder.php @@ -11,6 +11,15 @@ use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface; */ class Encoder implements PasswordEncoderInterface { + protected $method; + + /** + * @param $method + */ + public function __construct($method) + { + $this->method = $method; + } /** * @param string $raw * @param string $salt @@ -18,8 +27,20 @@ class Encoder implements PasswordEncoderInterface */ public function encodePassword($raw, $salt) { + $encrypted = null; + switch ($this->method) { + case 'sha1': + $encrypted = sha1($raw); + break; + case 'md5': + $encrypted = md5($raw); + break; + case 'none': + $encrypted = $raw; + } + // Do not use salt here. - return sha1($raw); + return $encrypted; } /**