From 167548ca2bb244f3389ba8750fcab3d749e0ebb3 Mon Sep 17 00:00:00 2001 From: jmontoya Date: Wed, 30 Mar 2016 15:25:56 +0200 Subject: [PATCH] Add DB changes + fixing sonata dashboard --- .../Schema/V111/Version20160330103045.php | 16 ++++++++ app/config/config.yml | 2 +- app/config/security.yml | 6 +-- main/inc/lib/database.lib.php | 11 ++++- main/inc/lib/usermanager.lib.php | 39 +++++++++++------- main/inc/local.inc.php | 11 ++--- .../CoreBundle/Resources/config/routing.yml | 12 +++--- src/Chamilo/UserBundle/Entity/User.php | 40 ++++++++++++------- 8 files changed, 91 insertions(+), 46 deletions(-) diff --git a/app/Migrations/Schema/V111/Version20160330103045.php b/app/Migrations/Schema/V111/Version20160330103045.php index f7a207bca3..e2be4dcce7 100644 --- a/app/Migrations/Schema/V111/Version20160330103045.php +++ b/app/Migrations/Schema/V111/Version20160330103045.php @@ -100,6 +100,22 @@ class Version20160330103045 extends AbstractMigration $this->addSql('DROP INDEX user_sco_course_sv_stack ON track_stored_values_stack'); $this->addSql('ALTER TABLE session_rel_user ADD duration INT DEFAULT NULL'); $this->addSql('DROP INDEX user_sco_course_sv ON track_stored_values'); + + $this->addSql('DROP INDEX UNIQ_8D93D649F85E0677 ON user'); + $this->addSql( + 'ALTER TABLE user ADD email_canonical VARCHAR(255) NOT NULL, ADD enabled TINYINT(1) NOT NULL, ADD locked TINYINT(1) NOT NULL, ADD expired TINYINT(1) NOT NULL, ADD expires_at DATETIME DEFAULT NULL, ADD roles LONGTEXT NOT NULL COMMENT \'(DC2Type:array)\', ADD credentials_expired TINYINT(1) NOT NULL, ADD credentials_expire_at DATETIME DEFAULT NULL, CHANGE username username VARCHAR(255) NOT NULL, CHANGE username_canonical username_canonical VARCHAR(255) NOT NULL, CHANGE email email VARCHAR(255) NOT NULL' + ); + $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649A0D96FBF ON user (email_canonical)'); + + $sql = "UPDATE user SET email_canonical = email"; + $this->addSql($sql); + + $sql = "UPDATE user SET roles = 'a:0:{}'"; + $this->addSql($sql); + + $sql = "UPDATE user SET username_canonical = username"; + $this->addSql($sql); + } /** diff --git a/app/config/config.yml b/app/config/config.yml index 3af5cf7b46..4be4c1affe 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -173,7 +173,7 @@ swiftmailer: spool: { type: memory } doctrine_migrations: - dir_name: %kernel.root_dir%/../app/Migrations/V111 + dir_name: %kernel.root_dir%/../app/Migrations/Schema/V111 namespace: Application\Migrations\Schema\V111 table_name: version name: Chamilo Migrations diff --git a/app/config/security.yml b/app/config/security.yml index 94c0c76b6b..27e13f8a69 100644 --- a/app/config/security.yml +++ b/app/config/security.yml @@ -59,9 +59,9 @@ security: context: user form_login: provider: fos_userbundle - login_path: /login + login_path: /administration/login use_forward: false - check_path: /login_check + check_path: /administration/login_check failure_path: null default_target_path: / # success_handler: chamilo_core.listener.login_success_handler @@ -72,7 +72,7 @@ security: invalidate_session: false # handlers: ['sonata.page.cms_manager_selector'] anonymous: ~ - switch_user: true + switch_user: false # activate different ways to authenticate # http_basic: ~ diff --git a/main/inc/lib/database.lib.php b/main/inc/lib/database.lib.php index 6350b5b817..13d2c2592a 100755 --- a/main/inc/lib/database.lib.php +++ b/main/inc/lib/database.lib.php @@ -137,7 +137,9 @@ class Database array( 'ChamiloUserBundle' => 'Chamilo\UserBundle\Entity', 'ChamiloCoreBundle' => 'Chamilo\CoreBundle\Entity', - 'ChamiloCourseBundle' => 'Chamilo\CourseBundle\Entity' + 'ChamiloCourseBundle' => 'Chamilo\CourseBundle\Entity', + 'FosUserBundle' => 'FOS\UserBundle\Entity', + 'SonataUserBundle' => 'Sonata\UserBundle\Entity', ) ); @@ -649,10 +651,15 @@ class Database $paths = array( $path.'src/Chamilo/CoreBundle/Entity', $path.'src/Chamilo/UserBundle/Entity', - $path.'src/Chamilo/CourseBundle/Entity' + $path.'src/Chamilo/CourseBundle/Entity', + $path.'vendor/sonata-project/user-bundle/Entity', + $path.'vendor/sonata-project/user-bundle/Model', + $path.'vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Entity', + ); $proxyDir = $path.'app/cache/'; + $proxyDir = ''; return \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration( $paths, diff --git a/main/inc/lib/usermanager.lib.php b/main/inc/lib/usermanager.lib.php index 5a28d8e6fb..f8401ff846 100755 --- a/main/inc/lib/usermanager.lib.php +++ b/main/inc/lib/usermanager.lib.php @@ -39,12 +39,11 @@ class UserManager private static $encryptionMethod; /** - * The default constructor only instanciates an empty user object + * Constructor * @assert () === null */ public function __construct() { - } /** @@ -65,7 +64,6 @@ class UserManager public static function getManager() { $encoderFactory = self::getEncoderFactory(); - $userManager = new Chamilo\UserBundle\Entity\Manager\UserManager( $encoderFactory, new \FOS\UserBundle\Util\Canonicalizer(), @@ -139,19 +137,21 @@ class UserManager /** * Validates the password - * @param string $password - * @param User $user * + * @param $encoded + * @param $raw + * @param $salt * @return bool */ - public static function isPasswordValid($password, User $user) + public static function isPasswordValid($encoded, $raw, $salt) { - $encoder = self::getEncoder($user); - $validPassword = $encoder->isPasswordValid( - $user->getPassword(), + //$encoder = self::getEncoder($user); + $encoder = new \Chamilo\UserBundle\Security\Encoder(self::getPasswordEncryption()); + /*$user->getPassword(), $password, - $user->getSalt() - ); + $user->getSalt()*/ + $validPassword = $encoder->isPasswordValid($encoded, $raw, $salt); + return $validPassword; } @@ -279,7 +279,9 @@ class UserManager } if (empty($password)) { - Display::addFlash(Display::return_message(get_lang('ThisFieldIsRequired').': '.get_lang('Password') , 'warning')); + Display::addFlash( + Display::return_message(get_lang('ThisFieldIsRequired').': '.get_lang('Password'), 'warning') + ); return false; } @@ -328,7 +330,12 @@ class UserManager $userManager = self::getManager(); /** @var User $user */ - $user = $userManager->createUser(); + //$user = $userManager->createUser(); + + $em = Database::getManager(); + + /** @var User $user */ + $user = new User(); $user ->setLastname($lastName) ->setFirstname($firstName) @@ -349,8 +356,12 @@ class UserManager if (!empty($expirationDate)) { $user->setExpirationDate($expirationDate); } + var_dump($user->getUsername()); - $userManager->updateUser($user, true); + $em->persist($user); + $em->flush(); + + //$userManager->updateUser($user); $userId = $user->getId(); if (!empty($userId)) { diff --git a/main/inc/local.inc.php b/main/inc/local.inc.php index 571b743319..3d5ac15151 100755 --- a/main/inc/local.inc.php +++ b/main/inc/local.inc.php @@ -267,7 +267,7 @@ if (!empty($_SESSION['_user']['user_id']) && !($login || $logout)) { // Lookup the user in the main database $user_table = Database::get_main_table(TABLE_MAIN_USER); - $sql = "SELECT user_id, username, password, auth_source, active, expiration_date, status + $sql = "SELECT user_id, username, password, auth_source, active, expiration_date, status, salt FROM $user_table WHERE username = '".Database::escape_string($login)."'"; $result = Database::query($sql); @@ -331,14 +331,15 @@ if (!empty($_SESSION['_user']['user_id']) && !($login || $logout)) { $uData['auth_source'] == CAS_AUTH_SOURCE ) { $validPassword = false; - $user = $userManager->findUserByUsername($login); + //$user = $userManager->findUserByUsername($login); + - if ($user) { $validPassword = UserManager::isPasswordValid( + $uData['password'], $password, - $user + $uData['salt'] ); - } + // The authentication of this user is managed by Chamilo itself //$password = api_get_encrypted_password(trim(stripslashes($password))); diff --git a/src/Chamilo/CoreBundle/Resources/config/routing.yml b/src/Chamilo/CoreBundle/Resources/config/routing.yml index 8c2a13f138..341c9bcb0f 100644 --- a/src/Chamilo/CoreBundle/Resources/config/routing.yml +++ b/src/Chamilo/CoreBundle/Resources/config/routing.yml @@ -91,12 +91,12 @@ web.img: # _controller: chamilo_course.controller.tool:indexAction # Redirects /url/ to /url -remove_trailing_slash: - path: /{url} - defaults: { _controller: ChamiloCoreBundle:Redirecting:removeTrailingSlash } - requirements: - url: .*/$ - _method: GET +#remove_trailing_slash: +# path: /{url} +# defaults: { _controller: ChamiloCoreBundle:Redirecting:removeTrailingSlash } +# requirements: +# url: .*/$ +# _method: GET ## Course settings diff --git a/src/Chamilo/UserBundle/Entity/User.php b/src/Chamilo/UserBundle/Entity/User.php index bd90e4b8c3..4f62f8b635 100644 --- a/src/Chamilo/UserBundle/Entity/User.php +++ b/src/Chamilo/UserBundle/Entity/User.php @@ -8,12 +8,14 @@ use Chamilo\CoreBundle\Entity\UsergroupRelUser; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Event\LifecycleEventArgs; use Doctrine\ORM\Mapping as ORM; -use Sonata\UserBundle\Entity\BaseUser as BaseUser; +//use Sonata\UserBundle\Entity\BaseUser as BaseUser; +use Sonata\UserBundle\Model\User as BaseUser; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\HttpFoundation\File\File; -use Symfony\Component\Security\Core\User\UserInterface; +//use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Mapping\ClassMetadata; +use FOS\UserBundle\Model\UserInterface; //use Chamilo\CoreBundle\Component\Auth; //use FOS\MessageBundle\Model\ParticipantInterface; @@ -27,6 +29,7 @@ use Symfony\Component\Validator\Mapping\ClassMetadata; //use Sylius\Component\Variation\Model\VariantInterface as BaseVariantInterface; /** + * * @ORM\HasLifecycleCallbacks * @ORM\Table( * name="user", @@ -35,12 +38,11 @@ use Symfony\Component\Validator\Mapping\ClassMetadata; * @ORM\Index(name="status", columns={"status"}) * } * ) - * //Vich\Uploadable * @UniqueEntity("username") * @ORM\Entity(repositoryClass="Chamilo\UserBundle\Entity\Repository\UserRepository") * */ -class User// extends BaseUser //implements ParticipantInterface, ThemeUser +class User extends BaseUser //implements ParticipantInterface, ThemeUser { const COURSE_MANAGER = 1; const TEACHER = 1; @@ -70,42 +72,42 @@ class User// extends BaseUser //implements ParticipantInterface, ThemeUser * * @ORM\Column(name="username", type="string", length=100, nullable=false, unique=true) */ - protected $username; + //protected $username; /** * @var string * * * @ORM\Column(name="username_canonical", type="string", length=100, nullable=false, unique=true) */ - protected $usernameCanonical; + //protected $usernameCanonical; /** * @var string * * @ORM\Column(name="email", type="string", length=100, nullable=false, unique=false) */ - protected $email; + //protected $email; /** * @var string * * @ORM\Column(name="lastname", type="string", length=60, nullable=true, unique=false) */ - protected $lastname; + //protected $lastname; /** * @var string * * @ORM\Column(name="firstname", type="string", length=60, nullable=true, unique=false) */ - protected $firstname; + //protected $firstname; /** * @var string * * @ORM\Column(name="password", type="string", length=255, nullable=false, unique=false) */ - protected $password; + //protected $password; /** * @var string @@ -119,7 +121,7 @@ class User// extends BaseUser //implements ParticipantInterface, ThemeUser * * @ORM\Column(name="status", type="integer", nullable=false) */ - private $status = STUDENT; + private $status; /** * @var string @@ -271,14 +273,14 @@ class User// extends BaseUser //implements ParticipantInterface, ThemeUser /** * @ORM\Column(type="string", length=255) */ - protected $salt; + //protected $salt; /** * @var \DateTime * * @ORM\Column(name="last_login", type="datetime", nullable=true, unique=false) */ - protected $lastLogin; + //protected $lastLogin; /** * Random string sent to the user email address in order to verify it @@ -286,14 +288,14 @@ class User// extends BaseUser //implements ParticipantInterface, ThemeUser * @var string * @ORM\Column(name="confirmation_token", type="string", length=255, nullable=true) */ - protected $confirmationToken; + //protected $confirmationToken; /** * @var \DateTime * * @ORM\Column(name="password_requested_at", type="datetime", nullable=true, unique=false) */ - protected $passwordRequestedAt; + //protected $passwordRequestedAt; /** * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\CourseRelUser", mappedBy="user") @@ -375,6 +377,8 @@ class User// extends BaseUser //implements ParticipantInterface, ThemeUser public function __construct() { parent::__construct(); + $this->status = self::STUDENT; + $this->salt = sha1(uniqid(null, true)); $this->isActive = true; @@ -1565,5 +1569,11 @@ class User// extends BaseUser //implements ParticipantInterface, ThemeUser $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time(); } + public function getUsername() + { + return $this->username; + } + + }